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

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

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

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

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

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

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

References C4AUL_MAX_Par.

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

References C4AUL_MAX_Par.

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

References C4ScriptHost::Appends.

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

References C4ScriptHost::Includes.

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

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

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