OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4AulCompiler::CodegenAstVisitor Class Reference
Inheritance diagram for C4AulCompiler::CodegenAstVisitor:
[legend]
Collaboration diagram for C4AulCompiler::CodegenAstVisitor:
[legend]

Public Member Functions

 CodegenAstVisitor (C4ScriptHost *host, C4ScriptHost *source_host)
 
 CodegenAstVisitor (C4AulScriptFunc *func)
 
 ~CodegenAstVisitor () override=default
 
void visit (const ::aul::ast::Noop *) override
 
void visit (const ::aul::ast::StringLit *n) override
 
void visit (const ::aul::ast::IntLit *n) override
 
void visit (const ::aul::ast::BoolLit *n) override
 
void visit (const ::aul::ast::ArrayLit *n) override
 
void visit (const ::aul::ast::ProplistLit *n) override
 
void visit (const ::aul::ast::NilLit *n) override
 
void visit (const ::aul::ast::ThisLit *n) override
 
void visit (const ::aul::ast::VarExpr *n) override
 
void visit (const ::aul::ast::UnOpExpr *n) override
 
void visit (const ::aul::ast::BinOpExpr *n) override
 
void visit (const ::aul::ast::AssignmentExpr *n) override
 
void visit (const ::aul::ast::SubscriptExpr *n) override
 
void visit (const ::aul::ast::SliceExpr *n) override
 
void visit (const ::aul::ast::CallExpr *n) override
 
void visit (const ::aul::ast::ParExpr *n) override
 
void visit (const ::aul::ast::Block *n) override
 
void visit (const ::aul::ast::Return *n) override
 
void visit (const ::aul::ast::ForLoop *n) override
 
void visit (const ::aul::ast::RangeLoop *n) override
 
void visit (const ::aul::ast::DoLoop *n) override
 
void visit (const ::aul::ast::WhileLoop *n) override
 
void visit (const ::aul::ast::Break *n) override
 
void visit (const ::aul::ast::Continue *n) override
 
void visit (const ::aul::ast::If *n) override
 
void visit (const ::aul::ast::VarDecl *n) override
 
void visit (const ::aul::ast::FunctionDecl *n) override
 
void visit (const ::aul::ast::FunctionExpr *n) override
 
void visit (const ::aul::ast::Script *n) override
 
template<class T >
void EmitFunctionCode (const T *n)
 
virtual void visit (const ::aul::ast::IncludePragma *)
 
virtual void visit (const ::aul::ast::AppendtoPragma *)
 
template<class T >
void visit (const T *)=delete
 

Detailed Description

Definition at line 144 of file C4AulCompiler.cpp.

Constructor & Destructor Documentation

C4AulCompiler::CodegenAstVisitor::CodegenAstVisitor ( C4ScriptHost host,
C4ScriptHost source_host 
)
inline

Definition at line 253 of file C4AulCompiler.cpp.

253 : target_host(host), host(source_host) {}
C4AulCompiler::CodegenAstVisitor::CodegenAstVisitor ( C4AulScriptFunc func)
inlineexplicit

Definition at line 254 of file C4AulCompiler.cpp.

254 : Fn(func), target_host(func->pOrgScript), host(target_host) {}
C4ScriptHost * pOrgScript
C4AulCompiler::CodegenAstVisitor::~CodegenAstVisitor ( )
overridedefault

Member Function Documentation

template<class T >
void C4AulCompiler::CodegenAstVisitor::EmitFunctionCode ( const T *  n)
inline

Definition at line 290 of file C4AulCompiler.cpp.

Referenced by C4AulCompiler::Compile(), and C4AulCompiler::ConstexprEvaluator::visit().

291  {
292  // This dynamic_cast resolves the problem where we have a Function*
293  // and want to emit code to it. All classes derived from Function
294  // are also ultimately derived from Node, so this call is fine
295  // without any additional checking.
296  EmitFunctionCode(n, dynamic_cast<const ::aul::ast::Node*>(n));
297  }

Here is the caller graph for this function:

virtual void aul::AstVisitor::visit ( const ::aul::ast::IncludePragma )
inlinevirtualinherited

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 91 of file C4AulAST.h.

91 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::AppendtoPragma )
inlinevirtualinherited

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 92 of file C4AulAST.h.

92 {}
template<class T >
void aul::AstVisitor::visit ( const T *  )
deleteinherited
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::Noop )
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 969 of file C4AulCompiler.cpp.

Referenced by C4AulCompiler::Compile().

969 {}

Here is the caller graph for this function:

void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::StringLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 971 of file C4AulCompiler.cpp.

References AB_STRING, C4V_String, C4StringTable::RegString(), and Strings.

972 {
973  StackGuard g(this, 1);
974  AddBCC(n->loc, AB_STRING, (intptr_t)::Strings.RegString(n->value.c_str()));
975  type_of_stack_top = C4V_String;
976 }
C4String * RegString(StdStrBuf String)
C4StringTable Strings
Definition: C4Globals.cpp:42
std::string value
Definition: C4AulAST.h:157
const char * loc
Definition: C4AulAST.h:123

Here is the call graph for this function:

void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::IntLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 978 of file C4AulCompiler.cpp.

References AB_INT, and C4V_Int.

979 {
980  StackGuard g(this, 1);
981  AddBCC(n->loc, AB_INT, n->value);
982  type_of_stack_top = C4V_Int;
983 }
uint32_t value
Definition: C4AulAST.h:165
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::BoolLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 985 of file C4AulCompiler.cpp.

References AB_BOOL, and C4V_Bool.

986 {
987  StackGuard g(this, 1);
988  AddBCC(n->loc, AB_BOOL, n->value);
989  type_of_stack_top = C4V_Bool;
990 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::ArrayLit n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 992 of file C4AulCompiler.cpp.

References AB_NEW_ARRAY, and C4V_Array.

993 {
994  StackGuard g(this, 1);
995  for (const auto &e : n->values)
996  {
997  SafeVisit(e);
998  }
999  AddBCC(n->loc, AB_NEW_ARRAY, n->values.size());
1000  type_of_stack_top = C4V_Array;
1001 }
std::vector< ExprPtr > values
Definition: C4AulAST.h:180
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::ProplistLit n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1003 of file C4AulCompiler.cpp.

References AB_NEW_PROPLIST, AB_STRING, C4V_PropList, C4StringTable::RegString(), and Strings.

1004 {
1005  StackGuard g(this, 1);
1006  for (const auto &e : n->values)
1007  {
1008  StackGuard g(this, 2);
1009  AddBCC(n->loc, AB_STRING, (intptr_t)::Strings.RegString(e.first.c_str()));
1010  SafeVisit(e.second);
1011  }
1012  AddBCC(n->loc, AB_NEW_PROPLIST, n->values.size());
1013  type_of_stack_top = C4V_PropList;
1014 }
C4String * RegString(StdStrBuf String)
C4StringTable Strings
Definition: C4Globals.cpp:42
const char * loc
Definition: C4AulAST.h:123
std::vector< std::pair< std::string, ExprPtr > > values
Definition: C4AulAST.h:187

Here is the call graph for this function:

void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::NilLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1016 of file C4AulCompiler.cpp.

References AB_NIL, and C4V_Nil.

1017 {
1018  StackGuard g(this, 1);
1019  AddBCC(n->loc, AB_NIL);
1020  type_of_stack_top = C4V_Nil;
1021 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::ThisLit n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1023 of file C4AulCompiler.cpp.

References AB_THIS, and C4V_PropList.

1024 {
1025  StackGuard g(this, 1);
1026  AddBCC(n->loc, AB_THIS);
1027  type_of_stack_top = C4V_PropList;
1028 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::VarExpr n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1030 of file C4AulCompiler.cpp.

References C4Value::_getArray(), C4Value::_getBool(), C4Value::_getFunction(), C4Value::_getInt(), C4Value::_getPropList(), C4Value::_getStr(), AB_BOOL, AB_CARRAY, AB_CFUNCTION, AB_CPROPLIST, AB_DUP, AB_GLOBALN, AB_INT, AB_LOCALN, AB_NIL, AB_STRING, C4V_Any, C4V_Array, C4V_Bool, C4V_Function, C4V_Int, C4V_Nil, C4V_PropList, C4V_String, ENSURE_COND, C4StringTable::FindString(), C4AulScriptEngine::GetGlobalConstant(), C4ValueMapNames::GetItemNr(), C4Value::GetType(), C4Value::GetTypeName(), C4AulScriptEngine::GlobalConstNames, C4AulScriptEngine::GlobalNamedNames, ScriptEngine, and Strings.

1031 {
1032  StackGuard g(this, 1);
1033  assert(Fn);
1034  C4Value dummy;
1035  const char *cname = n->identifier.c_str();
1036  C4String *interned = ::Strings.FindString(cname);
1037 
1038  // Reset known type of top of value stack so we don't keep the old one around
1039  type_of_stack_top = C4V_Any;
1040 
1041  // Lookup order: Parameters > var > local > global > global const
1042  // Why parameters are considered before function-scoped variables
1043  // you ask? I've no idea, but that's how it was before I started
1044  // changing things.
1045  // NOTE: If you change this, remember to also change the warning
1046  // (variable_shadows_variable) in PreparseAstVisitor.
1047  if (Fn->ParNamed.GetItemNr(cname) != -1)
1048  {
1049  int pos = Fn->ParNamed.GetItemNr(cname);
1050  AddVarAccess(n->loc, AB_DUP, -Fn->GetParCount() + pos);
1051  type_of_stack_top = Fn->GetParType()[pos];
1052  }
1053  else if (Fn->VarNamed.GetItemNr(cname) != -1)
1054  {
1055  AddVarAccess(n->loc, AB_DUP, Fn->VarNamed.GetItemNr(cname));
1056  }
1057  // Can't use Fn->Parent->HasProperty here because that only returns true
1058  // for immediate properties, while we also want to interrogate prototypes
1059  else if (Fn->Parent && interned && Fn->Parent->GetPropertyByS(interned, &dummy))
1060  {
1061  AddBCC(n->loc, AB_LOCALN, (intptr_t)interned);
1062  }
1063  else if (ScriptEngine.GlobalNamedNames.GetItemNr(cname) != -1)
1064  {
1066  }
1067  else if (ScriptEngine.GlobalConstNames.GetItemNr(cname) != -1)
1068  {
1069  C4Value v;
1070  ENSURE_COND(ScriptEngine.GetGlobalConstant(cname, &v), "internal error: global constant not retrievable");
1071  switch (v.GetType())
1072  {
1073  case C4V_Nil:
1074  AddBCC(n->loc, AB_NIL);
1075  break;
1076  case C4V_Int:
1077  AddBCC(n->loc, AB_INT, v._getInt());
1078  break;
1079  case C4V_Bool:
1080  AddBCC(n->loc, AB_BOOL, v._getBool());
1081  break;
1082  case C4V_PropList:
1083  AddBCC(n->loc, AB_CPROPLIST, reinterpret_cast<intptr_t>(v._getPropList()));
1084  break;
1085  case C4V_String:
1086  AddBCC(n->loc, AB_STRING, reinterpret_cast<intptr_t>(v._getStr()));
1087  break;
1088  case C4V_Array:
1089  AddBCC(n->loc, AB_CARRAY, reinterpret_cast<intptr_t>(v._getArray()));
1090  break;
1091  case C4V_Function:
1092  AddBCC(n->loc, AB_CFUNCTION, reinterpret_cast<intptr_t>(v._getFunction()));
1093  default:
1094  AddBCC(n->loc, AB_NIL);
1095  throw Error(target_host, host, n, Fn, "internal error: global constant of unexpected type: %s (of type %s)", cname, v.GetTypeName());
1096  }
1097  type_of_stack_top = v.GetType();
1098  }
1099  else
1100  {
1101  AddBCC(n->loc, AB_NIL);
1102  throw Error(target_host, host, n, Fn, "symbol not found in any symbol table: %s", cname);
1103  }
1104 }
C4ValueMapNames GlobalNamedNames
Definition: C4Aul.h:134
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
C4ValueMapNames ParNamed
bool _getBool() const
Definition: C4Value.h:123
C4String * FindString(const char *strString) const
bool GetGlobalConstant(const char *szName, C4Value *pTargetValue)
C4String * _getStr() const
Definition: C4Value.h:126
#define ENSURE_COND(cond, failmsg)
C4ValueMapNames GlobalConstNames
Definition: C4Aul.h:141
int GetParCount() const override
C4ValueArray * _getArray() const
Definition: C4Value.h:127
C4V_Type GetType() const
Definition: C4Value.h:161
C4StringTable Strings
Definition: C4Globals.cpp:42
const char * GetTypeName() const
Definition: C4Value.h:164
virtual bool GetPropertyByS(const C4String *k, C4Value *pResult) const
Definition: C4PropList.cpp:753
C4ValueMapNames VarNamed
int32_t _getInt() const
Definition: C4Value.h:122
int32_t GetItemNr(const char *strName) const
Definition: C4ValueMap.cpp:459
const C4V_Type * GetParType() const override
const char * loc
Definition: C4AulAST.h:123
C4AulFunc * _getFunction() const
Definition: C4Value.h:128
std::string identifier
Definition: C4AulAST.h:205
C4PropListStatic * Parent
Definition: C4AulFunc.h:56
C4PropList * _getPropList() const
Definition: C4Value.h:129

Here is the call graph for this function:

void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::UnOpExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1106 of file C4AulCompiler.cpp.

References AB_Dec, AB_Inc, and C4ScriptOpMap.

1107 {
1108  StackGuard g(this, 1);
1109 
1110  n->operand->accept(this);
1111  const auto &op = C4ScriptOpMap[n->op];
1112  if (op.Changer)
1113  {
1114  C4AulBCC setter = MakeSetter(n->loc, true);
1115  AddBCC(n->loc, op.Code, 0);
1116  AddBCC(n->loc, setter);
1117  // On postfix inc/dec, regenerate the previous value
1118  if (op.Postfix && (op.Code == AB_Inc || op.Code == AB_Dec))
1119  {
1120  AddBCC(n->loc, op.Code == AB_Inc ? AB_Dec : AB_Inc, 1);
1121  }
1122  }
1123  else
1124  {
1125  AddBCC(n->loc, op.Code);
1126  }
1127  type_of_stack_top = op.RetType;
1128 }
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:261
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::BinOpExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1130 of file C4AulCompiler.cpp.

References AB_JUMPAND, AB_JUMPNNIL, AB_JUMPOR, and C4ScriptOpMap.

1131 {
1132  StackGuard g(this, 1);
1133 
1134  SafeVisit(n->lhs);
1135 
1136  const auto &op = C4ScriptOpMap[n->op];
1137  if (op.Code == AB_JUMPAND || op.Code == AB_JUMPOR || op.Code == AB_JUMPNNIL)
1138  {
1139  // Short-circuiting operators. These are slightly more complex
1140  // because we don't want to evaluate their rhs operand when the
1141  // lhs one already decided the result
1142  int jump = AddBCC(n->loc, op.Code);
1143  SafeVisit(n->rhs);
1144  UpdateJump(jump, AddJumpTarget());
1145  }
1146  else if (op.Changer)
1147  {
1148  try
1149  {
1150  C4AulBCC setter = MakeSetter(n->loc, true);
1151  SafeVisit(n->rhs);
1152  AddBCC(n->loc, op.Code);
1153  AddBCC(n->loc, setter);
1154  }
1155  catch (C4AulParseError &e)
1156  {
1157  HandleError(e);
1158  }
1159  }
1160  else
1161  {
1162  SafeVisit(n->rhs);
1163  AddBCC(n->loc, op.Code, 0);
1164  }
1165 
1166  type_of_stack_top = op.RetType;
1167 }
const C4ScriptOpDef C4ScriptOpMap[]
Definition: C4AulParse.cpp:261
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::AssignmentExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1169 of file C4AulCompiler.cpp.

1170 {
1171  StackGuard g(this, 1);
1172  SafeVisit(n->lhs);
1173  try
1174  {
1175  C4AulBCC setter = MakeSetter(n->loc, false);
1176  SafeVisit(n->rhs);
1177  AddBCC(n->loc, setter);
1178  }
1179  catch (C4AulParseError &e)
1180  {
1181  HandleError(e);
1182  }
1183  // Assignment does not change the type of the variable
1184 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::SubscriptExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1186 of file C4AulCompiler.cpp.

References AB_ARRAYA, and C4V_Any.

1187 {
1188  StackGuard g(this, 1);
1189  SafeVisit(n->object);
1190  SafeVisit(n->index);
1191  AddBCC(n->loc, AB_ARRAYA);
1192 
1193  // FIXME: Check if the subscripted object is a literal and if so, retrieve type
1194  type_of_stack_top = C4V_Any;
1195 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::SliceExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1197 of file C4AulCompiler.cpp.

References AB_ARRAY_SLICE, and C4V_Array.

1198 {
1199  StackGuard g(this, 1);
1200  SafeVisit(n->object);
1201  SafeVisit(n->start);
1202  SafeVisit(n->end);
1203  AddBCC(n->loc, AB_ARRAY_SLICE);
1204 
1205  type_of_stack_top = C4V_Array;
1206 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::CallExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1208 of file C4AulCompiler.cpp.

References a, AB_CALL, AB_CALLFS, AB_DEBUG, AB_DUP, AB_FUNC, AB_NIL, AB_STACK, b, C4AUL_DebugBreak, C4AUL_Inherited, C4AUL_MAX_Par, C4AUL_SafeInherited, C4V_Any, C4V_Nil, C4ScriptHost::Engine, GetC4VName(), C4AulScriptEngine::GetFirstFunc(), C4PropList::GetFunc(), C4AulScriptEngine::GetNextSNFunc(), C4AulFunc::GetParCount(), C4AulFunc::Parent, C4StringTable::RegString(), Strings, and C4Value::WarnAboutConversion().

1209 {
1210  const char *cname = n->callee.c_str();
1211 
1212  if (n->callee == C4AUL_DebugBreak)
1213  {
1214  if (n->context)
1215  throw Error(target_host, host, n, Fn, R"("%s" can't be called in a different context)", cname);
1216  if (!n->args.empty())
1217  throw Error(target_host, host, n, Fn, R"("%s" must not have any arguments)", cname);
1218 
1219  AddBCC(n->loc, AB_DEBUG);
1220  // Add a pseudo-nil to keep the stack balanced
1221  AddBCC(n->loc, AB_NIL);
1222  type_of_stack_top = C4V_Nil;
1223  return;
1224  }
1225 
1226  if (n->callee == C4AUL_Inherited || n->callee == C4AUL_SafeInherited)
1227  {
1228  // inherited can only be called within the same context
1229  if (n->context)
1230  {
1231  throw Error(target_host, host, n, Fn, R"("%s" can't be called in a different context)", cname);
1232  }
1233  }
1234 
1235  if (n->callee == C4AUL_Inherited && !Fn->OwnerOverloaded)
1236  {
1237  throw Error(target_host, host, n, Fn, "inherited function not found (use " C4AUL_SafeInherited " to disable this message)");
1238  }
1239 
1240  const auto pre_call_stack = stack_height;
1241 
1242  if (n->context)
1243  SafeVisit(n->context);
1244 
1245  std::vector<C4V_Type> known_par_types;
1246  known_par_types.reserve(n->args.size());
1247 
1248  for (const auto &arg : n->args)
1249  {
1250  SafeVisit(arg);
1251  known_par_types.push_back(type_of_stack_top);
1252  }
1253 
1254  C4AulFunc *callee = nullptr;
1255 
1256  // Special handling for the overload chain
1257  if (n->callee == C4AUL_Inherited || n->callee == C4AUL_SafeInherited)
1258  {
1259  callee = Fn->OwnerOverloaded;
1260  }
1261 
1262  size_t fn_argc = C4AUL_MAX_Par;
1263  if (!n->context)
1264  {
1265  // if this is a function without explicit context, we resolve it
1266  if (!callee)
1267  callee = Fn->Parent->GetFunc(cname);
1268  if (!callee && target_host)
1269  callee = target_host->Engine->GetFunc(cname);
1270 
1271  if (callee)
1272  {
1273  fn_argc = callee->GetParCount();
1274  }
1275  else
1276  {
1277  // pop all args off the stack
1278  if (!n->args.empty())
1279  AddBCC(n->loc, AB_STACK, -(intptr_t)n->args.size());
1280  // and "return" nil
1281  AddBCC(n->loc, AB_NIL);
1282  type_of_stack_top = C4V_Nil;
1283 
1284  if (n->callee != C4AUL_SafeInherited)
1285  {
1286  HandleError(Error(target_host, host, n, Fn, "called function not found: %s", cname));
1287  }
1288  return;
1289  }
1290  }
1291 
1292  if (n->args.size() > fn_argc)
1293  {
1294  // Pop off any args that are over the limit
1295  Warn(target_host, host, n->args[fn_argc].get(), Fn, C4AulWarningId::arg_count_mismatch,
1296  cname, (unsigned)n->args.size(), fn_argc);
1297  AddBCC(n->loc, AB_STACK, fn_argc - n->args.size());
1298  }
1299  else if (n->args.size() < fn_argc)
1300  {
1301  if (n->append_unnamed_pars)
1302  {
1303  assert(Fn->GetParCount() == C4AUL_MAX_Par);
1304  int missing_par_count = fn_argc - n->args.size();
1305  int available_par_count = Fn->GetParCount() - Fn->ParNamed.iSize;
1306  for (int i = 0; i < std::min(missing_par_count, available_par_count); ++i)
1307  {
1308  AddVarAccess(n->loc, AB_DUP, -Fn->GetParCount() + Fn->ParNamed.iSize + i);
1309  }
1310  // Fill up remaining, unsettable parameters with nil
1311  if (available_par_count < missing_par_count)
1312  AddBCC(n->loc, AB_STACK, missing_par_count - available_par_count);
1313  }
1314  else if (fn_argc > n->args.size())
1315  {
1316  // Add nil for each missing parameter
1317  AddBCC(n->loc, AB_STACK, fn_argc - n->args.size());
1318  }
1319  }
1320 
1321  // Check passed parameters for this call (as far as possible)
1322  std::vector<C4V_Type> expected_par_types;
1323  if (n->context)
1324  {
1325  AddBCC(n->loc, n->safe_call ? AB_CALLFS : AB_CALL, (intptr_t)::Strings.RegString(cname));
1326  // Since we don't know the context in which this call will happen at
1327  // runtime, we'll check whether all available functions with the same
1328  // name agree on their parameters.
1329  const C4AulFunc *candidate = target_host ? target_host->Engine->GetFirstFunc(cname) : nullptr;
1330  if (candidate)
1331  {
1332  expected_par_types.assign(candidate->GetParType(), candidate->GetParType() + candidate->GetParCount());
1333  while ((candidate = target_host->Engine->GetNextSNFunc(candidate)) != nullptr)
1334  {
1335  if (candidate->GetParCount() > expected_par_types.size())
1336  {
1337  expected_par_types.resize(candidate->GetParCount(), C4V_Any);
1338  }
1339  for (size_t i = 0; i < expected_par_types.size(); ++i)
1340  {
1341  C4V_Type a = expected_par_types[i];
1342  C4V_Type b = candidate->GetParType()[i];
1343  // If we can convert one of the types into the other
1344  // without a warning, use the wider one
1345  bool implicit_a_to_b = !C4Value::WarnAboutConversion(a, b);
1346  bool implicit_b_to_a = !C4Value::WarnAboutConversion(b, a);
1347  if (implicit_a_to_b && !implicit_b_to_a)
1348  expected_par_types[i] = b;
1349  else if (implicit_b_to_a && !implicit_a_to_b)
1350  expected_par_types[i] = a;
1351  // but if we can convert neither of the types into the
1352  // other, give up and assume the user will do the right
1353  // thing
1354  else if (!implicit_a_to_b && !implicit_b_to_a)
1355  expected_par_types[i] = C4V_Any;
1356  }
1357  }
1358  }
1359  type_of_stack_top = C4V_Any;
1360  }
1361  else
1362  {
1363  assert(callee);
1364  AddBCC(n->loc, AB_FUNC, (intptr_t)callee);
1365  expected_par_types.assign(callee->GetParType(), callee->GetParType() + callee->GetParCount());
1366  type_of_stack_top = callee->GetRetType();
1367  }
1368 
1369  // Check parameters
1370  for (size_t i = 0; i < std::min(known_par_types.size(), expected_par_types.size()); ++i)
1371  {
1372  C4V_Type from = known_par_types[i];
1373  C4V_Type to = expected_par_types[i];
1374  if (C4Value::WarnAboutConversion(from, to))
1375  {
1376  Warn(target_host, host, n->args[i].get(), Fn, C4AulWarningId::arg_type_mismatch, (unsigned)i, cname, GetC4VName(from), GetC4VName(to));
1377  }
1378  }
1379 
1380  // We leave one value (the return value) on the stack
1381  assert(pre_call_stack + 1 == stack_height);
1382 }
C4V_Type
Definition: C4Value.h:23
#define C4AUL_Inherited
C4ValueMapNames ParNamed
#define b
C4AulFunc * GetFunc(C4PropertyName k) const
Definition: C4PropList.h:105
C4String * RegString(StdStrBuf String)
bool append_unnamed_pars
Definition: C4AulAST.h:255
std::string callee
Definition: C4AulAST.h:258
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
#define a
virtual const C4V_Type * GetParType() const =0
std::vector< ExprPtr > args
Definition: C4AulAST.h:257
C4AulFunc * OwnerOverloaded
C4AulFunc * GetFirstFunc(const char *Name)
Definition: C4Aul.h:117
int GetParCount() const override
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
C4AulFunc * GetNextSNFunc(const C4AulFunc *After)
Definition: C4Aul.h:119
C4StringTable Strings
Definition: C4Globals.cpp:42
virtual C4V_Type GetRetType() const =0
static bool WarnAboutConversion(C4V_Type Type, C4V_Type vtToType)
Definition: C4Value.cpp:111
const char * GetC4VName(const C4V_Type Type)
Definition: C4Value.cpp:32
#define C4AUL_DebugBreak
const char * loc
Definition: C4AulAST.h:123
#define C4AUL_SafeInherited
C4PropListStatic * Parent
Definition: C4AulFunc.h:56
virtual int GetParCount() const
Definition: C4AulFunc.h:70

Here is the call graph for this function:

void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::ParExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1384 of file C4AulCompiler.cpp.

References AB_PAR, and C4V_Any.

1385 {
1386  StackGuard g(this, 1);
1387 
1388  SafeVisit(n->arg);
1389  AddBCC(n->loc, AB_PAR);
1390  type_of_stack_top = C4V_Any;
1391 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::Block n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1393 of file C4AulCompiler.cpp.

References s.

1394 {
1395  for (const auto &s : n->children)
1396  {
1397  StackGuard g(this, 0);
1398  if (SafeVisit(s))
1399  {
1400  // If the statement has left a stack value, pop it off
1401  MaybePopValueOf(s);
1402  }
1403  }
1404 }
std::vector< StmtPtr > children
Definition: C4AulAST.h:273
#define s
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::Return n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1406 of file C4AulCompiler.cpp.

References AB_RETURN.

1407 {
1408  StackGuard g(this, 0);
1409 
1410  SafeVisit(n->value);
1411  AddBCC(n->loc, AB_RETURN);
1412 }
ExprPtr value
Definition: C4AulAST.h:284
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::ForLoop n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1414 of file C4AulCompiler.cpp.

References AB_CONDN, and AB_JUMP.

1415 {
1416  // Bytecode arranged like this:
1417  // initializer
1418  // cond: condition
1419  // CONDN exit
1420  // body: body
1421  // incr: incrementor
1422  // JUMP cond
1423  // exit:
1424  //
1425  // continue jumps to incr
1426  // break jumps to exit
1427 
1428  if (n->init)
1429  {
1430  if (SafeVisit(n->init))
1431  MaybePopValueOf(n->init);
1432  }
1433  int cond = -1, condition_jump = -1;
1434  PushLoop();
1435  if (n->cond)
1436  {
1437  cond = AddJumpTarget();
1438  SafeVisit(n->cond);
1439  active_loops.top().breaks.push_back(AddBCC(n->cond->loc, AB_CONDN));
1440  }
1441 
1442  int body = AddJumpTarget();
1443  if (!n->cond)
1444  cond = body;
1445  if (SafeVisit(n->body))
1446  MaybePopValueOf(n->body);
1447 
1448  int incr = -1;
1449  if (n->incr)
1450  {
1451  incr = AddJumpTarget();
1452  if (SafeVisit(n->incr))
1453  MaybePopValueOf(n->incr);
1454  }
1455  else
1456  {
1457  // If no incrementor exists, just jump straight to the condition
1458  incr = cond;
1459  }
1460  // start the next iteration of the loop
1461  AddJumpTo(n->loc, AB_JUMP, cond);
1462  PopLoop(incr);
1463 }
StmtPtr body
Definition: C4AulAST.h:291
ExprPtr cond
Definition: C4AulAST.h:290
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::RangeLoop n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1465 of file C4AulCompiler.cpp.

References AB_FOREACH_NEXT, AB_INT, and AB_STACK.

1466 {
1467  // Bytecode arranged like this:
1468  // condition (aka iterated array)
1469  // INT 0 (the loop index variable)
1470  // cond: FOREACH_NEXT
1471  // JUMP exit
1472  // body: body
1473  // JUMP cond
1474  // exit: STACK -2 (to clean the iteration variables)
1475  //
1476  // continue jumps to cond
1477  // break jumps to exit
1478 
1479  const char *cname = n->var.c_str();
1480  int var_id = Fn->VarNamed.GetItemNr(cname);
1481  assert(var_id != -1 && "CodegenAstVisitor: unable to find variable in foreach");
1482  if (var_id == -1)
1483  throw Error(target_host, host, n, Fn, "internal error: unable to find variable in foreach: %s", cname);
1484  // Emit code for array
1485  SafeVisit(n->cond);
1486  // Emit code for iteration
1487  AddBCC(n->loc, AB_INT, 0);
1488  int cond = AddJumpTarget();
1489  PushLoop();
1490  AddVarAccess(n->loc, AB_FOREACH_NEXT, var_id);
1491  AddLoopControl(n->loc, Loop::Control::Break); // Will be skipped by AB_FOREACH_NEXT as long as more entries exist
1492 
1493  // Emit body
1494  if (SafeVisit(n->body))
1495  MaybePopValueOf(n->body);
1496  // continue starts the next iteration of the loop
1497  AddLoopControl(n->loc, Loop::Control::Continue);
1498  PopLoop(cond);
1499  // Pop off iterator and array
1500  AddBCC(n->loc, AB_STACK, -2);
1501 }
std::string var
Definition: C4AulAST.h:307
StmtPtr body
Definition: C4AulAST.h:291
ExprPtr cond
Definition: C4AulAST.h:290
C4ValueMapNames VarNamed
int32_t GetItemNr(const char *strName) const
Definition: C4ValueMap.cpp:459
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::DoLoop n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1539 of file C4AulCompiler.cpp.

References AB_COND.

1540 {
1541  int body = AddJumpTarget();
1542  PushLoop();
1543  if (SafeVisit(n->body))
1544  MaybePopValueOf(n->body);
1545  int cond = AddJumpTarget();
1546  SafeVisit(n->cond);
1547  AddJumpTo(n->loc, AB_COND, body);
1548  PopLoop(cond);
1549 }
StmtPtr body
Definition: C4AulAST.h:291
ExprPtr cond
Definition: C4AulAST.h:290
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::WhileLoop n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1551 of file C4AulCompiler.cpp.

References AB_CONDN.

1552 {
1553  int cond = AddJumpTarget();
1554  PushLoop();
1555  SafeVisit(n->cond);
1556  active_loops.top().breaks.push_back(AddBCC(n->cond->loc, AB_CONDN));
1557  if (SafeVisit(n->body))
1558  MaybePopValueOf(n->body);
1559  // continue starts the next iteration of the loop
1560  AddLoopControl(n->loc, Loop::Control::Continue);
1561  PopLoop(cond);
1562 }
StmtPtr body
Definition: C4AulAST.h:291
ExprPtr cond
Definition: C4AulAST.h:290
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::Break n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1564 of file C4AulCompiler.cpp.

References ENSURE_COND.

1565 {
1566  ENSURE_COND(!active_loops.empty(), "'break' outside loop");
1567  AddLoopControl(n->loc, Loop::Control::Break);
1568 }
#define ENSURE_COND(cond, failmsg)
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::Continue n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 1570 of file C4AulCompiler.cpp.

References ENSURE_COND.

1571 {
1572  ENSURE_COND(!active_loops.empty(), "'continue' outside loop");
1573  AddLoopControl(n->loc, Loop::Control::Continue);
1574 }
#define ENSURE_COND(cond, failmsg)
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::If n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1576 of file C4AulCompiler.cpp.

References AB_CONDN, and AB_JUMP.

1577 {
1578  SafeVisit(n->cond);
1579  int jump = AddBCC(n->loc, AB_CONDN);
1580  // Warn if we're controlling a no-op ("if (...);")
1581  if (dynamic_cast<::aul::ast::Noop*>(n->iftrue.get()))
1582  {
1583  Warn(target_host, host, n->iftrue->loc, Fn, C4AulWarningId::empty_if);
1584  }
1585  if (SafeVisit(n->iftrue))
1586  MaybePopValueOf(n->iftrue);
1587 
1588  if (dynamic_cast<::aul::ast::Noop*>(n->iffalse.get()))
1589  {
1590  Warn(target_host, host, n->iffalse->loc, Fn, C4AulWarningId::empty_if);
1591  }
1592 
1593  if (n->iffalse)
1594  {
1595  int jumpout = AddBCC(n->loc, AB_JUMP);
1596  UpdateJump(jump, AddJumpTarget());
1597  jump = jumpout;
1598  if (SafeVisit(n->iffalse))
1599  MaybePopValueOf(n->iffalse);
1600  }
1601  UpdateJump(jump, AddJumpTarget());
1602 }
ExprPtr cond
Definition: C4AulAST.h:338
StmtPtr iftrue
Definition: C4AulAST.h:339
StmtPtr iffalse
Definition: C4AulAST.h:339
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::VarDecl n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1604 of file C4AulCompiler.cpp.

References AB_POP_TO, AB_STACK, and Object().

1605 {
1606  for (const auto &dec : n->decls)
1607  {
1608  const char *cname = dec.name.c_str();
1609  switch (n->scope)
1610  {
1611  case ::aul::ast::VarDecl::Scope::Func:
1612  if (dec.init)
1613  {
1614  // Emit code for the initializer
1615  SafeVisit(dec.init);
1616  int var_idx = Fn->VarNamed.GetItemNr(cname);
1617  assert(var_idx >= 0 && "CodegenAstVisitor: var not found in variable table");
1618  if (var_idx < 0)
1619  {
1620  AddBCC(n->loc, AB_STACK, -1);
1621  throw Error(target_host, host, n, Fn, "internal error: var not found in variable table: %s", cname);
1622  }
1623  AddVarAccess(n->loc, AB_POP_TO, var_idx);
1624  }
1625  break;
1627  case ::aul::ast::VarDecl::Scope::Global:
1628  // Object-local and global constants are handled by ConstantResolver.
1629  break;
1630  }
1631  }
1632 }
C4ValueMapNames VarNamed
int32_t GetItemNr(const char *strName) const
Definition: C4ValueMap.cpp:459
std::vector< Var > decls
Definition: C4AulAST.h:365
C4Object * Object(C4PropList *_this)
Definition: C4AulDefFunc.h:34
const char * loc
Definition: C4AulAST.h:123

Here is the call graph for this function:

void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::FunctionDecl n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1634 of file C4AulCompiler.cpp.

References C4ScriptHost::Engine, C4StringTable::FindString(), C4PropList::GetFunc(), C4AulFunc::GetName(), C4ScriptHost::GetPropList(), C4AulScriptEngine::GetPropList(), C4AulScriptFunc::OwnerOverloaded, C4AulFunc::Parent, C4AulScriptFunc::pOrgScript, C4AulFunc::SFunc(), and Strings.

1635 {
1636  assert(!Fn && "CodegenAstVisitor: function declaration encountered within active function");
1637  if (Fn)
1638  throw Error(target_host, host, n, Fn, "internal error: function declaration for '%s' encountered within active function", n->name.c_str());
1639 
1640  C4PropListStatic *Parent = n->is_global ? target_host->Engine->GetPropList() : target_host->GetPropList();
1641 
1642  C4String *name = ::Strings.FindString(n->name.c_str());
1643  C4AulFunc *f = Parent->GetFunc(name);
1644  while (f)
1645  {
1646  if (f->SFunc() && f->SFunc()->pOrgScript == host && f->Parent == Parent)
1647  {
1648  if (Fn)
1649  Warn(target_host, host, n, Fn, C4AulWarningId::redeclaration, "function", f->GetName());
1650  Fn = f->SFunc();
1651  }
1652  f = f->SFunc() ? f->SFunc()->OwnerOverloaded : nullptr;
1653  }
1654 
1655  if (!Fn && Parent->HasProperty(name))
1656  {
1657  throw Error(target_host, host, n, Fn, "declaration of '%s': cannot override local variable via 'func %s'", n->name.c_str(), n->name.c_str());
1658  }
1659 
1660  assert(Fn && "CodegenAstVisitor: unable to find function definition");
1661  if (!Fn)
1662  throw Error(target_host, host, n, Fn, "internal error: unable to find function definition for %s", n->name.c_str());
1663 
1664  // If this isn't a global function, but there is a global one with
1665  // the same name, and this function isn't overloading a different
1666  // one, add the global function to the overload chain
1667  if (!n->is_global && !Fn->OwnerOverloaded)
1668  {
1669  C4AulFunc *global_parent = target_host->Engine->GetFunc(Fn->GetName());
1670  if (global_parent)
1671  Fn->SetOverloaded(global_parent);
1672  }
1673 
1674  try
1675  {
1676  EmitFunctionCode(n);
1677  Fn = nullptr;
1678  }
1679  catch (...)
1680  {
1681  Fn = nullptr;
1682  throw;
1683  }
1684 }
C4PropListStatic * GetPropList()
Definition: C4Aul.h:151
C4String * FindString(const char *strString) const
const char * GetName() const
Definition: C4AulFunc.h:57
C4AulFunc * GetFunc(C4PropertyName k) const
Definition: C4PropList.h:105
C4ScriptHost * pOrgScript
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
C4AulFunc * OwnerOverloaded
C4StringTable Strings
Definition: C4Globals.cpp:42
std::string name
Definition: C4AulAST.h:390
C4PropListStatic * Parent
Definition: C4AulFunc.h:56
virtual C4AulScriptFunc * SFunc()
Definition: C4AulFunc.h:66

Here is the call graph for this function:

void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::FunctionExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1686 of file C4AulCompiler.cpp.

References AB_NIL.

1687 {
1688  AddBCC(n->loc, AB_NIL);
1689  throw Error(target_host, host, n, Fn, "can't define a function in a function-scoped proplist");
1690 }
const char * loc
Definition: C4AulAST.h:123
void C4AulCompiler::CodegenAstVisitor::visit ( const ::aul::ast::Script n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 1692 of file C4AulCompiler.cpp.

1693 {
1694  for (const auto &d : n->declarations)
1695  {
1696  SafeVisit(d);
1697  }
1698 }
std::vector< DeclPtr > declarations
Definition: C4AulAST.h:425

The documentation for this class was generated from the following file: