Joos1W Compiler Framework
All Classes Functions Typedefs Pages
ast::ExprEvaluator< T > Class Template Referenceabstract
Inheritance diagram for ast::ExprEvaluator< T >:

Public Member Functions

T Evaluate (Expr *expr)
 Evaluates the given expression. More...
 
virtual T EvaluateList (ExprNodeList subexpr)
 Evaluates the given subexpression. More...
 

Protected Types

using op_array = std::pmr::vector< T >
 

Protected Member Functions

virtual T mapValue (exprnode::ExprValue &node) const =0
 
virtual T evalBinaryOp (exprnode::BinaryOp &op, const T lhs, const T rhs) const =0
 
virtual T evalUnaryOp (exprnode::UnaryOp &op, const T rhs) const =0
 
virtual T evalMemberAccess (exprnode::MemberAccess &op, const T lhs, const T field) const =0
 
virtual T evalMethodCall (exprnode::MethodInvocation &op, const T method, const op_array &args) const =0
 
virtual T evalNewObject (exprnode::ClassInstanceCreation &op, const T object, const op_array &args) const =0
 
virtual T evalNewArray (exprnode::ArrayInstanceCreation &op, const T type, const T size) const =0
 
virtual T evalArrayAccess (exprnode::ArrayAccess &op, const T array, const T index) const =0
 
virtual T evalCast (exprnode::Cast &op, const T type, const T value) const =0
 
SourceRange argLocation (int arg_index)
 Gets the location of the argument at the given index. More...
 
virtual bool validate (T const &) const
 
virtual bool validatePop (T const &) const
 
int opStackSize () const
 
SourceRange argLocation (int argno) const
 Gets the location of the argument at the given index. Note the 0th argument is the first argument, not the operator. More...
 

Detailed Description

template<typename T>
class ast::ExprEvaluator< T >

Definition at line 12 of file ExprEvaluator.h.

Member Function Documentation

◆ argLocation() [1/2]

template<typename T >
SourceRange ast::ExprEvaluator< T >::argLocation ( int  arg_index)
inlineprotected

Gets the location of the argument at the given index.

Parameters
arg_indexThe index of the argument.
Returns
SourceLocation

Definition at line 38 of file ExprEvaluator.h.

38  {
39  // FIXME(kevin): Implement this
40  (void)arg_index;
41  return SourceRange{};
42  }
A range of locations in a source file.
Definition: Location.h:54

References SourceRange::SourceRange().

◆ argLocation() [2/2]

template<typename T >
SourceRange ast::ExprEvaluator< T >::argLocation ( int  argno) const
inlineprotected

Gets the location of the argument at the given index. Note the 0th argument is the first argument, not the operator.

Parameters
argnoThe index of the argument.
Returns
SourceRange The location of the argument.

Definition at line 139 of file ExprEvaluator.h.

139  {
140  assert(argno < cur_op->nargs() && argno >= 0);
141  // Arguments are offset from the top of the stack
142  return arg_locs_[arg_locs_.size() - cur_op->nargs() + argno];
143  }

◆ Evaluate()

template<typename T >
T ast::ExprEvaluator< T >::Evaluate ( Expr expr)
inline

Evaluates the given expression.

Parameters
exprThe expression to evaluate.

Definition at line 49 of file ExprEvaluator.h.

49 { return EvaluateList(expr->list()); }
virtual T EvaluateList(ExprNodeList subexpr)
Evaluates the given subexpression.
Definition: ExprEvaluator.h:55

References ast::ExprEvaluator< T >::EvaluateList().

◆ EvaluateList()

template<typename T >
virtual T ast::ExprEvaluator< T >::EvaluateList ( ExprNodeList  subexpr)
inlinevirtual

Evaluates the given subexpression.

Parameters
subexprThe list of expression nodes to evaluate.

Definition at line 55 of file ExprEvaluator.h.

55  {
56  using namespace ast::exprnode;
57 
58  std::pmr::vector<T> op_args;
59  const auto getArgs = [&op_args, this](int nargs) {
60  op_args.clear();
61  for(int i = 0; i < nargs; ++i) op_args.push_back(popSafe());
62  };
63 
64  // Clear the stack
65  while(!op_stack_.empty()) popSafe();
66 
67  // Lock all the nodes
68  for(auto const* nodes : subexpr.nodes()) {
69  nodes->const_lock();
70  }
71 
72  // Evaluate the RPN expression
73  auto* node = subexpr.mut_head();
74  for(size_t i = 0; i < subexpr.size(); ++i) {
75  // Push on the location of the current node if it's a value
76  if(dyn_cast<ExprValue*>(node)) arg_locs_.push_back(node->location());
77  // We grab the next node because we will unlock the current node
78  auto next_node = node->mut_next();
79  node->const_unlock();
80  cur_op = dyn_cast<ExprOp*>(node);
81  if(auto* value = dyn_cast<ExprValue*>(node)) {
82  op_stack_.push(mapValue(*value));
83  } else if(auto* unary = dyn_cast<UnaryOp*>(node)) {
84  auto rhs = popSafe();
85  op_stack_.push(evalUnaryOp(*unary, rhs));
86  } else if(auto* binary = dyn_cast<BinaryOp*>(node)) {
87  auto rhs = popSafe();
88  auto lhs = popSafe();
89  op_stack_.push(evalBinaryOp(*binary, lhs, rhs));
90  } else if(auto op = dyn_cast<MemberAccess*>(node)) {
91  auto field = popSafe();
92  auto lhs = popSafe();
93  op_stack_.push(evalMemberAccess(*op, lhs, field));
94  } else if(auto* method = dyn_cast<MethodInvocation*>(node)) {
95  op_args.clear();
96  if(method->nargs() > 1) getArgs(method->nargs() - 1);
97  auto method_name = popSafe();
98  op_stack_.push(evalMethodCall(*method, method_name, op_args));
99  } else if(auto* newObj = dyn_cast<ClassInstanceCreation*>(node)) {
100  op_args.clear();
101  if(newObj->nargs() > 1) getArgs(newObj->nargs() - 1);
102  auto type = popSafe();
103  op_stack_.push(evalNewObject(*newObj, type, op_args));
104  } else if(auto op = dyn_cast<ArrayInstanceCreation*>(node)) {
105  auto size = popSafe();
106  auto type = popSafe();
107  op_stack_.push(evalNewArray(*op, type, size));
108  } else if(auto op = dyn_cast<ArrayAccess*>(node)) {
109  auto index = popSafe();
110  auto array = popSafe();
111  op_stack_.push(evalArrayAccess(*op, array, index));
112  } else if(auto op = dyn_cast<Cast*>(node)) {
113  auto value = popSafe();
114  auto type = popSafe();
115  op_stack_.push(evalCast(*op, type, value));
116  }
117  assert(validate(op_stack_.top()));
118  node = next_node;
119  if(cur_op) mergeLocations(cur_op->nargs());
120  }
121 
122  // Return the result
123  auto result = popSafe();
124  assert(op_stack_.empty() && "Stack not empty after evaluation");
125  return result;
126  }

References ast::ExprNodeList::nodes(), and ast::ExprNodeList::size().

Referenced by ast::ExprEvaluator< T >::Evaluate().


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