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

virtual ~ConstantResolver ()
 
void visit (const ::aul::ast::Script *n) override
 
void visit (const ::aul::ast::VarDecl *n) override
 
virtual void visit (const ::aul::ast::ArrayLit *n) override
 
virtual void visit (const ::aul::ast::ProplistLit *n) override
 
virtual void visit (const ::aul::ast::UnOpExpr *n) override
 
virtual void visit (const ::aul::ast::BinOpExpr *n) override
 
virtual void visit (const ::aul::ast::AssignmentExpr *n) override
 
virtual void visit (const ::aul::ast::SubscriptExpr *n) override
 
virtual void visit (const ::aul::ast::SliceExpr *n) override
 
virtual void visit (const ::aul::ast::CallExpr *n) override
 
virtual void visit (const ::aul::ast::ParExpr *n) override
 
virtual void visit (const ::aul::ast::Block *n) override
 
virtual void visit (const ::aul::ast::Return *n) override
 
virtual void visit (const ::aul::ast::ForLoop *n) override
 
virtual void visit (const ::aul::ast::RangeLoop *n) override
 
virtual void visit (const ::aul::ast::DoLoop *n) override
 
virtual void visit (const ::aul::ast::WhileLoop *n) override
 
virtual void visit (const ::aul::ast::If *n) override
 
virtual void visit (const ::aul::ast::FunctionDecl *n) override
 
virtual 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 380 of file C4AulCompiler.cpp.

Constructor & Destructor Documentation

virtual C4AulCompiler::ConstantResolver::~ConstantResolver ( )
inlinevirtual

Definition at line 408 of file C4AulCompiler.cpp.

408 {}

Member Function Documentation

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

Definition at line 395 of file C4AulCompiler.cpp.

References visit().

Referenced by C4AulCompiler::Compile().

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

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

References visit().

Referenced by C4AulCompiler::Preparse().

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

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 68 of file C4AulAST.h.

68 {}
virtual void aul::AstVisitor::visit ( const ::aul::ast::StringLit )
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::IntLit )
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::BoolLit )
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::NilLit )
inlinevirtualinherited

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

Definition at line 74 of file C4AulAST.h.

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

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

Definition at line 75 of file C4AulAST.h.

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

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

Definition at line 76 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 90 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 91 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 96 of file C4AulAST.h.

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

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 97 of file C4AulAST.h.

97 {}
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 2032 of file C4AulCompiler.cpp.

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

Referenced by resolve(), and resolve_quiet().

2033 {
2034  for (const auto &d : n->declarations)
2035  {
2036  try
2037  {
2038  d->accept(this);
2039  }
2040  catch (C4AulParseError &e)
2041  {
2042  host->Engine->GetErrorHandler()->OnError(e.what());
2043  }
2044  }
2045 }
C4AulErrorHandler * GetErrorHandler() const
Definition: C4Aul.h:175
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual const char * what() const noexcept
std::vector< DeclPtr > declarations
Definition: C4AulAST.h:430
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 2047 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().

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

Here is the call graph for this function:

virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ArrayLit 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->accept(this);
450  }
std::vector< ExprPtr > values
Definition: C4AulAST.h:185
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ProplistLit n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 451 of file C4AulAST.h.

452  {
453  for (const auto &c : n->values)
454  c.second->accept(this);
455  }
std::vector< std::pair< std::string, ExprPtr > > values
Definition: C4AulAST.h:192
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::UnOpExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 456 of file C4AulAST.h.

457  {
458  n->operand->accept(this);
459  }
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::BinOpExpr 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  }
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::AssignmentExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 465 of file C4AulAST.h.

466  {
467  n->lhs->accept(this);
468  n->rhs->accept(this);
469  }
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::SubscriptExpr 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->index->accept(this);
474  }
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::SliceExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 475 of file C4AulAST.h.

476  {
477  n->object->accept(this);
478  n->start->accept(this);
479  n->end->accept(this);
480  }
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::CallExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 481 of file C4AulAST.h.

References a.

482  {
483  if (n->context)
484  n->context->accept(this);
485  for (const auto &a : n->args)
486  a->accept(this);
487  }
#define a
std::vector< ExprPtr > args
Definition: C4AulAST.h:262
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ParExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 488 of file C4AulAST.h.

489  {
490  n->arg->accept(this);
491  }
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::Block n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 492 of file C4AulAST.h.

References s.

493  {
494  for (const auto &s : n->children)
495  s->accept(this);
496  }
std::vector< StmtPtr > children
Definition: C4AulAST.h:278
#define s
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::Return n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 497 of file C4AulAST.h.

498  {
499  n->value->accept(this);
500  }
ExprPtr value
Definition: C4AulAST.h:289
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::ForLoop n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 501 of file C4AulAST.h.

502  {
503  if (n->init)
504  n->init->accept(this);
505  if (n->cond)
506  n->cond->accept(this);
507  if (n->incr)
508  n->incr->accept(this);
509  n->body->accept(this);
510  }
StmtPtr body
Definition: C4AulAST.h:296
ExprPtr cond
Definition: C4AulAST.h:295
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::RangeLoop n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 511 of file C4AulAST.h.

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

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 516 of file C4AulAST.h.

517  {
518  n->body->accept(this);
519  n->cond->accept(this);
520  }
StmtPtr body
Definition: C4AulAST.h:296
ExprPtr cond
Definition: C4AulAST.h:295
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::WhileLoop 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->body->accept(this);
525  }
StmtPtr body
Definition: C4AulAST.h:296
ExprPtr cond
Definition: C4AulAST.h:295
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::If n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 526 of file C4AulAST.h.

527  {
528  n->cond->accept(this);
529  n->iftrue->accept(this);
530  if (n->iffalse)
531  n->iffalse->accept(this);
532  }
ExprPtr cond
Definition: C4AulAST.h:343
StmtPtr iftrue
Definition: C4AulAST.h:344
StmtPtr iffalse
Definition: C4AulAST.h:344
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::FunctionDecl n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

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

Definition at line 539 of file C4AulAST.h.

540  {
541  n->body->accept(this);
542  }
std::unique_ptr< Block > body
Definition: C4AulAST.h:384
virtual void aul::DefaultRecursiveVisitor::visit ( const ::aul::ast::FunctionExpr n)
inlineoverridevirtualinherited

Reimplemented from aul::AstVisitor.

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 543 of file C4AulAST.h.

544  {
545  n->body->accept(this);
546  }
std::unique_ptr< Block > body
Definition: C4AulAST.h:384

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