DeclSpec.cpp 46 KB

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