1 //===--- DeclSpec.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/DeclCXX.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/LocInfoType.h"
19 #include "clang/AST/TypeLoc.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/TargetInfo.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/ADT/SmallString.h"
28 using namespace clang;
31 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
32 assert(TemplateId && "NULL template-id annotation?");
34 this->TemplateId = TemplateId;
35 StartLocation = TemplateId->TemplateNameLoc;
36 EndLocation = TemplateId->RAngleLoc;
39 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
40 assert(TemplateId && "NULL template-id annotation?");
41 Kind = IK_ConstructorTemplateId;
42 this->TemplateId = TemplateId;
43 StartLocation = TemplateId->TemplateNameLoc;
44 EndLocation = TemplateId->RAngleLoc;
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);
54 assert(Range == Builder.getSourceRange() &&
55 "NestedNameSpecifierLoc range computation incorrect");
58 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
59 SourceLocation IdentifierLoc,
60 SourceLocation ColonColonLoc) {
61 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
63 if (Range.getBegin().isInvalid())
64 Range.setBegin(IdentifierLoc);
65 Range.setEnd(ColonColonLoc);
67 assert(Range == Builder.getSourceRange() &&
68 "NestedNameSpecifierLoc range computation incorrect");
71 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
72 SourceLocation NamespaceLoc,
73 SourceLocation ColonColonLoc) {
74 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
76 if (Range.getBegin().isInvalid())
77 Range.setBegin(NamespaceLoc);
78 Range.setEnd(ColonColonLoc);
80 assert(Range == Builder.getSourceRange() &&
81 "NestedNameSpecifierLoc range computation incorrect");
84 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
85 SourceLocation AliasLoc,
86 SourceLocation ColonColonLoc) {
87 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
89 if (Range.getBegin().isInvalid())
90 Range.setBegin(AliasLoc);
91 Range.setEnd(ColonColonLoc);
93 assert(Range == Builder.getSourceRange() &&
94 "NestedNameSpecifierLoc range computation incorrect");
97 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
98 SourceLocation ColonColonLoc) {
99 Builder.MakeGlobal(Context, ColonColonLoc);
101 Range = SourceRange(ColonColonLoc);
103 assert(Range == Builder.getSourceRange() &&
104 "NestedNameSpecifierLoc range computation incorrect");
107 void CXXScopeSpec::MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
108 SourceLocation SuperLoc,
109 SourceLocation ColonColonLoc) {
110 Builder.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
112 Range.setBegin(SuperLoc);
113 Range.setEnd(ColonColonLoc);
115 assert(Range == Builder.getSourceRange() &&
116 "NestedNameSpecifierLoc range computation incorrect");
119 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
120 NestedNameSpecifier *Qualifier, SourceRange R) {
121 Builder.MakeTrivial(Context, Qualifier, R);
125 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
127 Range = SourceRange();
132 Range = Other.getSourceRange();
133 Builder.Adopt(Other);
136 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
137 if (!Builder.getRepresentation())
138 return SourceLocation();
139 return Builder.getTemporary().getLocalBeginLoc();
142 NestedNameSpecifierLoc
143 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
144 if (!Builder.getRepresentation())
145 return NestedNameSpecifierLoc();
147 return Builder.getWithLocInContext(Context);
150 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
151 /// "TheDeclarator" is the declarator that this will be added to.
152 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
154 SourceLocation LParenLoc,
157 SourceLocation EllipsisLoc,
158 SourceLocation RParenLoc,
160 bool RefQualifierIsLvalueRef,
161 SourceLocation RefQualifierLoc,
162 SourceLocation ConstQualifierLoc,
164 VolatileQualifierLoc,
166 RestrictQualifierLoc,
167 SourceLocation MutableLoc,
168 ExceptionSpecificationType
170 SourceRange ESpecRange,
171 ParsedType *Exceptions,
172 SourceRange *ExceptionRanges,
173 unsigned NumExceptions,
175 CachedTokens *ExceptionSpecTokens,
176 SourceLocation LocalRangeBegin,
177 SourceLocation LocalRangeEnd,
178 Declarator &TheDeclarator,
179 TypeResult TrailingReturnType) {
180 assert(!(TypeQuals & DeclSpec::TQ_atomic) &&
181 "function cannot have _Atomic qualifier");
185 I.Loc = LocalRangeBegin;
186 I.EndLoc = LocalRangeEnd;
187 I.Fun.AttrList = nullptr;
188 I.Fun.hasPrototype = hasProto;
189 I.Fun.isVariadic = EllipsisLoc.isValid();
190 I.Fun.isAmbiguous = isAmbiguous;
191 I.Fun.LParenLoc = LParenLoc.getRawEncoding();
192 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
193 I.Fun.RParenLoc = RParenLoc.getRawEncoding();
194 I.Fun.DeleteParams = false;
195 I.Fun.TypeQuals = TypeQuals;
196 I.Fun.NumParams = NumParams;
197 I.Fun.Params = nullptr;
198 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
199 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
200 I.Fun.ConstQualifierLoc = ConstQualifierLoc.getRawEncoding();
201 I.Fun.VolatileQualifierLoc = VolatileQualifierLoc.getRawEncoding();
202 I.Fun.RestrictQualifierLoc = RestrictQualifierLoc.getRawEncoding();
203 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
204 I.Fun.ExceptionSpecType = ESpecType;
205 I.Fun.ExceptionSpecLocBeg = ESpecRange.getBegin().getRawEncoding();
206 I.Fun.ExceptionSpecLocEnd = ESpecRange.getEnd().getRawEncoding();
207 I.Fun.NumExceptions = 0;
208 I.Fun.Exceptions = nullptr;
209 I.Fun.NoexceptExpr = nullptr;
210 I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
211 TrailingReturnType.isInvalid();
212 I.Fun.TrailingReturnType = TrailingReturnType.get();
214 assert(I.Fun.TypeQuals == TypeQuals && "bitfield overflow");
215 assert(I.Fun.ExceptionSpecType == ESpecType && "bitfield overflow");
217 // new[] a parameter array if needed.
219 // If the 'InlineParams' in Declarator is unused and big enough, put our
220 // parameter list there (in an effort to avoid new/delete traffic). If it
221 // is already used (consider a function returning a function pointer) or too
222 // small (function with too many parameters), go to the heap.
223 if (!TheDeclarator.InlineParamsUsed &&
224 NumParams <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
225 I.Fun.Params = TheDeclarator.InlineParams;
226 I.Fun.DeleteParams = false;
227 TheDeclarator.InlineParamsUsed = true;
229 I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams];
230 I.Fun.DeleteParams = true;
232 memcpy(I.Fun.Params, Params, sizeof(Params[0]) * NumParams);
235 // Check what exception specification information we should actually store.
237 default: break; // By default, save nothing.
239 // new[] an exception array if needed
241 I.Fun.NumExceptions = NumExceptions;
242 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
243 for (unsigned i = 0; i != NumExceptions; ++i) {
244 I.Fun.Exceptions[i].Ty = Exceptions[i];
245 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
250 case EST_ComputedNoexcept:
251 I.Fun.NoexceptExpr = NoexceptExpr;
255 I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
261 bool Declarator::isDeclarationOfFunction() const {
262 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
263 switch (DeclTypeInfo[i].Kind) {
264 case DeclaratorChunk::Function:
266 case DeclaratorChunk::Paren:
268 case DeclaratorChunk::Pointer:
269 case DeclaratorChunk::Reference:
270 case DeclaratorChunk::Array:
271 case DeclaratorChunk::BlockPointer:
272 case DeclaratorChunk::MemberPointer:
273 case DeclaratorChunk::Pipe:
276 llvm_unreachable("Invalid type chunk");
279 switch (DS.getTypeSpecType()) {
302 case TST_unknown_anytype:
303 case TST_unspecified:
306 #define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
307 #include "clang/Basic/OpenCLImageTypes.def"
310 case TST_decltype_auto:
311 // This must have an initializer, so can't be a function declaration,
312 // even if the initializer has function type.
317 if (Expr *E = DS.getRepAsExpr())
318 return E->getType()->isFunctionType();
321 case TST_underlyingType:
323 case TST_typeofType: {
324 QualType QT = DS.getRepAsType().get();
328 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
334 return QT->isFunctionType();
338 llvm_unreachable("Invalid TypeSpecType!");
341 bool Declarator::isStaticMember() {
342 assert(getContext() == MemberContext);
343 return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
344 (getName().Kind == UnqualifiedId::IK_OperatorFunctionId &&
345 CXXMethodDecl::isStaticOverloadedOperator(
346 getName().OperatorFunctionId.Operator));
349 bool Declarator::isCtorOrDtor() {
350 return (getName().getKind() == UnqualifiedId::IK_ConstructorName) ||
351 (getName().getKind() == UnqualifiedId::IK_DestructorName);
354 bool DeclSpec::hasTagDefinition() const {
357 return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
360 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
361 /// declaration specifier includes.
363 unsigned DeclSpec::getParsedSpecifiers() const {
365 if (StorageClassSpec != SCS_unspecified ||
366 ThreadStorageClassSpec != TSCS_unspecified)
367 Res |= PQ_StorageClassSpecifier;
369 if (TypeQualifiers != TQ_unspecified)
370 Res |= PQ_TypeQualifier;
372 if (hasTypeSpecifier())
373 Res |= PQ_TypeSpecifier;
375 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified ||
376 FS_noreturn_specified || FS_forceinline_specified)
377 Res |= PQ_FunctionSpecifier;
381 template <class T> static bool BadSpecifier(T TNew, T TPrev,
382 const char *&PrevSpec,
384 bool IsExtension = true) {
385 PrevSpec = DeclSpec::getSpecifierName(TPrev);
387 DiagID = diag::err_invalid_decl_spec_combination;
389 DiagID = IsExtension ? diag::ext_duplicate_declspec :
390 diag::warn_duplicate_declspec;
394 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
396 case DeclSpec::SCS_unspecified: return "unspecified";
397 case DeclSpec::SCS_typedef: return "typedef";
398 case DeclSpec::SCS_extern: return "extern";
399 case DeclSpec::SCS_static: return "static";
400 case DeclSpec::SCS_auto: return "auto";
401 case DeclSpec::SCS_register: return "register";
402 case DeclSpec::SCS_private_extern: return "__private_extern__";
403 case DeclSpec::SCS_mutable: return "mutable";
405 llvm_unreachable("Unknown typespec!");
408 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
410 case DeclSpec::TSCS_unspecified: return "unspecified";
411 case DeclSpec::TSCS___thread: return "__thread";
412 case DeclSpec::TSCS_thread_local: return "thread_local";
413 case DeclSpec::TSCS__Thread_local: return "_Thread_local";
415 llvm_unreachable("Unknown typespec!");
418 const char *DeclSpec::getSpecifierName(TSW W) {
420 case TSW_unspecified: return "unspecified";
421 case TSW_short: return "short";
422 case TSW_long: return "long";
423 case TSW_longlong: return "long long";
425 llvm_unreachable("Unknown typespec!");
428 const char *DeclSpec::getSpecifierName(TSC C) {
430 case TSC_unspecified: return "unspecified";
431 case TSC_imaginary: return "imaginary";
432 case TSC_complex: return "complex";
434 llvm_unreachable("Unknown typespec!");
438 const char *DeclSpec::getSpecifierName(TSS S) {
440 case TSS_unspecified: return "unspecified";
441 case TSS_signed: return "signed";
442 case TSS_unsigned: return "unsigned";
444 llvm_unreachable("Unknown typespec!");
447 const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
448 const PrintingPolicy &Policy) {
450 case DeclSpec::TST_unspecified: return "unspecified";
451 case DeclSpec::TST_void: return "void";
452 case DeclSpec::TST_char: return "char";
453 case DeclSpec::TST_wchar: return Policy.MSWChar ? "__wchar_t" : "wchar_t";
454 case DeclSpec::TST_char16: return "char16_t";
455 case DeclSpec::TST_char32: return "char32_t";
456 case DeclSpec::TST_int: return "int";
457 case DeclSpec::TST_int128: return "__int128";
458 case DeclSpec::TST_half: return "half";
459 case DeclSpec::TST_float: return "float";
460 case DeclSpec::TST_double: return "double";
461 case DeclSpec::TST_float128: return "__float128";
462 case DeclSpec::TST_bool: return Policy.Bool ? "bool" : "_Bool";
463 case DeclSpec::TST_decimal32: return "_Decimal32";
464 case DeclSpec::TST_decimal64: return "_Decimal64";
465 case DeclSpec::TST_decimal128: return "_Decimal128";
466 case DeclSpec::TST_enum: return "enum";
467 case DeclSpec::TST_class: return "class";
468 case DeclSpec::TST_union: return "union";
469 case DeclSpec::TST_struct: return "struct";
470 case DeclSpec::TST_interface: return "__interface";
471 case DeclSpec::TST_typename: return "type-name";
472 case DeclSpec::TST_typeofType:
473 case DeclSpec::TST_typeofExpr: return "typeof";
474 case DeclSpec::TST_auto: return "auto";
475 case DeclSpec::TST_auto_type: return "__auto_type";
476 case DeclSpec::TST_decltype: return "(decltype)";
477 case DeclSpec::TST_decltype_auto: return "decltype(auto)";
478 case DeclSpec::TST_underlyingType: return "__underlying_type";
479 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
480 case DeclSpec::TST_atomic: return "_Atomic";
481 #define GENERIC_IMAGE_TYPE(ImgType, Id) \
482 case DeclSpec::TST_##ImgType##_t: \
483 return #ImgType "_t";
484 #include "clang/Basic/OpenCLImageTypes.def"
485 case DeclSpec::TST_error: return "(error)";
487 llvm_unreachable("Unknown typespec!");
490 const char *DeclSpec::getSpecifierName(TQ T) {
492 case DeclSpec::TQ_unspecified: return "unspecified";
493 case DeclSpec::TQ_const: return "const";
494 case DeclSpec::TQ_restrict: return "restrict";
495 case DeclSpec::TQ_volatile: return "volatile";
496 case DeclSpec::TQ_atomic: return "_Atomic";
497 case DeclSpec::TQ_unaligned: return "__unaligned";
499 llvm_unreachable("Unknown typespec!");
502 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
503 const char *&PrevSpec,
505 const PrintingPolicy &Policy) {
506 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
507 // specifiers are not supported.
508 // It seems sensible to prohibit private_extern too
509 // The cl_clang_storage_class_specifiers extension enables support for
510 // these storage-class specifiers.
511 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
512 // specifiers are not supported."
513 if (S.getLangOpts().OpenCL &&
514 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
517 case SCS_private_extern:
519 if (S.getLangOpts().OpenCLVersion < 120) {
520 DiagID = diag::err_opencl_unknown_type_specifier;
521 PrevSpec = getSpecifierName(SC);
527 DiagID = diag::err_opencl_unknown_type_specifier;
528 PrevSpec = getSpecifierName(SC);
535 if (StorageClassSpec != SCS_unspecified) {
536 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
537 bool isInvalid = true;
538 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
540 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy);
541 if (StorageClassSpec == SCS_auto) {
542 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
543 PrevSpec, DiagID, Policy);
544 assert(!isInvalid && "auto SCS -> TST recovery failed");
548 // Changing storage class is allowed only if the previous one
549 // was the 'extern' that is part of a linkage specification and
550 // the new storage class is 'typedef'.
552 !(SCS_extern_in_linkage_spec &&
553 StorageClassSpec == SCS_extern &&
555 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
557 StorageClassSpec = SC;
558 StorageClassSpecLoc = Loc;
559 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
563 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
564 const char *&PrevSpec,
566 if (ThreadStorageClassSpec != TSCS_unspecified)
567 return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
569 ThreadStorageClassSpec = TSC;
570 ThreadStorageClassSpecLoc = Loc;
574 /// These methods set the specified attribute of the DeclSpec, but return true
575 /// and ignore the request if invalid (e.g. "extern" then "auto" is
577 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
578 const char *&PrevSpec,
580 const PrintingPolicy &Policy) {
581 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
582 // for 'long long' we will keep the source location of the first 'long'.
583 if (TypeSpecWidth == TSW_unspecified)
585 // Allow turning long -> long long.
586 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
587 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
592 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
593 const char *&PrevSpec,
595 if (TypeSpecComplex != TSC_unspecified)
596 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
602 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
603 const char *&PrevSpec,
605 if (TypeSpecSign != TSS_unspecified)
606 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
612 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
613 const char *&PrevSpec,
616 const PrintingPolicy &Policy) {
617 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy);
620 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
621 SourceLocation TagNameLoc,
622 const char *&PrevSpec,
625 const PrintingPolicy &Policy) {
626 assert(isTypeRep(T) && "T does not store a type");
627 assert(Rep && "no type provided!");
628 if (TypeSpecType != TST_unspecified) {
629 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
630 DiagID = diag::err_invalid_decl_spec_combination;
636 TSTNameLoc = TagNameLoc;
637 TypeSpecOwned = false;
641 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
642 const char *&PrevSpec,
645 const PrintingPolicy &Policy) {
646 assert(isExprRep(T) && "T does not store an expr");
647 assert(Rep && "no expression provided!");
648 if (TypeSpecType != TST_unspecified) {
649 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
650 DiagID = diag::err_invalid_decl_spec_combination;
657 TypeSpecOwned = false;
661 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
662 const char *&PrevSpec,
664 Decl *Rep, bool Owned,
665 const PrintingPolicy &Policy) {
666 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy);
669 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
670 SourceLocation TagNameLoc,
671 const char *&PrevSpec,
673 Decl *Rep, bool Owned,
674 const PrintingPolicy &Policy) {
675 assert(isDeclRep(T) && "T does not store a decl");
676 // Unlike the other cases, we don't assert that we actually get a decl.
678 if (TypeSpecType != TST_unspecified) {
679 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
680 DiagID = diag::err_invalid_decl_spec_combination;
686 TSTNameLoc = TagNameLoc;
687 TypeSpecOwned = Owned && Rep != nullptr;
691 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
692 const char *&PrevSpec,
694 const PrintingPolicy &Policy) {
695 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
696 "rep required for these type-spec kinds!");
697 if (TypeSpecType != TST_unspecified) {
698 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
699 DiagID = diag::err_invalid_decl_spec_combination;
704 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
705 TypeAltiVecBool = true;
709 TypeSpecOwned = false;
713 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
714 const char *&PrevSpec, unsigned &DiagID,
715 const PrintingPolicy &Policy) {
716 if (TypeSpecType != TST_unspecified) {
717 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
718 DiagID = diag::err_invalid_vector_decl_spec_combination;
721 TypeAltiVecVector = isAltiVecVector;
726 bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
727 const char *&PrevSpec, unsigned &DiagID,
728 const PrintingPolicy &Policy) {
730 if (TypeSpecType != TST_unspecified) {
731 PrevSpec = DeclSpec::getSpecifierName((TST)TypeSpecType, Policy);
732 DiagID = diag::err_invalid_decl_spec_combination;
737 TypeSpecPipe = TSP_pipe;
742 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
743 const char *&PrevSpec, unsigned &DiagID,
744 const PrintingPolicy &Policy) {
745 if (!TypeAltiVecVector || TypeAltiVecPixel ||
746 (TypeSpecType != TST_unspecified)) {
747 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
748 DiagID = diag::err_invalid_pixel_decl_spec_combination;
751 TypeAltiVecPixel = isAltiVecPixel;
757 bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
758 const char *&PrevSpec, unsigned &DiagID,
759 const PrintingPolicy &Policy) {
760 if (!TypeAltiVecVector || TypeAltiVecBool ||
761 (TypeSpecType != TST_unspecified)) {
762 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
763 DiagID = diag::err_invalid_vector_bool_decl_spec;
766 TypeAltiVecBool = isAltiVecBool;
772 bool DeclSpec::SetTypeSpecError() {
773 TypeSpecType = TST_error;
774 TypeSpecOwned = false;
775 TSTLoc = SourceLocation();
776 TSTNameLoc = SourceLocation();
780 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
781 unsigned &DiagID, const LangOptions &Lang) {
782 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
783 // C++. However, since this is likely not what the user intended, we will
784 // always warn. We do not need to set the qualifier's location since we
786 if (TypeQualifiers & T) {
787 bool IsExtension = true;
790 return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
795 case TQ_unspecified: break;
796 case TQ_const: TQ_constLoc = Loc; return false;
797 case TQ_restrict: TQ_restrictLoc = Loc; return false;
798 case TQ_volatile: TQ_volatileLoc = Loc; return false;
799 case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
800 case TQ_atomic: TQ_atomicLoc = Loc; return false;
803 llvm_unreachable("Unknown type qualifier!");
806 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
808 // 'inline inline' is ok. However, since this is likely not what the user
809 // intended, we will always warn, similar to duplicates of type qualifiers.
810 if (FS_inline_specified) {
811 DiagID = diag::warn_duplicate_declspec;
815 FS_inline_specified = true;
820 bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
822 if (FS_forceinline_specified) {
823 DiagID = diag::warn_duplicate_declspec;
824 PrevSpec = "__forceinline";
827 FS_forceinline_specified = true;
828 FS_forceinlineLoc = Loc;
832 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
833 const char *&PrevSpec,
835 // 'virtual virtual' is ok, but warn as this is likely not what the user
837 if (FS_virtual_specified) {
838 DiagID = diag::warn_duplicate_declspec;
839 PrevSpec = "virtual";
842 FS_virtual_specified = true;
847 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
848 const char *&PrevSpec,
850 // 'explicit explicit' is ok, but warn as this is likely not what the user
852 if (FS_explicit_specified) {
853 DiagID = diag::warn_duplicate_declspec;
854 PrevSpec = "explicit";
857 FS_explicit_specified = true;
858 FS_explicitLoc = Loc;
862 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
863 const char *&PrevSpec,
865 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
867 if (FS_noreturn_specified) {
868 DiagID = diag::warn_duplicate_declspec;
869 PrevSpec = "_Noreturn";
872 FS_noreturn_specified = true;
873 FS_noreturnLoc = Loc;
877 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
879 if (Friend_specified) {
881 // Keep the later location, so that we can later diagnose ill-formed
882 // declarations like 'friend class X friend;'. Per [class.friend]p3,
883 // 'friend' must be the first token in a friend declaration that is
884 // not a function declaration.
886 DiagID = diag::warn_duplicate_declspec;
890 Friend_specified = true;
895 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
897 if (isModulePrivateSpecified()) {
898 PrevSpec = "__module_private__";
899 DiagID = diag::ext_duplicate_declspec;
903 ModulePrivateLoc = Loc;
907 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
909 // 'constexpr constexpr' is ok, but warn as this is likely not what the user
911 if (Constexpr_specified) {
912 DiagID = diag::warn_duplicate_declspec;
913 PrevSpec = "constexpr";
916 Constexpr_specified = true;
921 bool DeclSpec::SetConceptSpec(SourceLocation Loc, const char *&PrevSpec,
923 if (Concept_specified) {
924 DiagID = diag::ext_duplicate_declspec;
925 PrevSpec = "concept";
928 Concept_specified = true;
933 void DeclSpec::SaveWrittenBuiltinSpecs() {
934 writtenBS.Sign = getTypeSpecSign();
935 writtenBS.Width = getTypeSpecWidth();
936 writtenBS.Type = getTypeSpecType();
937 // Search the list of attributes for the presence of a mode attribute.
938 writtenBS.ModeAttr = false;
939 AttributeList* attrs = getAttributes().getList();
941 if (attrs->getKind() == AttributeList::AT_Mode) {
942 writtenBS.ModeAttr = true;
945 attrs = attrs->getNext();
949 /// Finish - This does final analysis of the declspec, rejecting things like
950 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
951 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
952 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
953 void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
954 // Before possibly changing their values, save specs as written.
955 SaveWrittenBuiltinSpecs();
957 // Check the type specifier components first.
959 // If decltype(auto) is used, no other type specifiers are permitted.
960 if (TypeSpecType == TST_decltype_auto &&
961 (TypeSpecWidth != TSW_unspecified ||
962 TypeSpecComplex != TSC_unspecified ||
963 TypeSpecSign != TSS_unspecified ||
964 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
966 const unsigned NumLocs = 9;
967 SourceLocation ExtraLocs[NumLocs] = {
968 TSWLoc, TSCLoc, TSSLoc, AltiVecLoc,
969 TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc
971 FixItHint Hints[NumLocs];
972 SourceLocation FirstLoc;
973 for (unsigned I = 0; I != NumLocs; ++I) {
974 if (ExtraLocs[I].isValid()) {
975 if (FirstLoc.isInvalid() ||
976 S.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
978 FirstLoc = ExtraLocs[I];
979 Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
982 TypeSpecWidth = TSW_unspecified;
983 TypeSpecComplex = TSC_unspecified;
984 TypeSpecSign = TSS_unspecified;
985 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
987 S.Diag(TSTLoc, diag::err_decltype_auto_cannot_be_combined)
988 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
989 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
992 // Validate and finalize AltiVec vector declspec.
993 if (TypeAltiVecVector) {
994 if (TypeAltiVecBool) {
995 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
996 if (TypeSpecSign != TSS_unspecified) {
997 S.Diag(TSSLoc, diag::err_invalid_vector_bool_decl_spec)
998 << getSpecifierName((TSS)TypeSpecSign);
1001 // Only char/int are valid with vector bool. (PIM 2.1)
1002 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
1003 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
1004 S.Diag(TSTLoc, diag::err_invalid_vector_bool_decl_spec)
1005 << (TypeAltiVecPixel ? "__pixel" :
1006 getSpecifierName((TST)TypeSpecType, Policy));
1009 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1010 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short) &&
1011 (TypeSpecWidth != TSW_longlong))
1012 S.Diag(TSWLoc, diag::err_invalid_vector_bool_decl_spec)
1013 << getSpecifierName((TSW)TypeSpecWidth);
1015 // vector bool long long requires VSX support or ZVector.
1016 if ((TypeSpecWidth == TSW_longlong) &&
1017 (!S.Context.getTargetInfo().hasFeature("vsx")) &&
1018 (!S.Context.getTargetInfo().hasFeature("power8-vector")) &&
1019 !S.getLangOpts().ZVector)
1020 S.Diag(TSTLoc, diag::err_invalid_vector_long_long_decl_spec);
1022 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1023 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1024 (TypeSpecWidth != TSW_unspecified))
1025 TypeSpecSign = TSS_unsigned;
1026 } else if (TypeSpecType == TST_double) {
1027 // vector long double and vector long long double are never allowed.
1028 // vector double is OK for Power7 and later, and ZVector.
1029 if (TypeSpecWidth == TSW_long || TypeSpecWidth == TSW_longlong)
1030 S.Diag(TSWLoc, diag::err_invalid_vector_long_double_decl_spec);
1031 else if (!S.Context.getTargetInfo().hasFeature("vsx") &&
1032 !S.getLangOpts().ZVector)
1033 S.Diag(TSTLoc, diag::err_invalid_vector_double_decl_spec);
1034 } else if (TypeSpecType == TST_float) {
1035 // vector float is unsupported for ZVector.
1036 if (S.getLangOpts().ZVector)
1037 S.Diag(TSTLoc, diag::err_invalid_vector_float_decl_spec);
1038 } else if (TypeSpecWidth == TSW_long) {
1039 // vector long is unsupported for ZVector and deprecated for AltiVec.
1040 if (S.getLangOpts().ZVector)
1041 S.Diag(TSWLoc, diag::err_invalid_vector_long_decl_spec);
1043 S.Diag(TSWLoc, diag::warn_vector_long_decl_spec_combination)
1044 << getSpecifierName((TST)TypeSpecType, Policy);
1047 if (TypeAltiVecPixel) {
1048 //TODO: perform validation
1049 TypeSpecType = TST_int;
1050 TypeSpecSign = TSS_unsigned;
1051 TypeSpecWidth = TSW_short;
1052 TypeSpecOwned = false;
1056 // signed/unsigned are only valid with int/char/wchar_t.
1057 if (TypeSpecSign != TSS_unspecified) {
1058 if (TypeSpecType == TST_unspecified)
1059 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
1060 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
1061 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
1062 S.Diag(TSSLoc, diag::err_invalid_sign_spec)
1063 << getSpecifierName((TST)TypeSpecType, Policy);
1064 // signed double -> double.
1065 TypeSpecSign = TSS_unspecified;
1069 // Validate the width of the type.
1070 switch (TypeSpecWidth) {
1071 case TSW_unspecified: break;
1072 case TSW_short: // short int
1073 case TSW_longlong: // long long int
1074 if (TypeSpecType == TST_unspecified)
1075 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1076 else if (TypeSpecType != TST_int) {
1077 S.Diag(TSWLoc, diag::err_invalid_width_spec) << (int)TypeSpecWidth
1078 << getSpecifierName((TST)TypeSpecType, Policy);
1079 TypeSpecType = TST_int;
1080 TypeSpecOwned = false;
1083 case TSW_long: // long double, long int
1084 if (TypeSpecType == TST_unspecified)
1085 TypeSpecType = TST_int; // long -> long int.
1086 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
1087 S.Diag(TSWLoc, diag::err_invalid_width_spec) << (int)TypeSpecWidth
1088 << getSpecifierName((TST)TypeSpecType, Policy);
1089 TypeSpecType = TST_int;
1090 TypeSpecOwned = false;
1095 // TODO: if the implementation does not implement _Complex or _Imaginary,
1096 // disallow their use. Need information about the backend.
1097 if (TypeSpecComplex != TSC_unspecified) {
1098 if (TypeSpecType == TST_unspecified) {
1099 S.Diag(TSCLoc, diag::ext_plain_complex)
1100 << FixItHint::CreateInsertion(
1101 S.getLocForEndOfToken(getTypeSpecComplexLoc()),
1103 TypeSpecType = TST_double; // _Complex -> _Complex double.
1104 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1105 // Note that this intentionally doesn't include _Complex _Bool.
1106 if (!S.getLangOpts().CPlusPlus)
1107 S.Diag(TSTLoc, diag::ext_integer_complex);
1108 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
1109 S.Diag(TSCLoc, diag::err_invalid_complex_spec)
1110 << getSpecifierName((TST)TypeSpecType, Policy);
1111 TypeSpecComplex = TSC_unspecified;
1115 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1116 // _Thread_local can only appear with the 'static' and 'extern' storage class
1117 // specifiers. We also allow __private_extern__ as an extension.
1118 if (ThreadStorageClassSpec != TSCS_unspecified) {
1119 switch (StorageClassSpec) {
1120 case SCS_unspecified:
1122 case SCS_private_extern:
1126 if (S.getSourceManager().isBeforeInTranslationUnit(
1127 getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
1128 S.Diag(getStorageClassSpecLoc(),
1129 diag::err_invalid_decl_spec_combination)
1130 << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1131 << SourceRange(getThreadStorageClassSpecLoc());
1133 S.Diag(getThreadStorageClassSpecLoc(),
1134 diag::err_invalid_decl_spec_combination)
1135 << DeclSpec::getSpecifierName(getStorageClassSpec())
1136 << SourceRange(getStorageClassSpecLoc());
1137 // Discard the thread storage class specifier to recover.
1138 ThreadStorageClassSpec = TSCS_unspecified;
1139 ThreadStorageClassSpecLoc = SourceLocation();
1143 // If no type specifier was provided and we're parsing a language where
1144 // the type specifier is not optional, but we got 'auto' as a storage
1145 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1147 if (S.getLangOpts().CPlusPlus &&
1148 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1149 TypeSpecType = TST_auto;
1150 StorageClassSpec = SCS_unspecified;
1151 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1152 StorageClassSpecLoc = SourceLocation();
1154 // Diagnose if we've recovered from an ill-formed 'auto' storage class
1155 // specifier in a pre-C++11 dialect of C++.
1156 if (!S.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1157 S.Diag(TSTLoc, diag::ext_auto_type_specifier);
1158 if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
1159 StorageClassSpec == SCS_auto)
1160 S.Diag(StorageClassSpecLoc, diag::warn_auto_storage_class)
1161 << FixItHint::CreateRemoval(StorageClassSpecLoc);
1162 if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1163 S.Diag(TSTLoc, diag::warn_cxx98_compat_unicode_type)
1164 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1165 if (Constexpr_specified)
1166 S.Diag(ConstexprLoc, diag::warn_cxx98_compat_constexpr);
1168 // C++ [class.friend]p6:
1169 // No storage-class-specifier shall appear in the decl-specifier-seq
1170 // of a friend declaration.
1171 if (isFriendSpecified() &&
1172 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1173 SmallString<32> SpecName;
1174 SourceLocation SCLoc;
1175 FixItHint StorageHint, ThreadHint;
1177 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1178 SpecName = getSpecifierName(SC);
1179 SCLoc = getStorageClassSpecLoc();
1180 StorageHint = FixItHint::CreateRemoval(SCLoc);
1183 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1184 if (!SpecName.empty()) SpecName += " ";
1185 SpecName += getSpecifierName(TSC);
1186 SCLoc = getThreadStorageClassSpecLoc();
1187 ThreadHint = FixItHint::CreateRemoval(SCLoc);
1190 S.Diag(SCLoc, diag::err_friend_decl_spec)
1191 << SpecName << StorageHint << ThreadHint;
1193 ClearStorageClassSpecs();
1196 // C++11 [dcl.fct.spec]p5:
1197 // The virtual specifier shall be used only in the initial
1198 // declaration of a non-static class member function;
1199 // C++11 [dcl.fct.spec]p6:
1200 // The explicit specifier shall be used only in the declaration of
1201 // a constructor or conversion function within its class
1203 if (isFriendSpecified() && (isVirtualSpecified() || isExplicitSpecified())) {
1205 SourceLocation SCLoc;
1207 if (isVirtualSpecified()) {
1208 Keyword = "virtual";
1209 SCLoc = getVirtualSpecLoc();
1211 Keyword = "explicit";
1212 SCLoc = getExplicitSpecLoc();
1215 FixItHint Hint = FixItHint::CreateRemoval(SCLoc);
1216 S.Diag(SCLoc, diag::err_friend_decl_spec)
1219 FS_virtual_specified = FS_explicit_specified = false;
1220 FS_virtualLoc = FS_explicitLoc = SourceLocation();
1223 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1225 // Okay, now we can infer the real type.
1227 // TODO: return "auto function" and other bad things based on the real type.
1229 // 'data definition has no type or storage class'?
1232 bool DeclSpec::isMissingDeclaratorOk() {
1233 TST tst = getTypeSpecType();
1234 return isDeclRep(tst) && getRepAsDecl() != nullptr &&
1235 StorageClassSpec != DeclSpec::SCS_typedef;
1238 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1239 OverloadedOperatorKind Op,
1240 SourceLocation SymbolLocations[3]) {
1241 Kind = IK_OperatorFunctionId;
1242 StartLocation = OperatorLoc;
1243 EndLocation = OperatorLoc;
1244 OperatorFunctionId.Operator = Op;
1245 for (unsigned I = 0; I != 3; ++I) {
1246 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
1248 if (SymbolLocations[I].isValid())
1249 EndLocation = SymbolLocations[I];
1253 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1254 const char *&PrevSpec) {
1255 if (!FirstLocation.isValid())
1256 FirstLocation = Loc;
1260 if (Specifiers & VS) {
1261 PrevSpec = getSpecifierName(VS);
1268 default: llvm_unreachable("Unknown specifier!");
1269 case VS_Override: VS_overrideLoc = Loc; break;
1271 case VS_Final: VS_finalLoc = Loc; break;
1277 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1279 default: llvm_unreachable("Unknown specifier");
1280 case VS_Override: return "override";
1281 case VS_Final: return "final";
1282 case VS_Sealed: return "sealed";