00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef COMMA_AST_DECL_HDR_GUARD
00010 #define COMMA_AST_DECL_HDR_GUARD
00011
00012 #include "comma/ast/AstBase.h"
00013 #include "comma/ast/Type.h"
00014 #include "llvm/Support/Casting.h"
00015 #include "llvm/ADT/FoldingSet.h"
00016 #include "llvm/ADT/SmallPtrSet.h"
00017 #include <map>
00018
00019 namespace comma {
00020
00021
00022
00023
00024
00025 class Decl : public Ast {
00026
00027 public:
00028 virtual ~Decl() { };
00029
00030
00031
00032 IdentifierInfo *getIdInfo() const { return idInfo; }
00033
00034
00035
00036 const char *getString() const {
00037 return idInfo ? idInfo->getString() : 0;
00038 }
00039
00040
00041
00042 bool isAnonymous() const { return idInfo == 0; }
00043
00044
00045 static bool classof(const Decl *node) { return true; }
00046 static bool classof(const Ast *node) {
00047 return node->denotesDecl();
00048 }
00049
00050 protected:
00051 Decl(AstKind kind, IdentifierInfo *info = 0) : Ast(kind), idInfo(info) {
00052 assert(this->denotesDecl());
00053 }
00054
00055 IdentifierInfo *idInfo;
00056 };
00057
00058
00059
00060
00061
00062 class TypeDecl : public Decl {
00063
00064 public:
00065 virtual ~TypeDecl() { }
00066
00067
00068 static bool classof(const TypeDecl *node) { return true; }
00069 static bool classof(const Ast *node) {
00070 return node->denotesTypeDecl();
00071 }
00072
00073 protected:
00074 TypeDecl(AstKind kind, IdentifierInfo *info = 0)
00075 : Decl(kind, info) {
00076 assert(this->denotesTypeDecl());
00077 }
00078 };
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 class ModelDecl : public TypeDecl {
00092
00093 public:
00094 virtual ~ModelDecl() { }
00095
00096
00097 Location getLocation() const { return location; }
00098
00099
00100 bool isParameterized() const {
00101 return kind == AST_VarietyDecl || kind == AST_FunctorDecl;
00102 }
00103
00104 PercentType *getPercent() const { return percent; }
00105
00106
00107 virtual ModelType *getType() const = 0;
00108
00109
00110 static bool classof(const ModelDecl *node) { return true; }
00111 static bool classof(const Ast *node) {
00112 return node->denotesModelDecl();
00113 }
00114
00115 protected:
00116
00117
00118 ModelDecl(AstKind kind, IdentifierInfo *percentId);
00119
00120 ModelDecl(AstKind kind,
00121 IdentifierInfo *percentId,
00122 IdentifierInfo *name,
00123 const Location &loc);
00124
00125
00126 Location location;
00127
00128
00129 PercentType *percent;
00130 };
00131
00132
00133
00134
00135
00136
00137 class Sigoid : public ModelDecl {
00138
00139 public:
00140 Sigoid(AstKind kind, IdentifierInfo *percentId)
00141 : ModelDecl(kind, percentId) { }
00142
00143 Sigoid(AstKind kind,
00144 IdentifierInfo *percentId,
00145 IdentifierInfo *idInfo,
00146 Location loc)
00147 : ModelDecl(kind, percentId, idInfo, loc) { }
00148
00149 virtual ~Sigoid() { }
00150
00151
00152
00153 SignatureDecl *getSignature();
00154
00155
00156
00157 VarietyDecl *getVariety();
00158
00159 protected:
00160 typedef llvm::SmallPtrSet<SignatureType*, 8> SignatureTable;
00161 SignatureTable directSupers;
00162 SignatureTable supersignatures;
00163
00164 public:
00165
00166 void addSupersignature(SignatureType *supersignature);
00167
00168 typedef SignatureTable::const_iterator sig_iterator;
00169 sig_iterator beginDirectSupers() const { return directSupers.begin(); }
00170 sig_iterator endDirectSupers() const { return directSupers.end(); }
00171
00172 sig_iterator beginSupers() const { return supersignatures.begin(); }
00173 sig_iterator endSupers() const { return supersignatures.end(); }
00174
00175 protected:
00176 typedef std::multimap<IdentifierInfo*, FunctionDecl*> ComponentTable;
00177 ComponentTable components;
00178
00179 public:
00180 typedef ComponentTable::iterator ComponentIter;
00181 ComponentIter beginComponents() { return components.begin(); }
00182 ComponentIter endComponents() { return components.end(); }
00183
00184
00185 void addComponent(FunctionDecl *fdecl);
00186
00187 FunctionDecl *findComponent(IdentifierInfo *name,
00188 FunctionType *ftype);
00189
00190 FunctionDecl *findDirectComponent(IdentifierInfo *name,
00191 FunctionType *ftype);
00192
00193
00194 typedef std::pair<ComponentIter, ComponentIter> ComponentRange;
00195 ComponentRange findComponents(IdentifierInfo *name) {
00196 return components.equal_range(name);
00197 }
00198
00199
00200
00201 bool removeComponent(FunctionDecl *fdecl);
00202
00203 static bool classof(const Sigoid *node) { return true; }
00204 static bool classof(const Ast *node) {
00205 AstKind kind = node->getKind();
00206 return kind == AST_SignatureDecl || kind == AST_VarietyDecl;
00207 }
00208 };
00209
00210
00211
00212
00213
00214 class SignatureDecl : public Sigoid {
00215
00216 public:
00217
00218 SignatureDecl(IdentifierInfo *percentId);
00219 SignatureDecl(IdentifierInfo *percentId,
00220 IdentifierInfo *name,
00221 const Location &loc);
00222
00223 SignatureType *getCorrespondingType() { return canonicalType; }
00224 SignatureType *getType() const { return canonicalType; }
00225
00226
00227 static bool classof(const SignatureDecl *node) { return true; }
00228 static bool classof(const Ast *node) {
00229 return node->getKind() == AST_SignatureDecl;
00230 }
00231
00232 private:
00233
00234
00235 SignatureType *canonicalType;
00236 };
00237
00238
00239
00240
00241
00242 class VarietyDecl : public Sigoid {
00243
00244 public:
00245
00246 VarietyDecl(IdentifierInfo *percentId,
00247 AbstractDomainType **formals,
00248 unsigned arity);
00249
00250
00251
00252 VarietyDecl(IdentifierInfo *percentId,
00253 IdentifierInfo *name,
00254 Location loc,
00255 AbstractDomainType **formals,
00256 unsigned arity);
00257
00258
00259
00260 SignatureType *getCorrespondingType(DomainType **args, unsigned numArgs);
00261 SignatureType *getCorrespondingType();
00262
00263
00264 VarietyType *getVarietyType() const { return varietyType; }
00265 VarietyType *getType() const { return varietyType; }
00266
00267
00268 unsigned getArity() const { return getVarietyType()->getArity(); }
00269
00270
00271
00272 AbstractDomainType *getFormalDomain(unsigned i) const {
00273 return getVarietyType()->getFormalDomain(i);
00274 }
00275
00276 typedef llvm::FoldingSet<SignatureType>::iterator type_iterator;
00277 type_iterator beginTypes() { return types.begin(); }
00278 type_iterator endTypes() { return types.end(); }
00279
00280
00281 static bool classof(const VarietyDecl *node) { return true; }
00282 static bool classof(const Ast *node) {
00283 return node->getKind() == AST_VarietyDecl;
00284 }
00285
00286 private:
00287 mutable llvm::FoldingSet<SignatureType> types;
00288
00289 VarietyType *varietyType;
00290 };
00291
00292
00293
00294
00295
00296
00297
00298 class Domoid : public ModelDecl {
00299
00300 public:
00301 virtual ~Domoid() { }
00302
00303
00304 DomainDecl *getDomain();
00305
00306
00307 FunctorDecl *getFunctor();
00308
00309
00310
00311
00312
00313 SignatureDecl *getPrincipleSignature() const {
00314 return principleSignature;
00315 }
00316
00317 static bool classof(const Domoid *node) { return true; }
00318 static bool classof(const Ast *node) {
00319 AstKind kind = node->getKind();
00320 return kind == AST_DomainDecl || kind == AST_FunctorDecl;
00321 }
00322
00323 protected:
00324 Domoid(AstKind kind,
00325 IdentifierInfo *percentId,
00326 IdentifierInfo *idInfo,
00327 Location loc);
00328
00329 SignatureDecl *principleSignature;
00330 };
00331
00332
00333
00334
00335 class DomainDecl : public Domoid {
00336
00337 public:
00338 DomainDecl(IdentifierInfo *percentId,
00339 IdentifierInfo *name,
00340 const Location &loc);
00341
00342 ConcreteDomainType *getCorrespondingType() { return canonicalType; }
00343 ConcreteDomainType *getType() const { return canonicalType; }
00344
00345 SignatureDecl *getPrincipleSignature() const { return principleSignature; }
00346
00347
00348 static bool classof(const DomainDecl *node) { return true; }
00349 static bool classof(const Ast *node) {
00350 AstKind kind = node->getKind();
00351 return kind == AST_DomainDecl || kind == AST_FunctorDecl;
00352 }
00353
00354 protected:
00355
00356 DomainDecl(AstKind kind,
00357 IdentifierInfo *percentId,
00358 IdentifierInfo *info,
00359 Location loc);
00360
00361 private:
00362 ConcreteDomainType *canonicalType;
00363 SignatureDecl *principleSignature;
00364 };
00365
00366
00367
00368
00369
00370 class FunctorDecl : public Domoid {
00371
00372 public:
00373 FunctorDecl(IdentifierInfo *percentId,
00374 IdentifierInfo *name,
00375 Location loc,
00376 AbstractDomainType **formals,
00377 unsigned arity);
00378
00379
00380
00381
00382 ConcreteDomainType *getCorrespondingType(DomainType **args,
00383 unsigned numArgs);
00384
00385 VarietyDecl *getPrincipleSignature() const { return principleSignature; }
00386
00387
00388 FunctorType *getFunctorType() const { return functor; }
00389 FunctorType *getType() const { return functor; }
00390
00391
00392 unsigned getArity() const { return getFunctorType()->getArity(); }
00393
00394 AbstractDomainType *getFormalDomain(unsigned i) const {
00395 return getFunctorType()->getFormalDomain(i);
00396 }
00397
00398
00399 static bool classof(const FunctorDecl *node) { return true; }
00400 static bool classof(const Ast *node) {
00401 return node->getKind() == AST_FunctorDecl;
00402 }
00403
00404 private:
00405 mutable llvm::FoldingSet<ConcreteDomainType> types;
00406
00407 FunctorType *functor;
00408 VarietyDecl *principleSignature;
00409 };
00410
00411
00412
00413
00414
00415 class FunctionDecl : public Decl {
00416
00417 public:
00418 FunctionDecl(IdentifierInfo *name,
00419 FunctionType *type,
00420 ModelType *context,
00421 Location loc);
00422
00423
00424
00425 bool isTypeContext(const ModelType *type) const {
00426 return type == context;
00427 }
00428
00429
00430 FunctionType *getFunctionType() const { return ftype; }
00431
00432 unsigned getArity() const { return ftype->getArity(); }
00433
00434 IdentifierInfo *getSelector(unsigned i) const {
00435 return ftype->getSelector(i);
00436 }
00437
00438 DomainType *getArgType(unsigned i) const {
00439 return ftype->getArgType(i);
00440 }
00441
00442 DomainType *getReturnType() const {
00443 return ftype->getReturnType();
00444 }
00445
00446
00447 static bool classof(const FunctionDecl *node) { return true; }
00448 static bool classof(const Ast *node) {
00449 return node->getKind() == AST_FunctionDecl;
00450 }
00451
00452 private:
00453 FunctionType *ftype;
00454 ModelType *context;
00455 Location location;
00456 };
00457
00458
00459
00460
00461 inline SignatureDecl *Sigoid::getSignature()
00462 {
00463 return llvm::dyn_cast<SignatureDecl>(this);
00464 }
00465
00466 inline VarietyDecl *Sigoid::getVariety()
00467 {
00468 return llvm::dyn_cast<VarietyDecl>(this);
00469 }
00470
00471 inline DomainDecl *Domoid::getDomain()
00472 {
00473 return llvm::dyn_cast<DomainDecl>(this);
00474 }
00475
00476 inline FunctorDecl *Domoid::getFunctor()
00477 {
00478 return llvm::dyn_cast<FunctorDecl>(this);
00479 }
00480
00481 }
00482
00483 #endif