1 //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for declaration specifiers.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Sema/DeclSpec.h"
15 #include "clang/AST/ASTContext.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/Lex/Preprocessor.h"
21 #include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
22 #include "clang/Sema/LocInfoType.h"
23 #include "clang/Sema/ParsedTemplate.h"
24 #include "clang/Sema/Sema.h"
25 #include "clang/Sema/SemaDiagnostic.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/Support/ErrorHandling.h"
30 using namespace clang;
33 static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
35 return D.Report(Loc, DiagID);
39 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
40 assert(TemplateId && "NULL template-id annotation?");
42 this->TemplateId = TemplateId;
43 StartLocation = TemplateId->TemplateNameLoc;
44 EndLocation = TemplateId->RAngleLoc;
47 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
48 assert(TemplateId && "NULL template-id annotation?");
49 Kind = IK_ConstructorTemplateId;
50 this->TemplateId = TemplateId;
51 StartLocation = TemplateId->TemplateNameLoc;
52 EndLocation = TemplateId->RAngleLoc;
55 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
56 TypeLoc TL, SourceLocation ColonColonLoc) {
57 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
58 if (Range.getBegin().isInvalid())
59 Range.setBegin(TL.getBeginLoc());
60 Range.setEnd(ColonColonLoc);
62 assert(Range == Builder.getSourceRange() &&
63 "NestedNameSpecifierLoc range computation incorrect");
66 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
67 SourceLocation IdentifierLoc,
68 SourceLocation ColonColonLoc) {
69 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
71 if (Range.getBegin().isInvalid())
72 Range.setBegin(IdentifierLoc);
73 Range.setEnd(ColonColonLoc);
75 assert(Range == Builder.getSourceRange() &&
76 "NestedNameSpecifierLoc range computation incorrect");
79 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
80 SourceLocation NamespaceLoc,
81 SourceLocation ColonColonLoc) {
82 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
84 if (Range.getBegin().isInvalid())
85 Range.setBegin(NamespaceLoc);
86 Range.setEnd(ColonColonLoc);
88 assert(Range == Builder.getSourceRange() &&
89 "NestedNameSpecifierLoc range computation incorrect");
92 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
93 SourceLocation AliasLoc,
94 SourceLocation ColonColonLoc) {
95 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
97 if (Range.getBegin().isInvalid())
98 Range.setBegin(AliasLoc);
99 Range.setEnd(ColonColonLoc);
101 assert(Range == Builder.getSourceRange() &&
102 "NestedNameSpecifierLoc range computation incorrect");
105 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
106 SourceLocation ColonColonLoc) {
107 Builder.MakeGlobal(Context, ColonColonLoc);
109 Range = SourceRange(ColonColonLoc);
111 assert(Range == Builder.getSourceRange() &&
112 "NestedNameSpecifierLoc range computation incorrect");
115 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
116 NestedNameSpecifier *Qualifier, SourceRange R) {
117 Builder.MakeTrivial(Context, Qualifier, R);
121 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
123 Range = SourceRange();
128 Range = Other.getSourceRange();
129 Builder.Adopt(Other);
132 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
133 if (!Builder.getRepresentation())
134 return SourceLocation();
135 return Builder.getTemporary().getLocalBeginLoc();
138 NestedNameSpecifierLoc
139 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
140 if (!Builder.getRepresentation())
141 return NestedNameSpecifierLoc();
143 return Builder.getWithLocInContext(Context);
146 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
147 /// "TheDeclarator" is the declarator that this will be added to.
148 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
150 SourceLocation LParenLoc,
153 SourceLocation EllipsisLoc,
154 SourceLocation RParenLoc,
156 bool RefQualifierIsLvalueRef,
157 SourceLocation RefQualifierLoc,
158 SourceLocation ConstQualifierLoc,
160 VolatileQualifierLoc,
161 SourceLocation MutableLoc,
162 ExceptionSpecificationType
164 SourceLocation ESpecLoc,
165 ParsedType *Exceptions,
166 SourceRange *ExceptionRanges,
167 unsigned NumExceptions,
169 SourceLocation LocalRangeBegin,
170 SourceLocation LocalRangeEnd,
171 Declarator &TheDeclarator,
172 TypeResult TrailingReturnType) {
173 assert(!(TypeQuals & DeclSpec::TQ_atomic) &&
174 "function cannot have _Atomic qualifier");
178 I.Loc = LocalRangeBegin;
179 I.EndLoc = LocalRangeEnd;
181 I.Fun.hasPrototype = hasProto;
182 I.Fun.isVariadic = EllipsisLoc.isValid();
183 I.Fun.isAmbiguous = isAmbiguous;
184 I.Fun.LParenLoc = LParenLoc.getRawEncoding();
185 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
186 I.Fun.RParenLoc = RParenLoc.getRawEncoding();
187 I.Fun.DeleteArgInfo = false;
188 I.Fun.TypeQuals = TypeQuals;
189 I.Fun.NumArgs = NumArgs;
191 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
192 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
193 I.Fun.ConstQualifierLoc = ConstQualifierLoc.getRawEncoding();
194 I.Fun.VolatileQualifierLoc = VolatileQualifierLoc.getRawEncoding();
195 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
196 I.Fun.ExceptionSpecType = ESpecType;
197 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
198 I.Fun.NumExceptions = 0;
199 I.Fun.Exceptions = 0;
200 I.Fun.NoexceptExpr = 0;
201 I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
202 TrailingReturnType.isInvalid();
203 I.Fun.TrailingReturnType = TrailingReturnType.get();
205 // new[] an argument array if needed.
207 // If the 'InlineParams' in Declarator is unused and big enough, put our
208 // parameter list there (in an effort to avoid new/delete traffic). If it
209 // is already used (consider a function returning a function pointer) or too
210 // small (function taking too many arguments), go to the heap.
211 if (!TheDeclarator.InlineParamsUsed &&
212 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
213 I.Fun.ArgInfo = TheDeclarator.InlineParams;
214 I.Fun.DeleteArgInfo = false;
215 TheDeclarator.InlineParamsUsed = true;
217 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
218 I.Fun.DeleteArgInfo = true;
220 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
223 // Check what exception specification information we should actually store.
225 default: break; // By default, save nothing.
227 // new[] an exception array if needed
229 I.Fun.NumExceptions = NumExceptions;
230 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
231 for (unsigned i = 0; i != NumExceptions; ++i) {
232 I.Fun.Exceptions[i].Ty = Exceptions[i];
233 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
238 case EST_ComputedNoexcept:
239 I.Fun.NoexceptExpr = NoexceptExpr;
245 bool Declarator::isDeclarationOfFunction() const {
246 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
247 switch (DeclTypeInfo[i].Kind) {
248 case DeclaratorChunk::Function:
250 case DeclaratorChunk::Paren:
252 case DeclaratorChunk::Pointer:
253 case DeclaratorChunk::Reference:
254 case DeclaratorChunk::Array:
255 case DeclaratorChunk::BlockPointer:
256 case DeclaratorChunk::MemberPointer:
259 llvm_unreachable("Invalid type chunk");
262 switch (DS.getTypeSpecType()) {
283 case TST_unknown_anytype:
284 case TST_unspecified:
288 case TST_image1d_array_t:
289 case TST_image1d_buffer_t:
291 case TST_image2d_array_t:
297 case TST_decltype_auto:
298 // This must have an initializer, so can't be a function declaration,
299 // even if the initializer has function type.
304 if (Expr *E = DS.getRepAsExpr())
305 return E->getType()->isFunctionType();
308 case TST_underlyingType:
310 case TST_typeofType: {
311 QualType QT = DS.getRepAsType().get();
315 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
321 return QT->isFunctionType();
325 llvm_unreachable("Invalid TypeSpecType!");
328 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
329 /// declaration specifier includes.
331 unsigned DeclSpec::getParsedSpecifiers() const {
333 if (StorageClassSpec != SCS_unspecified ||
334 ThreadStorageClassSpec != TSCS_unspecified)
335 Res |= PQ_StorageClassSpecifier;
337 if (TypeQualifiers != TQ_unspecified)
338 Res |= PQ_TypeQualifier;
340 if (hasTypeSpecifier())
341 Res |= PQ_TypeSpecifier;
343 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified ||
344 FS_noreturn_specified)
345 Res |= PQ_FunctionSpecifier;
349 template <class T> static bool BadSpecifier(T TNew, T TPrev,
350 const char *&PrevSpec,
352 bool IsExtension = true) {
353 PrevSpec = DeclSpec::getSpecifierName(TPrev);
355 DiagID = diag::err_invalid_decl_spec_combination;
357 DiagID = IsExtension ? diag::ext_duplicate_declspec :
358 diag::warn_duplicate_declspec;
362 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
364 case DeclSpec::SCS_unspecified: return "unspecified";
365 case DeclSpec::SCS_typedef: return "typedef";
366 case DeclSpec::SCS_extern: return "extern";
367 case DeclSpec::SCS_static: return "static";
368 case DeclSpec::SCS_auto: return "auto";
369 case DeclSpec::SCS_register: return "register";
370 case DeclSpec::SCS_private_extern: return "__private_extern__";
371 case DeclSpec::SCS_mutable: return "mutable";
373 llvm_unreachable("Unknown typespec!");
376 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
378 case DeclSpec::TSCS_unspecified: return "unspecified";
379 case DeclSpec::TSCS___thread: return "__thread";
380 case DeclSpec::TSCS_thread_local: return "thread_local";
381 case DeclSpec::TSCS__Thread_local: return "_Thread_local";
383 llvm_unreachable("Unknown typespec!");
386 const char *DeclSpec::getSpecifierName(TSW W) {
388 case TSW_unspecified: return "unspecified";
389 case TSW_short: return "short";
390 case TSW_long: return "long";
391 case TSW_longlong: return "long long";
393 llvm_unreachable("Unknown typespec!");
396 const char *DeclSpec::getSpecifierName(TSC C) {
398 case TSC_unspecified: return "unspecified";
399 case TSC_imaginary: return "imaginary";
400 case TSC_complex: return "complex";
402 llvm_unreachable("Unknown typespec!");
406 const char *DeclSpec::getSpecifierName(TSS S) {
408 case TSS_unspecified: return "unspecified";
409 case TSS_signed: return "signed";
410 case TSS_unsigned: return "unsigned";
412 llvm_unreachable("Unknown typespec!");
415 const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
417 case DeclSpec::TST_unspecified: return "unspecified";
418 case DeclSpec::TST_void: return "void";
419 case DeclSpec::TST_char: return "char";
420 case DeclSpec::TST_wchar: return "wchar_t";
421 case DeclSpec::TST_char16: return "char16_t";
422 case DeclSpec::TST_char32: return "char32_t";
423 case DeclSpec::TST_int: return "int";
424 case DeclSpec::TST_int128: return "__int128";
425 case DeclSpec::TST_half: return "half";
426 case DeclSpec::TST_float: return "float";
427 case DeclSpec::TST_double: return "double";
428 case DeclSpec::TST_bool: return "_Bool";
429 case DeclSpec::TST_decimal32: return "_Decimal32";
430 case DeclSpec::TST_decimal64: return "_Decimal64";
431 case DeclSpec::TST_decimal128: return "_Decimal128";
432 case DeclSpec::TST_enum: return "enum";
433 case DeclSpec::TST_class: return "class";
434 case DeclSpec::TST_union: return "union";
435 case DeclSpec::TST_struct: return "struct";
436 case DeclSpec::TST_interface: return "__interface";
437 case DeclSpec::TST_typename: return "type-name";
438 case DeclSpec::TST_typeofType:
439 case DeclSpec::TST_typeofExpr: return "typeof";
440 case DeclSpec::TST_auto: return "auto";
441 case DeclSpec::TST_decltype: return "(decltype)";
442 case DeclSpec::TST_decltype_auto: return "decltype(auto)";
443 case DeclSpec::TST_underlyingType: return "__underlying_type";
444 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
445 case DeclSpec::TST_atomic: return "_Atomic";
446 case DeclSpec::TST_image1d_t: return "image1d_t";
447 case DeclSpec::TST_image1d_array_t: return "image1d_array_t";
448 case DeclSpec::TST_image1d_buffer_t: return "image1d_buffer_t";
449 case DeclSpec::TST_image2d_t: return "image2d_t";
450 case DeclSpec::TST_image2d_array_t: return "image2d_array_t";
451 case DeclSpec::TST_image3d_t: return "image3d_t";
452 case DeclSpec::TST_sampler_t: return "sampler_t";
453 case DeclSpec::TST_event_t: return "event_t";
454 case DeclSpec::TST_error: return "(error)";
456 llvm_unreachable("Unknown typespec!");
459 const char *DeclSpec::getSpecifierName(TQ T) {
461 case DeclSpec::TQ_unspecified: return "unspecified";
462 case DeclSpec::TQ_const: return "const";
463 case DeclSpec::TQ_restrict: return "restrict";
464 case DeclSpec::TQ_volatile: return "volatile";
465 case DeclSpec::TQ_atomic: return "_Atomic";
467 llvm_unreachable("Unknown typespec!");
470 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
471 const char *&PrevSpec,
473 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
474 // specifiers are not supported.
475 // It seems sensible to prohibit private_extern too
476 // The cl_clang_storage_class_specifiers extension enables support for
477 // these storage-class specifiers.
478 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
479 // specifiers are not supported."
480 if (S.getLangOpts().OpenCL &&
481 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
484 case SCS_private_extern:
486 if (S.getLangOpts().OpenCLVersion < 120) {
487 DiagID = diag::err_not_opencl_storage_class_specifier;
488 PrevSpec = getSpecifierName(SC);
494 DiagID = diag::err_not_opencl_storage_class_specifier;
495 PrevSpec = getSpecifierName(SC);
502 if (StorageClassSpec != SCS_unspecified) {
503 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
504 bool isInvalid = true;
505 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
507 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
508 if (StorageClassSpec == SCS_auto) {
509 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
511 assert(!isInvalid && "auto SCS -> TST recovery failed");
515 // Changing storage class is allowed only if the previous one
516 // was the 'extern' that is part of a linkage specification and
517 // the new storage class is 'typedef'.
519 !(SCS_extern_in_linkage_spec &&
520 StorageClassSpec == SCS_extern &&
522 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
524 StorageClassSpec = SC;
525 StorageClassSpecLoc = Loc;
526 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
530 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
531 const char *&PrevSpec,
533 if (ThreadStorageClassSpec != TSCS_unspecified)
534 return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
536 ThreadStorageClassSpec = TSC;
537 ThreadStorageClassSpecLoc = Loc;
541 /// These methods set the specified attribute of the DeclSpec, but return true
542 /// and ignore the request if invalid (e.g. "extern" then "auto" is
544 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
545 const char *&PrevSpec,
547 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
548 // for 'long long' we will keep the source location of the first 'long'.
549 if (TypeSpecWidth == TSW_unspecified)
551 // Allow turning long -> long long.
552 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
553 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
555 if (TypeAltiVecVector && !TypeAltiVecBool &&
556 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
557 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
558 DiagID = diag::warn_vector_long_decl_spec_combination;
564 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
565 const char *&PrevSpec,
567 if (TypeSpecComplex != TSC_unspecified)
568 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
574 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
575 const char *&PrevSpec,
577 if (TypeSpecSign != TSS_unspecified)
578 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
584 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
585 const char *&PrevSpec,
588 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
591 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
592 SourceLocation TagNameLoc,
593 const char *&PrevSpec,
596 assert(isTypeRep(T) && "T does not store a type");
597 assert(Rep && "no type provided!");
598 if (TypeSpecType != TST_unspecified) {
599 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
600 DiagID = diag::err_invalid_decl_spec_combination;
606 TSTNameLoc = TagNameLoc;
607 TypeSpecOwned = false;
611 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
612 const char *&PrevSpec,
615 assert(isExprRep(T) && "T does not store an expr");
616 assert(Rep && "no expression provided!");
617 if (TypeSpecType != TST_unspecified) {
618 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
619 DiagID = diag::err_invalid_decl_spec_combination;
626 TypeSpecOwned = false;
630 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
631 const char *&PrevSpec,
633 Decl *Rep, bool Owned) {
634 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
637 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
638 SourceLocation TagNameLoc,
639 const char *&PrevSpec,
641 Decl *Rep, bool Owned) {
642 assert(isDeclRep(T) && "T does not store a decl");
643 // Unlike the other cases, we don't assert that we actually get a decl.
645 if (TypeSpecType != TST_unspecified) {
646 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
647 DiagID = diag::err_invalid_decl_spec_combination;
653 TSTNameLoc = TagNameLoc;
654 TypeSpecOwned = Owned;
658 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
659 const char *&PrevSpec,
661 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
662 "rep required for these type-spec kinds!");
663 if (TypeSpecType != TST_unspecified) {
664 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
665 DiagID = diag::err_invalid_decl_spec_combination;
670 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
671 TypeAltiVecBool = true;
675 TypeSpecOwned = false;
676 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
677 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
678 DiagID = diag::err_invalid_vector_decl_spec;
684 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
685 const char *&PrevSpec, unsigned &DiagID) {
686 if (TypeSpecType != TST_unspecified) {
687 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
688 DiagID = diag::err_invalid_vector_decl_spec_combination;
691 TypeAltiVecVector = isAltiVecVector;
696 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
697 const char *&PrevSpec, unsigned &DiagID) {
698 if (!TypeAltiVecVector || TypeAltiVecPixel ||
699 (TypeSpecType != TST_unspecified)) {
700 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
701 DiagID = diag::err_invalid_pixel_decl_spec_combination;
704 TypeAltiVecPixel = isAltiVecPixel;
710 bool DeclSpec::SetTypeSpecError() {
711 TypeSpecType = TST_error;
712 TypeSpecOwned = false;
713 TSTLoc = SourceLocation();
714 TSTNameLoc = SourceLocation();
718 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
719 unsigned &DiagID, const LangOptions &Lang) {
720 // Duplicates are permitted in C99, but are not permitted in C++. However,
721 // since this is likely not what the user intended, we will always warn. We
722 // do not need to set the qualifier's location since we already have it.
723 if (TypeQualifiers & T) {
724 bool IsExtension = true;
727 return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
732 case TQ_unspecified: break;
733 case TQ_const: TQ_constLoc = Loc; return false;
734 case TQ_restrict: TQ_restrictLoc = Loc; return false;
735 case TQ_volatile: TQ_volatileLoc = Loc; return false;
736 case TQ_atomic: TQ_atomicLoc = Loc; return false;
739 llvm_unreachable("Unknown type qualifier!");
742 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc) {
743 // 'inline inline' is ok.
744 FS_inline_specified = true;
749 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc) {
750 // 'virtual virtual' is ok.
751 FS_virtual_specified = true;
756 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc) {
757 // 'explicit explicit' is ok.
758 FS_explicit_specified = true;
759 FS_explicitLoc = Loc;
763 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc) {
764 // '_Noreturn _Noreturn' is ok.
765 FS_noreturn_specified = true;
766 FS_noreturnLoc = Loc;
770 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
772 if (Friend_specified) {
774 DiagID = diag::ext_duplicate_declspec;
778 Friend_specified = true;
783 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
785 if (isModulePrivateSpecified()) {
786 PrevSpec = "__module_private__";
787 DiagID = diag::ext_duplicate_declspec;
791 ModulePrivateLoc = Loc;
795 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
797 // 'constexpr constexpr' is ok.
798 Constexpr_specified = true;
803 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
805 SourceLocation *ProtoLocs,
806 SourceLocation LAngleLoc) {
808 Decl **ProtoQuals = new Decl*[NP];
809 memcpy(ProtoQuals, Protos, sizeof(Decl*)*NP);
810 ProtocolQualifiers = ProtoQuals;
811 ProtocolLocs = new SourceLocation[NP];
812 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
813 NumProtocolQualifiers = NP;
814 ProtocolLAngleLoc = LAngleLoc;
817 void DeclSpec::SaveWrittenBuiltinSpecs() {
818 writtenBS.Sign = getTypeSpecSign();
819 writtenBS.Width = getTypeSpecWidth();
820 writtenBS.Type = getTypeSpecType();
821 // Search the list of attributes for the presence of a mode attribute.
822 writtenBS.ModeAttr = false;
823 AttributeList* attrs = getAttributes().getList();
825 if (attrs->getKind() == AttributeList::AT_Mode) {
826 writtenBS.ModeAttr = true;
829 attrs = attrs->getNext();
833 /// Finish - This does final analysis of the declspec, rejecting things like
834 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
835 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
836 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
837 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
838 // Before possibly changing their values, save specs as written.
839 SaveWrittenBuiltinSpecs();
841 // Check the type specifier components first.
843 // If decltype(auto) is used, no other type specifiers are permitted.
844 if (TypeSpecType == TST_decltype_auto &&
845 (TypeSpecWidth != TSW_unspecified ||
846 TypeSpecComplex != TSC_unspecified ||
847 TypeSpecSign != TSS_unspecified ||
848 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
850 const unsigned NumLocs = 8;
851 SourceLocation ExtraLocs[NumLocs] = {
852 TSWLoc, TSCLoc, TSSLoc, AltiVecLoc,
853 TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc
855 FixItHint Hints[NumLocs];
856 SourceLocation FirstLoc;
857 for (unsigned I = 0; I != NumLocs; ++I) {
858 if (!ExtraLocs[I].isInvalid()) {
859 if (FirstLoc.isInvalid() ||
860 PP.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
862 FirstLoc = ExtraLocs[I];
863 Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
866 TypeSpecWidth = TSW_unspecified;
867 TypeSpecComplex = TSC_unspecified;
868 TypeSpecSign = TSS_unspecified;
869 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
871 Diag(D, TSTLoc, diag::err_decltype_auto_cannot_be_combined)
872 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
873 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
876 // Validate and finalize AltiVec vector declspec.
877 if (TypeAltiVecVector) {
878 if (TypeAltiVecBool) {
879 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
880 if (TypeSpecSign != TSS_unspecified) {
881 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
882 << getSpecifierName((TSS)TypeSpecSign);
885 // Only char/int are valid with vector bool. (PIM 2.1)
886 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
887 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
888 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
889 << (TypeAltiVecPixel ? "__pixel" :
890 getSpecifierName((TST)TypeSpecType));
893 // Only 'short' is valid with vector bool. (PIM 2.1)
894 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
895 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
896 << getSpecifierName((TSW)TypeSpecWidth);
898 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
899 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
900 (TypeSpecWidth != TSW_unspecified))
901 TypeSpecSign = TSS_unsigned;
904 if (TypeAltiVecPixel) {
905 //TODO: perform validation
906 TypeSpecType = TST_int;
907 TypeSpecSign = TSS_unsigned;
908 TypeSpecWidth = TSW_short;
909 TypeSpecOwned = false;
913 // signed/unsigned are only valid with int/char/wchar_t.
914 if (TypeSpecSign != TSS_unspecified) {
915 if (TypeSpecType == TST_unspecified)
916 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
917 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
918 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
919 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
920 << getSpecifierName((TST)TypeSpecType);
921 // signed double -> double.
922 TypeSpecSign = TSS_unspecified;
926 // Validate the width of the type.
927 switch (TypeSpecWidth) {
928 case TSW_unspecified: break;
929 case TSW_short: // short int
930 case TSW_longlong: // long long int
931 if (TypeSpecType == TST_unspecified)
932 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
933 else if (TypeSpecType != TST_int) {
935 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
936 : diag::err_invalid_longlong_spec)
937 << getSpecifierName((TST)TypeSpecType);
938 TypeSpecType = TST_int;
939 TypeSpecOwned = false;
942 case TSW_long: // long double, long int
943 if (TypeSpecType == TST_unspecified)
944 TypeSpecType = TST_int; // long -> long int.
945 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
946 Diag(D, TSWLoc, diag::err_invalid_long_spec)
947 << getSpecifierName((TST)TypeSpecType);
948 TypeSpecType = TST_int;
949 TypeSpecOwned = false;
954 // TODO: if the implementation does not implement _Complex or _Imaginary,
955 // disallow their use. Need information about the backend.
956 if (TypeSpecComplex != TSC_unspecified) {
957 if (TypeSpecType == TST_unspecified) {
958 Diag(D, TSCLoc, diag::ext_plain_complex)
959 << FixItHint::CreateInsertion(
960 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
962 TypeSpecType = TST_double; // _Complex -> _Complex double.
963 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
964 // Note that this intentionally doesn't include _Complex _Bool.
965 if (!PP.getLangOpts().CPlusPlus)
966 Diag(D, TSTLoc, diag::ext_integer_complex);
967 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
968 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
969 << getSpecifierName((TST)TypeSpecType);
970 TypeSpecComplex = TSC_unspecified;
974 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
975 // _Thread_local can only appear with the 'static' and 'extern' storage class
976 // specifiers. We also allow __private_extern__ as an extension.
977 if (ThreadStorageClassSpec != TSCS_unspecified) {
978 switch (StorageClassSpec) {
979 case SCS_unspecified:
981 case SCS_private_extern:
985 if (PP.getSourceManager().isBeforeInTranslationUnit(
986 getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
987 Diag(D, getStorageClassSpecLoc(),
988 diag::err_invalid_decl_spec_combination)
989 << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
990 << SourceRange(getThreadStorageClassSpecLoc());
992 Diag(D, getThreadStorageClassSpecLoc(),
993 diag::err_invalid_decl_spec_combination)
994 << DeclSpec::getSpecifierName(getStorageClassSpec())
995 << SourceRange(getStorageClassSpecLoc());
996 // Discard the thread storage class specifier to recover.
997 ThreadStorageClassSpec = TSCS_unspecified;
998 ThreadStorageClassSpecLoc = SourceLocation();
1002 // If no type specifier was provided and we're parsing a language where
1003 // the type specifier is not optional, but we got 'auto' as a storage
1004 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1006 if (PP.getLangOpts().CPlusPlus &&
1007 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1008 TypeSpecType = TST_auto;
1009 StorageClassSpec = SCS_unspecified;
1010 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1011 StorageClassSpecLoc = SourceLocation();
1013 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1014 // specifier in a pre-C++11 dialect of C++.
1015 if (!PP.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1016 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
1017 if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus11 &&
1018 StorageClassSpec == SCS_auto)
1019 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
1020 << FixItHint::CreateRemoval(StorageClassSpecLoc);
1021 if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1022 Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
1023 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1024 if (Constexpr_specified)
1025 Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
1027 // C++ [class.friend]p6:
1028 // No storage-class-specifier shall appear in the decl-specifier-seq
1029 // of a friend declaration.
1030 if (isFriendSpecified() &&
1031 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1032 SmallString<32> SpecName;
1033 SourceLocation SCLoc;
1034 FixItHint StorageHint, ThreadHint;
1036 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1037 SpecName = getSpecifierName(SC);
1038 SCLoc = getStorageClassSpecLoc();
1039 StorageHint = FixItHint::CreateRemoval(SCLoc);
1042 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1043 if (!SpecName.empty()) SpecName += " ";
1044 SpecName += getSpecifierName(TSC);
1045 SCLoc = getThreadStorageClassSpecLoc();
1046 ThreadHint = FixItHint::CreateRemoval(SCLoc);
1049 Diag(D, SCLoc, diag::err_friend_storage_spec)
1050 << SpecName << StorageHint << ThreadHint;
1052 ClearStorageClassSpecs();
1055 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1057 // Okay, now we can infer the real type.
1059 // TODO: return "auto function" and other bad things based on the real type.
1061 // 'data definition has no type or storage class'?
1064 bool DeclSpec::isMissingDeclaratorOk() {
1065 TST tst = getTypeSpecType();
1066 return isDeclRep(tst) && getRepAsDecl() != 0 &&
1067 StorageClassSpec != DeclSpec::SCS_typedef;
1070 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1071 OverloadedOperatorKind Op,
1072 SourceLocation SymbolLocations[3]) {
1073 Kind = IK_OperatorFunctionId;
1074 StartLocation = OperatorLoc;
1075 EndLocation = OperatorLoc;
1076 OperatorFunctionId.Operator = Op;
1077 for (unsigned I = 0; I != 3; ++I) {
1078 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
1080 if (SymbolLocations[I].isValid())
1081 EndLocation = SymbolLocations[I];
1085 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1086 const char *&PrevSpec) {
1089 if (Specifiers & VS) {
1090 PrevSpec = getSpecifierName(VS);
1097 default: llvm_unreachable("Unknown specifier!");
1098 case VS_Override: VS_overrideLoc = Loc; break;
1099 case VS_Final: VS_finalLoc = Loc; break;
1105 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1107 default: llvm_unreachable("Unknown specifier");
1108 case VS_Override: return "override";
1109 case VS_Final: return "final";