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

Constructor & Destructor Documentation

◆ ~ConstantResolver()

C4AulCompiler::ConstantResolver::~ConstantResolver ( )
overridedefault

Member Function Documentation

◆ resolve()

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

Definition at line 410 of file C4AulCompiler.cpp.

References C4AulCompiler::PreparseAstVisitor::visit(), and visit().

Referenced by C4AulCompiler::Compile().

411  {
412  // We resolve constants *twice*; this allows people to create circular
413  // references in proplists or arrays.
414  // Unfortunately it also results in unexpected behaviour in code like
415  // this:
416  // static const c1 = c2, c2 = c3, c3 = 1;
417  // which will set c1 to nil, and both c2 and c3 to 1.
418  // While this is unlikely to happen often, we should fix that so it
419  // resolves all three constants to 1.
420  ConstantResolver r(host);
421  r.visit(script);
422  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ resolve_quiet()

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

Definition at line 402 of file C4AulCompiler.cpp.

References visit().

Referenced by C4AulCompiler::Preparse().

403  {
404  // Does the same as resolve, but doesn't emit errors/warnings
405  // (because we'll emit them again later).
406  ConstantResolver r(host);
407  r.quiet = true;
408  r.visit(script);
409  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ visit() [1/32]

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 63 of file C4AulAST.h.

63 {}

◆ visit() [2/32]

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 {}

◆ visit() [3/32]

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 {}

◆ visit() [4/32]

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 {}

◆ visit() [5/32]

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 {}

◆ visit() [6/32]

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 {}

◆ visit() [7/32]

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 {}

◆ visit() [8/32]

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 85 of file C4AulAST.h.

85 {}

◆ visit() [9/32]

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

Reimplemented in C4AulCompiler::CodegenAstVisitor.

Definition at line 86 of file C4AulAST.h.

86 {}

◆ visit() [10/32]

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

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 91 of file C4AulAST.h.

91 {}

◆ visit() [11/32]

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

Reimplemented in C4AulCompiler::PreparseAstVisitor.

Definition at line 92 of file C4AulAST.h.

92 {}

◆ visit() [12/32]

template<class T >
void aul::AstVisitor::visit ( const T *  )
deleteinherited

◆ visit() [13/32]

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

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 2054 of file C4AulCompiler.cpp.

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

Referenced by resolve(), and resolve_quiet().

2055 {
2056  for (const auto &d : n->declarations)
2057  {
2058  try
2059  {
2060  d->accept(this);
2061  }
2062  catch (C4AulParseError &e)
2063  {
2064  host->Engine->GetErrorHandler()->OnError(e.what());
2065  }
2066  }
2067 }
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
C4AulErrorHandler * GetErrorHandler() const
Definition: C4Aul.h:173
std::vector< DeclPtr > declarations
Definition: C4AulAST.h:425
const char * what() const noexcept override
Definition: C4Aul.cpp:59
virtual void OnError(const char *msg)=0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ visit() [14/32]

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

Reimplemented from aul::DefaultRecursiveVisitor.

Definition at line 2069 of file C4AulCompiler.cpp.

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

2070 {
2071  const int quiet_flag = quiet ? ConstexprEvaluator::SuppressErrors : 0;
2072  for (const auto &dec : n->decls)
2073  {
2074  const char *cname = dec.name.c_str();
2076  switch (n->scope)
2077  {
2078  case ::aul::ast::VarDecl::Scope::Func:
2079  // Function-scoped declarations and their initializers are handled by CodegenAstVisitor.
2080  break;
2082  if (!host->GetPropList()->HasProperty(name))
2083  host->GetPropList()->SetPropertyByS(name, C4VNull);
2084  if (dec.init)
2085  {
2086  assert(host->GetPropList()->IsStatic());
2087  try
2088  {
2089  C4Value v = ConstexprEvaluator::eval_static(host, host->GetPropList()->IsStatic(), dec.name, dec.init.get(), ConstexprEvaluator::IgnoreUnset | quiet_flag);
2090  host->GetPropList()->SetPropertyByS(name, v);
2091  }
2092  catch (C4AulParseError &e)
2093  {
2094  if (!quiet)
2095  host->Engine->ErrorHandler->OnError(e.what());
2096  }
2097  }
2098  break;
2099  case ::aul::ast::VarDecl::Scope::Global:
2100  if ((dec.init != nullptr) != n->constant)
2101  {
2102  if (!quiet)
2103  host->Engine->ErrorHandler->OnError(Error(host, host, n->loc, nullptr, "global variable must be either constant or uninitialized: %s", cname).what());
2104  }
2105  else if (dec.init)
2106  {
2107  try
2108  {
2109  assert(n->constant && "CodegenAstVisitor: initialized global variable isn't const");
2110  C4Value *v = host->Engine->GlobalConsts.GetItem(cname);
2111  assert(v && "CodegenAstVisitor: global constant not found in variable table");
2112  if (!v)
2113  throw Error(host, host, n->loc, nullptr, "internal error: global constant not found in variable table: %s", cname);
2114  *v = ConstexprEvaluator::eval_static(host, nullptr, dec.name, dec.init.get(), ConstexprEvaluator::IgnoreUnset | quiet_flag);
2115  }
2116  catch (C4AulParseError &e)
2117  {
2118  if (!quiet)
2119  host->Engine->ErrorHandler->OnError(e.what());
2120  }
2121  }
2122  break;
2123  }
2124  }
2125 }
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
const char * what() const noexcept override
Definition: C4Aul.cpp:59
C4StringTable Strings
Definition: C4Globals.cpp:42
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
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:971
C4AulErrorHandler * ErrorHandler
Definition: C4Aul.h:128
bool HasProperty(C4String *k) const
Definition: C4PropList.h:118
Here is the call graph for this function:

◆ visit() [15/32]

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

◆ visit() [16/32]

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

◆ visit() [17/32]

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  }

◆ visit() [18/32]

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  }

◆ visit() [19/32]

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  }

◆ visit() [20/32]

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  }

◆ visit() [21/32]

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  }

◆ visit() [22/32]

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

◆ visit() [23/32]

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  }

◆ visit() [24/32]

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

◆ visit() [25/32]

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

◆ visit() [26/32]

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

◆ visit() [27/32]

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

◆ visit() [28/32]

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

◆ visit() [29/32]

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

◆ visit() [30/32]

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

◆ visit() [31/32]

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

◆ visit() [32/32]

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: