DeclSpec.cpp 51 KB

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