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 118 of file C4AulCompiler.cpp.

Constructor & Destructor Documentation

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

Definition at line 128 of file C4AulCompiler.cpp.

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

Definition at line 129 of file C4AulCompiler.cpp.

129 : 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 446 of file C4AulCompiler.cpp.

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

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

447 {
448  const char *cname = n->var.c_str();
449  if (n->scoped_var)
450  {
451  Fn->VarNamed.AddName(cname);
452  }
453  else
454  {
455  // Loop variable not explicitly declared here. Look it up in
456  // the function and warn if it hasn't been declared at all.
457  if (Fn->VarNamed.GetItemNr(cname) == -1)
458  {
459  Warn(target_host, host, n, Fn, C4AulWarningId::implicit_range_loop_var_decl, cname);
460  Fn->VarNamed.AddName(cname);
461  }
462  }
463  DefaultRecursiveVisitor::visit(n);
464 }
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 466 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.

467 {
469  {
470  Warn(target_host, host, n, Fn, C4AulWarningId::non_global_var_is_never_const);
471  }
472  for (const auto &var : n->decls)
473  {
474  const char *cname = var.name.c_str();
475  switch (n->scope)
476  {
477  case ::aul::ast::VarDecl::Scope::Func:
478  {
479  assert(Fn && "function-local var declaration outside of function");
480  if (!Fn)
481  throw Error(target_host, host, n, Fn, "internal error: function-local var declaration outside of function");
482 
483  if (target_host)
484  {
485  // if target_host is unset, we're parsing this func for direct execution,
486  // in which case we don't want to warn about variable hiding.
487  if (target_host->Engine->GlobalNamedNames.GetItemNr(cname) >= 0 || target_host->Engine->GlobalConstNames.GetItemNr(cname) >= 0)
488  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "local variable", cname, "global variable");
489  C4String *s = ::Strings.FindString(cname);
490  if (s && target_host->GetPropList()->HasProperty(s))
491  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "local variable", cname, "object-local variable");
492  if (Fn->ParNamed.GetItemNr(cname) != -1)
493  {
494  // The parameter order of this warning is correct:
495  // Aul looks up parameters before local variables, so
496  // the parameter actually shadows the local variable.
497  // This doesn't make a whole lot of sense and should
498  // probably be changed.
499  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "parameter", cname, "local variable");
500  }
501  }
502  Fn->VarNamed.AddName(cname);
503  break;
504  }
506  {
507  if (host->Engine->GlobalNamedNames.GetItemNr(cname) >= 0 || host->Engine->GlobalConstNames.GetItemNr(cname) >= 0)
508  Warn(target_host, host, n, Fn, C4AulWarningId::variable_shadows_variable, "object-local variable", cname, "global variable");
509  C4String *s = ::Strings.RegString(cname);
510  if (target_host->GetPropList()->HasProperty(s))
511  Warn(target_host, host, n, Fn, C4AulWarningId::redeclaration, "object-local variable", cname);
512  else
513  target_host->GetPropList()->SetPropertyByS(s, C4VNull);
514  break;
515  }
516  case ::aul::ast::VarDecl::Scope::Global:
517  assert(!Fn && "global var declaration inside function");
518  if (Fn)
519  throw Error(target_host, host, n, Fn, "internal error: global var declaration inside function");
520 
521  if (host->Engine->GlobalNamedNames.GetItemNr(cname) >= 0 || host->Engine->GlobalConstNames.GetItemNr(cname) >= 0)
522  Warn(target_host, host, n, Fn, C4AulWarningId::redeclaration, "global variable", cname);
523  if (n->constant)
524  host->Engine->GlobalConstNames.AddName(cname);
525  else
526  host->Engine->GlobalNamedNames.AddName(cname);
527  break;
528  }
529  }
530 
532  {
533  // only func-scoped variables can potentially have initializers we care
534  // about in the pre-parsing stage: they may have calls that pass
535  // unnamed parameters
536  DefaultRecursiveVisitor::visit(n);
537  }
538 }
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:952
#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 540 of file C4AulCompiler.cpp.

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

541 {
542  // create script fn
543  C4PropListStatic *Parent = n->is_global ? target_host->Engine->GetPropList() : target_host->GetPropList();
544  const char *cname = n->name.c_str();
545 
546  assert(!Fn);
547 
548  // Look up the overloaded function before adding the overloading one
549  C4AulFunc *parent_func = Parent->GetFunc(cname);
550 
551  Fn = new C4AulScriptFunc(Parent, target_host, cname, n->loc);
552  host->ownedFunctions.push_back(C4VFunction(Fn));
553  for (const auto &param : n->params)
554  {
555  Fn->AddPar(param.name.c_str(), param.type);
556  }
557  if (n->has_unnamed_params)
558  Fn->ParCount = C4AUL_MAX_Par;
559 
560  // Add function to def/engine
561  Fn->SetOverloaded(parent_func);
562  Parent->SetPropertyByS(Fn->Name, C4VFunction(Fn));
563 
564  try
565  {
566  DefaultRecursiveVisitor::visit(n);
567  Fn = nullptr;
568  }
569  catch (...)
570  {
571  Fn = nullptr;
572  throw;
573  }
574 }
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:952
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 576 of file C4AulCompiler.cpp.

References C4AUL_MAX_Par.

577 {
578  if (n->append_unnamed_pars && Fn->ParCount != C4AUL_MAX_Par)
579  {
580  Fn->ParCount = C4AUL_MAX_Par;
581  }
582  DefaultRecursiveVisitor::visit(n);
583 }
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 585 of file C4AulCompiler.cpp.

References C4AUL_MAX_Par.

586 {
587  if (Fn->ParCount != C4AUL_MAX_Par)
588  {
589  Warn(target_host, host, n, Fn, C4AulWarningId::undeclared_varargs, "Par()");
590  Fn->ParCount = C4AUL_MAX_Par;
591  }
592  DefaultRecursiveVisitor::visit(n);
593 }
#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 595 of file C4AulCompiler.cpp.

References C4ScriptHost::Appends.

596 {
597  if (n->what.empty())
598  host->Appends.emplace_back("*");
599  else
600  host->Appends.emplace_back(n->what.c_str());
601 }
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 603 of file C4AulCompiler.cpp.

References C4ScriptHost::Includes.

604 {
605  host->Includes.emplace_back(n->what.c_str());
606 }
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 608 of file C4AulCompiler.cpp.

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

609 {
610  for (const auto &d : n->declarations)
611  {
612  try
613  {
614  d->accept(this);
615  }
616  catch (C4AulParseError &e)
617  {
618  target_host->Engine->GetErrorHandler()->OnError(e.what());
619  }
620  }
621 }
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: