DeclSpec.cpp 55 KB

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