TypeLoc.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //===--- TypeLoc.cpp - Type Source Info Wrapper -----------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the TypeLoc subclasses implementations.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/TypeLoc.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Expr.h"
  16. #include "clang/AST/TypeLocVisitor.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. using namespace clang;
  19. static const unsigned TypeLocMaxDataAlign = alignof(void *);
  20. //===----------------------------------------------------------------------===//
  21. // TypeLoc Implementation
  22. //===----------------------------------------------------------------------===//
  23. namespace {
  24. class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
  25. public:
  26. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  27. #define TYPELOC(CLASS, PARENT) \
  28. SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  29. return TyLoc.getLocalSourceRange(); \
  30. }
  31. #include "clang/AST/TypeLocNodes.def"
  32. };
  33. }
  34. SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
  35. if (TL.isNull()) return SourceRange();
  36. return TypeLocRanger().Visit(TL);
  37. }
  38. namespace {
  39. class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
  40. public:
  41. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  42. #define TYPELOC(CLASS, PARENT) \
  43. unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  44. return TyLoc.getLocalDataAlignment(); \
  45. }
  46. #include "clang/AST/TypeLocNodes.def"
  47. };
  48. }
  49. /// \brief Returns the alignment of the type source info data block.
  50. unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
  51. if (Ty.isNull()) return 1;
  52. return TypeAligner().Visit(TypeLoc(Ty, nullptr));
  53. }
  54. namespace {
  55. class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
  56. public:
  57. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  58. #define TYPELOC(CLASS, PARENT) \
  59. unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  60. return TyLoc.getLocalDataSize(); \
  61. }
  62. #include "clang/AST/TypeLocNodes.def"
  63. };
  64. }
  65. /// \brief Returns the size of the type source info data block.
  66. unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
  67. unsigned Total = 0;
  68. TypeLoc TyLoc(Ty, nullptr);
  69. unsigned MaxAlign = 1;
  70. while (!TyLoc.isNull()) {
  71. unsigned Align = getLocalAlignmentForType(TyLoc.getType());
  72. MaxAlign = std::max(Align, MaxAlign);
  73. Total = llvm::alignTo(Total, Align);
  74. Total += TypeSizer().Visit(TyLoc);
  75. TyLoc = TyLoc.getNextTypeLoc();
  76. }
  77. Total = llvm::alignTo(Total, MaxAlign);
  78. return Total;
  79. }
  80. namespace {
  81. class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
  82. public:
  83. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  84. #define TYPELOC(CLASS, PARENT) \
  85. TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  86. return TyLoc.getNextTypeLoc(); \
  87. }
  88. #include "clang/AST/TypeLocNodes.def"
  89. };
  90. }
  91. /// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
  92. /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
  93. TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
  94. return NextLoc().Visit(TL);
  95. }
  96. /// \brief Initializes a type location, and all of its children
  97. /// recursively, as if the entire tree had been written in the
  98. /// given location.
  99. void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
  100. SourceLocation Loc) {
  101. while (true) {
  102. switch (TL.getTypeLocClass()) {
  103. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  104. #define TYPELOC(CLASS, PARENT) \
  105. case CLASS: { \
  106. CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
  107. TLCasted.initializeLocal(Context, Loc); \
  108. TL = TLCasted.getNextTypeLoc(); \
  109. if (!TL) return; \
  110. continue; \
  111. }
  112. #include "clang/AST/TypeLocNodes.def"
  113. }
  114. }
  115. }
  116. namespace {
  117. class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
  118. TypeLoc Source;
  119. public:
  120. TypeLocCopier(TypeLoc source) : Source(source) { }
  121. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  122. #define TYPELOC(CLASS, PARENT) \
  123. void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) { \
  124. dest.copyLocal(Source.castAs<CLASS##TypeLoc>()); \
  125. }
  126. #include "clang/AST/TypeLocNodes.def"
  127. };
  128. }
  129. void TypeLoc::copy(TypeLoc other) {
  130. assert(getFullDataSize() == other.getFullDataSize());
  131. // If both data pointers are aligned to the maximum alignment, we
  132. // can memcpy because getFullDataSize() accurately reflects the
  133. // layout of the data.
  134. if (reinterpret_cast<uintptr_t>(Data) ==
  135. llvm::alignTo(reinterpret_cast<uintptr_t>(Data),
  136. TypeLocMaxDataAlign) &&
  137. reinterpret_cast<uintptr_t>(other.Data) ==
  138. llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
  139. TypeLocMaxDataAlign)) {
  140. memcpy(Data, other.Data, getFullDataSize());
  141. return;
  142. }
  143. // Copy each of the pieces.
  144. TypeLoc TL(getType(), Data);
  145. do {
  146. TypeLocCopier(other).Visit(TL);
  147. other = other.getNextTypeLoc();
  148. } while ((TL = TL.getNextTypeLoc()));
  149. }
  150. SourceLocation TypeLoc::getBeginLoc() const {
  151. TypeLoc Cur = *this;
  152. TypeLoc LeftMost = Cur;
  153. while (true) {
  154. switch (Cur.getTypeLocClass()) {
  155. case Elaborated:
  156. LeftMost = Cur;
  157. break;
  158. case FunctionProto:
  159. if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
  160. ->hasTrailingReturn()) {
  161. LeftMost = Cur;
  162. break;
  163. }
  164. /* Fall through */
  165. case FunctionNoProto:
  166. case ConstantArray:
  167. case DependentSizedArray:
  168. case IncompleteArray:
  169. case VariableArray:
  170. // FIXME: Currently QualifiedTypeLoc does not have a source range
  171. case Qualified:
  172. Cur = Cur.getNextTypeLoc();
  173. continue;
  174. default:
  175. if (Cur.getLocalSourceRange().getBegin().isValid())
  176. LeftMost = Cur;
  177. Cur = Cur.getNextTypeLoc();
  178. if (Cur.isNull())
  179. break;
  180. continue;
  181. } // switch
  182. break;
  183. } // while
  184. return LeftMost.getLocalSourceRange().getBegin();
  185. }
  186. SourceLocation TypeLoc::getEndLoc() const {
  187. TypeLoc Cur = *this;
  188. TypeLoc Last;
  189. while (true) {
  190. switch (Cur.getTypeLocClass()) {
  191. default:
  192. if (!Last)
  193. Last = Cur;
  194. return Last.getLocalSourceRange().getEnd();
  195. case Paren:
  196. case ConstantArray:
  197. case DependentSizedArray:
  198. case IncompleteArray:
  199. case VariableArray:
  200. case FunctionNoProto:
  201. Last = Cur;
  202. break;
  203. case FunctionProto:
  204. if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
  205. Last = TypeLoc();
  206. else
  207. Last = Cur;
  208. break;
  209. case Pointer:
  210. case BlockPointer:
  211. case MemberPointer:
  212. case LValueReference:
  213. case RValueReference:
  214. case PackExpansion:
  215. if (!Last)
  216. Last = Cur;
  217. break;
  218. case Qualified:
  219. case Elaborated:
  220. break;
  221. }
  222. Cur = Cur.getNextTypeLoc();
  223. }
  224. }
  225. namespace {
  226. struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
  227. // Overload resolution does the real work for us.
  228. static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
  229. static bool isTypeSpec(TypeLoc _) { return false; }
  230. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  231. #define TYPELOC(CLASS, PARENT) \
  232. bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  233. return isTypeSpec(TyLoc); \
  234. }
  235. #include "clang/AST/TypeLocNodes.def"
  236. };
  237. }
  238. /// \brief Determines if the given type loc corresponds to a
  239. /// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in
  240. /// the type hierarchy, this is made somewhat complicated.
  241. ///
  242. /// There are a lot of types that currently use TypeSpecTypeLoc
  243. /// because it's a convenient base class. Ideally we would not accept
  244. /// those here, but ideally we would have better implementations for
  245. /// them.
  246. bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
  247. if (TL.getType().hasLocalQualifiers()) return false;
  248. return TSTChecker().Visit(TL);
  249. }
  250. // Reimplemented to account for GNU/C++ extension
  251. // typeof unary-expression
  252. // where there are no parentheses.
  253. SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
  254. if (getRParenLoc().isValid())
  255. return SourceRange(getTypeofLoc(), getRParenLoc());
  256. else
  257. return SourceRange(getTypeofLoc(),
  258. getUnderlyingExpr()->getSourceRange().getEnd());
  259. }
  260. TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
  261. if (needsExtraLocalData())
  262. return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
  263. switch (getTypePtr()->getKind()) {
  264. case BuiltinType::Void:
  265. return TST_void;
  266. case BuiltinType::Bool:
  267. return TST_bool;
  268. case BuiltinType::Char_U:
  269. case BuiltinType::Char_S:
  270. return TST_char;
  271. case BuiltinType::Char16:
  272. return TST_char16;
  273. case BuiltinType::Char32:
  274. return TST_char32;
  275. case BuiltinType::WChar_S:
  276. case BuiltinType::WChar_U:
  277. return TST_wchar;
  278. case BuiltinType::UChar:
  279. case BuiltinType::UShort:
  280. case BuiltinType::UInt:
  281. case BuiltinType::ULong:
  282. case BuiltinType::ULongLong:
  283. case BuiltinType::UInt128:
  284. case BuiltinType::SChar:
  285. case BuiltinType::Short:
  286. case BuiltinType::Int:
  287. case BuiltinType::Long:
  288. case BuiltinType::LongLong:
  289. case BuiltinType::Int128:
  290. case BuiltinType::Half:
  291. case BuiltinType::Float:
  292. case BuiltinType::Double:
  293. case BuiltinType::LongDouble:
  294. case BuiltinType::Float16:
  295. case BuiltinType::Float128:
  296. llvm_unreachable("Builtin type needs extra local data!");
  297. // Fall through, if the impossible happens.
  298. case BuiltinType::NullPtr:
  299. case BuiltinType::Overload:
  300. case BuiltinType::Dependent:
  301. case BuiltinType::BoundMember:
  302. case BuiltinType::UnknownAny:
  303. case BuiltinType::ARCUnbridgedCast:
  304. case BuiltinType::PseudoObject:
  305. case BuiltinType::ObjCId:
  306. case BuiltinType::ObjCClass:
  307. case BuiltinType::ObjCSel:
  308. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  309. case BuiltinType::Id:
  310. #include "clang/Basic/OpenCLImageTypes.def"
  311. case BuiltinType::OCLSampler:
  312. case BuiltinType::OCLEvent:
  313. case BuiltinType::OCLClkEvent:
  314. case BuiltinType::OCLQueue:
  315. case BuiltinType::OCLReserveID:
  316. case BuiltinType::BuiltinFn:
  317. case BuiltinType::OMPArraySection:
  318. return TST_unspecified;
  319. }
  320. llvm_unreachable("Invalid BuiltinType Kind!");
  321. }
  322. TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
  323. while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
  324. TL = PTL.getInnerLoc();
  325. return TL;
  326. }
  327. SourceLocation TypeLoc::findNullabilityLoc() const {
  328. if (auto attributedLoc = getAs<AttributedTypeLoc>()) {
  329. if (attributedLoc.getAttrKind() == AttributedType::attr_nullable ||
  330. attributedLoc.getAttrKind() == AttributedType::attr_nonnull ||
  331. attributedLoc.getAttrKind() == AttributedType::attr_null_unspecified)
  332. return attributedLoc.getAttrNameLoc();
  333. }
  334. return SourceLocation();
  335. }
  336. TypeLoc TypeLoc::findExplicitQualifierLoc() const {
  337. // Qualified types.
  338. if (auto qual = getAs<QualifiedTypeLoc>())
  339. return qual;
  340. TypeLoc loc = IgnoreParens();
  341. // Attributed types.
  342. if (auto attr = loc.getAs<AttributedTypeLoc>()) {
  343. if (attr.isQualifier()) return attr;
  344. return attr.getModifiedLoc().findExplicitQualifierLoc();
  345. }
  346. // C11 _Atomic types.
  347. if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
  348. return atomic;
  349. }
  350. return TypeLoc();
  351. }
  352. void ObjCTypeParamTypeLoc::initializeLocal(ASTContext &Context,
  353. SourceLocation Loc) {
  354. setNameLoc(Loc);
  355. if (!getNumProtocols()) return;
  356. setProtocolLAngleLoc(Loc);
  357. setProtocolRAngleLoc(Loc);
  358. for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
  359. setProtocolLoc(i, Loc);
  360. }
  361. void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
  362. SourceLocation Loc) {
  363. setHasBaseTypeAsWritten(true);
  364. setTypeArgsLAngleLoc(Loc);
  365. setTypeArgsRAngleLoc(Loc);
  366. for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
  367. setTypeArgTInfo(i,
  368. Context.getTrivialTypeSourceInfo(
  369. getTypePtr()->getTypeArgsAsWritten()[i], Loc));
  370. }
  371. setProtocolLAngleLoc(Loc);
  372. setProtocolRAngleLoc(Loc);
  373. for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
  374. setProtocolLoc(i, Loc);
  375. }
  376. void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
  377. SourceLocation Loc) {
  378. TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
  379. ::initializeLocal(Context, Loc);
  380. this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
  381. getUnderlyingType(), Loc);
  382. }
  383. void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
  384. SourceLocation Loc) {
  385. setElaboratedKeywordLoc(Loc);
  386. NestedNameSpecifierLocBuilder Builder;
  387. Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
  388. setQualifierLoc(Builder.getWithLocInContext(Context));
  389. }
  390. void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
  391. SourceLocation Loc) {
  392. setElaboratedKeywordLoc(Loc);
  393. NestedNameSpecifierLocBuilder Builder;
  394. Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
  395. setQualifierLoc(Builder.getWithLocInContext(Context));
  396. setNameLoc(Loc);
  397. }
  398. void
  399. DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
  400. SourceLocation Loc) {
  401. setElaboratedKeywordLoc(Loc);
  402. if (getTypePtr()->getQualifier()) {
  403. NestedNameSpecifierLocBuilder Builder;
  404. Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
  405. setQualifierLoc(Builder.getWithLocInContext(Context));
  406. } else {
  407. setQualifierLoc(NestedNameSpecifierLoc());
  408. }
  409. setTemplateKeywordLoc(Loc);
  410. setTemplateNameLoc(Loc);
  411. setLAngleLoc(Loc);
  412. setRAngleLoc(Loc);
  413. TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
  414. getTypePtr()->getArgs(),
  415. getArgInfos(), Loc);
  416. }
  417. void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
  418. unsigned NumArgs,
  419. const TemplateArgument *Args,
  420. TemplateArgumentLocInfo *ArgInfos,
  421. SourceLocation Loc) {
  422. for (unsigned i = 0, e = NumArgs; i != e; ++i) {
  423. switch (Args[i].getKind()) {
  424. case TemplateArgument::Null:
  425. llvm_unreachable("Impossible TemplateArgument");
  426. case TemplateArgument::Integral:
  427. case TemplateArgument::Declaration:
  428. case TemplateArgument::NullPtr:
  429. ArgInfos[i] = TemplateArgumentLocInfo();
  430. break;
  431. case TemplateArgument::Expression:
  432. ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
  433. break;
  434. case TemplateArgument::Type:
  435. ArgInfos[i] = TemplateArgumentLocInfo(
  436. Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
  437. Loc));
  438. break;
  439. case TemplateArgument::Template:
  440. case TemplateArgument::TemplateExpansion: {
  441. NestedNameSpecifierLocBuilder Builder;
  442. TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
  443. if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
  444. Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
  445. else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
  446. Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
  447. ArgInfos[i] = TemplateArgumentLocInfo(
  448. Builder.getWithLocInContext(Context), Loc,
  449. Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
  450. : Loc);
  451. break;
  452. }
  453. case TemplateArgument::Pack:
  454. ArgInfos[i] = TemplateArgumentLocInfo();
  455. break;
  456. }
  457. }
  458. }