DeclSpec.cpp 45 KB

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