00001 //===-- ast/AstRewriter.h ------------------------------------- -*- C++ -*-===// 00002 // 00003 // This file is distributed under the MIT license. See LICENSE.txt for details. 00004 // 00005 // Copyright (C) 2008-2010, Stephen Wilson 00006 // 00007 //===----------------------------------------------------------------------===// 00008 00009 //===----------------------------------------------------------------------===// 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef COMMA_AST_ASTREWRITER_HDR_GUARD 00016 #define COMMA_AST_ASTREWRITER_HDR_GUARD 00017 00018 #include "comma/ast/AstBase.h" 00019 00020 #include <map> 00021 00022 namespace comma { 00023 00035 class AstRewriter { 00036 00037 public: 00039 AstRewriter(AstResource &resource) : resource(resource) { } 00040 00047 void addTypeRewrite(Type *source, Type *target) { 00048 rewrites[source] = target; 00049 } 00050 00053 template <class Iter> 00054 void addTypeRewrites(Iter I, Iter E) { 00055 for ( ; I != E; ++I) 00056 rewrites[I->first] = I->second; 00057 } 00058 00064 void installRewrites(DomainType *context); 00065 00072 void installRewrites(SigInstanceDecl *context); 00073 00075 bool hasRewriteRule(Type *source) const { 00076 return getRewrite(source) != source; 00077 } 00078 00080 void clear() { rewrites.clear(); } 00081 00089 00090 Type *rewriteType(Type *type) const; 00091 00092 DomainType *rewriteType(DomainType *dom) const; 00093 00094 SubroutineType *rewriteType(SubroutineType *srType) const; 00095 00096 FunctionType *rewriteType(FunctionType *ftype) const; 00097 00098 ProcedureType *rewriteType(ProcedureType *ftype) const; 00100 00106 SigInstanceDecl *rewriteSigInstance(SigInstanceDecl *sig) const; 00107 00108 // Returns the AstResource used to construct rewritten nodes. 00109 AstResource &getAstResource() const { return resource; } 00110 00111 protected: 00113 Type *findRewrite(Type *source) const; 00114 00115 private: 00116 AstResource &resource; 00117 00118 typedef std::map<Type*, Type*> RewriteMap; 00119 RewriteMap rewrites; 00120 00126 Type *&operator [](Type *source) { 00127 return rewrites[source]; 00128 } 00129 00132 Type *getRewrite(Type *source) const; 00133 00136 void rewriteParameters(SubroutineType *srType, 00137 unsigned count, Type **params) const; 00138 }; 00139 00140 } // End comma namespace 00141 00142 #endif