00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "comma/ast/Type.h"
00010 #include "comma/ast/Decl.h"
00011 #include <algorithm>
00012
00013 using namespace comma;
00014
00015
00016
00017
00018 SignatureType::SignatureType(SignatureDecl *decl)
00019 : ModelType(AST_SignatureType, decl->getIdInfo()), sigoid(decl)
00020 {
00021 deletable = false;
00022 }
00023
00024 SignatureType::SignatureType(VarietyDecl *decl,
00025 DomainType **args, unsigned numArgs)
00026 : ModelType(AST_SignatureType, decl->getIdInfo()), sigoid(decl)
00027 {
00028 deletable = false;
00029 arguments = new DomainType*[numArgs];
00030 std::copy(args, args + numArgs, arguments);
00031 }
00032
00033 Sigoid *SignatureType::getDeclaration() const
00034 {
00035 return sigoid;
00036 }
00037
00038 SignatureDecl *SignatureType::getSignature() const
00039 {
00040 return llvm::dyn_cast<SignatureDecl>(sigoid);
00041 }
00042
00043 VarietyDecl *SignatureType::getVariety() const
00044 {
00045 return llvm::dyn_cast<VarietyDecl>(sigoid);
00046 }
00047
00048 unsigned SignatureType::getArity() const
00049 {
00050 VarietyDecl *variety = getVariety();
00051 if (variety)
00052 return variety->getArity();
00053 return 0;
00054 }
00055
00056 DomainType *SignatureType::getActualParameter(unsigned n) const
00057 {
00058 assert(isParameterized() &&
00059 "Cannot fetch parameter from non-parameterized type!");
00060 assert(n < getArity() && "Parameter index out of range!");
00061 return arguments[n];
00062 }
00063
00064 void SignatureType::Profile(llvm::FoldingSetNodeID &id,
00065 DomainType **args, unsigned numArgs)
00066 {
00067 for (unsigned i = 0; i < numArgs; ++i)
00068 id.AddPointer(args[i]);
00069 }
00070
00071
00072
00073
00074 ParameterizedType::ParameterizedType(AstKind kind,
00075 IdentifierInfo *idInfo,
00076 AbstractDomainType **formalArgs,
00077 unsigned arity)
00078 : ModelType(kind, idInfo),
00079 numFormals(arity)
00080 {
00081 assert(kind == AST_VarietyType || kind == AST_FunctorType);
00082 formals = new AbstractDomainType*[arity];
00083 std::copy(formalArgs, formalArgs + arity, formals);
00084 }
00085
00086 AbstractDomainType *ParameterizedType::getFormalDomain(unsigned i) const
00087 {
00088 assert(i < getArity() && "Formal domain index out of bounds!");
00089 return formals[i];
00090 }
00091
00092 SignatureType *ParameterizedType::getFormalType(unsigned i) const
00093 {
00094 return getFormalDomain(i)->getSignature();
00095 }
00096
00097 IdentifierInfo *ParameterizedType::getFormalIdInfo(unsigned i) const
00098 {
00099 return getFormalDomain(i)->getIdInfo();
00100 }
00101
00102 int ParameterizedType::getSelectorIndex(IdentifierInfo *selector) const
00103 {
00104 for (unsigned i = 0; i < getArity(); ++i) {
00105 if (getFormalIdInfo(i) == selector)
00106 return i;
00107 }
00108 return -1;
00109 }
00110
00111
00112
00113
00114 VarietyType::VarietyType(AbstractDomainType **formalArguments,
00115 VarietyDecl *variety, unsigned arity)
00116 : ParameterizedType(AST_VarietyType,
00117 variety->getIdInfo(), formalArguments, arity),
00118 variety(variety)
00119 {
00120
00121
00122 deletable = false;
00123 }
00124
00125 VarietyType::~VarietyType()
00126 {
00127 delete[] formals;
00128 }
00129
00130
00131
00132
00133 FunctorType::FunctorType(AbstractDomainType **formalArguments,
00134 FunctorDecl *functor, unsigned arity)
00135 : ParameterizedType(AST_FunctorType,
00136 functor->getIdInfo(), formalArguments, arity),
00137 functor(functor)
00138 {
00139
00140
00141 deletable = false;
00142 }
00143
00144 FunctorType::~FunctorType()
00145 {
00146 delete[] formals;
00147 }
00148
00149
00150
00151
00152 DomainType::DomainType(AstKind kind, IdentifierInfo *idInfo, ModelDecl *decl)
00153 : ModelType(kind, idInfo), modelDecl(decl)
00154 {
00155 assert(this->denotesDomainType());
00156 }
00157
00158 DomainType::DomainType(AstKind kind, IdentifierInfo *idInfo, SignatureType *sig)
00159 : ModelType(kind, idInfo), signatureType(sig)
00160 {
00161 assert(this->denotesDomainType());
00162 }
00163
00164 bool DomainType::isAbstract() const
00165 {
00166 return astNode->getKind() == AST_SignatureType;
00167 }
00168
00169 ModelDecl *DomainType::getDeclaration() const
00170 {
00171 if (isAbstract() && getKind() != AST_PercentType)
00172 return signatureType->getDeclaration();
00173 else
00174 return modelDecl;
00175 }
00176
00177 Domoid *DomainType::getDomoid() const
00178 {
00179 return llvm::dyn_cast<Domoid>(modelDecl);
00180 }
00181
00182 DomainDecl *DomainType::getDomain() const
00183 {
00184 return llvm::dyn_cast<DomainDecl>(modelDecl);
00185 }
00186
00187 FunctorDecl *DomainType::getFunctor() const
00188 {
00189 return llvm::dyn_cast<FunctorDecl>(modelDecl);
00190 }
00191
00192
00193
00194
00195 ConcreteDomainType::ConcreteDomainType(DomainDecl *decl)
00196 : DomainType(AST_ConcreteDomainType, decl->getIdInfo(), decl),
00197 arguments(0)
00198 {
00199 deletable = false;
00200 }
00201
00202 ConcreteDomainType::ConcreteDomainType(FunctorDecl *decl,
00203 DomainType **args,
00204 unsigned numArgs)
00205 : DomainType(AST_ConcreteDomainType, decl->getIdInfo(), decl)
00206 {
00207 deletable = false;
00208 arguments = new DomainType*[numArgs];
00209 std::copy(args, args + numArgs, arguments);
00210 }
00211
00212 unsigned ConcreteDomainType::getArity() const
00213 {
00214 FunctorDecl *functor = getFunctor();
00215 if (functor) return functor->getArity();
00216 return 0;
00217 }
00218
00219 DomainType *ConcreteDomainType::getActualParameter(unsigned i) const
00220 {
00221 assert(i < getArity() && "Index out of range!");
00222 return arguments[i];
00223 }
00224
00225 void ConcreteDomainType::Profile(llvm::FoldingSetNodeID &id,
00226 DomainType **args, unsigned numArgs)
00227 {
00228 for (unsigned i = 0; i < numArgs; ++i)
00229 id.AddPointer(args[i]);
00230 }
00231
00232
00233
00234
00235 FunctionType::FunctionType(IdentifierInfo **formals,
00236 DomainType **argTypes,
00237 unsigned numArgs,
00238 DomainType *returnType)
00239 : Type(AST_FunctionType),
00240 returnType(returnType),
00241 numArgs(numArgs)
00242 {
00243 selectors = new IdentifierInfo*[numArgs];
00244 argumentTypes = new DomainType*[numArgs];
00245 std::copy(formals, formals + numArgs, selectors);
00246 std::copy(argTypes, argTypes + numArgs, argumentTypes);
00247 }
00248
00249 bool FunctionType::selectorsMatch(const FunctionType *ftype) const
00250 {
00251 unsigned arity = getArity();
00252 if (ftype->getArity() == arity) {
00253 for (unsigned i = 0; i < arity; ++i)
00254 if (getSelector(i) != ftype->getSelector(i))
00255 return false;
00256 return true;
00257 }
00258 return false;
00259 }
00260
00261 bool FunctionType::equals(const FunctionType *ftype) const
00262 {
00263 unsigned arity = getArity();
00264
00265 if (arity != ftype->getArity())
00266 return false;
00267
00268 if (getReturnType() != ftype->getReturnType())
00269 return false;
00270
00271 for (unsigned i = 0; i < arity; ++i)
00272 if (getArgType(i) != ftype->getArgType(i))
00273 return false;
00274
00275 return true;
00276 }