Matcher.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and Digia. For licensing terms and
  13. ** conditions see http://www.qt.io/licensing. For further information
  14. ** use the contact form at http://www.qt.io/contact-us.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 or version 3 as published by the Free
  19. ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
  20. ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
  21. ** following information to ensure the GNU Lesser General Public License
  22. ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
  23. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ****************************************************************************/
  30. #include "Matcher.h"
  31. #include "CoreTypes.h"
  32. #include "Symbols.h"
  33. #include "Names.h"
  34. #include "Literals.h"
  35. using namespace CPlusPlus;
  36. static Matcher *defaultMatcher()
  37. {
  38. static Matcher matcher;
  39. return &matcher;
  40. }
  41. Matcher::Matcher()
  42. {
  43. }
  44. Matcher::~Matcher()
  45. {
  46. }
  47. bool Matcher::match(const Type *type, const Type *otherType, Matcher *matcher)
  48. {
  49. if (type == otherType)
  50. return true;
  51. if (!type || !otherType)
  52. return false;
  53. return type->match0(otherType, matcher ? matcher : defaultMatcher());
  54. }
  55. bool Matcher::match(const Name *name, const Name *otherName, Matcher *matcher)
  56. {
  57. if (name == otherName)
  58. return true;
  59. if (!name || !otherName)
  60. return false;
  61. return name->match0(otherName, matcher ? matcher : defaultMatcher());
  62. }
  63. bool Matcher::match(const UndefinedType *, const UndefinedType *)
  64. {
  65. return true;
  66. }
  67. bool Matcher::match(const VoidType *, const VoidType *)
  68. {
  69. return true;
  70. }
  71. bool Matcher::match(const IntegerType *type, const IntegerType *otherType)
  72. {
  73. if (type == otherType)
  74. return true;
  75. else if (type->kind() != otherType->kind())
  76. return false;
  77. return true;
  78. }
  79. bool Matcher::match(const FloatType *type, const FloatType *otherType)
  80. {
  81. if (type == otherType)
  82. return true;
  83. else if (type->kind() != otherType->kind())
  84. return false;
  85. return true;
  86. }
  87. bool Matcher::match(const PointerToMemberType *type, const PointerToMemberType *otherType)
  88. {
  89. if (type == otherType)
  90. return true;
  91. else if (! Matcher::match(type->memberName(), otherType->memberName(), this))
  92. return false;
  93. else if (! type->elementType().match(otherType->elementType(), this))
  94. return false;
  95. return true;
  96. }
  97. bool Matcher::match(const PointerType *type, const PointerType *otherType)
  98. {
  99. if (type == otherType)
  100. return true;
  101. else if (! type->elementType().match(otherType->elementType(), this))
  102. return false;
  103. return true;
  104. }
  105. bool Matcher::match(const ReferenceType *type, const ReferenceType *otherType)
  106. {
  107. if (type == otherType)
  108. return true;
  109. else if (type->isRvalueReference() != otherType->isRvalueReference())
  110. return false;
  111. else if (! type->elementType().match(otherType->elementType(), this))
  112. return false;
  113. return true;
  114. }
  115. bool Matcher::match(const ArrayType *type, const ArrayType *otherType)
  116. {
  117. if (type == otherType)
  118. return true;
  119. else if (type->size() != otherType->size())
  120. return false;
  121. else if (! type->elementType().match(otherType->elementType(), this))
  122. return false;
  123. return true;
  124. }
  125. bool Matcher::match(const NamedType *type, const NamedType *otherType)
  126. {
  127. if (type == otherType)
  128. return true;
  129. const Name *name = type->name();
  130. if (const QualifiedNameId *q = name->asQualifiedNameId())
  131. name = q->name();
  132. const Name *otherName = otherType->name();
  133. if (const QualifiedNameId *q = otherName->asQualifiedNameId())
  134. otherName = q->name();
  135. if (! Matcher::match(name, otherName, this))
  136. return false;
  137. return true;
  138. }
  139. bool Matcher::match(const Function *type, const Function *otherType)
  140. {
  141. if (type == otherType)
  142. return true;
  143. else if (! type->isSignatureEqualTo(otherType, this))
  144. return false;
  145. else if (! type->returnType().match(otherType->returnType(), this))
  146. return false;
  147. return true;
  148. }
  149. bool Matcher::match(const Enum *type, const Enum *otherType)
  150. {
  151. if (type == otherType)
  152. return true;
  153. else if (! Matcher::match(type->unqualifiedName(), otherType->unqualifiedName(), this))
  154. return false;
  155. return true;
  156. }
  157. bool Matcher::match(const Namespace *type, const Namespace *otherType)
  158. {
  159. if (type == otherType)
  160. return true;
  161. else if (! Matcher::match(type->unqualifiedName(), otherType->unqualifiedName(), this))
  162. return false;
  163. return true;
  164. }
  165. bool Matcher::match(const Template *type, const Template *otherType)
  166. {
  167. if (type != otherType)
  168. return false;
  169. return true;
  170. }
  171. bool Matcher::match(const ForwardClassDeclaration *type, const ForwardClassDeclaration *otherType)
  172. {
  173. if (type == otherType)
  174. return true;
  175. else if (! Matcher::match(type->name(), otherType->name(), this))
  176. return false;
  177. return true;
  178. }
  179. bool Matcher::match(const Class *type, const Class *otherType)
  180. {
  181. if (type == otherType)
  182. return true;
  183. else if (! Matcher::match(type->unqualifiedName(), otherType->unqualifiedName(), this))
  184. return false;
  185. return true;
  186. }
  187. bool Matcher::match(const ObjCClass *type, const ObjCClass *otherType)
  188. {
  189. if (type == otherType)
  190. return true;
  191. else if (! Matcher::match(type->unqualifiedName(), otherType->unqualifiedName(), this))
  192. return false;
  193. return true;
  194. }
  195. bool Matcher::match(const ObjCProtocol *type, const ObjCProtocol *otherType)
  196. {
  197. if (type == otherType)
  198. return true;
  199. else if (! Matcher::match(type->unqualifiedName(), otherType->unqualifiedName(), this))
  200. return false;
  201. return true;
  202. }
  203. bool Matcher::match(const ObjCForwardClassDeclaration *type, const ObjCForwardClassDeclaration *otherType)
  204. {
  205. if (type == otherType)
  206. return true;
  207. else if (! Matcher::match(type->name(), otherType->name(), this))
  208. return false;
  209. return true;
  210. }
  211. bool Matcher::match(const ObjCForwardProtocolDeclaration *type, const ObjCForwardProtocolDeclaration *otherType)
  212. {
  213. if (type == otherType)
  214. return true;
  215. else if (! Matcher::match(type->name(), otherType->name(), this))
  216. return false;
  217. return true;
  218. }
  219. bool Matcher::match(const ObjCMethod *type, const ObjCMethod *otherType)
  220. {
  221. if (type == otherType)
  222. return true;
  223. else if (! Matcher::match(type->unqualifiedName(), otherType->unqualifiedName(), this))
  224. return false;
  225. else if (type->argumentCount() != otherType->argumentCount())
  226. return false;
  227. else if (! type->returnType().match(otherType->returnType(), this))
  228. return false;
  229. for (unsigned i = 0; i < type->argumentCount(); ++i) {
  230. Symbol *l = type->argumentAt(i);
  231. Symbol *r = otherType->argumentAt(i);
  232. if (! l->type().match(r->type(), this))
  233. return false;
  234. }
  235. return true;
  236. }
  237. bool Matcher::match(const Identifier *name, const Identifier *otherName)
  238. {
  239. if (name == otherName)
  240. return true;
  241. return name->equalTo(otherName);
  242. }
  243. bool Matcher::match(const AnonymousNameId *name, const AnonymousNameId *otherName)
  244. {
  245. return otherName && name->classTokenIndex() == otherName->classTokenIndex();
  246. }
  247. bool Matcher::match(const TemplateNameId *name, const TemplateNameId *otherName)
  248. {
  249. const Identifier *l = name->identifier();
  250. const Identifier *r = otherName->identifier();
  251. if (! match(l, r))
  252. return false;
  253. if (name->templateArgumentCount() != otherName->templateArgumentCount())
  254. return false;
  255. for (unsigned i = 0, ei = name->templateArgumentCount(); i != ei; ++i) {
  256. const FullySpecifiedType &l = name->templateArgumentAt(i);
  257. const FullySpecifiedType &r = otherName->templateArgumentAt(i);
  258. if (! l.match(r, this))
  259. return false;
  260. }
  261. return true;
  262. }
  263. bool Matcher::match(const DestructorNameId *name, const DestructorNameId *otherName)
  264. {
  265. return Matcher::match(name->name(), otherName->name(), this);
  266. }
  267. bool Matcher::match(const OperatorNameId *name, const OperatorNameId *otherName)
  268. {
  269. return name->kind() == otherName->kind();
  270. }
  271. bool Matcher::match(const ConversionNameId *name, const ConversionNameId *otherName)
  272. {
  273. return name->type().match(otherName->type(), this);
  274. }
  275. bool Matcher::match(const QualifiedNameId *name, const QualifiedNameId *otherName)
  276. {
  277. if (Matcher::match(name->base(), otherName->base(), this))
  278. return Matcher::match(name->name(), otherName->name(), this);
  279. return false;
  280. }
  281. bool Matcher::match(const SelectorNameId *name, const SelectorNameId *otherName)
  282. {
  283. const unsigned nc = name->nameCount();
  284. if (name->hasArguments() != otherName->hasArguments() || nc != otherName->nameCount())
  285. return false;
  286. for (unsigned i = 0; i < nc; ++i)
  287. if (! Matcher::match(name->nameAt(i), otherName->nameAt(i), this))
  288. return false;
  289. return true;
  290. }