00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef COMMA_AST_EXPR_HDR_GUARD
00010 #define COMMA_AST_EXPR_HDR_GUARD
00011
00012 #include "comma/ast/AstBase.h"
00013 #include "comma/ast/Decl.h"
00014 #include "comma/ast/Type.h"
00015
00016 #include "llvm/ADT/APInt.h"
00017 #include "llvm/Support/DataTypes.h"
00018
00019 namespace comma {
00020
00021
00022
00023
00024
00025 class Expr : public Ast {
00026
00027 public:
00028 Expr(AstKind kind, Type *type, Location loc = 0)
00029 : Ast(kind), type(type), location(loc) {
00030 assert(this->denotesExpr());
00031 }
00032
00033 Expr(AstKind kind, Location loc = 0)
00034 : Ast(kind), type(0), location(loc) {
00035 assert(this->denotesExpr());
00036 }
00037
00038
00039
00040 bool hasType() const { return type != 0; }
00041
00042
00043
00044
00045
00046
00047
00048
00049 Type *getType() const {
00050 assert(hasType() && "Expr does not have an associated type!");
00051 return type;
00052 }
00053
00054
00055
00056
00057 void setType(Type *type) { this->type = type; }
00058
00059 Location getLocation() const { return location; }
00060
00061 static bool classof(const Expr *node) { return true; }
00062 static bool classof(const Ast *node) {
00063 return node->denotesExpr();
00064 }
00065
00066 private:
00067 Type *type;
00068 Location location;
00069 };
00070
00071
00072
00073
00074
00075
00076
00077
00078 class Qualifier : public Ast {
00079
00080 public:
00081 Qualifier(DeclRegion *qualifier, Location loc)
00082 : Ast(AST_Qualifier) {
00083 qualifiers.push_back(QualPair(qualifier, loc));
00084 }
00085
00086 void addQualifier(DeclRegion *qualifier, Location loc) {
00087 qualifiers.push_back(QualPair(qualifier, loc));
00088 }
00089
00090 unsigned numQualifiers() const { return qualifiers.size(); }
00091
00092 typedef std::pair<DeclRegion*, Location> QualPair;
00093
00094 QualPair getQualifier(unsigned n) const {
00095 assert(n < numQualifiers() && "Index out of range!");
00096 return qualifiers[n];
00097 }
00098
00099
00100 DeclRegion *resolve() {
00101 return qualifiers.back().first;
00102 }
00103
00104 private:
00105 typedef llvm::SmallVector<QualPair, 2> QualVector;
00106 QualVector qualifiers;
00107
00108 public:
00109 typedef QualVector::iterator iterator;
00110 iterator begin() { return qualifiers.begin(); }
00111 iterator end() { return qualifiers.end(); }
00112
00113 typedef QualVector::const_iterator const_iterator;
00114 const_iterator begin() const { return qualifiers.begin(); }
00115 const_iterator end() const { return qualifiers.end(); }
00116
00117 static bool classof(const Qualifier *node) { return true; }
00118 static bool classof(const Ast *node) {
00119 return node->getKind() == AST_Qualifier;
00120 }
00121 };
00122
00123
00124
00125
00126
00127 class DeclRefExpr : public Expr {
00128
00129 public:
00130 DeclRefExpr(ValueDecl *decl, Location loc)
00131 : Expr(AST_DeclRefExpr, decl->getType(), loc),
00132 declaration(decl) { }
00133
00134 IdentifierInfo *getIdInfo() const { return declaration->getIdInfo(); }
00135 const char *getString() const { return declaration->getString(); }
00136
00137 ValueDecl *getDeclaration() { return declaration; }
00138
00139 void setDeclaration(ValueDecl *decl) { declaration = decl; }
00140
00141 static bool classof(const DeclRefExpr *node) { return true; }
00142 static bool classof(const Ast *node) {
00143 return node->getKind() == AST_DeclRefExpr;
00144 }
00145
00146 private:
00147 ValueDecl *declaration;
00148 };
00149
00150
00151
00152
00153
00154 class KeywordSelector : public Expr {
00155
00156 public:
00164 KeywordSelector(IdentifierInfo *key, Location loc, Expr *expr);
00165
00166 IdentifierInfo *getKeyword() const { return keyword; }
00167 void setKeyword(IdentifierInfo *key) { keyword = key; }
00168
00169 Expr *getExpression() { return expression; }
00170 const Expr *getExpression() const { return expression; }
00171 void setExpression(Expr *expr) { expression = expr; }
00172
00173 static bool classof(const KeywordSelector *node) { return true; }
00174 static bool classof(const Ast *node) {
00175 return node->getKind() == AST_KeywordSelector;
00176 }
00177
00178 private:
00179 IdentifierInfo *keyword;
00180 Expr *expression;
00181 };
00182
00183
00184
00185
00186 class FunctionCallExpr : public Expr {
00187
00188 public:
00189 FunctionCallExpr(FunctionDecl *connective,
00190 Expr **arguments,
00191 unsigned numArgs,
00192 Location loc);
00193
00194 FunctionCallExpr(FunctionDecl **connectives,
00195 unsigned numConnectives,
00196 Expr **arguments,
00197 unsigned numArgs,
00198 Location loc);
00199
00200 ~FunctionCallExpr();
00201
00202
00203
00204 void setQualifier(Qualifier *qualifier) { this->qualifier = qualifier; }
00205
00206
00207 bool isQualified() const { return qualifier != 0; }
00208
00209
00210
00211 Qualifier *getQualifier() { return qualifier; }
00212
00213
00214
00215 const Qualifier *getQualifier() const { return qualifier; }
00216
00217
00218
00219 Ast *getConnective() { return connective; }
00220 const Ast *getConnective() const { return connective; }
00221
00222
00223
00224 void resolveConnective(FunctionDecl *connective);
00225
00226
00227 bool isAmbiguous() const {
00228 return llvm::isa<OverloadedDeclName>(connective);
00229 }
00230
00231
00232 bool isUnambiguous() const {
00233 return !isAmbiguous();
00234 }
00235
00236
00237 bool containsConnective(FunctionType *) const;
00238
00239
00240
00241 unsigned numConnectives() const;
00242
00243
00244
00245 FunctionDecl *getConnective(unsigned i) const;
00246
00247
00248
00249 class ConnectiveIterator {
00250
00251 const FunctionCallExpr *callExpr;
00252 unsigned index;
00253
00254 public:
00255
00256 ConnectiveIterator() :
00257 callExpr(0),
00258 index(0) { }
00259
00260
00261
00262 ConnectiveIterator(const FunctionCallExpr *callExpr)
00263 : callExpr(callExpr),
00264 index(0) { }
00265
00266 ConnectiveIterator(const ConnectiveIterator &iter)
00267 : callExpr(iter.callExpr),
00268 index(iter.index) { }
00269
00270 FunctionDecl *operator *() {
00271 assert(callExpr && "Cannot dereference an empty iterator!");
00272 return callExpr->getConnective(index);
00273 }
00274
00275 bool operator ==(const ConnectiveIterator &iter) const {
00276 return (this->callExpr == iter.callExpr &&
00277 this->index == iter.index);
00278 }
00279
00280 bool operator !=(const ConnectiveIterator &iter) const {
00281 return !this->operator==(iter);
00282 }
00283
00284 ConnectiveIterator &operator ++() {
00285 if (++index == callExpr->numConnectives()) {
00286 callExpr = 0;
00287 index = 0;
00288 }
00289 return *this;
00290 }
00291
00292 ConnectiveIterator operator ++(int) {
00293 ConnectiveIterator tmp = *this;
00294 this->operator++();
00295 return tmp;
00296 }
00297 };
00298
00299 ConnectiveIterator beginConnectives() {
00300 return ConnectiveIterator(this);
00301 }
00302
00303 ConnectiveIterator endConnectives() {
00304 return ConnectiveIterator();
00305 }
00306
00307
00308 unsigned getNumArgs() const { return numArgs; }
00309
00310 Expr *getArg(unsigned i) {
00311 assert(i < numArgs && "Index out of range!");
00312 return arguments[i];
00313 }
00314
00315 void setArg(Expr *expr, unsigned i) {
00316 assert(i < numArgs && "Index out of range!");
00317 arguments[i] = expr;
00318 }
00319
00320 static bool classof(const FunctionCallExpr *node) { return true; }
00321 static bool classof(const Ast *node) {
00322 return node->getKind() == AST_FunctionCallExpr;
00323 }
00324
00325 private:
00326 Ast *connective;
00327 Expr **arguments;
00328 unsigned numArgs;
00329 Qualifier *qualifier;
00330
00331 void setTypeForConnective();
00332 };
00333
00334
00335
00336
00337
00338 class InjExpr : public Expr
00339 {
00340 public:
00341 InjExpr(Expr *argument, Type *resultType, Location loc)
00342 : Expr(AST_InjExpr, resultType, loc),
00343 operand(argument) { }
00344
00345 Expr *getOperand() { return operand; }
00346 const Expr *getOperand() const { return operand; }
00347
00348 static bool classof(const InjExpr *node) { return true; }
00349 static bool classof(const Ast *node) {
00350 return node->getKind() == AST_InjExpr;
00351 }
00352
00353 private:
00354 Expr *operand;
00355 };
00356
00357
00358
00359
00360
00361 class PrjExpr : public Expr
00362 {
00363 public:
00364 PrjExpr(Expr *argument, DomainType *resultType, Location loc)
00365 : Expr(AST_PrjExpr, resultType, loc),
00366 operand(argument) { }
00367
00368 Expr *getOperand() { return operand; }
00369 const Expr *getOperand() const { return operand; }
00370
00371 static bool classof(const PrjExpr *node) { return true; }
00372 static bool classof(const Ast *node) {
00373 return node->getKind() == AST_PrjExpr;
00374 }
00375
00376 private:
00377 Expr *operand;
00378 };
00379
00380
00381
00382
00383
00384
00385
00386 class IntegerLiteral : public Expr
00387 {
00388 public:
00389 IntegerLiteral(const llvm::APInt &value, Location loc)
00390 : Expr(AST_IntegerLiteral, loc), value(value) { }
00391
00392 const llvm::APInt &getValue() const { return value; }
00393
00394 void setValue(const llvm::APInt &V) { value = V; }
00395
00396 static bool classof(const IntegerLiteral *node) { return true; }
00397 static bool classof(const Ast *node) {
00398 return node->getKind() == AST_IntegerLiteral;
00399 }
00400
00401 private:
00402 llvm::APInt value;
00403 };
00404
00405 }
00406
00407 #endif