Names.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "Names.h"
  21. #include "Matcher.h"
  22. #include "NameVisitor.h"
  23. #include "Literals.h"
  24. #include <algorithm>
  25. #include <cstring>
  26. using namespace CPlusPlus;
  27. QualifiedNameId::~QualifiedNameId()
  28. { }
  29. void QualifiedNameId::accept0(NameVisitor *visitor) const
  30. { visitor->visit(this); }
  31. bool QualifiedNameId::match0(const Name *otherName, Matcher *matcher) const
  32. {
  33. if (const QualifiedNameId *name = otherName->asQualifiedNameId())
  34. return matcher->match(this, name);
  35. return false;
  36. }
  37. const Identifier *QualifiedNameId::identifier() const
  38. {
  39. if (const Name *u = name())
  40. return u->identifier();
  41. return 0;
  42. }
  43. const Name *QualifiedNameId::base() const
  44. { return _base; }
  45. const Name *QualifiedNameId::name() const
  46. { return _name; }
  47. DestructorNameId::DestructorNameId(const Name *name)
  48. : _name(name)
  49. { }
  50. DestructorNameId::~DestructorNameId()
  51. { }
  52. void DestructorNameId::accept0(NameVisitor *visitor) const
  53. { visitor->visit(this); }
  54. bool DestructorNameId::match0(const Name *otherName, Matcher *matcher) const
  55. {
  56. if (const DestructorNameId *name = otherName->asDestructorNameId())
  57. return matcher->match(this, name);
  58. return false;
  59. }
  60. const Name *DestructorNameId::name() const
  61. { return _name; }
  62. const Identifier *DestructorNameId::identifier() const
  63. { return _name->identifier(); }
  64. TemplateNameId::~TemplateNameId()
  65. { }
  66. void TemplateNameId::accept0(NameVisitor *visitor) const
  67. { visitor->visit(this); }
  68. bool TemplateNameId::match0(const Name *otherName, Matcher *matcher) const
  69. {
  70. if (const TemplateNameId *other = otherName->asTemplateNameId())
  71. return matcher->match(this, other);
  72. return false;
  73. }
  74. const Identifier *TemplateNameId::identifier() const
  75. { return _identifier; }
  76. unsigned TemplateNameId::templateArgumentCount() const
  77. { return unsigned(_templateArguments.size()); }
  78. const FullySpecifiedType &TemplateNameId::templateArgumentAt(unsigned index) const
  79. { return _templateArguments[index]; }
  80. bool TemplateNameId::Compare::operator()(const TemplateNameId *name,
  81. const TemplateNameId *other) const
  82. {
  83. if (name == 0)
  84. return other != 0;
  85. if (other == 0)
  86. return false;
  87. if (name == other)
  88. return false;
  89. const Identifier *id = name->identifier();
  90. const Identifier *otherId = other->identifier();
  91. if (id == 0)
  92. return otherId != 0;
  93. if (otherId == 0)
  94. return false;
  95. const int c = std::strcmp(id->chars(), otherId->chars());
  96. if (c == 0) {
  97. // we have to differentiate TemplateNameId with respect to specialization or instantiation
  98. if (name->isSpecialization() == other->isSpecialization()) {
  99. return std::lexicographical_compare(name->firstTemplateArgument(),
  100. name->lastTemplateArgument(),
  101. other->firstTemplateArgument(),
  102. other->lastTemplateArgument());
  103. } else {
  104. return name->isSpecialization();
  105. }
  106. }
  107. return c < 0;
  108. }
  109. OperatorNameId::OperatorNameId(Kind kind)
  110. : _kind(kind)
  111. { }
  112. OperatorNameId::~OperatorNameId()
  113. { }
  114. void OperatorNameId::accept0(NameVisitor *visitor) const
  115. { visitor->visit(this); }
  116. bool OperatorNameId::match0(const Name *otherName, Matcher *matcher) const
  117. {
  118. if (const OperatorNameId *name = otherName->asOperatorNameId())
  119. return matcher->match(this, name);
  120. return false;
  121. }
  122. OperatorNameId::Kind OperatorNameId::kind() const
  123. { return _kind; }
  124. const Identifier *OperatorNameId::identifier() const
  125. { return 0; }
  126. ConversionNameId::ConversionNameId(const FullySpecifiedType &type)
  127. : _type(type)
  128. { }
  129. ConversionNameId::~ConversionNameId()
  130. { }
  131. void ConversionNameId::accept0(NameVisitor *visitor) const
  132. { visitor->visit(this); }
  133. bool ConversionNameId::match0(const Name *otherName, Matcher *matcher) const
  134. {
  135. if (const ConversionNameId *name = otherName->asConversionNameId())
  136. return matcher->match(this, name);
  137. return false;
  138. }
  139. FullySpecifiedType ConversionNameId::type() const
  140. { return _type; }
  141. const Identifier *ConversionNameId::identifier() const
  142. { return 0; }
  143. SelectorNameId::~SelectorNameId()
  144. { }
  145. void SelectorNameId::accept0(NameVisitor *visitor) const
  146. { visitor->visit(this); }
  147. bool SelectorNameId::match0(const Name *otherName, Matcher *matcher) const
  148. {
  149. if (const SelectorNameId *name = otherName->asSelectorNameId())
  150. return matcher->match(this, name);
  151. return false;
  152. }
  153. const Identifier *SelectorNameId::identifier() const
  154. {
  155. if (_names.empty())
  156. return 0;
  157. return nameAt(0)->identifier();
  158. }
  159. unsigned SelectorNameId::nameCount() const
  160. { return unsigned(_names.size()); }
  161. const Name *SelectorNameId::nameAt(unsigned index) const
  162. { return _names[index]; }
  163. bool SelectorNameId::hasArguments() const
  164. { return _hasArguments; }
  165. AnonymousNameId::AnonymousNameId(unsigned classTokenIndex)
  166. : _classTokenIndex(classTokenIndex)
  167. { }
  168. AnonymousNameId::~AnonymousNameId()
  169. { }
  170. unsigned AnonymousNameId::classTokenIndex() const
  171. {
  172. return _classTokenIndex;
  173. }
  174. void AnonymousNameId::accept0(NameVisitor *visitor) const
  175. { visitor->visit(this); }
  176. bool AnonymousNameId::match0(const Name *otherName, Matcher *matcher) const
  177. {
  178. if (const AnonymousNameId *id = otherName->asAnonymousNameId())
  179. return matcher->match(this, id);
  180. return false;
  181. }
  182. const Identifier *AnonymousNameId::identifier() const
  183. { return 0; }