DeclSpec.cpp 55 KB

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