Templates.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (c) 2008 Roberto Raggi <[email protected]>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #ifndef CPLUSPLUS_TEMPLATES_H
  21. #define CPLUSPLUS_TEMPLATES_H
  22. #include "CPlusPlusForwardDeclarations.h"
  23. #include "TypeVisitor.h"
  24. #include "FullySpecifiedType.h"
  25. #include "Name.h"
  26. #include "NameVisitor.h"
  27. #include "SymbolVisitor.h"
  28. #include <map>
  29. #include <utility>
  30. namespace CPlusPlus {
  31. class Clone;
  32. class CPLUSPLUS_EXPORT Subst
  33. {
  34. Subst(const Subst &other);
  35. Subst &operator = (const Subst &other);
  36. public:
  37. Subst(Control *control, Subst *previous = 0)
  38. : _control(control)
  39. , _previous(previous)
  40. { }
  41. Control *control() const { return _control; }
  42. Subst *previous() const { return _previous; }
  43. FullySpecifiedType apply(const Name *name) const;
  44. void bind(const Name *name, const FullySpecifiedType &ty)
  45. { _map.insert(std::make_pair(name, ty)); }
  46. FullySpecifiedType &operator[](const Name *name) { return _map[name]; }
  47. bool contains(const Name *name) const { return _map.find(name) != _map.end(); }
  48. private:
  49. Control *_control;
  50. Subst *_previous;
  51. std::map<const Name *, FullySpecifiedType, Name::Compare> _map;
  52. };
  53. class CPLUSPLUS_EXPORT CloneType: protected TypeVisitor
  54. {
  55. public:
  56. CloneType(Clone *clone);
  57. FullySpecifiedType operator()(const FullySpecifiedType &type, Subst *subst) { return cloneType(type, subst); }
  58. FullySpecifiedType cloneType(const FullySpecifiedType &type, Subst *subst);
  59. protected:
  60. virtual void visit(UndefinedType *type);
  61. virtual void visit(VoidType *type);
  62. virtual void visit(IntegerType *type);
  63. virtual void visit(FloatType *type);
  64. virtual void visit(PointerToMemberType *type);
  65. virtual void visit(PointerType *type);
  66. virtual void visit(ReferenceType *type);
  67. virtual void visit(ArrayType *type);
  68. virtual void visit(NamedType *type);
  69. virtual void visit(Function *type);
  70. virtual void visit(Namespace *type);
  71. virtual void visit(Template *type);
  72. virtual void visit(Class *type);
  73. virtual void visit(Enum *type);
  74. virtual void visit(ForwardClassDeclaration *type);
  75. virtual void visit(ObjCClass *type);
  76. virtual void visit(ObjCProtocol *type);
  77. virtual void visit(ObjCMethod *type);
  78. virtual void visit(ObjCForwardClassDeclaration *type);
  79. virtual void visit(ObjCForwardProtocolDeclaration *type);
  80. protected:
  81. typedef std::pair <const FullySpecifiedType, Subst *> TypeSubstPair;
  82. std::map<TypeSubstPair, FullySpecifiedType> _cache;
  83. Clone *_clone;
  84. Control *_control;
  85. Subst *_subst;
  86. FullySpecifiedType _type;
  87. };
  88. class CPLUSPLUS_EXPORT CloneName: protected NameVisitor
  89. {
  90. public:
  91. CloneName(Clone *clone);
  92. const Name *operator()(const Name *name, Subst *subst) { return cloneName(name, subst); }
  93. const Name *cloneName(const Name *name, Subst *subst);
  94. protected:
  95. virtual void visit(const Identifier *name);
  96. virtual void visit(const AnonymousNameId *name);
  97. virtual void visit(const TemplateNameId *name);
  98. virtual void visit(const DestructorNameId *name);
  99. virtual void visit(const OperatorNameId *name);
  100. virtual void visit(const ConversionNameId *name);
  101. virtual void visit(const QualifiedNameId *name);
  102. virtual void visit(const SelectorNameId *name);
  103. protected:
  104. typedef std::pair <const Name *, Subst *> NameSubstPair;
  105. std::map<NameSubstPair, const Name *> _cache;
  106. Clone *_clone;
  107. Control *_control;
  108. Subst *_subst;
  109. const Name *_name;
  110. };
  111. class CPLUSPLUS_EXPORT CloneSymbol: protected SymbolVisitor
  112. {
  113. public:
  114. CloneSymbol(Clone *clone);
  115. Symbol *operator()(Symbol *symbol, Subst *subst) { return cloneSymbol(symbol, subst); }
  116. Symbol *cloneSymbol(Symbol *symbol, Subst *subst);
  117. protected:
  118. virtual bool visit(UsingNamespaceDirective *symbol);
  119. virtual bool visit(UsingDeclaration *symbol);
  120. virtual bool visit(NamespaceAlias *symbol);
  121. virtual bool visit(Declaration *symbol);
  122. virtual bool visit(Argument *symbol);
  123. virtual bool visit(TypenameArgument *symbol);
  124. virtual bool visit(BaseClass *symbol);
  125. virtual bool visit(Enum *symbol);
  126. virtual bool visit(Function *symbol);
  127. virtual bool visit(Namespace *symbol);
  128. virtual bool visit(Template *symbol);
  129. virtual bool visit(Class *symbol);
  130. virtual bool visit(Block *symbol);
  131. virtual bool visit(ForwardClassDeclaration *symbol);
  132. // Qt
  133. virtual bool visit(QtPropertyDeclaration *symbol);
  134. virtual bool visit(QtEnum *symbol);
  135. // Objective-C
  136. virtual bool visit(ObjCBaseClass *symbol);
  137. virtual bool visit(ObjCBaseProtocol *symbol);
  138. virtual bool visit(ObjCClass *symbol);
  139. virtual bool visit(ObjCForwardClassDeclaration *symbol);
  140. virtual bool visit(ObjCProtocol *symbol);
  141. virtual bool visit(ObjCForwardProtocolDeclaration *symbol);
  142. virtual bool visit(ObjCMethod *symbol);
  143. virtual bool visit(ObjCPropertyDeclaration *symbol);
  144. protected:
  145. typedef std::pair <Symbol *, Subst *> SymbolSubstPair;
  146. std::map<SymbolSubstPair, Symbol *> _cache;
  147. Clone *_clone;
  148. Control *_control;
  149. Subst *_subst;
  150. Symbol *_symbol;
  151. };
  152. class CPLUSPLUS_EXPORT Clone
  153. {
  154. Control *_control;
  155. public:
  156. Clone(Control *control);
  157. Control *control() const { return _control; }
  158. const StringLiteral *stringLiteral(const StringLiteral *literal);
  159. const NumericLiteral *numericLiteral(const NumericLiteral *literal);
  160. const Identifier *identifier(const Identifier *id);
  161. FullySpecifiedType type(const FullySpecifiedType &type, Subst *subst);
  162. const Name *name(const Name *name, Subst *subst);
  163. Symbol *symbol(Symbol *symbol, Subst *subst);
  164. Symbol *instantiate(Template *templ,
  165. const FullySpecifiedType *const args, unsigned argc,
  166. Subst *subst = 0);
  167. private:
  168. CloneType _type;
  169. CloneName _name;
  170. CloneSymbol _symbol;
  171. };
  172. } // end of namespace CPlusPlus
  173. #endif // CPLUSPLUS_TEMPLATES_H