File: Synopsis/TypeAnalysis/ConstEvaluator.hh
 1//
 2// Copyright (C) 2004 Stefan Seefeld
 3// All rights reserved.
 4// Licensed to the public under the terms of the GNU LGPL (>= 2),
 5// see the file COPYING for details.
 6//
 7#ifndef Synopsis_TypeAnalysis_ConstEvaluator_hh_
 8#define Synopsis_TypeAnalysis_ConstEvaluator_hh_
 9
10#include <Synopsis/PTree/Visitor.hh>
11#include <Synopsis/PTree/Atoms.hh>
12#include <Synopsis/PTree/Lists.hh>
13#include <Synopsis/SymbolLookup/Scope.hh>
14#include <cassert>
15
16namespace Synopsis
17{
18namespace TypeAnalysis
19{
20
21//. Evaluate the value of a constant expression.
22class ConstEvaluator : private PTree::Visitor
23{
24public:
25  ConstEvaluator(SymbolLookup::Scope const *s) : my_valid(false), my_scope(s) {}
26  bool evaluate(PTree::Node const *node, long &value);
27
28private:
29  virtual void visit(PTree::Literal *);
30  virtual void visit(PTree::Identifier *);
31  virtual void visit(PTree::FstyleCastExpr *);
32  virtual void visit(PTree::InfixExpr *);
33  virtual void visit(PTree::SizeofExpr *);
34  virtual void visit(PTree::UnaryExpr *);
35  virtual void visit(PTree::CondExpr *);
36  virtual void visit(PTree::ParenExpr *);
37  
38  bool                       my_valid;
39  long                       my_value;
40  SymbolLookup::Scope const *my_scope;
41};
42
43//. Evaluate the value of a constant expression.
44//. TODO: This may also return the type of the expression...
45inline bool evaluate_const(SymbolLookup::Scope const *scope,
46			   PTree::Node const *node, long &value)
47{
48  if (!node) return false;
49  ConstEvaluator e(scope);
50  return e.evaluate(node, value);
51}
52
53}
54}
55
56#endif
57