00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "comma/ast/Decl.h"
00010 #include "comma/ast/AstRewriter.h"
00011 #include <algorithm>
00012
00013 using namespace comma;
00014
00015
00016
00017 ModelDecl::ModelDecl(AstKind kind, IdentifierInfo *percentId)
00018 : TypeDecl(kind),
00019 location()
00020 {
00021 assert(std::strcmp(percentId->getString(), "%") == 0 &&
00022 "Percent IdInfo not == \"%\"!");
00023 percent = new PercentType(percentId, this);
00024 }
00025
00026 ModelDecl::ModelDecl(AstKind kind,
00027 IdentifierInfo *percentId,
00028 IdentifierInfo *info,
00029 const Location &loc)
00030 : TypeDecl(kind, info),
00031 location(loc)
00032 {
00033 assert(std::strcmp(percentId->getString(), "%") == 0 &&
00034 "Percent IdInfo not == \"%\"!");
00035 percent = new PercentType(percentId, this);
00036 }
00037
00038
00039
00040
00041 void Sigoid::addSupersignature(SignatureType *supersignature)
00042 {
00043 if (directSupers.insert(supersignature)) {
00044 Sigoid *superDecl = supersignature->getDeclaration();
00045 AstRewriter rewriter;
00046
00047
00048
00049 rewriter.addRewrite(superDecl->getPercent(), getPercent());
00050
00051
00052
00053
00054 if (VarietyDecl *variety = superDecl->getVariety()) {
00055 unsigned arity = variety->getArity();
00056 for (unsigned i = 0; i < arity; ++i) {
00057 DomainType *formal = variety->getFormalDomain(i);
00058 DomainType *actual = supersignature->getActualParameter(i);
00059 rewriter.addRewrite(formal, actual);
00060 }
00061 }
00062
00063 sig_iterator iter = superDecl->beginSupers();
00064 sig_iterator endIter = superDecl->endSupers();
00065 for ( ; iter != endIter; ++iter) {
00066 SignatureType *rewrite = rewriter.rewrite(*iter);
00067 supersignatures.insert(rewrite);
00068 }
00069 supersignatures.insert(supersignature);
00070 }
00071 }
00072
00073 void Sigoid::addComponent(FunctionDecl *fdecl)
00074 {
00075 IdentifierInfo *name = fdecl->getIdInfo();
00076 components.insert(ComponentTable::value_type(name, fdecl));
00077 }
00078
00079 FunctionDecl *Sigoid::findComponent(IdentifierInfo *name,
00080 FunctionType *ftype)
00081 {
00082 if (FunctionDecl *fdecl = findDirectComponent(name, ftype))
00083 return fdecl;
00084
00085 ComponentRange range = findComponents(name);
00086 for (ComponentIter iter = range.first; iter != range.second; ++iter) {
00087 FunctionDecl *fdecl = iter->second;
00088 FunctionType *candidateType = fdecl->getFunctionType();
00089 if (candidateType->equals(ftype))
00090 return fdecl;
00091 }
00092 return 0;
00093 }
00094
00095 FunctionDecl *Sigoid::findDirectComponent(IdentifierInfo *name,
00096 FunctionType *ftype)
00097 {
00098 ComponentRange range = findComponents(name);
00099 PercentType *percent = this->getPercent();
00100 for (ComponentIter iter = range.first; iter != range.second; ++iter) {
00101 FunctionDecl *fdecl = iter->second;
00102 FunctionType *candidateType = fdecl->getFunctionType();
00103 if (fdecl->isTypeContext(percent) && candidateType->equals(ftype))
00104 return fdecl;
00105 }
00106 return 0;
00107 }
00108
00109 bool Sigoid::removeComponent(FunctionDecl *fdecl)
00110 {
00111 IdentifierInfo *name = fdecl->getIdInfo();
00112 ComponentRange range = findComponents(name);
00113 for (ComponentIter iter = range.first; iter != range.second; ++iter)
00114 if (iter->second == fdecl) {
00115 components.erase(iter);
00116 return true;
00117 }
00118 return false;
00119 }
00120
00121
00122
00123 SignatureDecl::SignatureDecl(IdentifierInfo *percentId)
00124 : Sigoid(AST_SignatureDecl, percentId)
00125 {
00126 canonicalType = new SignatureType(this);
00127 }
00128
00129 SignatureDecl::SignatureDecl(IdentifierInfo *percentId,
00130 IdentifierInfo *info,
00131 const Location &loc)
00132 : Sigoid(AST_SignatureDecl, percentId, info, loc)
00133 {
00134 canonicalType = new SignatureType(this);
00135 }
00136
00137
00138
00139
00140 VarietyDecl::VarietyDecl(IdentifierInfo *percentId,
00141 AbstractDomainType **formals,
00142 unsigned arity)
00143 : Sigoid(AST_VarietyDecl, percentId)
00144 {
00145 varietyType = new VarietyType(formals, this, arity);
00146 }
00147
00148 VarietyDecl::VarietyDecl(IdentifierInfo *percentId,
00149 IdentifierInfo *name,
00150 Location loc,
00151 AbstractDomainType **formals,
00152 unsigned arity)
00153 : Sigoid(AST_VarietyDecl, percentId, name, loc)
00154 {
00155 varietyType = new VarietyType(formals, this, arity);
00156 }
00157
00158 SignatureType *
00159 VarietyDecl::getCorrespondingType(DomainType **args, unsigned numArgs)
00160 {
00161 llvm::FoldingSetNodeID id;
00162 void *insertPos = 0;
00163 SignatureType *type;
00164
00165 SignatureType::Profile(id, args, numArgs);
00166 type = types.FindNodeOrInsertPos(id, insertPos);
00167 if (type) return type;
00168
00169 type = new SignatureType(this, args, numArgs);
00170 types.InsertNode(type, insertPos);
00171 return type;
00172 }
00173
00174 SignatureType *VarietyDecl::getCorrespondingType()
00175 {
00176 VarietyType *thisType = getType();
00177 DomainType **formals = reinterpret_cast<DomainType**>(thisType->formals);
00178 return getCorrespondingType(formals, getArity());
00179 }
00180
00181
00182
00183
00184 Domoid::Domoid(AstKind kind,
00185 IdentifierInfo *percentId,
00186 IdentifierInfo *idInfo,
00187 Location loc)
00188 : ModelDecl(kind, percentId, idInfo, loc)
00189 {
00190 principleSignature = new SignatureDecl(percentId);
00191 }
00192
00193
00194
00195 DomainDecl::DomainDecl(IdentifierInfo *percentId,
00196 IdentifierInfo *name,
00197 const Location &loc)
00198 : Domoid(AST_DomainDecl, percentId, name, loc)
00199 {
00200 canonicalType = new ConcreteDomainType(this);
00201 }
00202
00203 DomainDecl::DomainDecl(AstKind kind,
00204 IdentifierInfo *percentId,
00205 IdentifierInfo *info,
00206 Location loc)
00207 : Domoid(kind, percentId, info, loc)
00208 {
00209 canonicalType = new ConcreteDomainType(this);
00210 }
00211
00212
00213
00214
00215 FunctorDecl::FunctorDecl(IdentifierInfo *percentId,
00216 IdentifierInfo *name,
00217 Location loc,
00218 AbstractDomainType **formals,
00219 unsigned arity)
00220 : Domoid(AST_FunctorDecl, percentId, name, loc)
00221 {
00222
00223
00224 functor = new FunctorType(formals, this, arity);
00225 }
00226
00227 ConcreteDomainType *
00228 FunctorDecl::getCorrespondingType(DomainType **args, unsigned numArgs)
00229 {
00230 llvm::FoldingSetNodeID id;
00231 void *insertPos = 0;
00232 ConcreteDomainType *type;
00233
00234 ConcreteDomainType::Profile(id, args, numArgs);
00235 type = types.FindNodeOrInsertPos(id, insertPos);
00236 if (type) return type;
00237
00238 type = new ConcreteDomainType(this, args, numArgs);
00239 types.InsertNode(type, insertPos);
00240 return type;
00241 }
00242
00243
00244
00245
00246 FunctionDecl::FunctionDecl(IdentifierInfo *name,
00247 FunctionType *type,
00248 ModelType *context,
00249 Location loc)
00250 : Decl(AST_FunctionDecl, name),
00251 ftype(type),
00252 context(context),
00253 location(loc)
00254 {
00255 }