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

Public Member Functions

 PreparseAstVisitor (C4ScriptHost *host, C4ScriptHost *source_host, C4AulScriptFunc *func=nullptr)
 
 PreparseAstVisitor (C4AulScriptFunc *func)
 
 ~PreparseAstVisitor () override=default
 
void visit (const ::aul::ast::RangeLoop *n) override
 
void visit (const ::aul::ast::VarDecl *n) override
 
void visit (const ::aul::ast::FunctionDecl *n) override
 
void visit (const ::aul::ast::CallExpr *n) override
 
void visit (const ::aul::ast::ParExpr *n) override
 
void visit (const ::aul::ast::AppendtoPragma *n) override
 
void visit (const ::aul::ast::IncludePragma *n) override
 
void visit (const ::aul::ast::Script *n) override
 
void visit (const ::aul::ast::ArrayLit *n) override
 
void visit (const ::aul::ast::ProplistLit *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::Block *n) override
 
void visit (const ::aul::ast::Return *n) override
 
void visit (const ::aul::ast::ForLoop *n) override
 
void visit (const ::aul::ast::DoLoop *n) override
 
void visit (const ::aul::ast::WhileLoop *n) override
 
void visit (const ::aul::ast::If *n) override
 
void visit (const ::aul::ast::FunctionExpr *n) override
 
virtual void visit (const ::aul::ast::Noop *)
 
virtual void visit (const ::aul::ast::StringLit *)
 
virtual void visit (const ::aul::ast::IntLit *)
 
virtual void visit (const ::aul::ast::BoolLit *)
 
virtual void visit (const ::aul::ast::NilLit *)
 
virtual void visit (const ::aul::ast::ThisLit *)
 
virtual void visit (const ::aul::ast::VarExpr *n)
 
virtual void visit (const ::aul::ast::Break *)
 
virtual void visit (const ::aul::ast::Continue *)
 
template<class T >
void visit (const T *)=delete
 

Detailed Description

Definition at line 124 of file C4AulCompiler.cpp.

Constructor & Destructor Documentation

C4AulCompiler::PreparseAstVisitor::PreparseAstVisitor ( C4ScriptHost host,
C4ScriptHost source_host,
C4AulScriptFunc func = nullptr 
)
inline

Definition at line 134 of file C4AulCompiler.cpp.

134 : target_host(host), host(source_host), Fn(func) {}
C4AulCompiler::PreparseAstVisitor::PreparseAstVisitor ( C4AulScriptFunc func)
inlineexplicit

Definition at line 135 of file C4AulCompiler.cpp.

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

Member Function Documentation

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 63 of file C4AulAST.h.

63 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::StringLit )
inlinevirtualinherited

Reimplemented in C4AulCompiler::ConstexprEvaluator, and C4AulCompiler::CodegenAstVisitor.

Definition at line 64 of file C4AulAST.h.

64 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::IntLit )
inlinevirtualinherited

Reimplemented in C4AulCompiler::ConstexprEvaluator, and C4AulCompiler::CodegenAstVisitor.

Definition at line 65 of file C4AulAST.h.

65 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::BoolLit )
inlinevirtualinherited

Reimplemented in C4AulCompiler::ConstexprEvaluator, and C4AulCompiler::CodegenAstVisitor.

Definition at line 66 of file C4AulAST.h.

66 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::NilLit )
inlinevirtualinherited

Reimplemented in C4AulCompiler::ConstexprEvaluator, and C4AulCompiler::CodegenAstVisitor.

Definition at line 69 of file C4AulAST.h.

69 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::ThisLit )
inlinevirtualinherited

Reimplemented in C4AulCompiler::ConstexprEvaluator, and C4AulCompiler::CodegenAstVisitor.

Definition at line 70 of file C4AulAST.h.

70 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::VarExpr n)
inlinevirtualinherited

Reimplemented in C4AulCompiler::ConstexprEvaluator, and C4AulCompiler::CodegenAstVisitor.

Definition at line 71 of file C4AulAST.h.

71 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::Break )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 85 of file C4AulAST.h.

85 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::Continue )
inlinevirtualinherited

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 86 of file C4AulAST.h.

86 {}
template<class T >
void aul::AstVisitor::visit ( const T *  )
deleteinherited
void C4AulCompiler::PreparseAstVisitor::visit ( const ::aul::ast::RangeLoop n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 454 of file C4AulCompiler.cpp.

References C4ValueMapNames::AddName(), C4ValueMapNames::GetItemNr(), and C4AulScriptFunc::VarNamed.

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

455 {
456  const char *cname = n->var.c_str();
457  if (n->scoped_var)
458  {
459  Fn->VarNamed.AddName(cname);
460  }
461  else
462  {
463  // Loop variable not explicitly declared here. Look it up in
464  // the function and warn if it hasn't been declared at all.
465  if (Fn->VarNamed.GetItemNr(cname) == -1)
466  {
467  Warn(target_host, host, n, Fn, C4AulWarningId::implicit_range_loop_var_decl, cname);
468  Fn->VarNamed.AddName(cname);
469  }
470  }
471  DefaultRecursiveVisitor::visit(n);
472 }
std::string var
Definition: C4AulAST.h:307
int32_t AddName(const char *pnName)
Definition: C4ValueMap.cpp:429
C4ValueMapNames VarNamed
int32_t GetItemNr(const char *strName) const
Definition: C4ValueMap.cpp:459

Here is the call graph for this function:

Here is the caller graph for this function:

void C4AulCompiler::PreparseAstVisitor::visit ( const ::aul::ast::VarDecl n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 474 of file C4AulCompiler.cpp.

References C4ValueMapNames::AddName(), C4VNull, C4ScriptHost::Engine, C4StringTable::FindString(), aul::ast::VarDecl::Func, C4ValueMapNames::GetItemNr(), C4ScriptHost::GetPropList(), aul::ast::VarDecl::Global, C4AulScriptEngine::GlobalConstNames, C4AulScriptEngine::GlobalNamedNames, C4PropList::HasProperty(), Object(), C4StringTable::RegString(), s, C4PropList::SetPropertyByS(), and Strings.

475 {
477  {
478  Warn(target_host, host, n, Fn, C4AulWarningId::non_global_var_is_never_const);
479  }
480  for (const auto &var : n->decls)
481  {
482  const char *cname = var.name.c_str();
483  switch (n->scope)
484  {
485  case ::aul::ast::VarDecl::Scope::Func:
486  {
487  assert(Fn && "function-local var declaration outside of function");
488  if (!Fn)
489  throw Error(target_host, host, n, Fn, "internal error: function-local var declaration outside of function");
490 
491  if (target_host)
492  {
493  // if target_host is unset, we're parsing this func for direct execution,
494  // in which case we don't want to warn about variable hiding.
495  if (target_host->Engine->GlobalNamedNames.GetItemNr(cname) >= 0 || target_host->Engine->GlobalConstNames.GetItemNr(cname) >= 0)
496  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "local variable", cname, "global variable");
497  C4String *s = ::Strings.FindString(cname);
498  if (s && target_host->GetPropList()->HasProperty(s))
499  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "local variable", cname, "object-local variable");
500  if (Fn->ParNamed.GetItemNr(cname) != -1)
501  {
502  // The parameter order of this warning is correct:
503  // Aul looks up parameters before local variables, so
504  // the parameter actually shadows the local variable.
505  // This doesn't make a whole lot of sense and should
506  // probably be changed.
507  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "parameter", cname, "local variable");
508  }
509  }
510  Fn->VarNamed.AddName(cname);
511  break;
512  }
514  {
515  if (host->Engine->GlobalNamedNames.GetItemNr(cname) >= 0 || host->Engine->GlobalConstNames.GetItemNr(cname) >= 0)
516  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "object-local variable", cname, "global variable");
517  C4String *s = ::Strings.RegString(cname);
518  if (target_host->GetPropList()->HasProperty(s))
519  Warn(target_host, host, n, Fn, C4AulWarningId::redeclaration, "object-local variable", cname);
520  else
521  target_host->GetPropList()->SetPropertyByS(s, C4VNull);
522  break;
523  }
524  case ::aul::ast::VarDecl::Scope::Global:
525  assert(!Fn && "global var declaration inside function");
526  if (Fn)
527  throw Error(target_host, host, n, Fn, "internal error: global var declaration inside function");
528 
529  if (host->Engine->GlobalNamedNames.GetItemNr(cname) >= 0 || host->Engine->GlobalConstNames.GetItemNr(cname) >= 0)
530  Warn(target_host, host, n, Fn, C4AulWarningId::redeclaration, "global variable", cname);
531  if (n->constant)
532  host->Engine->GlobalConstNames.AddName(cname);
533  else
534  host->Engine->GlobalNamedNames.AddName(cname);
535  break;
536  }
537  }
538 
540  {
541  // only func-scoped variables can potentially have initializers we care
542  // about in the pre-parsing stage: they may have calls that pass
543  // unnamed parameters
544  DefaultRecursiveVisitor::visit(n);
545  }
546 }
C4ValueMapNames GlobalNamedNames
Definition: C4Aul.h:134
C4ValueMapNames ParNamed
C4String * FindString(const char *strString) const
C4String * RegString(StdStrBuf String)
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
C4ValueMapNames GlobalConstNames
Definition: C4Aul.h:141
int32_t AddName(const char *pnName)
Definition: C4ValueMap.cpp:429
C4StringTable Strings
Definition: C4Globals.cpp:42
C4ValueMapNames VarNamed
int32_t GetItemNr(const char *strName) const
Definition: C4ValueMap.cpp:459
std::vector< Var > decls
Definition: C4AulAST.h:365
bool HasProperty(C4String *k) const
Definition: C4PropList.h:118
C4Object * Object(C4PropList *_this)
Definition: C4AulDefFunc.h:34
const C4Value C4VNull
Definition: C4Value.cpp:30
virtual void SetPropertyByS(C4String *k, const C4Value &to)
Definition: C4PropList.cpp:971
#define s

Here is the call graph for this function:

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

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 548 of file C4AulCompiler.cpp.

References C4AUL_MAX_Par, C4VFunction(), C4ScriptHost::Engine, C4PropList::GetFunc(), C4ScriptHost::GetPropList(), C4AulScriptEngine::GetPropList(), C4ScriptHost::ownedFunctions, and C4PropList::SetPropertyByS().

549 {
550  // create script fn
551  C4PropListStatic *Parent = n->is_global ? target_host->Engine->GetPropList() : target_host->GetPropList();
552  const char *cname = n->name.c_str();
553 
554  assert(!Fn);
555 
556  // Look up the overloaded function before adding the overloading one
557  C4AulFunc *parent_func = Parent->GetFunc(cname);
558 
559  Fn = new C4AulScriptFunc(Parent, target_host, cname, n->loc);
560  host->ownedFunctions.push_back(C4VFunction(Fn));
561  for (const auto &param : n->params)
562  {
563  Fn->AddPar(param.name.c_str(), param.type);
564  }
565  if (n->has_unnamed_params)
566  Fn->ParCount = C4AUL_MAX_Par;
567 
568  // Add function to def/engine
569  Fn->SetOverloaded(parent_func);
570  Parent->SetPropertyByS(Fn->Name, C4VFunction(Fn));
571 
572  try
573  {
574  DefaultRecursiveVisitor::visit(n);
575  Fn = nullptr;
576  }
577  catch (...)
578  {
579  Fn = nullptr;
580  throw;
581  }
582 }
C4PropListStatic * GetPropList()
Definition: C4Aul.h:151
C4AulFunc * GetFunc(C4PropertyName k) const
Definition: C4PropList.h:105
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
std::vector< Parameter > params
Definition: C4AulAST.h:377
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
bool has_unnamed_params
Definition: C4AulAST.h:378
void AddPar(const char *Idtf, C4V_Type type=C4V_Any)
const char * loc
Definition: C4AulAST.h:123
void SetOverloaded(C4AulFunc *)
std::string name
Definition: C4AulAST.h:390
C4Value C4VFunction(C4AulFunc *pFn)
Definition: C4Value.h:250
virtual void SetPropertyByS(C4String *k, const C4Value &to)
Definition: C4PropList.cpp:971
C4RefCntPointer< C4String > Name
Definition: C4AulFunc.h:60
std::vector< C4Value > ownedFunctions
Definition: C4ScriptHost.h:96

Here is the call graph for this function:

void C4AulCompiler::PreparseAstVisitor::visit ( const ::aul::ast::CallExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 584 of file C4AulCompiler.cpp.

References C4AUL_MAX_Par.

585 {
586  if (n->append_unnamed_pars && Fn->ParCount != C4AUL_MAX_Par)
587  {
588  Fn->ParCount = C4AUL_MAX_Par;
589  }
590  DefaultRecursiveVisitor::visit(n);
591 }
bool append_unnamed_pars
Definition: C4AulAST.h:255
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
void C4AulCompiler::PreparseAstVisitor::visit ( const ::aul::ast::ParExpr n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 593 of file C4AulCompiler.cpp.

References C4AUL_MAX_Par.

594 {
595  if (Fn->ParCount != C4AUL_MAX_Par)
596  {
597  Warn(target_host, host, n, Fn, C4AulWarningId::undeclared_varargs, "Par()");
598  Fn->ParCount = C4AUL_MAX_Par;
599  }
600  DefaultRecursiveVisitor::visit(n);
601 }
#define C4AUL_MAX_Par
Definition: C4AulFunc.h:26
void C4AulCompiler::PreparseAstVisitor::visit ( const ::aul::ast::AppendtoPragma n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 603 of file C4AulCompiler.cpp.

References C4ScriptHost::Appends.

604 {
605  if (n->what.empty())
606  host->Appends.emplace_back("*");
607  else
608  host->Appends.emplace_back(n->what.c_str());
609 }
std::list< StdCopyStrBuf > Appends
Definition: C4ScriptHost.h:80
void C4AulCompiler::PreparseAstVisitor::visit ( const ::aul::ast::IncludePragma n)
overridevirtual

Reimplemented from aul::AstVisitor.

Definition at line 611 of file C4AulCompiler.cpp.

References C4ScriptHost::Includes.

612 {
613  host->Includes.emplace_back(n->what.c_str());
614 }
std::list< StdCopyStrBuf > Includes
Definition: C4ScriptHost.h:79
void C4AulCompiler::PreparseAstVisitor::visit ( const ::aul::ast::Script n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 616 of file C4AulCompiler.cpp.

References C4ScriptHost::Engine, C4AulScriptEngine::GetErrorHandler(), C4AulErrorHandler::OnError(), and C4AulError::what().

617 {
618  for (const auto &d : n->declarations)
619  {
620  try
621  {
622  d->accept(this);
623  }
624  catch (C4AulParseError &e)
625  {
626  target_host->Engine->GetErrorHandler()->OnError(e.what());
627  }
628  }
629 }
C4AulErrorHandler * GetErrorHandler() const
Definition: C4Aul.h:173
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
std::vector< DeclPtr > declarations
Definition: C4AulAST.h:425
const char * what() const noexceptoverride
virtual void OnError(const char *msg)=0

Here is the call graph for this function:

void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ArrayLit n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 441 of file C4AulAST.h.

442  {
443  for (const auto &c : n->values)
444  c->accept(this);
445  }
std::vector< ExprPtr > values
Definition: C4AulAST.h:180
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ProplistLit n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 446 of file C4AulAST.h.

447  {
448  for (const auto &c : n->values)
449  c.second->accept(this);
450  }
std::vector< std::pair< std::string, ExprPtr > > values
Definition: C4AulAST.h:187
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::UnOpExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 451 of file C4AulAST.h.

452  {
453  n->operand->accept(this);
454  }
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::BinOpExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 455 of file C4AulAST.h.

456  {
457  n->lhs->accept(this);
458  n->rhs->accept(this);
459  }
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::AssignmentExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 460 of file C4AulAST.h.

461  {
462  n->lhs->accept(this);
463  n->rhs->accept(this);
464  }
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::SubscriptExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 465 of file C4AulAST.h.

466  {
467  n->object->accept(this);
468  n->index->accept(this);
469  }
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::SliceExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 470 of file C4AulAST.h.

471  {
472  n->object->accept(this);
473  n->start->accept(this);
474  n->end->accept(this);
475  }
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::Block n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 487 of file C4AulAST.h.

References s.

488  {
489  for (const auto &s : n->children)
490  s->accept(this);
491  }
std::vector< StmtPtr > children
Definition: C4AulAST.h:273
#define s
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::Return n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 492 of file C4AulAST.h.

493  {
494  n->value->accept(this);
495  }
ExprPtr value
Definition: C4AulAST.h:284
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ForLoop n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 496 of file C4AulAST.h.

497  {
498  if (n->init)
499  n->init->accept(this);
500  if (n->cond)
501  n->cond->accept(this);
502  if (n->incr)
503  n->incr->accept(this);
504  n->body->accept(this);
505  }
StmtPtr body
Definition: C4AulAST.h:291
ExprPtr cond
Definition: C4AulAST.h:290
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::DoLoop n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 511 of file C4AulAST.h.

512  {
513  n->body->accept(this);
514  n->cond->accept(this);
515  }
StmtPtr body
Definition: C4AulAST.h:291
ExprPtr cond
Definition: C4AulAST.h:290
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::WhileLoop n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 516 of file C4AulAST.h.

517  {
518  n->cond->accept(this);
519  n->body->accept(this);
520  }
StmtPtr body
Definition: C4AulAST.h:291
ExprPtr cond
Definition: C4AulAST.h:290
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::If n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 521 of file C4AulAST.h.

522  {
523  n->cond->accept(this);
524  n->iftrue->accept(this);
525  if (n->iffalse)
526  n->iffalse->accept(this);
527  }
ExprPtr cond
Definition: C4AulAST.h:338
StmtPtr iftrue
Definition: C4AulAST.h:339
StmtPtr iffalse
Definition: C4AulAST.h:339
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::FunctionExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 538 of file C4AulAST.h.

539  {
540  n->body->accept(this);
541  }
std::unique_ptr< Block > body
Definition: C4AulAST.h:379

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