DeclSpec.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. //===--- DeclSpec.cpp - Declaration Specifier Semantic Analysis -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements semantic analysis for declaration specifiers.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Sema/DeclSpec.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/DeclCXX.h"
  15. #include "clang/AST/Expr.h"
  16. #include "clang/AST/LocInfoType.h"
  17. #include "clang/AST/TypeLoc.h"
  18. #include "clang/Basic/LangOptions.h"
  19. #include "clang/Basic/TargetInfo.h"
  20. #include "clang/Sema/ParsedTemplate.h"
  21. #include "clang/Sema/Sema.h"
  22. #include "clang/Sema/SemaDiagnostic.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/SmallString.h"
  25. #include <cstring>
  26. using namespace clang;
  27. void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
  28. assert(TemplateId && "NULL template-id annotation?");
  29. Kind = UnqualifiedIdKind::IK_TemplateId;
  30. this->TemplateId = TemplateId;
  31. StartLocation = TemplateId->TemplateNameLoc;
  32. EndLocation = TemplateId->RAngleLoc;
  33. }
  34. void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
  35. assert(TemplateId && "NULL template-id annotation?");
  36. Kind = UnqualifiedIdKind::IK_ConstructorTemplateId;
  37. this->TemplateId = TemplateId;
  38. StartLocation = TemplateId->TemplateNameLoc;
  39. EndLocation = TemplateId->RAngleLoc;
  40. }
  41. void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
  42. TypeLoc TL, SourceLocation ColonColonLoc) {
  43. Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
  44. if (Range.getBegin().isInvalid())
  45. Range.setBegin(TL.getBeginLoc());
  46. Range.setEnd(ColonColonLoc);
  47. assert(Range == Builder.getSourceRange() &&
  48. "NestedNameSpecifierLoc range computation incorrect");
  49. }
  50. void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
  51. SourceLocation IdentifierLoc,
  52. SourceLocation ColonColonLoc) {
  53. Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
  54. if (Range.getBegin().isInvalid())
  55. Range.setBegin(IdentifierLoc);
  56. Range.setEnd(ColonColonLoc);
  57. assert(Range == Builder.getSourceRange() &&
  58. "NestedNameSpecifierLoc range computation incorrect");
  59. }
  60. void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
  61. SourceLocation NamespaceLoc,
  62. SourceLocation ColonColonLoc) {
  63. Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
  64. if (Range.getBegin().isInvalid())
  65. Range.setBegin(NamespaceLoc);
  66. Range.setEnd(ColonColonLoc);
  67. assert(Range == Builder.getSourceRange() &&
  68. "NestedNameSpecifierLoc range computation incorrect");
  69. }
  70. void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
  71. SourceLocation AliasLoc,
  72. SourceLocation ColonColonLoc) {
  73. Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
  74. if (Range.getBegin().isInvalid())
  75. Range.setBegin(AliasLoc);
  76. Range.setEnd(ColonColonLoc);
  77. assert(Range == Builder.getSourceRange() &&
  78. "NestedNameSpecifierLoc range computation incorrect");
  79. }
  80. void CXXScopeSpec::MakeGlobal(ASTContext &Context,
  81. SourceLocation ColonColonLoc) {
  82. Builder.MakeGlobal(Context, ColonColonLoc);
  83. Range = SourceRange(ColonColonLoc);
  84. assert(Range == Builder.getSourceRange() &&
  85. "NestedNameSpecifierLoc range computation incorrect");
  86. }
  87. void CXXScopeSpec::MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
  88. SourceLocation SuperLoc,
  89. SourceLocation ColonColonLoc) {
  90. Builder.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
  91. Range.setBegin(SuperLoc);
  92. Range.setEnd(ColonColonLoc);
  93. assert(Range == Builder.getSourceRange() &&
  94. "NestedNameSpecifierLoc range computation incorrect");
  95. }
  96. void CXXScopeSpec::MakeTrivial(ASTContext &Context,
  97. NestedNameSpecifier *Qualifier, SourceRange R) {
  98. Builder.MakeTrivial(Context, Qualifier, R);
  99. Range = R;
  100. }
  101. void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
  102. if (!Other) {
  103. Range = SourceRange();
  104. Builder.Clear();
  105. return;
  106. }
  107. Range = Other.getSourceRange();
  108. Builder.Adopt(Other);
  109. }
  110. SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
  111. if (!Builder.getRepresentation())
  112. return SourceLocation();
  113. return Builder.getTemporary().getLocalBeginLoc();
  114. }
  115. NestedNameSpecifierLoc
  116. CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
  117. if (!Builder.getRepresentation())
  118. return NestedNameSpecifierLoc();
  119. return Builder.getWithLocInContext(Context);
  120. }
  121. /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
  122. /// "TheDeclarator" is the declarator that this will be added to.
  123. DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
  124. bool isAmbiguous,
  125. SourceLocation LParenLoc,
  126. ParamInfo *Params,
  127. unsigned NumParams,
  128. SourceLocation EllipsisLoc,
  129. SourceLocation RParenLoc,
  130. bool RefQualifierIsLvalueRef,
  131. SourceLocation RefQualifierLoc,
  132. SourceLocation MutableLoc,
  133. ExceptionSpecificationType
  134. ESpecType,
  135. SourceRange ESpecRange,
  136. ParsedType *Exceptions,
  137. SourceRange *ExceptionRanges,
  138. unsigned NumExceptions,
  139. Expr *NoexceptExpr,
  140. CachedTokens *ExceptionSpecTokens,
  141. ArrayRef<NamedDecl*>
  142. DeclsInPrototype,
  143. SourceLocation LocalRangeBegin,
  144. SourceLocation LocalRangeEnd,
  145. Declarator &TheDeclarator,
  146. TypeResult TrailingReturnType,
  147. DeclSpec *MethodQualifiers) {
  148. assert(!(MethodQualifiers && MethodQualifiers->getTypeQualifiers() & DeclSpec::TQ_atomic) &&
  149. "function cannot have _Atomic qualifier");
  150. DeclaratorChunk I;
  151. I.Kind = Function;
  152. I.Loc = LocalRangeBegin;
  153. I.EndLoc = LocalRangeEnd;
  154. I.Fun.hasPrototype = hasProto;
  155. I.Fun.isVariadic = EllipsisLoc.isValid();
  156. I.Fun.isAmbiguous = isAmbiguous;
  157. I.Fun.LParenLoc = LParenLoc.getRawEncoding();
  158. I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
  159. I.Fun.RParenLoc = RParenLoc.getRawEncoding();
  160. I.Fun.DeleteParams = false;
  161. I.Fun.NumParams = NumParams;
  162. I.Fun.Params = nullptr;
  163. I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
  164. I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
  165. I.Fun.MutableLoc = MutableLoc.getRawEncoding();
  166. I.Fun.ExceptionSpecType = ESpecType;
  167. I.Fun.ExceptionSpecLocBeg = ESpecRange.getBegin().getRawEncoding();
  168. I.Fun.ExceptionSpecLocEnd = ESpecRange.getEnd().getRawEncoding();
  169. I.Fun.NumExceptionsOrDecls = 0;
  170. I.Fun.Exceptions = nullptr;
  171. I.Fun.NoexceptExpr = nullptr;
  172. I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
  173. TrailingReturnType.isInvalid();
  174. I.Fun.TrailingReturnType = TrailingReturnType.get();
  175. I.Fun.MethodQualifiers = nullptr;
  176. I.Fun.QualAttrFactory = nullptr;
  177. if (MethodQualifiers && (MethodQualifiers->getTypeQualifiers() ||
  178. MethodQualifiers->getAttributes().size())) {
  179. auto &attrs = MethodQualifiers->getAttributes();
  180. I.Fun.MethodQualifiers = new DeclSpec(attrs.getPool().getFactory());
  181. MethodQualifiers->forEachCVRUQualifier(
  182. [&](DeclSpec::TQ TypeQual, StringRef PrintName, SourceLocation SL) {
  183. I.Fun.MethodQualifiers->SetTypeQual(TypeQual, SL);
  184. });
  185. I.Fun.MethodQualifiers->getAttributes().takeAllFrom(attrs);
  186. I.Fun.MethodQualifiers->getAttributePool().takeAllFrom(attrs.getPool());
  187. }
  188. assert(I.Fun.ExceptionSpecType == ESpecType && "bitfield overflow");
  189. // new[] a parameter array if needed.
  190. if (NumParams) {
  191. // If the 'InlineParams' in Declarator is unused and big enough, put our
  192. // parameter list there (in an effort to avoid new/delete traffic). If it
  193. // is already used (consider a function returning a function pointer) or too
  194. // small (function with too many parameters), go to the heap.
  195. if (!TheDeclarator.InlineStorageUsed &&
  196. NumParams <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
  197. I.Fun.Params = TheDeclarator.InlineParams;
  198. new (I.Fun.Params) ParamInfo[NumParams];
  199. I.Fun.DeleteParams = false;
  200. TheDeclarator.InlineStorageUsed = true;
  201. } else {
  202. I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams];
  203. I.Fun.DeleteParams = true;
  204. }
  205. for (unsigned i = 0; i < NumParams; i++)
  206. I.Fun.Params[i] = std::move(Params[i]);
  207. }
  208. // Check what exception specification information we should actually store.
  209. switch (ESpecType) {
  210. default: break; // By default, save nothing.
  211. case EST_Dynamic:
  212. // new[] an exception array if needed
  213. if (NumExceptions) {
  214. I.Fun.NumExceptionsOrDecls = NumExceptions;
  215. I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
  216. for (unsigned i = 0; i != NumExceptions; ++i) {
  217. I.Fun.Exceptions[i].Ty = Exceptions[i];
  218. I.Fun.Exceptions[i].Range = ExceptionRanges[i];
  219. }
  220. }
  221. break;
  222. case EST_DependentNoexcept:
  223. case EST_NoexceptFalse:
  224. case EST_NoexceptTrue:
  225. I.Fun.NoexceptExpr = NoexceptExpr;
  226. break;
  227. case EST_Unparsed:
  228. I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
  229. break;
  230. }
  231. if (!DeclsInPrototype.empty()) {
  232. assert(ESpecType == EST_None && NumExceptions == 0 &&
  233. "cannot have exception specifiers and decls in prototype");
  234. I.Fun.NumExceptionsOrDecls = DeclsInPrototype.size();
  235. // Copy the array of decls into stable heap storage.
  236. I.Fun.DeclsInPrototype = new NamedDecl *[DeclsInPrototype.size()];
  237. for (size_t J = 0; J < DeclsInPrototype.size(); ++J)
  238. I.Fun.DeclsInPrototype[J] = DeclsInPrototype[J];
  239. }
  240. return I;
  241. }
  242. void Declarator::setDecompositionBindings(
  243. SourceLocation LSquareLoc,
  244. ArrayRef<DecompositionDeclarator::Binding> Bindings,
  245. SourceLocation RSquareLoc) {
  246. assert(!hasName() && "declarator given multiple names!");
  247. BindingGroup.LSquareLoc = LSquareLoc;
  248. BindingGroup.RSquareLoc = RSquareLoc;
  249. BindingGroup.NumBindings = Bindings.size();
  250. Range.setEnd(RSquareLoc);
  251. // We're now past the identifier.
  252. SetIdentifier(nullptr, LSquareLoc);
  253. Name.EndLocation = RSquareLoc;
  254. // Allocate storage for bindings and stash them away.
  255. if (Bindings.size()) {
  256. if (!InlineStorageUsed &&
  257. Bindings.size() <= llvm::array_lengthof(InlineBindings)) {
  258. BindingGroup.Bindings = InlineBindings;
  259. BindingGroup.DeleteBindings = false;
  260. InlineStorageUsed = true;
  261. } else {
  262. BindingGroup.Bindings =
  263. new DecompositionDeclarator::Binding[Bindings.size()];
  264. BindingGroup.DeleteBindings = true;
  265. }
  266. std::uninitialized_copy(Bindings.begin(), Bindings.end(),
  267. BindingGroup.Bindings);
  268. }
  269. }
  270. bool Declarator::isDeclarationOfFunction() const {
  271. for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
  272. switch (DeclTypeInfo[i].Kind) {
  273. case DeclaratorChunk::Function:
  274. return true;
  275. case DeclaratorChunk::Paren:
  276. continue;
  277. case DeclaratorChunk::Pointer:
  278. case DeclaratorChunk::Reference:
  279. case DeclaratorChunk::Array:
  280. case DeclaratorChunk::BlockPointer:
  281. case DeclaratorChunk::MemberPointer:
  282. case DeclaratorChunk::Pipe:
  283. return false;
  284. }
  285. llvm_unreachable("Invalid type chunk");
  286. }
  287. switch (DS.getTypeSpecType()) {
  288. case TST_atomic:
  289. case TST_auto:
  290. case TST_auto_type:
  291. case TST_bool:
  292. case TST_char:
  293. case TST_char8:
  294. case TST_char16:
  295. case TST_char32:
  296. case TST_class:
  297. case TST_decimal128:
  298. case TST_decimal32:
  299. case TST_decimal64:
  300. case TST_double:
  301. case TST_Accum:
  302. case TST_Fract:
  303. case TST_Float16:
  304. case TST_float128:
  305. case TST_enum:
  306. case TST_error:
  307. case TST_float:
  308. case TST_half:
  309. case TST_int:
  310. case TST_int128:
  311. case TST_struct:
  312. case TST_interface:
  313. case TST_union:
  314. case TST_unknown_anytype:
  315. case TST_unspecified:
  316. case TST_void:
  317. case TST_wchar:
  318. #define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
  319. #include "clang/Basic/OpenCLImageTypes.def"
  320. return false;
  321. case TST_decltype_auto:
  322. // This must have an initializer, so can't be a function declaration,
  323. // even if the initializer has function type.
  324. return false;
  325. case TST_decltype:
  326. case TST_typeofExpr:
  327. if (Expr *E = DS.getRepAsExpr())
  328. return E->getType()->isFunctionType();
  329. return false;
  330. case TST_underlyingType:
  331. case TST_typename:
  332. case TST_typeofType: {
  333. QualType QT = DS.getRepAsType().get();
  334. if (QT.isNull())
  335. return false;
  336. if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
  337. QT = LIT->getType();
  338. if (QT.isNull())
  339. return false;
  340. return QT->isFunctionType();
  341. }
  342. }
  343. llvm_unreachable("Invalid TypeSpecType!");
  344. }
  345. bool Declarator::isStaticMember() {
  346. assert(getContext() == DeclaratorContext::MemberContext);
  347. return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
  348. (getName().Kind == UnqualifiedIdKind::IK_OperatorFunctionId &&
  349. CXXMethodDecl::isStaticOverloadedOperator(
  350. getName().OperatorFunctionId.Operator));
  351. }
  352. bool Declarator::isCtorOrDtor() {
  353. return (getName().getKind() == UnqualifiedIdKind::IK_ConstructorName) ||
  354. (getName().getKind() == UnqualifiedIdKind::IK_DestructorName);
  355. }
  356. void DeclSpec::forEachCVRUQualifier(
  357. llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
  358. if (TypeQualifiers & TQ_const)
  359. Handle(TQ_const, "const", TQ_constLoc);
  360. if (TypeQualifiers & TQ_volatile)
  361. Handle(TQ_volatile, "volatile", TQ_volatileLoc);
  362. if (TypeQualifiers & TQ_restrict)
  363. Handle(TQ_restrict, "restrict", TQ_restrictLoc);
  364. if (TypeQualifiers & TQ_unaligned)
  365. Handle(TQ_unaligned, "unaligned", TQ_unalignedLoc);
  366. }
  367. void DeclSpec::forEachQualifier(
  368. llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
  369. forEachCVRUQualifier(Handle);
  370. // FIXME: Add code below to iterate through the attributes and call Handle.
  371. }
  372. bool DeclSpec::hasTagDefinition() const {
  373. if (!TypeSpecOwned)
  374. return false;
  375. return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
  376. }
  377. /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
  378. /// declaration specifier includes.
  379. ///
  380. unsigned DeclSpec::getParsedSpecifiers() const {
  381. unsigned Res = 0;
  382. if (StorageClassSpec != SCS_unspecified ||
  383. ThreadStorageClassSpec != TSCS_unspecified)
  384. Res |= PQ_StorageClassSpecifier;
  385. if (TypeQualifiers != TQ_unspecified)
  386. Res |= PQ_TypeQualifier;
  387. if (hasTypeSpecifier())
  388. Res |= PQ_TypeSpecifier;
  389. if (FS_inline_specified || FS_virtual_specified || hasExplicitSpecifier() ||
  390. FS_noreturn_specified || FS_forceinline_specified)
  391. Res |= PQ_FunctionSpecifier;
  392. return Res;
  393. }
  394. template <class T> static bool BadSpecifier(T TNew, T TPrev,
  395. const char *&PrevSpec,
  396. unsigned &DiagID,
  397. bool IsExtension = true) {
  398. PrevSpec = DeclSpec::getSpecifierName(TPrev);
  399. if (TNew != TPrev)
  400. DiagID = diag::err_invalid_decl_spec_combination;
  401. else
  402. DiagID = IsExtension ? diag::ext_warn_duplicate_declspec :
  403. diag::warn_duplicate_declspec;
  404. return true;
  405. }
  406. const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
  407. switch (S) {
  408. case DeclSpec::SCS_unspecified: return "unspecified";
  409. case DeclSpec::SCS_typedef: return "typedef";
  410. case DeclSpec::SCS_extern: return "extern";
  411. case DeclSpec::SCS_static: return "static";
  412. case DeclSpec::SCS_auto: return "auto";
  413. case DeclSpec::SCS_register: return "register";
  414. case DeclSpec::SCS_private_extern: return "__private_extern__";
  415. case DeclSpec::SCS_mutable: return "mutable";
  416. }
  417. llvm_unreachable("Unknown typespec!");
  418. }
  419. const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
  420. switch (S) {
  421. case DeclSpec::TSCS_unspecified: return "unspecified";
  422. case DeclSpec::TSCS___thread: return "__thread";
  423. case DeclSpec::TSCS_thread_local: return "thread_local";
  424. case DeclSpec::TSCS__Thread_local: return "_Thread_local";
  425. }
  426. llvm_unreachable("Unknown typespec!");
  427. }
  428. const char *DeclSpec::getSpecifierName(TSW W) {
  429. switch (W) {
  430. case TSW_unspecified: return "unspecified";
  431. case TSW_short: return "short";
  432. case TSW_long: return "long";
  433. case TSW_longlong: return "long long";
  434. }
  435. llvm_unreachable("Unknown typespec!");
  436. }
  437. const char *DeclSpec::getSpecifierName(TSC C) {
  438. switch (C) {
  439. case TSC_unspecified: return "unspecified";
  440. case TSC_imaginary: return "imaginary";
  441. case TSC_complex: return "complex";
  442. }
  443. llvm_unreachable("Unknown typespec!");
  444. }
  445. const char *DeclSpec::getSpecifierName(TSS S) {
  446. switch (S) {
  447. case TSS_unspecified: return "unspecified";
  448. case TSS_signed: return "signed";
  449. case TSS_unsigned: return "unsigned";
  450. }
  451. llvm_unreachable("Unknown typespec!");
  452. }
  453. const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
  454. const PrintingPolicy &Policy) {
  455. switch (T) {
  456. case DeclSpec::TST_unspecified: return "unspecified";
  457. case DeclSpec::TST_void: return "void";
  458. case DeclSpec::TST_char: return "char";
  459. case DeclSpec::TST_wchar: return Policy.MSWChar ? "__wchar_t" : "wchar_t";
  460. case DeclSpec::TST_char8: return "char8_t";
  461. case DeclSpec::TST_char16: return "char16_t";
  462. case DeclSpec::TST_char32: return "char32_t";
  463. case DeclSpec::TST_int: return "int";
  464. case DeclSpec::TST_int128: return "__int128";
  465. case DeclSpec::TST_half: return "half";
  466. case DeclSpec::TST_float: return "float";
  467. case DeclSpec::TST_double: return "double";
  468. case DeclSpec::TST_accum: return "_Accum";
  469. case DeclSpec::TST_fract: return "_Fract";
  470. case DeclSpec::TST_float16: return "_Float16";
  471. case DeclSpec::TST_float128: return "__float128";
  472. case DeclSpec::TST_bool: return Policy.Bool ? "bool" : "_Bool";
  473. case DeclSpec::TST_decimal32: return "_Decimal32";
  474. case DeclSpec::TST_decimal64: return "_Decimal64";
  475. case DeclSpec::TST_decimal128: return "_Decimal128";
  476. case DeclSpec::TST_enum: return "enum";
  477. case DeclSpec::TST_class: return "class";
  478. case DeclSpec::TST_union: return "union";
  479. case DeclSpec::TST_struct: return "struct";
  480. case DeclSpec::TST_interface: return "__interface";
  481. case DeclSpec::TST_typename: return "type-name";
  482. case DeclSpec::TST_typeofType:
  483. case DeclSpec::TST_typeofExpr: return "typeof";
  484. case DeclSpec::TST_auto: return "auto";
  485. case DeclSpec::TST_auto_type: return "__auto_type";
  486. case DeclSpec::TST_decltype: return "(decltype)";
  487. case DeclSpec::TST_decltype_auto: return "decltype(auto)";
  488. case DeclSpec::TST_underlyingType: return "__underlying_type";
  489. case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
  490. case DeclSpec::TST_atomic: return "_Atomic";
  491. #define GENERIC_IMAGE_TYPE(ImgType, Id) \
  492. case DeclSpec::TST_##ImgType##_t: \
  493. return #ImgType "_t";
  494. #include "clang/Basic/OpenCLImageTypes.def"
  495. case DeclSpec::TST_error: return "(error)";
  496. }
  497. llvm_unreachable("Unknown typespec!");
  498. }
  499. const char *DeclSpec::getSpecifierName(ConstexprSpecKind C) {
  500. switch (C) {
  501. case CSK_unspecified: return "unspecified";
  502. case CSK_constexpr: return "constexpr";
  503. case CSK_consteval: return "consteval";
  504. case CSK_constinit: return "constinit";
  505. }
  506. llvm_unreachable("Unknown ConstexprSpecKind");
  507. }
  508. const char *DeclSpec::getSpecifierName(TQ T) {
  509. switch (T) {
  510. case DeclSpec::TQ_unspecified: return "unspecified";
  511. case DeclSpec::TQ_const: return "const";
  512. case DeclSpec::TQ_restrict: return "restrict";
  513. case DeclSpec::TQ_volatile: return "volatile";
  514. case DeclSpec::TQ_atomic: return "_Atomic";
  515. case DeclSpec::TQ_unaligned: return "__unaligned";
  516. }
  517. llvm_unreachable("Unknown typespec!");
  518. }
  519. bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
  520. const char *&PrevSpec,
  521. unsigned &DiagID,
  522. const PrintingPolicy &Policy) {
  523. // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
  524. // specifiers are not supported.
  525. // It seems sensible to prohibit private_extern too
  526. // The cl_clang_storage_class_specifiers extension enables support for
  527. // these storage-class specifiers.
  528. // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
  529. // specifiers are not supported."
  530. if (S.getLangOpts().OpenCL &&
  531. !S.getOpenCLOptions().isEnabled("cl_clang_storage_class_specifiers")) {
  532. switch (SC) {
  533. case SCS_extern:
  534. case SCS_private_extern:
  535. case SCS_static:
  536. if (S.getLangOpts().OpenCLVersion < 120 &&
  537. !S.getLangOpts().OpenCLCPlusPlus) {
  538. DiagID = diag::err_opencl_unknown_type_specifier;
  539. PrevSpec = getSpecifierName(SC);
  540. return true;
  541. }
  542. break;
  543. case SCS_auto:
  544. case SCS_register:
  545. DiagID = diag::err_opencl_unknown_type_specifier;
  546. PrevSpec = getSpecifierName(SC);
  547. return true;
  548. default:
  549. break;
  550. }
  551. }
  552. if (StorageClassSpec != SCS_unspecified) {
  553. // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
  554. bool isInvalid = true;
  555. if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
  556. if (SC == SCS_auto)
  557. return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy);
  558. if (StorageClassSpec == SCS_auto) {
  559. isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
  560. PrevSpec, DiagID, Policy);
  561. assert(!isInvalid && "auto SCS -> TST recovery failed");
  562. }
  563. }
  564. // Changing storage class is allowed only if the previous one
  565. // was the 'extern' that is part of a linkage specification and
  566. // the new storage class is 'typedef'.
  567. if (isInvalid &&
  568. !(SCS_extern_in_linkage_spec &&
  569. StorageClassSpec == SCS_extern &&
  570. SC == SCS_typedef))
  571. return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
  572. }
  573. StorageClassSpec = SC;
  574. StorageClassSpecLoc = Loc;
  575. assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
  576. return false;
  577. }
  578. bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
  579. const char *&PrevSpec,
  580. unsigned &DiagID) {
  581. if (ThreadStorageClassSpec != TSCS_unspecified)
  582. return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
  583. ThreadStorageClassSpec = TSC;
  584. ThreadStorageClassSpecLoc = Loc;
  585. return false;
  586. }
  587. /// These methods set the specified attribute of the DeclSpec, but return true
  588. /// and ignore the request if invalid (e.g. "extern" then "auto" is
  589. /// specified).
  590. bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
  591. const char *&PrevSpec,
  592. unsigned &DiagID,
  593. const PrintingPolicy &Policy) {
  594. // Overwrite TSWRange.Begin only if TypeSpecWidth was unspecified, so that
  595. // for 'long long' we will keep the source location of the first 'long'.
  596. if (TypeSpecWidth == TSW_unspecified)
  597. TSWRange.setBegin(Loc);
  598. // Allow turning long -> long long.
  599. else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
  600. return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
  601. TypeSpecWidth = W;
  602. // Remember location of the last 'long'
  603. TSWRange.setEnd(Loc);
  604. return false;
  605. }
  606. bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
  607. const char *&PrevSpec,
  608. unsigned &DiagID) {
  609. if (TypeSpecComplex != TSC_unspecified)
  610. return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
  611. TypeSpecComplex = C;
  612. TSCLoc = Loc;
  613. return false;
  614. }
  615. bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
  616. const char *&PrevSpec,
  617. unsigned &DiagID) {
  618. if (TypeSpecSign != TSS_unspecified)
  619. return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
  620. TypeSpecSign = S;
  621. TSSLoc = Loc;
  622. return false;
  623. }
  624. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  625. const char *&PrevSpec,
  626. unsigned &DiagID,
  627. ParsedType Rep,
  628. const PrintingPolicy &Policy) {
  629. return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy);
  630. }
  631. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
  632. SourceLocation TagNameLoc,
  633. const char *&PrevSpec,
  634. unsigned &DiagID,
  635. ParsedType Rep,
  636. const PrintingPolicy &Policy) {
  637. assert(isTypeRep(T) && "T does not store a type");
  638. assert(Rep && "no type provided!");
  639. if (TypeSpecType == TST_error)
  640. return false;
  641. if (TypeSpecType != TST_unspecified) {
  642. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
  643. DiagID = diag::err_invalid_decl_spec_combination;
  644. return true;
  645. }
  646. TypeSpecType = T;
  647. TypeRep = Rep;
  648. TSTLoc = TagKwLoc;
  649. TSTNameLoc = TagNameLoc;
  650. TypeSpecOwned = false;
  651. return false;
  652. }
  653. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  654. const char *&PrevSpec,
  655. unsigned &DiagID,
  656. Expr *Rep,
  657. const PrintingPolicy &Policy) {
  658. assert(isExprRep(T) && "T does not store an expr");
  659. assert(Rep && "no expression provided!");
  660. if (TypeSpecType == TST_error)
  661. return false;
  662. if (TypeSpecType != TST_unspecified) {
  663. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
  664. DiagID = diag::err_invalid_decl_spec_combination;
  665. return true;
  666. }
  667. TypeSpecType = T;
  668. ExprRep = Rep;
  669. TSTLoc = Loc;
  670. TSTNameLoc = Loc;
  671. TypeSpecOwned = false;
  672. return false;
  673. }
  674. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  675. const char *&PrevSpec,
  676. unsigned &DiagID,
  677. Decl *Rep, bool Owned,
  678. const PrintingPolicy &Policy) {
  679. return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy);
  680. }
  681. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
  682. SourceLocation TagNameLoc,
  683. const char *&PrevSpec,
  684. unsigned &DiagID,
  685. Decl *Rep, bool Owned,
  686. const PrintingPolicy &Policy) {
  687. assert(isDeclRep(T) && "T does not store a decl");
  688. // Unlike the other cases, we don't assert that we actually get a decl.
  689. if (TypeSpecType == TST_error)
  690. return false;
  691. if (TypeSpecType != TST_unspecified) {
  692. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
  693. DiagID = diag::err_invalid_decl_spec_combination;
  694. return true;
  695. }
  696. TypeSpecType = T;
  697. DeclRep = Rep;
  698. TSTLoc = TagKwLoc;
  699. TSTNameLoc = TagNameLoc;
  700. TypeSpecOwned = Owned && Rep != nullptr;
  701. return false;
  702. }
  703. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  704. const char *&PrevSpec,
  705. unsigned &DiagID,
  706. const PrintingPolicy &Policy) {
  707. assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
  708. "rep required for these type-spec kinds!");
  709. if (TypeSpecType == TST_error)
  710. return false;
  711. if (TypeSpecType != TST_unspecified) {
  712. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
  713. DiagID = diag::err_invalid_decl_spec_combination;
  714. return true;
  715. }
  716. TSTLoc = Loc;
  717. TSTNameLoc = Loc;
  718. if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
  719. TypeAltiVecBool = true;
  720. return false;
  721. }
  722. TypeSpecType = T;
  723. TypeSpecOwned = false;
  724. return false;
  725. }
  726. bool DeclSpec::SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
  727. unsigned &DiagID) {
  728. // Cannot set twice
  729. if (TypeSpecSat) {
  730. DiagID = diag::warn_duplicate_declspec;
  731. PrevSpec = "_Sat";
  732. return true;
  733. }
  734. TypeSpecSat = true;
  735. TSSatLoc = Loc;
  736. return false;
  737. }
  738. bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
  739. const char *&PrevSpec, unsigned &DiagID,
  740. const PrintingPolicy &Policy) {
  741. if (TypeSpecType == TST_error)
  742. return false;
  743. if (TypeSpecType != TST_unspecified) {
  744. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
  745. DiagID = diag::err_invalid_vector_decl_spec_combination;
  746. return true;
  747. }
  748. TypeAltiVecVector = isAltiVecVector;
  749. AltiVecLoc = Loc;
  750. return false;
  751. }
  752. bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
  753. const char *&PrevSpec, unsigned &DiagID,
  754. const PrintingPolicy &Policy) {
  755. if (TypeSpecType == TST_error)
  756. return false;
  757. if (TypeSpecType != TST_unspecified) {
  758. PrevSpec = DeclSpec::getSpecifierName((TST)TypeSpecType, Policy);
  759. DiagID = diag::err_invalid_decl_spec_combination;
  760. return true;
  761. }
  762. if (isPipe) {
  763. TypeSpecPipe = TSP_pipe;
  764. }
  765. return false;
  766. }
  767. bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
  768. const char *&PrevSpec, unsigned &DiagID,
  769. const PrintingPolicy &Policy) {
  770. if (TypeSpecType == TST_error)
  771. return false;
  772. if (!TypeAltiVecVector || TypeAltiVecPixel ||
  773. (TypeSpecType != TST_unspecified)) {
  774. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
  775. DiagID = diag::err_invalid_pixel_decl_spec_combination;
  776. return true;
  777. }
  778. TypeAltiVecPixel = isAltiVecPixel;
  779. TSTLoc = Loc;
  780. TSTNameLoc = Loc;
  781. return false;
  782. }
  783. bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
  784. const char *&PrevSpec, unsigned &DiagID,
  785. const PrintingPolicy &Policy) {
  786. if (TypeSpecType == TST_error)
  787. return false;
  788. if (!TypeAltiVecVector || TypeAltiVecBool ||
  789. (TypeSpecType != TST_unspecified)) {
  790. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
  791. DiagID = diag::err_invalid_vector_bool_decl_spec;
  792. return true;
  793. }
  794. TypeAltiVecBool = isAltiVecBool;
  795. TSTLoc = Loc;
  796. TSTNameLoc = Loc;
  797. return false;
  798. }
  799. bool DeclSpec::SetTypeSpecError() {
  800. TypeSpecType = TST_error;
  801. TypeSpecOwned = false;
  802. TSTLoc = SourceLocation();
  803. TSTNameLoc = SourceLocation();
  804. return false;
  805. }
  806. bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
  807. unsigned &DiagID, const LangOptions &Lang) {
  808. // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
  809. // C++. However, since this is likely not what the user intended, we will
  810. // always warn. We do not need to set the qualifier's location since we
  811. // already have it.
  812. if (TypeQualifiers & T) {
  813. bool IsExtension = true;
  814. if (Lang.C99)
  815. IsExtension = false;
  816. return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
  817. }
  818. return SetTypeQual(T, Loc);
  819. }
  820. bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc) {
  821. TypeQualifiers |= T;
  822. switch (T) {
  823. case TQ_unspecified: break;
  824. case TQ_const: TQ_constLoc = Loc; return false;
  825. case TQ_restrict: TQ_restrictLoc = Loc; return false;
  826. case TQ_volatile: TQ_volatileLoc = Loc; return false;
  827. case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
  828. case TQ_atomic: TQ_atomicLoc = Loc; return false;
  829. }
  830. llvm_unreachable("Unknown type qualifier!");
  831. }
  832. bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
  833. unsigned &DiagID) {
  834. // 'inline inline' is ok. However, since this is likely not what the user
  835. // intended, we will always warn, similar to duplicates of type qualifiers.
  836. if (FS_inline_specified) {
  837. DiagID = diag::warn_duplicate_declspec;
  838. PrevSpec = "inline";
  839. return true;
  840. }
  841. FS_inline_specified = true;
  842. FS_inlineLoc = Loc;
  843. return false;
  844. }
  845. bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
  846. unsigned &DiagID) {
  847. if (FS_forceinline_specified) {
  848. DiagID = diag::warn_duplicate_declspec;
  849. PrevSpec = "__forceinline";
  850. return true;
  851. }
  852. FS_forceinline_specified = true;
  853. FS_forceinlineLoc = Loc;
  854. return false;
  855. }
  856. bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
  857. const char *&PrevSpec,
  858. unsigned &DiagID) {
  859. // 'virtual virtual' is ok, but warn as this is likely not what the user
  860. // intended.
  861. if (FS_virtual_specified) {
  862. DiagID = diag::warn_duplicate_declspec;
  863. PrevSpec = "virtual";
  864. return true;
  865. }
  866. FS_virtual_specified = true;
  867. FS_virtualLoc = Loc;
  868. return false;
  869. }
  870. bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
  871. const char *&PrevSpec, unsigned &DiagID,
  872. ExplicitSpecifier ExplicitSpec,
  873. SourceLocation CloseParenLoc) {
  874. assert((ExplicitSpec.getKind() == ExplicitSpecKind::ResolvedTrue ||
  875. ExplicitSpec.getExpr()) &&
  876. "invalid ExplicitSpecifier");
  877. // 'explicit explicit' is ok, but warn as this is likely not what the user
  878. // intended.
  879. if (hasExplicitSpecifier()) {
  880. DiagID = (ExplicitSpec.getExpr() || FS_explicit_specifier.getExpr())
  881. ? diag::err_duplicate_declspec
  882. : diag::ext_warn_duplicate_declspec;
  883. PrevSpec = "explicit";
  884. return true;
  885. }
  886. FS_explicit_specifier = ExplicitSpec;
  887. FS_explicitLoc = Loc;
  888. FS_explicitCloseParenLoc = CloseParenLoc;
  889. return false;
  890. }
  891. bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
  892. const char *&PrevSpec,
  893. unsigned &DiagID) {
  894. // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
  895. // intended.
  896. if (FS_noreturn_specified) {
  897. DiagID = diag::warn_duplicate_declspec;
  898. PrevSpec = "_Noreturn";
  899. return true;
  900. }
  901. FS_noreturn_specified = true;
  902. FS_noreturnLoc = Loc;
  903. return false;
  904. }
  905. bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
  906. unsigned &DiagID) {
  907. if (Friend_specified) {
  908. PrevSpec = "friend";
  909. // Keep the later location, so that we can later diagnose ill-formed
  910. // declarations like 'friend class X friend;'. Per [class.friend]p3,
  911. // 'friend' must be the first token in a friend declaration that is
  912. // not a function declaration.
  913. FriendLoc = Loc;
  914. DiagID = diag::warn_duplicate_declspec;
  915. return true;
  916. }
  917. Friend_specified = true;
  918. FriendLoc = Loc;
  919. return false;
  920. }
  921. bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
  922. unsigned &DiagID) {
  923. if (isModulePrivateSpecified()) {
  924. PrevSpec = "__module_private__";
  925. DiagID = diag::ext_warn_duplicate_declspec;
  926. return true;
  927. }
  928. ModulePrivateLoc = Loc;
  929. return false;
  930. }
  931. bool DeclSpec::SetConstexprSpec(ConstexprSpecKind ConstexprKind,
  932. SourceLocation Loc, const char *&PrevSpec,
  933. unsigned &DiagID) {
  934. if (getConstexprSpecifier() != CSK_unspecified)
  935. return BadSpecifier(ConstexprKind, getConstexprSpecifier(), PrevSpec,
  936. DiagID);
  937. ConstexprSpecifier = ConstexprKind;
  938. ConstexprLoc = Loc;
  939. return false;
  940. }
  941. void DeclSpec::SaveWrittenBuiltinSpecs() {
  942. writtenBS.Sign = getTypeSpecSign();
  943. writtenBS.Width = getTypeSpecWidth();
  944. writtenBS.Type = getTypeSpecType();
  945. // Search the list of attributes for the presence of a mode attribute.
  946. writtenBS.ModeAttr = getAttributes().hasAttribute(ParsedAttr::AT_Mode);
  947. }
  948. /// Finish - This does final analysis of the declspec, rejecting things like
  949. /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
  950. /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
  951. /// DeclSpec is guaranteed self-consistent, even if an error occurred.
  952. void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
  953. // Before possibly changing their values, save specs as written.
  954. SaveWrittenBuiltinSpecs();
  955. // Check the type specifier components first. No checking for an invalid
  956. // type.
  957. if (TypeSpecType == TST_error)
  958. return;
  959. // If decltype(auto) is used, no other type specifiers are permitted.
  960. if (TypeSpecType == TST_decltype_auto &&
  961. (TypeSpecWidth != TSW_unspecified ||
  962. TypeSpecComplex != TSC_unspecified ||
  963. TypeSpecSign != TSS_unspecified ||
  964. TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
  965. TypeQualifiers)) {
  966. const unsigned NumLocs = 9;
  967. SourceLocation ExtraLocs[NumLocs] = {
  968. TSWRange.getBegin(), TSCLoc, TSSLoc,
  969. AltiVecLoc, TQ_constLoc, TQ_restrictLoc,
  970. TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc};
  971. FixItHint Hints[NumLocs];
  972. SourceLocation FirstLoc;
  973. for (unsigned I = 0; I != NumLocs; ++I) {
  974. if (ExtraLocs[I].isValid()) {
  975. if (FirstLoc.isInvalid() ||
  976. S.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
  977. FirstLoc))
  978. FirstLoc = ExtraLocs[I];
  979. Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
  980. }
  981. }
  982. TypeSpecWidth = TSW_unspecified;
  983. TypeSpecComplex = TSC_unspecified;
  984. TypeSpecSign = TSS_unspecified;
  985. TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
  986. TypeQualifiers = 0;
  987. S.Diag(TSTLoc, diag::err_decltype_auto_cannot_be_combined)
  988. << Hints[0] << Hints[1] << Hints[2] << Hints[3]
  989. << Hints[4] << Hints[5] << Hints[6] << Hints[7];
  990. }
  991. // Validate and finalize AltiVec vector declspec.
  992. if (TypeAltiVecVector) {
  993. if (TypeAltiVecBool) {
  994. // Sign specifiers are not allowed with vector bool. (PIM 2.1)
  995. if (TypeSpecSign != TSS_unspecified) {
  996. S.Diag(TSSLoc, diag::err_invalid_vector_bool_decl_spec)
  997. << getSpecifierName((TSS)TypeSpecSign);
  998. }
  999. // Only char/int are valid with vector bool. (PIM 2.1)
  1000. if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
  1001. (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
  1002. S.Diag(TSTLoc, diag::err_invalid_vector_bool_decl_spec)
  1003. << (TypeAltiVecPixel ? "__pixel" :
  1004. getSpecifierName((TST)TypeSpecType, Policy));
  1005. }
  1006. // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
  1007. if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short) &&
  1008. (TypeSpecWidth != TSW_longlong))
  1009. S.Diag(TSWRange.getBegin(), diag::err_invalid_vector_bool_decl_spec)
  1010. << getSpecifierName((TSW)TypeSpecWidth);
  1011. // vector bool long long requires VSX support or ZVector.
  1012. if ((TypeSpecWidth == TSW_longlong) &&
  1013. (!S.Context.getTargetInfo().hasFeature("vsx")) &&
  1014. (!S.Context.getTargetInfo().hasFeature("power8-vector")) &&
  1015. !S.getLangOpts().ZVector)
  1016. S.Diag(TSTLoc, diag::err_invalid_vector_long_long_decl_spec);
  1017. // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
  1018. if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
  1019. (TypeSpecWidth != TSW_unspecified))
  1020. TypeSpecSign = TSS_unsigned;
  1021. } else if (TypeSpecType == TST_double) {
  1022. // vector long double and vector long long double are never allowed.
  1023. // vector double is OK for Power7 and later, and ZVector.
  1024. if (TypeSpecWidth == TSW_long || TypeSpecWidth == TSW_longlong)
  1025. S.Diag(TSWRange.getBegin(),
  1026. diag::err_invalid_vector_long_double_decl_spec);
  1027. else if (!S.Context.getTargetInfo().hasFeature("vsx") &&
  1028. !S.getLangOpts().ZVector)
  1029. S.Diag(TSTLoc, diag::err_invalid_vector_double_decl_spec);
  1030. } else if (TypeSpecType == TST_float) {
  1031. // vector float is unsupported for ZVector unless we have the
  1032. // vector-enhancements facility 1 (ISA revision 12).
  1033. if (S.getLangOpts().ZVector &&
  1034. !S.Context.getTargetInfo().hasFeature("arch12"))
  1035. S.Diag(TSTLoc, diag::err_invalid_vector_float_decl_spec);
  1036. } else if (TypeSpecWidth == TSW_long) {
  1037. // vector long is unsupported for ZVector and deprecated for AltiVec.
  1038. if (S.getLangOpts().ZVector)
  1039. S.Diag(TSWRange.getBegin(), diag::err_invalid_vector_long_decl_spec);
  1040. else
  1041. S.Diag(TSWRange.getBegin(),
  1042. diag::warn_vector_long_decl_spec_combination)
  1043. << getSpecifierName((TST)TypeSpecType, Policy);
  1044. }
  1045. if (TypeAltiVecPixel) {
  1046. //TODO: perform validation
  1047. TypeSpecType = TST_int;
  1048. TypeSpecSign = TSS_unsigned;
  1049. TypeSpecWidth = TSW_short;
  1050. TypeSpecOwned = false;
  1051. }
  1052. }
  1053. bool IsFixedPointType =
  1054. TypeSpecType == TST_accum || TypeSpecType == TST_fract;
  1055. // signed/unsigned are only valid with int/char/wchar_t/_Accum.
  1056. if (TypeSpecSign != TSS_unspecified) {
  1057. if (TypeSpecType == TST_unspecified)
  1058. TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
  1059. else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
  1060. TypeSpecType != TST_char && TypeSpecType != TST_wchar &&
  1061. !IsFixedPointType) {
  1062. S.Diag(TSSLoc, diag::err_invalid_sign_spec)
  1063. << getSpecifierName((TST)TypeSpecType, Policy);
  1064. // signed double -> double.
  1065. TypeSpecSign = TSS_unspecified;
  1066. }
  1067. }
  1068. // Validate the width of the type.
  1069. switch (TypeSpecWidth) {
  1070. case TSW_unspecified: break;
  1071. case TSW_short: // short int
  1072. case TSW_longlong: // long long int
  1073. if (TypeSpecType == TST_unspecified)
  1074. TypeSpecType = TST_int; // short -> short int, long long -> long long int.
  1075. else if (!(TypeSpecType == TST_int ||
  1076. (IsFixedPointType && TypeSpecWidth != TSW_longlong))) {
  1077. S.Diag(TSWRange.getBegin(), diag::err_invalid_width_spec)
  1078. << (int)TypeSpecWidth << getSpecifierName((TST)TypeSpecType, Policy);
  1079. TypeSpecType = TST_int;
  1080. TypeSpecSat = false;
  1081. TypeSpecOwned = false;
  1082. }
  1083. break;
  1084. case TSW_long: // long double, long int
  1085. if (TypeSpecType == TST_unspecified)
  1086. TypeSpecType = TST_int; // long -> long int.
  1087. else if (TypeSpecType != TST_int && TypeSpecType != TST_double &&
  1088. !IsFixedPointType) {
  1089. S.Diag(TSWRange.getBegin(), diag::err_invalid_width_spec)
  1090. << (int)TypeSpecWidth << getSpecifierName((TST)TypeSpecType, Policy);
  1091. TypeSpecType = TST_int;
  1092. TypeSpecSat = false;
  1093. TypeSpecOwned = false;
  1094. }
  1095. break;
  1096. }
  1097. // TODO: if the implementation does not implement _Complex or _Imaginary,
  1098. // disallow their use. Need information about the backend.
  1099. if (TypeSpecComplex != TSC_unspecified) {
  1100. if (TypeSpecType == TST_unspecified) {
  1101. S.Diag(TSCLoc, diag::ext_plain_complex)
  1102. << FixItHint::CreateInsertion(
  1103. S.getLocForEndOfToken(getTypeSpecComplexLoc()),
  1104. " double");
  1105. TypeSpecType = TST_double; // _Complex -> _Complex double.
  1106. } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
  1107. // Note that this intentionally doesn't include _Complex _Bool.
  1108. if (!S.getLangOpts().CPlusPlus)
  1109. S.Diag(TSTLoc, diag::ext_integer_complex);
  1110. } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
  1111. S.Diag(TSCLoc, diag::err_invalid_complex_spec)
  1112. << getSpecifierName((TST)TypeSpecType, Policy);
  1113. TypeSpecComplex = TSC_unspecified;
  1114. }
  1115. }
  1116. // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
  1117. // _Thread_local can only appear with the 'static' and 'extern' storage class
  1118. // specifiers. We also allow __private_extern__ as an extension.
  1119. if (ThreadStorageClassSpec != TSCS_unspecified) {
  1120. switch (StorageClassSpec) {
  1121. case SCS_unspecified:
  1122. case SCS_extern:
  1123. case SCS_private_extern:
  1124. case SCS_static:
  1125. break;
  1126. default:
  1127. if (S.getSourceManager().isBeforeInTranslationUnit(
  1128. getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
  1129. S.Diag(getStorageClassSpecLoc(),
  1130. diag::err_invalid_decl_spec_combination)
  1131. << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
  1132. << SourceRange(getThreadStorageClassSpecLoc());
  1133. else
  1134. S.Diag(getThreadStorageClassSpecLoc(),
  1135. diag::err_invalid_decl_spec_combination)
  1136. << DeclSpec::getSpecifierName(getStorageClassSpec())
  1137. << SourceRange(getStorageClassSpecLoc());
  1138. // Discard the thread storage class specifier to recover.
  1139. ThreadStorageClassSpec = TSCS_unspecified;
  1140. ThreadStorageClassSpecLoc = SourceLocation();
  1141. }
  1142. }
  1143. // If no type specifier was provided and we're parsing a language where
  1144. // the type specifier is not optional, but we got 'auto' as a storage
  1145. // class specifier, then assume this is an attempt to use C++0x's 'auto'
  1146. // type specifier.
  1147. if (S.getLangOpts().CPlusPlus &&
  1148. TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
  1149. TypeSpecType = TST_auto;
  1150. StorageClassSpec = SCS_unspecified;
  1151. TSTLoc = TSTNameLoc = StorageClassSpecLoc;
  1152. StorageClassSpecLoc = SourceLocation();
  1153. }
  1154. // Diagnose if we've recovered from an ill-formed 'auto' storage class
  1155. // specifier in a pre-C++11 dialect of C++.
  1156. if (!S.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
  1157. S.Diag(TSTLoc, diag::ext_auto_type_specifier);
  1158. if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
  1159. StorageClassSpec == SCS_auto)
  1160. S.Diag(StorageClassSpecLoc, diag::warn_auto_storage_class)
  1161. << FixItHint::CreateRemoval(StorageClassSpecLoc);
  1162. if (TypeSpecType == TST_char8)
  1163. S.Diag(TSTLoc, diag::warn_cxx17_compat_unicode_type);
  1164. else if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
  1165. S.Diag(TSTLoc, diag::warn_cxx98_compat_unicode_type)
  1166. << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
  1167. if (getConstexprSpecifier() == CSK_constexpr)
  1168. S.Diag(ConstexprLoc, diag::warn_cxx98_compat_constexpr);
  1169. else if (getConstexprSpecifier() == CSK_consteval)
  1170. S.Diag(ConstexprLoc, diag::warn_cxx20_compat_consteval);
  1171. else if (getConstexprSpecifier() == CSK_constinit)
  1172. S.Diag(ConstexprLoc, diag::warn_cxx20_compat_constinit);
  1173. // C++ [class.friend]p6:
  1174. // No storage-class-specifier shall appear in the decl-specifier-seq
  1175. // of a friend declaration.
  1176. if (isFriendSpecified() &&
  1177. (getStorageClassSpec() || getThreadStorageClassSpec())) {
  1178. SmallString<32> SpecName;
  1179. SourceLocation SCLoc;
  1180. FixItHint StorageHint, ThreadHint;
  1181. if (DeclSpec::SCS SC = getStorageClassSpec()) {
  1182. SpecName = getSpecifierName(SC);
  1183. SCLoc = getStorageClassSpecLoc();
  1184. StorageHint = FixItHint::CreateRemoval(SCLoc);
  1185. }
  1186. if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
  1187. if (!SpecName.empty()) SpecName += " ";
  1188. SpecName += getSpecifierName(TSC);
  1189. SCLoc = getThreadStorageClassSpecLoc();
  1190. ThreadHint = FixItHint::CreateRemoval(SCLoc);
  1191. }
  1192. S.Diag(SCLoc, diag::err_friend_decl_spec)
  1193. << SpecName << StorageHint << ThreadHint;
  1194. ClearStorageClassSpecs();
  1195. }
  1196. // C++11 [dcl.fct.spec]p5:
  1197. // The virtual specifier shall be used only in the initial
  1198. // declaration of a non-static class member function;
  1199. // C++11 [dcl.fct.spec]p6:
  1200. // The explicit specifier shall be used only in the declaration of
  1201. // a constructor or conversion function within its class
  1202. // definition;
  1203. if (isFriendSpecified() && (isVirtualSpecified() || hasExplicitSpecifier())) {
  1204. StringRef Keyword;
  1205. FixItHint Hint;
  1206. SourceLocation SCLoc;
  1207. if (isVirtualSpecified()) {
  1208. Keyword = "virtual";
  1209. SCLoc = getVirtualSpecLoc();
  1210. Hint = FixItHint::CreateRemoval(SCLoc);
  1211. } else {
  1212. Keyword = "explicit";
  1213. SCLoc = getExplicitSpecLoc();
  1214. Hint = FixItHint::CreateRemoval(getExplicitSpecRange());
  1215. }
  1216. S.Diag(SCLoc, diag::err_friend_decl_spec)
  1217. << Keyword << Hint;
  1218. FS_virtual_specified = false;
  1219. FS_explicit_specifier = ExplicitSpecifier();
  1220. FS_virtualLoc = FS_explicitLoc = SourceLocation();
  1221. }
  1222. assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
  1223. // Okay, now we can infer the real type.
  1224. // TODO: return "auto function" and other bad things based on the real type.
  1225. // 'data definition has no type or storage class'?
  1226. }
  1227. bool DeclSpec::isMissingDeclaratorOk() {
  1228. TST tst = getTypeSpecType();
  1229. return isDeclRep(tst) && getRepAsDecl() != nullptr &&
  1230. StorageClassSpec != DeclSpec::SCS_typedef;
  1231. }
  1232. void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
  1233. OverloadedOperatorKind Op,
  1234. SourceLocation SymbolLocations[3]) {
  1235. Kind = UnqualifiedIdKind::IK_OperatorFunctionId;
  1236. StartLocation = OperatorLoc;
  1237. EndLocation = OperatorLoc;
  1238. OperatorFunctionId.Operator = Op;
  1239. for (unsigned I = 0; I != 3; ++I) {
  1240. OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
  1241. if (SymbolLocations[I].isValid())
  1242. EndLocation = SymbolLocations[I];
  1243. }
  1244. }
  1245. bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
  1246. const char *&PrevSpec) {
  1247. if (!FirstLocation.isValid())
  1248. FirstLocation = Loc;
  1249. LastLocation = Loc;
  1250. LastSpecifier = VS;
  1251. if (Specifiers & VS) {
  1252. PrevSpec = getSpecifierName(VS);
  1253. return true;
  1254. }
  1255. Specifiers |= VS;
  1256. switch (VS) {
  1257. default: llvm_unreachable("Unknown specifier!");
  1258. case VS_Override: VS_overrideLoc = Loc; break;
  1259. case VS_GNU_Final:
  1260. case VS_Sealed:
  1261. case VS_Final: VS_finalLoc = Loc; break;
  1262. }
  1263. return false;
  1264. }
  1265. const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
  1266. switch (VS) {
  1267. default: llvm_unreachable("Unknown specifier");
  1268. case VS_Override: return "override";
  1269. case VS_Final: return "final";
  1270. case VS_GNU_Final: return "__final";
  1271. case VS_Sealed: return "sealed";
  1272. }
  1273. }