Symbol.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "Symbol.h"
  21. #include "Symbols.h"
  22. #include "Control.h"
  23. #include "Names.h"
  24. #include "TranslationUnit.h"
  25. #include "Literals.h"
  26. #include "MemoryPool.h"
  27. #include "SymbolVisitor.h"
  28. #include "NameVisitor.h"
  29. #include "Scope.h"
  30. #include "Templates.h"
  31. #include "cppassert.h"
  32. using namespace CPlusPlus;
  33. class Symbol::HashCode: protected NameVisitor
  34. {
  35. public:
  36. HashCode()
  37. : _value(0)
  38. { }
  39. virtual ~HashCode()
  40. { }
  41. unsigned operator()(const Name *name)
  42. {
  43. unsigned previousValue = switchValue(0);
  44. accept(name);
  45. return switchValue(previousValue);
  46. }
  47. protected:
  48. unsigned switchValue(unsigned value)
  49. {
  50. unsigned previousValue = _value;
  51. _value = value;
  52. return previousValue;
  53. }
  54. virtual void visit(const Identifier *name)
  55. { _value = name->identifier()->hashCode(); }
  56. virtual void visit(const TemplateNameId *name)
  57. { _value = name->identifier()->hashCode(); }
  58. virtual void visit(const DestructorNameId *name)
  59. { _value = name->identifier()->hashCode(); }
  60. virtual void visit(const OperatorNameId *name)
  61. { _value = unsigned(name->kind()); }
  62. virtual void visit(const ConversionNameId *)
  63. { _value = 0; } // ### TODO: implement me
  64. virtual void visit(const QualifiedNameId *name)
  65. { _value = operator()(name->name()); }
  66. virtual void visit(const SelectorNameId *name)
  67. { _value = name->identifier()->hashCode(); }
  68. private:
  69. unsigned _value;
  70. };
  71. Symbol::Symbol(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
  72. : _name(0),
  73. _enclosingScope(0),
  74. _next(0),
  75. _fileId(0),
  76. _sourceLocation(0),
  77. _hashCode(0),
  78. _storage(Symbol::NoStorage),
  79. _visibility(Symbol::Public),
  80. _index(0),
  81. _line(0),
  82. _column(0),
  83. _isGenerated(false),
  84. _isDeprecated(false),
  85. _isUnavailable(false)
  86. {
  87. setSourceLocation(sourceLocation, translationUnit);
  88. setName(name);
  89. }
  90. Symbol::Symbol(Clone *clone, Subst *subst, Symbol *original)
  91. : _name(clone->name(original->_name, subst)),
  92. _enclosingScope(0),
  93. _next(0),
  94. _fileId(clone->control()->stringLiteral(original->fileName(), original->fileNameLength())),
  95. _sourceLocation(original->_sourceLocation),
  96. _hashCode(original->_hashCode),
  97. _storage(original->_storage),
  98. _visibility(original->_visibility),
  99. _index(0),
  100. _line(original->_line),
  101. _column(original->_column),
  102. _isGenerated(original->_isGenerated),
  103. _isDeprecated(original->_isDeprecated),
  104. _isUnavailable(original->_isUnavailable)
  105. {
  106. }
  107. Symbol::~Symbol()
  108. { }
  109. void Symbol::visitSymbol(SymbolVisitor *visitor)
  110. {
  111. if (visitor->preVisit(this))
  112. visitSymbol0(visitor);
  113. visitor->postVisit(this);
  114. }
  115. void Symbol::visitSymbol(Symbol *symbol, SymbolVisitor *visitor)
  116. {
  117. if (! symbol)
  118. return;
  119. symbol->visitSymbol(visitor);
  120. }
  121. unsigned Symbol::sourceLocation() const
  122. { return _sourceLocation; }
  123. bool Symbol::isGenerated() const
  124. { return _isGenerated; }
  125. bool Symbol::isDeprecated() const
  126. { return _isDeprecated; }
  127. void Symbol::setDeprecated(bool isDeprecated)
  128. { _isDeprecated = isDeprecated; }
  129. bool Symbol::isUnavailable() const
  130. { return _isUnavailable; }
  131. void Symbol::setUnavailable(bool isUnavailable)
  132. { _isUnavailable = isUnavailable; }
  133. void Symbol::setSourceLocation(unsigned sourceLocation, TranslationUnit *translationUnit)
  134. {
  135. _sourceLocation = sourceLocation;
  136. if (translationUnit) {
  137. const Token &tk = translationUnit->tokenAt(sourceLocation);
  138. _isGenerated = tk.generated();
  139. translationUnit->getPosition(tk.utf16charsBegin(), &_line, &_column, &_fileId);
  140. } else {
  141. _isGenerated = false;
  142. _line = 0;
  143. _column = 0;
  144. _fileId = 0;
  145. }
  146. }
  147. unsigned Symbol::line() const
  148. {
  149. return _line;
  150. }
  151. unsigned Symbol::column() const
  152. {
  153. return _column;
  154. }
  155. const StringLiteral *Symbol::fileId() const
  156. {
  157. return _fileId;
  158. }
  159. const char *Symbol::fileName() const
  160. { return fileId()->chars(); }
  161. unsigned Symbol::fileNameLength() const
  162. { return fileId()->size(); }
  163. const Name *Symbol::unqualifiedName() const
  164. {
  165. if (! _name)
  166. return 0;
  167. else if (const QualifiedNameId *q = _name->asQualifiedNameId())
  168. return q->name();
  169. return _name;
  170. }
  171. const Name *Symbol::name() const
  172. { return _name; }
  173. void Symbol::setName(const Name *name)
  174. {
  175. _name = name;
  176. if (! _name)
  177. _hashCode = 0;
  178. else {
  179. HashCode hh;
  180. _hashCode = hh(unqualifiedName());
  181. }
  182. }
  183. const Identifier *Symbol::identifier() const
  184. {
  185. if (_name)
  186. return _name->identifier();
  187. return 0;
  188. }
  189. Scope *Symbol::enclosingScope() const
  190. { return _enclosingScope; }
  191. void Symbol::setEnclosingScope(Scope *scope)
  192. {
  193. CPP_CHECK(! _enclosingScope);
  194. _enclosingScope = scope;
  195. }
  196. void Symbol::resetEnclosingScope()
  197. {
  198. _enclosingScope = 0;
  199. }
  200. Namespace *Symbol::enclosingNamespace() const
  201. {
  202. for (Scope *s = _enclosingScope; s; s = s->enclosingScope()) {
  203. if (Namespace *ns = s->asNamespace())
  204. return ns;
  205. }
  206. return 0;
  207. }
  208. Template *Symbol::enclosingTemplate() const
  209. {
  210. for (Scope *s = _enclosingScope; s; s = s->enclosingScope()) {
  211. if (Template *templ = s->asTemplate())
  212. return templ;
  213. }
  214. return 0;
  215. }
  216. Class *Symbol::enclosingClass() const
  217. {
  218. for (Scope *s = _enclosingScope; s; s = s->enclosingScope()) {
  219. if (Class *klass = s->asClass())
  220. return klass;
  221. }
  222. return 0;
  223. }
  224. Enum *Symbol::enclosingEnum() const
  225. {
  226. for (Scope *s = _enclosingScope; s; s = s->enclosingScope()) {
  227. if (Enum *e = s->asEnum())
  228. return e;
  229. }
  230. return 0;
  231. }
  232. Function *Symbol::enclosingFunction() const
  233. {
  234. for (Scope *s = _enclosingScope; s; s = s->enclosingScope()) {
  235. if (Function *fun = s->asFunction())
  236. return fun;
  237. }
  238. return 0;
  239. }
  240. Block *Symbol::enclosingBlock() const
  241. {
  242. for (Scope *s = _enclosingScope; s; s = s->enclosingScope()) {
  243. if (Block *block = s->asBlock())
  244. return block;
  245. }
  246. return 0;
  247. }
  248. unsigned Symbol::index() const
  249. { return _index; }
  250. Symbol *Symbol::next() const
  251. { return _next; }
  252. unsigned Symbol::hashCode() const
  253. { return _hashCode; }
  254. int Symbol::storage() const
  255. { return _storage; }
  256. void Symbol::setStorage(int storage)
  257. { _storage = storage; }
  258. int Symbol::visibility() const
  259. { return _visibility; }
  260. void Symbol::setVisibility(int visibility)
  261. { _visibility = visibility; }
  262. bool Symbol::isFriend() const
  263. { return _storage == Friend; }
  264. bool Symbol::isRegister() const
  265. { return _storage == Register; }
  266. bool Symbol::isStatic() const
  267. { return _storage == Static; }
  268. bool Symbol::isExtern() const
  269. { return _storage == Extern; }
  270. bool Symbol::isMutable() const
  271. { return _storage == Mutable; }
  272. bool Symbol::isTypedef() const
  273. { return _storage == Typedef; }
  274. bool Symbol::isPublic() const
  275. { return _visibility == Public; }
  276. bool Symbol::isProtected() const
  277. { return _visibility == Protected; }
  278. bool Symbol::isPrivate() const
  279. { return _visibility == Private; }
  280. bool Symbol::isScope() const
  281. { return asScope() != 0; }
  282. bool Symbol::isEnum() const
  283. { return asEnum() != 0; }
  284. bool Symbol::isFunction() const
  285. { return asFunction() != 0; }
  286. bool Symbol::isNamespace() const
  287. { return asNamespace() != 0; }
  288. bool Symbol::isTemplate() const
  289. { return asTemplate() != 0; }
  290. bool Symbol::isClass() const
  291. { return asClass() != 0; }
  292. bool Symbol::isForwardClassDeclaration() const
  293. { return asForwardClassDeclaration() != 0; }
  294. bool Symbol::isQtPropertyDeclaration() const
  295. { return asQtPropertyDeclaration() != 0; }
  296. bool Symbol::isQtEnum() const
  297. { return asQtEnum() != 0; }
  298. bool Symbol::isBlock() const
  299. { return asBlock() != 0; }
  300. bool Symbol::isUsingNamespaceDirective() const
  301. { return asUsingNamespaceDirective() != 0; }
  302. bool Symbol::isUsingDeclaration() const
  303. { return asUsingDeclaration() != 0; }
  304. bool Symbol::isDeclaration() const
  305. { return asDeclaration() != 0; }
  306. bool Symbol::isArgument() const
  307. { return asArgument() != 0; }
  308. bool Symbol::isTypenameArgument() const
  309. { return asTypenameArgument() != 0; }
  310. bool Symbol::isBaseClass() const
  311. { return asBaseClass() != 0; }
  312. bool Symbol::isObjCBaseClass() const
  313. { return asObjCBaseClass() != 0; }
  314. bool Symbol::isObjCBaseProtocol() const
  315. { return asObjCBaseProtocol() != 0; }
  316. bool Symbol::isObjCClass() const
  317. { return asObjCClass() != 0; }
  318. bool Symbol::isObjCForwardClassDeclaration() const
  319. { return asObjCForwardClassDeclaration() != 0; }
  320. bool Symbol::isObjCProtocol() const
  321. { return asObjCProtocol() != 0; }
  322. bool Symbol::isObjCForwardProtocolDeclaration() const
  323. { return asObjCForwardProtocolDeclaration() != 0; }
  324. bool Symbol::isObjCMethod() const
  325. { return asObjCMethod() != 0; }
  326. bool Symbol::isObjCPropertyDeclaration() const
  327. { return asObjCPropertyDeclaration() != 0; }
  328. void Symbol::copy(Symbol *other)
  329. {
  330. _sourceLocation = other->_sourceLocation;
  331. _name = other->_name;
  332. _hashCode = other->_hashCode;
  333. _storage = other->_storage;
  334. _visibility = other->_visibility;
  335. _enclosingScope = other->_enclosingScope;
  336. _index = other->_index;
  337. _next = other->_next;
  338. _fileId = other->_fileId;
  339. _line = other->_line;
  340. _column = other->_column;
  341. _isGenerated = other->_isGenerated;
  342. _isDeprecated = other->_isDeprecated;
  343. }