00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "comma/ast/AstRewriter.h"
00010 #include "comma/ast/Decl.h"
00011 #include "comma/ast/Type.h"
00012 #include "llvm/Support/Casting.h"
00013 #include "llvm/ADT/SmallVector.h"
00014
00015 using namespace comma;
00016 using llvm::dyn_cast;
00017 using llvm::isa;
00018
00019 DomainType *AstRewriter::getRewrite(DomainType *source) const
00020 {
00021 RewriteMap::const_iterator iter = rewrites.find(source);
00022 if (iter == rewrites.end())
00023 return source;
00024 return iter->second;
00025 }
00026
00027
00028 void AstRewriter::installRewrites(DomainType *context)
00029 {
00030 if (isa<PercentType>(context)) return;
00031
00032 ModelDecl *model = context->getDeclaration();
00033 addRewrite(model->getPercent(), context);
00034
00035 if (ConcreteDomainType *domain = dyn_cast<ConcreteDomainType>(context)) {
00036 if (domain->isParameterized()) {
00037 FunctorDecl *functor = domain->getFunctor();
00038 unsigned arity = functor->getArity();
00039 for (unsigned i = 0; i < arity; ++i) {
00040 AbstractDomainType *formal = functor->getFormalDomain(i);
00041 DomainType *actual = domain->getActualParameter(i);
00042 rewrites[formal] = actual;
00043 }
00044 }
00045 }
00046 }
00047
00048 void AstRewriter::installRewrites(SignatureType *context)
00049 {
00050 VarietyDecl *variety = context->getVariety();
00051
00052 if (variety) {
00053 unsigned arity = variety->getArity();
00054 for (unsigned i = 0; i < arity; ++i) {
00055 AbstractDomainType *formal = variety->getFormalDomain(i);
00056 DomainType *actual = context->getActualParameter(i);
00057 addRewrite(formal, actual);
00058 }
00059 }
00060 }
00061
00062 SignatureType *AstRewriter::rewrite(SignatureType *sig) const
00063 {
00064 if (sig->isParameterized()) {
00065 llvm::SmallVector<DomainType*, 4> args;
00066 SignatureType::arg_iterator iter;
00067 SignatureType::arg_iterator endIter = sig->endArguments();
00068 for (iter = sig->beginArguments(); iter != endIter; ++iter) {
00069 if (DomainType *dom = getRewrite(*iter))
00070 args.push_back(dom);
00071 else
00072 args.push_back(*iter);
00073 }
00074
00075 VarietyDecl *decl = sig->getVariety();
00076 return decl->getCorrespondingType(&args[0], args.size());
00077 }
00078 return sig;
00079 }
00080
00081
00082 DomainType *AstRewriter::rewrite(DomainType *dom) const
00083 {
00084 ConcreteDomainType *source = llvm::dyn_cast<ConcreteDomainType>(dom);
00085
00086 if (source && source->isParameterized()) {
00087 llvm::SmallVector<DomainType*, 4> args;
00088
00089 ConcreteDomainType::arg_iterator iter;
00090 ConcreteDomainType::arg_iterator endIter = source->endArguments();
00091 for (iter = source->beginArguments(); iter != endIter; ++iter) {
00092
00093
00094 if (DomainType *target = getRewrite(*iter))
00095 args.push_back(target);
00096 else
00097 args.push_back(*iter);
00098 }
00099
00100 FunctorDecl *decl = source->getFunctor();
00101 return decl->getCorrespondingType(&args[0], args.size());
00102
00103 }
00104 return dom;
00105 }
00106
00107 FunctionType *AstRewriter::rewrite(FunctionType *ftype) const
00108 {
00109 llvm::SmallVector<IdentifierInfo*, 4> formals;
00110 llvm::SmallVector<DomainType*, 4> args;
00111 DomainType *source;
00112 DomainType *target;
00113 unsigned arity;
00114
00115 arity = ftype->getArity();
00116 for (unsigned i = 0; i < arity; ++i) {
00117 source = ftype->getArgType(i);
00118 target = getRewrite(source);
00119 if (target)
00120 args.push_back(target);
00121 else
00122 args.push_back(source);
00123 formals.push_back(ftype->getSelector(i));
00124 }
00125 source = ftype->getReturnType();
00126 target = getRewrite(source);
00127 if (target)
00128 return new FunctionType(&formals[0], &args[0], args.size(), target);
00129 else
00130 return new FunctionType(&formals[0], &args[0], args.size(), source);
00131 }
00132