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

Public Member Functions

 ~ConstantResolver () override=default
 
void visit (const ::aul::ast::Script *n) override
 
void visit (const ::aul::ast::VarDecl *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::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::If *n) override
 
void visit (const ::aul::ast::FunctionDecl *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 *)
 
virtual void visit (const ::aul::ast::IncludePragma *)
 
virtual void visit (const ::aul::ast::AppendtoPragma *)
 
template<class T >
void visit (const T *)=delete
 

Static Public Member Functions

static void resolve_quiet (C4ScriptHost *host, const ::aul::ast::Script *script)
 
static void resolve (C4ScriptHost *host, const ::aul::ast::Script *script)
 

Detailed Description

Definition at line 379 of file C4AulCompiler.cpp.

Constructor & Destructor Documentation

C4AulCompiler::ConstantResolver::~ConstantResolver ( )
overridedefault

Member Function Documentation

static void C4AulCompiler::ConstantResolver::resolve ( C4ScriptHost host,
const ::aul::ast::Script script 
)
inlinestatic

Definition at line 394 of file C4AulCompiler.cpp.

References visit().

Referenced by C4AulCompiler::Compile().

395  {
396  // We resolve constants *twice*; this allows people to create circular
397  // references in proplists or arrays.
398  // Unfortunately it also results in unexpected behaviour in code like
399  // this:
400  // static const c1 = c2, c2 = c3, c3 = 1;
401  // which will set c1 to nil, and both c2 and c3 to 1.
402  // While this is unlikely to happen often, we should fix that so it
403  // resolves all three constants to 1.
404  ConstantResolver r(host);
405  r.visit(script);
406  }

Here is the call graph for this function:

Here is the caller graph for this function:

static void C4AulCompiler::ConstantResolver::resolve_quiet ( C4ScriptHost host,
const ::aul::ast::Script script 
)
inlinestatic

Definition at line 386 of file C4AulCompiler.cpp.

References visit().

Referenced by C4AulCompiler::Preparse().

387  {
388  // Does the same as resolve, but doesn't emit errors/warnings
389  // (because we'll emit them again later).
390  ConstantResolver r(host);
391  r.quiet = true;
392  r.visit(script);
393  }

Here is the call graph for this function:

Here is the caller graph for this function:

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 {}
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::ConstantResolver::visit ( const ::aul::ast::Script n)
overridevirtual

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 2031 of file C4AulCompiler.cpp.

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

Referenced by resolve(), and resolve_quiet().

2032 {
2033  for (const auto &d : n->declarations)
2034  {
2035  try
2036  {
2037  d->accept(this);
2038  }
2039  catch (C4AulParseError &e)
2040  {
2041  host->Engine->GetErrorHandler()->OnError(e.what());
2042  }
2043  }
2044 }
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:

Here is the caller graph for this function:

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

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 2046 of file C4AulCompiler.cpp.

References C4VNull, C4ScriptHost::Engine, C4AulScriptEngine::ErrorHandler, C4AulCompiler::ConstexprEvaluator::eval_static(), C4ValueMapData::GetItem(), C4ScriptHost::GetPropList(), C4AulScriptEngine::GlobalConsts, C4PropList::HasProperty(), C4AulCompiler::ConstexprEvaluator::IgnoreUnset, C4PropListStatic::IsStatic(), Object(), C4AulErrorHandler::OnError(), C4StringTable::RegString(), C4PropList::SetPropertyByS(), Strings, C4AulCompiler::ConstexprEvaluator::SuppressErrors, and C4AulError::what().

2047 {
2048  const int quiet_flag = quiet ? ConstexprEvaluator::SuppressErrors : 0;
2049  for (const auto &dec : n->decls)
2050  {
2051  const char *cname = dec.name.c_str();
2053  switch (n->scope)
2054  {
2055  case ::aul::ast::VarDecl::Scope::Func:
2056  // Function-scoped declarations and their initializers are handled by CodegenAstVisitor.
2057  break;
2059  if (!host->GetPropList()->HasProperty(name))
2060  host->GetPropList()->SetPropertyByS(name, C4VNull);
2061  if (dec.init)
2062  {
2063  assert(host->GetPropList()->IsStatic());
2064  try
2065  {
2066  C4Value v = ConstexprEvaluator::eval_static(host, host->GetPropList()->IsStatic(), dec.name, dec.init.get(), ConstexprEvaluator::IgnoreUnset | quiet_flag);
2067  host->GetPropList()->SetPropertyByS(name, v);
2068  }
2069  catch (C4AulParseError &e)
2070  {
2071  if (!quiet)
2072  host->Engine->ErrorHandler->OnError(e.what());
2073  }
2074  }
2075  break;
2076  case ::aul::ast::VarDecl::Scope::Global:
2077  if ((dec.init != nullptr) != n->constant)
2078  {
2079  if (!quiet)
2080  host->Engine->ErrorHandler->OnError(Error(host, host, n->loc, nullptr, "global variable must be either constant or uninitialized: %s", cname).what());
2081  }
2082  else if (dec.init)
2083  {
2084  try
2085  {
2086  assert(n->constant && "CodegenAstVisitor: initialized global variable isn't const");
2087  C4Value *v = host->Engine->GlobalConsts.GetItem(cname);
2088  assert(v && "CodegenAstVisitor: global constant not found in variable table");
2089  if (!v)
2090  throw Error(host, host, n->loc, nullptr, "internal error: global constant not found in variable table: %s", cname);
2091  *v = ConstexprEvaluator::eval_static(host, nullptr, dec.name, dec.init.get(), ConstexprEvaluator::IgnoreUnset | quiet_flag);
2092  }
2093  catch (C4AulParseError &e)
2094  {
2095  if (!quiet)
2096  host->Engine->ErrorHandler->OnError(e.what());
2097  }
2098  }
2099  break;
2100  }
2101  }
2102 }
C4Value * GetItem(const char *strName)
Definition: C4ValueMap.cpp:235
C4PropListStatic * IsStatic() override
Definition: C4PropList.h:266
C4String * RegString(StdStrBuf String)
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
C4StringTable Strings
Definition: C4Globals.cpp:42
const char * what() const noexceptoverride
C4ValueMapData GlobalConsts
Definition: C4Aul.h:142
static C4Value eval_static(C4ScriptHost *host, C4PropListStatic *parent, const std::string &parent_key, const ::aul::ast::Expr *e, EvalFlags flags=0)
virtual void OnError(const char *msg)=0
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
const char * loc
Definition: C4AulAST.h:123
virtual void SetPropertyByS(C4String *k, const C4Value &to)
Definition: C4PropList.cpp:948
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:128

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::CallExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 476 of file C4AulAST.h.

References a.

477  {
478  if (n->context)
479  n->context->accept(this);
480  for (const auto &a : n->args)
481  a->accept(this);
482  }
#define a
std::vector< ExprPtr > args
Definition: C4AulAST.h:257
void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ParExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 483 of file C4AulAST.h.

484  {
485  n->arg->accept(this);
486  }
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::RangeLoop n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 506 of file C4AulAST.h.

507  {
508  n->cond->accept(this);
509  n->body->accept(this);
510  }
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::FunctionDecl n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 534 of file C4AulAST.h.

535  {
536  n->body->accept(this);
537  }
std::unique_ptr< Block > body
Definition: C4AulAST.h:379
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: