DeclSpec.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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/Expr.h"
  16. #include "clang/AST/NestedNameSpecifier.h"
  17. #include "clang/AST/TypeLoc.h"
  18. #include "clang/Basic/LangOptions.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
  21. #include "clang/Sema/LocInfoType.h"
  22. #include "clang/Sema/ParsedTemplate.h"
  23. #include "clang/Sema/Sema.h"
  24. #include "clang/Sema/SemaDiagnostic.h"
  25. #include "llvm/ADT/STLExtras.h"
  26. #include "llvm/Support/ErrorHandling.h"
  27. #include <cstring>
  28. using namespace clang;
  29. static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
  30. unsigned DiagID) {
  31. return D.Report(Loc, DiagID);
  32. }
  33. void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
  34. assert(TemplateId && "NULL template-id annotation?");
  35. Kind = IK_TemplateId;
  36. this->TemplateId = TemplateId;
  37. StartLocation = TemplateId->TemplateNameLoc;
  38. EndLocation = TemplateId->RAngleLoc;
  39. }
  40. void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
  41. assert(TemplateId && "NULL template-id annotation?");
  42. Kind = IK_ConstructorTemplateId;
  43. this->TemplateId = TemplateId;
  44. StartLocation = TemplateId->TemplateNameLoc;
  45. EndLocation = TemplateId->RAngleLoc;
  46. }
  47. void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
  48. TypeLoc TL, SourceLocation ColonColonLoc) {
  49. Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
  50. if (Range.getBegin().isInvalid())
  51. Range.setBegin(TL.getBeginLoc());
  52. Range.setEnd(ColonColonLoc);
  53. assert(Range == Builder.getSourceRange() &&
  54. "NestedNameSpecifierLoc range computation incorrect");
  55. }
  56. void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
  57. SourceLocation IdentifierLoc,
  58. SourceLocation ColonColonLoc) {
  59. Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
  60. if (Range.getBegin().isInvalid())
  61. Range.setBegin(IdentifierLoc);
  62. Range.setEnd(ColonColonLoc);
  63. assert(Range == Builder.getSourceRange() &&
  64. "NestedNameSpecifierLoc range computation incorrect");
  65. }
  66. void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
  67. SourceLocation NamespaceLoc,
  68. SourceLocation ColonColonLoc) {
  69. Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
  70. if (Range.getBegin().isInvalid())
  71. Range.setBegin(NamespaceLoc);
  72. Range.setEnd(ColonColonLoc);
  73. assert(Range == Builder.getSourceRange() &&
  74. "NestedNameSpecifierLoc range computation incorrect");
  75. }
  76. void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
  77. SourceLocation AliasLoc,
  78. SourceLocation ColonColonLoc) {
  79. Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
  80. if (Range.getBegin().isInvalid())
  81. Range.setBegin(AliasLoc);
  82. Range.setEnd(ColonColonLoc);
  83. assert(Range == Builder.getSourceRange() &&
  84. "NestedNameSpecifierLoc range computation incorrect");
  85. }
  86. void CXXScopeSpec::MakeGlobal(ASTContext &Context,
  87. SourceLocation ColonColonLoc) {
  88. Builder.MakeGlobal(Context, ColonColonLoc);
  89. Range = SourceRange(ColonColonLoc);
  90. assert(Range == Builder.getSourceRange() &&
  91. "NestedNameSpecifierLoc range computation incorrect");
  92. }
  93. void CXXScopeSpec::MakeTrivial(ASTContext &Context,
  94. NestedNameSpecifier *Qualifier, SourceRange R) {
  95. Builder.MakeTrivial(Context, Qualifier, R);
  96. Range = R;
  97. }
  98. void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
  99. if (!Other) {
  100. Range = SourceRange();
  101. Builder.Clear();
  102. return;
  103. }
  104. Range = Other.getSourceRange();
  105. Builder.Adopt(Other);
  106. }
  107. SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
  108. if (!Builder.getRepresentation())
  109. return SourceLocation();
  110. return Builder.getTemporary().getLocalBeginLoc();
  111. }
  112. NestedNameSpecifierLoc
  113. CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
  114. if (!Builder.getRepresentation())
  115. return NestedNameSpecifierLoc();
  116. return Builder.getWithLocInContext(Context);
  117. }
  118. /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
  119. /// "TheDeclarator" is the declarator that this will be added to.
  120. DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
  121. bool isAmbiguous,
  122. SourceLocation LParenLoc,
  123. ParamInfo *ArgInfo,
  124. unsigned NumArgs,
  125. SourceLocation EllipsisLoc,
  126. SourceLocation RParenLoc,
  127. unsigned TypeQuals,
  128. bool RefQualifierIsLvalueRef,
  129. SourceLocation RefQualifierLoc,
  130. SourceLocation ConstQualifierLoc,
  131. SourceLocation
  132. VolatileQualifierLoc,
  133. SourceLocation MutableLoc,
  134. ExceptionSpecificationType
  135. ESpecType,
  136. SourceLocation ESpecLoc,
  137. ParsedType *Exceptions,
  138. SourceRange *ExceptionRanges,
  139. unsigned NumExceptions,
  140. Expr *NoexceptExpr,
  141. SourceLocation LocalRangeBegin,
  142. SourceLocation LocalRangeEnd,
  143. Declarator &TheDeclarator,
  144. TypeResult TrailingReturnType) {
  145. DeclaratorChunk I;
  146. I.Kind = Function;
  147. I.Loc = LocalRangeBegin;
  148. I.EndLoc = LocalRangeEnd;
  149. I.Fun.AttrList = 0;
  150. I.Fun.hasPrototype = hasProto;
  151. I.Fun.isVariadic = EllipsisLoc.isValid();
  152. I.Fun.isAmbiguous = isAmbiguous;
  153. I.Fun.LParenLoc = LParenLoc.getRawEncoding();
  154. I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
  155. I.Fun.RParenLoc = RParenLoc.getRawEncoding();
  156. I.Fun.DeleteArgInfo = false;
  157. I.Fun.TypeQuals = TypeQuals;
  158. I.Fun.NumArgs = NumArgs;
  159. I.Fun.ArgInfo = 0;
  160. I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
  161. I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
  162. I.Fun.ConstQualifierLoc = ConstQualifierLoc.getRawEncoding();
  163. I.Fun.VolatileQualifierLoc = VolatileQualifierLoc.getRawEncoding();
  164. I.Fun.MutableLoc = MutableLoc.getRawEncoding();
  165. I.Fun.ExceptionSpecType = ESpecType;
  166. I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
  167. I.Fun.NumExceptions = 0;
  168. I.Fun.Exceptions = 0;
  169. I.Fun.NoexceptExpr = 0;
  170. I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
  171. TrailingReturnType.isInvalid();
  172. I.Fun.TrailingReturnType = TrailingReturnType.get();
  173. // new[] an argument array if needed.
  174. if (NumArgs) {
  175. // If the 'InlineParams' in Declarator is unused and big enough, put our
  176. // parameter list there (in an effort to avoid new/delete traffic). If it
  177. // is already used (consider a function returning a function pointer) or too
  178. // small (function taking too many arguments), go to the heap.
  179. if (!TheDeclarator.InlineParamsUsed &&
  180. NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
  181. I.Fun.ArgInfo = TheDeclarator.InlineParams;
  182. I.Fun.DeleteArgInfo = false;
  183. TheDeclarator.InlineParamsUsed = true;
  184. } else {
  185. I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
  186. I.Fun.DeleteArgInfo = true;
  187. }
  188. memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
  189. }
  190. // Check what exception specification information we should actually store.
  191. switch (ESpecType) {
  192. default: break; // By default, save nothing.
  193. case EST_Dynamic:
  194. // new[] an exception array if needed
  195. if (NumExceptions) {
  196. I.Fun.NumExceptions = NumExceptions;
  197. I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
  198. for (unsigned i = 0; i != NumExceptions; ++i) {
  199. I.Fun.Exceptions[i].Ty = Exceptions[i];
  200. I.Fun.Exceptions[i].Range = ExceptionRanges[i];
  201. }
  202. }
  203. break;
  204. case EST_ComputedNoexcept:
  205. I.Fun.NoexceptExpr = NoexceptExpr;
  206. break;
  207. }
  208. return I;
  209. }
  210. bool Declarator::isDeclarationOfFunction() const {
  211. for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
  212. switch (DeclTypeInfo[i].Kind) {
  213. case DeclaratorChunk::Function:
  214. return true;
  215. case DeclaratorChunk::Paren:
  216. continue;
  217. case DeclaratorChunk::Pointer:
  218. case DeclaratorChunk::Reference:
  219. case DeclaratorChunk::Array:
  220. case DeclaratorChunk::BlockPointer:
  221. case DeclaratorChunk::MemberPointer:
  222. return false;
  223. }
  224. llvm_unreachable("Invalid type chunk");
  225. }
  226. switch (DS.getTypeSpecType()) {
  227. case TST_atomic:
  228. case TST_auto:
  229. case TST_bool:
  230. case TST_char:
  231. case TST_char16:
  232. case TST_char32:
  233. case TST_class:
  234. case TST_decimal128:
  235. case TST_decimal32:
  236. case TST_decimal64:
  237. case TST_double:
  238. case TST_enum:
  239. case TST_error:
  240. case TST_float:
  241. case TST_half:
  242. case TST_int:
  243. case TST_int128:
  244. case TST_struct:
  245. case TST_interface:
  246. case TST_union:
  247. case TST_unknown_anytype:
  248. case TST_unspecified:
  249. case TST_void:
  250. case TST_wchar:
  251. case TST_image1d_t:
  252. case TST_image1d_array_t:
  253. case TST_image1d_buffer_t:
  254. case TST_image2d_t:
  255. case TST_image2d_array_t:
  256. case TST_image3d_t:
  257. case TST_sampler_t:
  258. case TST_event_t:
  259. return false;
  260. case TST_decltype:
  261. case TST_typeofExpr:
  262. if (Expr *E = DS.getRepAsExpr())
  263. return E->getType()->isFunctionType();
  264. return false;
  265. case TST_underlyingType:
  266. case TST_typename:
  267. case TST_typeofType: {
  268. QualType QT = DS.getRepAsType().get();
  269. if (QT.isNull())
  270. return false;
  271. if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
  272. QT = LIT->getType();
  273. if (QT.isNull())
  274. return false;
  275. return QT->isFunctionType();
  276. }
  277. }
  278. llvm_unreachable("Invalid TypeSpecType!");
  279. }
  280. /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
  281. /// declaration specifier includes.
  282. ///
  283. unsigned DeclSpec::getParsedSpecifiers() const {
  284. unsigned Res = 0;
  285. if (StorageClassSpec != SCS_unspecified ||
  286. SCS_thread_specified)
  287. Res |= PQ_StorageClassSpecifier;
  288. if (TypeQualifiers != TQ_unspecified)
  289. Res |= PQ_TypeQualifier;
  290. if (hasTypeSpecifier())
  291. Res |= PQ_TypeSpecifier;
  292. if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified ||
  293. FS_noreturn_specified)
  294. Res |= PQ_FunctionSpecifier;
  295. return Res;
  296. }
  297. template <class T> static bool BadSpecifier(T TNew, T TPrev,
  298. const char *&PrevSpec,
  299. unsigned &DiagID,
  300. bool IsExtension = true) {
  301. PrevSpec = DeclSpec::getSpecifierName(TPrev);
  302. if (TNew != TPrev)
  303. DiagID = diag::err_invalid_decl_spec_combination;
  304. else
  305. DiagID = IsExtension ? diag::ext_duplicate_declspec :
  306. diag::warn_duplicate_declspec;
  307. return true;
  308. }
  309. const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
  310. switch (S) {
  311. case DeclSpec::SCS_unspecified: return "unspecified";
  312. case DeclSpec::SCS_typedef: return "typedef";
  313. case DeclSpec::SCS_extern: return "extern";
  314. case DeclSpec::SCS_static: return "static";
  315. case DeclSpec::SCS_auto: return "auto";
  316. case DeclSpec::SCS_register: return "register";
  317. case DeclSpec::SCS_private_extern: return "__private_extern__";
  318. case DeclSpec::SCS_mutable: return "mutable";
  319. }
  320. llvm_unreachable("Unknown typespec!");
  321. }
  322. const char *DeclSpec::getSpecifierName(TSW W) {
  323. switch (W) {
  324. case TSW_unspecified: return "unspecified";
  325. case TSW_short: return "short";
  326. case TSW_long: return "long";
  327. case TSW_longlong: return "long long";
  328. }
  329. llvm_unreachable("Unknown typespec!");
  330. }
  331. const char *DeclSpec::getSpecifierName(TSC C) {
  332. switch (C) {
  333. case TSC_unspecified: return "unspecified";
  334. case TSC_imaginary: return "imaginary";
  335. case TSC_complex: return "complex";
  336. }
  337. llvm_unreachable("Unknown typespec!");
  338. }
  339. const char *DeclSpec::getSpecifierName(TSS S) {
  340. switch (S) {
  341. case TSS_unspecified: return "unspecified";
  342. case TSS_signed: return "signed";
  343. case TSS_unsigned: return "unsigned";
  344. }
  345. llvm_unreachable("Unknown typespec!");
  346. }
  347. const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
  348. switch (T) {
  349. case DeclSpec::TST_unspecified: return "unspecified";
  350. case DeclSpec::TST_void: return "void";
  351. case DeclSpec::TST_char: return "char";
  352. case DeclSpec::TST_wchar: return "wchar_t";
  353. case DeclSpec::TST_char16: return "char16_t";
  354. case DeclSpec::TST_char32: return "char32_t";
  355. case DeclSpec::TST_int: return "int";
  356. case DeclSpec::TST_int128: return "__int128";
  357. case DeclSpec::TST_half: return "half";
  358. case DeclSpec::TST_float: return "float";
  359. case DeclSpec::TST_double: return "double";
  360. case DeclSpec::TST_bool: return "_Bool";
  361. case DeclSpec::TST_decimal32: return "_Decimal32";
  362. case DeclSpec::TST_decimal64: return "_Decimal64";
  363. case DeclSpec::TST_decimal128: return "_Decimal128";
  364. case DeclSpec::TST_enum: return "enum";
  365. case DeclSpec::TST_class: return "class";
  366. case DeclSpec::TST_union: return "union";
  367. case DeclSpec::TST_struct: return "struct";
  368. case DeclSpec::TST_interface: return "__interface";
  369. case DeclSpec::TST_typename: return "type-name";
  370. case DeclSpec::TST_typeofType:
  371. case DeclSpec::TST_typeofExpr: return "typeof";
  372. case DeclSpec::TST_auto: return "auto";
  373. case DeclSpec::TST_decltype: return "(decltype)";
  374. case DeclSpec::TST_underlyingType: return "__underlying_type";
  375. case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
  376. case DeclSpec::TST_atomic: return "_Atomic";
  377. case DeclSpec::TST_image1d_t: return "image1d_t";
  378. case DeclSpec::TST_image1d_array_t: return "image1d_array_t";
  379. case DeclSpec::TST_image1d_buffer_t: return "image1d_buffer_t";
  380. case DeclSpec::TST_image2d_t: return "image2d_t";
  381. case DeclSpec::TST_image2d_array_t: return "image2d_array_t";
  382. case DeclSpec::TST_image3d_t: return "image3d_t";
  383. case DeclSpec::TST_sampler_t: return "sampler_t";
  384. case DeclSpec::TST_event_t: return "event_t";
  385. case DeclSpec::TST_error: return "(error)";
  386. }
  387. llvm_unreachable("Unknown typespec!");
  388. }
  389. const char *DeclSpec::getSpecifierName(TQ T) {
  390. switch (T) {
  391. case DeclSpec::TQ_unspecified: return "unspecified";
  392. case DeclSpec::TQ_const: return "const";
  393. case DeclSpec::TQ_restrict: return "restrict";
  394. case DeclSpec::TQ_volatile: return "volatile";
  395. }
  396. llvm_unreachable("Unknown typespec!");
  397. }
  398. bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
  399. const char *&PrevSpec,
  400. unsigned &DiagID) {
  401. // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
  402. // specifiers are not supported.
  403. // It seems sensible to prohibit private_extern too
  404. // The cl_clang_storage_class_specifiers extension enables support for
  405. // these storage-class specifiers.
  406. // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
  407. // specifiers are not supported."
  408. if (S.getLangOpts().OpenCL &&
  409. !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
  410. switch (SC) {
  411. case SCS_extern:
  412. case SCS_private_extern:
  413. case SCS_static:
  414. if (S.getLangOpts().OpenCLVersion < 120) {
  415. DiagID = diag::err_not_opencl_storage_class_specifier;
  416. PrevSpec = getSpecifierName(SC);
  417. return true;
  418. }
  419. break;
  420. case SCS_auto:
  421. case SCS_register:
  422. DiagID = diag::err_not_opencl_storage_class_specifier;
  423. PrevSpec = getSpecifierName(SC);
  424. return true;
  425. default:
  426. break;
  427. }
  428. }
  429. if (StorageClassSpec != SCS_unspecified) {
  430. // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
  431. bool isInvalid = true;
  432. if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
  433. if (SC == SCS_auto)
  434. return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
  435. if (StorageClassSpec == SCS_auto) {
  436. isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
  437. PrevSpec, DiagID);
  438. assert(!isInvalid && "auto SCS -> TST recovery failed");
  439. }
  440. }
  441. // Changing storage class is allowed only if the previous one
  442. // was the 'extern' that is part of a linkage specification and
  443. // the new storage class is 'typedef'.
  444. if (isInvalid &&
  445. !(SCS_extern_in_linkage_spec &&
  446. StorageClassSpec == SCS_extern &&
  447. SC == SCS_typedef))
  448. return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
  449. }
  450. StorageClassSpec = SC;
  451. StorageClassSpecLoc = Loc;
  452. assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
  453. return false;
  454. }
  455. bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
  456. const char *&PrevSpec,
  457. unsigned &DiagID) {
  458. if (SCS_thread_specified) {
  459. PrevSpec = "__thread";
  460. DiagID = diag::ext_duplicate_declspec;
  461. return true;
  462. }
  463. SCS_thread_specified = true;
  464. SCS_threadLoc = Loc;
  465. return false;
  466. }
  467. /// These methods set the specified attribute of the DeclSpec, but return true
  468. /// and ignore the request if invalid (e.g. "extern" then "auto" is
  469. /// specified).
  470. bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
  471. const char *&PrevSpec,
  472. unsigned &DiagID) {
  473. // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
  474. // for 'long long' we will keep the source location of the first 'long'.
  475. if (TypeSpecWidth == TSW_unspecified)
  476. TSWLoc = Loc;
  477. // Allow turning long -> long long.
  478. else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
  479. return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
  480. TypeSpecWidth = W;
  481. if (TypeAltiVecVector && !TypeAltiVecBool &&
  482. ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
  483. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  484. DiagID = diag::warn_vector_long_decl_spec_combination;
  485. return true;
  486. }
  487. return false;
  488. }
  489. bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
  490. const char *&PrevSpec,
  491. unsigned &DiagID) {
  492. if (TypeSpecComplex != TSC_unspecified)
  493. return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
  494. TypeSpecComplex = C;
  495. TSCLoc = Loc;
  496. return false;
  497. }
  498. bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
  499. const char *&PrevSpec,
  500. unsigned &DiagID) {
  501. if (TypeSpecSign != TSS_unspecified)
  502. return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
  503. TypeSpecSign = S;
  504. TSSLoc = Loc;
  505. return false;
  506. }
  507. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  508. const char *&PrevSpec,
  509. unsigned &DiagID,
  510. ParsedType Rep) {
  511. return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
  512. }
  513. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
  514. SourceLocation TagNameLoc,
  515. const char *&PrevSpec,
  516. unsigned &DiagID,
  517. ParsedType Rep) {
  518. assert(isTypeRep(T) && "T does not store a type");
  519. assert(Rep && "no type provided!");
  520. if (TypeSpecType != TST_unspecified) {
  521. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  522. DiagID = diag::err_invalid_decl_spec_combination;
  523. return true;
  524. }
  525. TypeSpecType = T;
  526. TypeRep = Rep;
  527. TSTLoc = TagKwLoc;
  528. TSTNameLoc = TagNameLoc;
  529. TypeSpecOwned = false;
  530. return false;
  531. }
  532. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  533. const char *&PrevSpec,
  534. unsigned &DiagID,
  535. Expr *Rep) {
  536. assert(isExprRep(T) && "T does not store an expr");
  537. assert(Rep && "no expression provided!");
  538. if (TypeSpecType != TST_unspecified) {
  539. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  540. DiagID = diag::err_invalid_decl_spec_combination;
  541. return true;
  542. }
  543. TypeSpecType = T;
  544. ExprRep = Rep;
  545. TSTLoc = Loc;
  546. TSTNameLoc = Loc;
  547. TypeSpecOwned = false;
  548. return false;
  549. }
  550. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  551. const char *&PrevSpec,
  552. unsigned &DiagID,
  553. Decl *Rep, bool Owned) {
  554. return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
  555. }
  556. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
  557. SourceLocation TagNameLoc,
  558. const char *&PrevSpec,
  559. unsigned &DiagID,
  560. Decl *Rep, bool Owned) {
  561. assert(isDeclRep(T) && "T does not store a decl");
  562. // Unlike the other cases, we don't assert that we actually get a decl.
  563. if (TypeSpecType != TST_unspecified) {
  564. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  565. DiagID = diag::err_invalid_decl_spec_combination;
  566. return true;
  567. }
  568. TypeSpecType = T;
  569. DeclRep = Rep;
  570. TSTLoc = TagKwLoc;
  571. TSTNameLoc = TagNameLoc;
  572. TypeSpecOwned = Owned;
  573. return false;
  574. }
  575. bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
  576. const char *&PrevSpec,
  577. unsigned &DiagID) {
  578. assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
  579. "rep required for these type-spec kinds!");
  580. if (TypeSpecType != TST_unspecified) {
  581. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  582. DiagID = diag::err_invalid_decl_spec_combination;
  583. return true;
  584. }
  585. TSTLoc = Loc;
  586. TSTNameLoc = Loc;
  587. if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
  588. TypeAltiVecBool = true;
  589. return false;
  590. }
  591. TypeSpecType = T;
  592. TypeSpecOwned = false;
  593. if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
  594. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  595. DiagID = diag::err_invalid_vector_decl_spec;
  596. return true;
  597. }
  598. return false;
  599. }
  600. bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
  601. const char *&PrevSpec, unsigned &DiagID) {
  602. if (TypeSpecType != TST_unspecified) {
  603. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  604. DiagID = diag::err_invalid_vector_decl_spec_combination;
  605. return true;
  606. }
  607. TypeAltiVecVector = isAltiVecVector;
  608. AltiVecLoc = Loc;
  609. return false;
  610. }
  611. bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
  612. const char *&PrevSpec, unsigned &DiagID) {
  613. if (!TypeAltiVecVector || TypeAltiVecPixel ||
  614. (TypeSpecType != TST_unspecified)) {
  615. PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
  616. DiagID = diag::err_invalid_pixel_decl_spec_combination;
  617. return true;
  618. }
  619. TypeAltiVecPixel = isAltiVecPixel;
  620. TSTLoc = Loc;
  621. TSTNameLoc = Loc;
  622. return false;
  623. }
  624. bool DeclSpec::SetTypeSpecError() {
  625. TypeSpecType = TST_error;
  626. TypeSpecOwned = false;
  627. TSTLoc = SourceLocation();
  628. TSTNameLoc = SourceLocation();
  629. return false;
  630. }
  631. bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
  632. unsigned &DiagID, const LangOptions &Lang) {
  633. // Duplicates are permitted in C99, but are not permitted in C++. However,
  634. // since this is likely not what the user intended, we will always warn. We
  635. // do not need to set the qualifier's location since we already have it.
  636. if (TypeQualifiers & T) {
  637. bool IsExtension = true;
  638. if (Lang.C99)
  639. IsExtension = false;
  640. return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
  641. }
  642. TypeQualifiers |= T;
  643. switch (T) {
  644. default: llvm_unreachable("Unknown type qualifier!");
  645. case TQ_const: TQ_constLoc = Loc; break;
  646. case TQ_restrict: TQ_restrictLoc = Loc; break;
  647. case TQ_volatile: TQ_volatileLoc = Loc; break;
  648. }
  649. return false;
  650. }
  651. bool DeclSpec::setFunctionSpecInline(SourceLocation Loc) {
  652. // 'inline inline' is ok.
  653. FS_inline_specified = true;
  654. FS_inlineLoc = Loc;
  655. return false;
  656. }
  657. bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc) {
  658. // 'virtual virtual' is ok.
  659. FS_virtual_specified = true;
  660. FS_virtualLoc = Loc;
  661. return false;
  662. }
  663. bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc) {
  664. // 'explicit explicit' is ok.
  665. FS_explicit_specified = true;
  666. FS_explicitLoc = Loc;
  667. return false;
  668. }
  669. bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc) {
  670. // '_Noreturn _Noreturn' is ok.
  671. FS_noreturn_specified = true;
  672. FS_noreturnLoc = Loc;
  673. return false;
  674. }
  675. bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
  676. unsigned &DiagID) {
  677. if (Friend_specified) {
  678. PrevSpec = "friend";
  679. DiagID = diag::ext_duplicate_declspec;
  680. return true;
  681. }
  682. Friend_specified = true;
  683. FriendLoc = Loc;
  684. return false;
  685. }
  686. bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
  687. unsigned &DiagID) {
  688. if (isModulePrivateSpecified()) {
  689. PrevSpec = "__module_private__";
  690. DiagID = diag::ext_duplicate_declspec;
  691. return true;
  692. }
  693. ModulePrivateLoc = Loc;
  694. return false;
  695. }
  696. bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
  697. unsigned &DiagID) {
  698. // 'constexpr constexpr' is ok.
  699. Constexpr_specified = true;
  700. ConstexprLoc = Loc;
  701. return false;
  702. }
  703. void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
  704. unsigned NP,
  705. SourceLocation *ProtoLocs,
  706. SourceLocation LAngleLoc) {
  707. if (NP == 0) return;
  708. Decl **ProtoQuals = new Decl*[NP];
  709. memcpy(ProtoQuals, Protos, sizeof(Decl*)*NP);
  710. ProtocolQualifiers = ProtoQuals;
  711. ProtocolLocs = new SourceLocation[NP];
  712. memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
  713. NumProtocolQualifiers = NP;
  714. ProtocolLAngleLoc = LAngleLoc;
  715. }
  716. void DeclSpec::SaveWrittenBuiltinSpecs() {
  717. writtenBS.Sign = getTypeSpecSign();
  718. writtenBS.Width = getTypeSpecWidth();
  719. writtenBS.Type = getTypeSpecType();
  720. // Search the list of attributes for the presence of a mode attribute.
  721. writtenBS.ModeAttr = false;
  722. AttributeList* attrs = getAttributes().getList();
  723. while (attrs) {
  724. if (attrs->getKind() == AttributeList::AT_Mode) {
  725. writtenBS.ModeAttr = true;
  726. break;
  727. }
  728. attrs = attrs->getNext();
  729. }
  730. }
  731. void DeclSpec::SaveStorageSpecifierAsWritten() {
  732. if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
  733. // If 'extern' is part of a linkage specification,
  734. // then it is not a storage class "as written".
  735. StorageClassSpecAsWritten = SCS_unspecified;
  736. else
  737. StorageClassSpecAsWritten = StorageClassSpec;
  738. }
  739. /// Finish - This does final analysis of the declspec, rejecting things like
  740. /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
  741. /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
  742. /// DeclSpec is guaranteed self-consistent, even if an error occurred.
  743. void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
  744. // Before possibly changing their values, save specs as written.
  745. SaveWrittenBuiltinSpecs();
  746. SaveStorageSpecifierAsWritten();
  747. // Check the type specifier components first.
  748. // Validate and finalize AltiVec vector declspec.
  749. if (TypeAltiVecVector) {
  750. if (TypeAltiVecBool) {
  751. // Sign specifiers are not allowed with vector bool. (PIM 2.1)
  752. if (TypeSpecSign != TSS_unspecified) {
  753. Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
  754. << getSpecifierName((TSS)TypeSpecSign);
  755. }
  756. // Only char/int are valid with vector bool. (PIM 2.1)
  757. if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
  758. (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
  759. Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
  760. << (TypeAltiVecPixel ? "__pixel" :
  761. getSpecifierName((TST)TypeSpecType));
  762. }
  763. // Only 'short' is valid with vector bool. (PIM 2.1)
  764. if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
  765. Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
  766. << getSpecifierName((TSW)TypeSpecWidth);
  767. // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
  768. if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
  769. (TypeSpecWidth != TSW_unspecified))
  770. TypeSpecSign = TSS_unsigned;
  771. }
  772. if (TypeAltiVecPixel) {
  773. //TODO: perform validation
  774. TypeSpecType = TST_int;
  775. TypeSpecSign = TSS_unsigned;
  776. TypeSpecWidth = TSW_short;
  777. TypeSpecOwned = false;
  778. }
  779. }
  780. // signed/unsigned are only valid with int/char/wchar_t.
  781. if (TypeSpecSign != TSS_unspecified) {
  782. if (TypeSpecType == TST_unspecified)
  783. TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
  784. else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
  785. TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
  786. Diag(D, TSSLoc, diag::err_invalid_sign_spec)
  787. << getSpecifierName((TST)TypeSpecType);
  788. // signed double -> double.
  789. TypeSpecSign = TSS_unspecified;
  790. }
  791. }
  792. // Validate the width of the type.
  793. switch (TypeSpecWidth) {
  794. case TSW_unspecified: break;
  795. case TSW_short: // short int
  796. case TSW_longlong: // long long int
  797. if (TypeSpecType == TST_unspecified)
  798. TypeSpecType = TST_int; // short -> short int, long long -> long long int.
  799. else if (TypeSpecType != TST_int) {
  800. Diag(D, TSWLoc,
  801. TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
  802. : diag::err_invalid_longlong_spec)
  803. << getSpecifierName((TST)TypeSpecType);
  804. TypeSpecType = TST_int;
  805. TypeSpecOwned = false;
  806. }
  807. break;
  808. case TSW_long: // long double, long int
  809. if (TypeSpecType == TST_unspecified)
  810. TypeSpecType = TST_int; // long -> long int.
  811. else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
  812. Diag(D, TSWLoc, diag::err_invalid_long_spec)
  813. << getSpecifierName((TST)TypeSpecType);
  814. TypeSpecType = TST_int;
  815. TypeSpecOwned = false;
  816. }
  817. break;
  818. }
  819. // TODO: if the implementation does not implement _Complex or _Imaginary,
  820. // disallow their use. Need information about the backend.
  821. if (TypeSpecComplex != TSC_unspecified) {
  822. if (TypeSpecType == TST_unspecified) {
  823. Diag(D, TSCLoc, diag::ext_plain_complex)
  824. << FixItHint::CreateInsertion(
  825. PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
  826. " double");
  827. TypeSpecType = TST_double; // _Complex -> _Complex double.
  828. } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
  829. // Note that this intentionally doesn't include _Complex _Bool.
  830. if (!PP.getLangOpts().CPlusPlus)
  831. Diag(D, TSTLoc, diag::ext_integer_complex);
  832. } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
  833. Diag(D, TSCLoc, diag::err_invalid_complex_spec)
  834. << getSpecifierName((TST)TypeSpecType);
  835. TypeSpecComplex = TSC_unspecified;
  836. }
  837. }
  838. // If no type specifier was provided and we're parsing a language where
  839. // the type specifier is not optional, but we got 'auto' as a storage
  840. // class specifier, then assume this is an attempt to use C++0x's 'auto'
  841. // type specifier.
  842. // FIXME: Does Microsoft really support implicit int in C++?
  843. if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().MicrosoftExt &&
  844. TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
  845. TypeSpecType = TST_auto;
  846. StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
  847. TSTLoc = TSTNameLoc = StorageClassSpecLoc;
  848. StorageClassSpecLoc = SourceLocation();
  849. }
  850. // Diagnose if we've recovered from an ill-formed 'auto' storage class
  851. // specifier in a pre-C++0x dialect of C++.
  852. if (!PP.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
  853. Diag(D, TSTLoc, diag::ext_auto_type_specifier);
  854. if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus11 &&
  855. StorageClassSpec == SCS_auto)
  856. Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
  857. << FixItHint::CreateRemoval(StorageClassSpecLoc);
  858. if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
  859. Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
  860. << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
  861. if (Constexpr_specified)
  862. Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
  863. // C++ [class.friend]p6:
  864. // No storage-class-specifier shall appear in the decl-specifier-seq
  865. // of a friend declaration.
  866. if (isFriendSpecified() && getStorageClassSpec()) {
  867. DeclSpec::SCS SC = getStorageClassSpec();
  868. const char *SpecName = getSpecifierName(SC);
  869. SourceLocation SCLoc = getStorageClassSpecLoc();
  870. SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
  871. Diag(D, SCLoc, diag::err_friend_storage_spec)
  872. << SpecName
  873. << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
  874. ClearStorageClassSpecs();
  875. }
  876. assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
  877. // Okay, now we can infer the real type.
  878. // TODO: return "auto function" and other bad things based on the real type.
  879. // 'data definition has no type or storage class'?
  880. }
  881. bool DeclSpec::isMissingDeclaratorOk() {
  882. TST tst = getTypeSpecType();
  883. return isDeclRep(tst) && getRepAsDecl() != 0 &&
  884. StorageClassSpec != DeclSpec::SCS_typedef;
  885. }
  886. void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
  887. OverloadedOperatorKind Op,
  888. SourceLocation SymbolLocations[3]) {
  889. Kind = IK_OperatorFunctionId;
  890. StartLocation = OperatorLoc;
  891. EndLocation = OperatorLoc;
  892. OperatorFunctionId.Operator = Op;
  893. for (unsigned I = 0; I != 3; ++I) {
  894. OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
  895. if (SymbolLocations[I].isValid())
  896. EndLocation = SymbolLocations[I];
  897. }
  898. }
  899. bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
  900. const char *&PrevSpec) {
  901. LastLocation = Loc;
  902. if (Specifiers & VS) {
  903. PrevSpec = getSpecifierName(VS);
  904. return true;
  905. }
  906. Specifiers |= VS;
  907. switch (VS) {
  908. default: llvm_unreachable("Unknown specifier!");
  909. case VS_Override: VS_overrideLoc = Loc; break;
  910. case VS_Final: VS_finalLoc = Loc; break;
  911. }
  912. return false;
  913. }
  914. const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
  915. switch (VS) {
  916. default: llvm_unreachable("Unknown specifier");
  917. case VS_Override: return "override";
  918. case VS_Final: return "final";
  919. }
  920. }