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/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
15 #include "clang/Sema/DeclSpec.h"
16 #include "clang/Sema/LocInfoType.h"
17 #include "clang/Sema/ParsedTemplate.h"
18 #include "clang/Sema/SemaDiagnostic.h"
19 #include "clang/Sema/Sema.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/NestedNameSpecifier.h"
23 #include "clang/AST/TypeLoc.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Basic/LangOptions.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/Support/ErrorHandling.h"
29 using namespace clang;
32 static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
34 return D.Report(Loc, DiagID);
38 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
39 assert(TemplateId && "NULL template-id annotation?");
41 this->TemplateId = TemplateId;
42 StartLocation = TemplateId->TemplateNameLoc;
43 EndLocation = TemplateId->RAngleLoc;
46 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
47 assert(TemplateId && "NULL template-id annotation?");
48 Kind = IK_ConstructorTemplateId;
49 this->TemplateId = TemplateId;
50 StartLocation = TemplateId->TemplateNameLoc;
51 EndLocation = TemplateId->RAngleLoc;
54 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
55 TypeLoc TL, SourceLocation ColonColonLoc) {
56 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
57 if (Range.getBegin().isInvalid())
58 Range.setBegin(TL.getBeginLoc());
59 Range.setEnd(ColonColonLoc);
61 assert(Range == Builder.getSourceRange() &&
62 "NestedNameSpecifierLoc range computation incorrect");
65 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
66 SourceLocation IdentifierLoc,
67 SourceLocation ColonColonLoc) {
68 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
70 if (Range.getBegin().isInvalid())
71 Range.setBegin(IdentifierLoc);
72 Range.setEnd(ColonColonLoc);
74 assert(Range == Builder.getSourceRange() &&
75 "NestedNameSpecifierLoc range computation incorrect");
78 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
79 SourceLocation NamespaceLoc,
80 SourceLocation ColonColonLoc) {
81 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
83 if (Range.getBegin().isInvalid())
84 Range.setBegin(NamespaceLoc);
85 Range.setEnd(ColonColonLoc);
87 assert(Range == Builder.getSourceRange() &&
88 "NestedNameSpecifierLoc range computation incorrect");
91 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
92 SourceLocation AliasLoc,
93 SourceLocation ColonColonLoc) {
94 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
96 if (Range.getBegin().isInvalid())
97 Range.setBegin(AliasLoc);
98 Range.setEnd(ColonColonLoc);
100 assert(Range == Builder.getSourceRange() &&
101 "NestedNameSpecifierLoc range computation incorrect");
104 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
105 SourceLocation ColonColonLoc) {
106 Builder.MakeGlobal(Context, ColonColonLoc);
108 Range = SourceRange(ColonColonLoc);
110 assert(Range == Builder.getSourceRange() &&
111 "NestedNameSpecifierLoc range computation incorrect");
114 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
115 NestedNameSpecifier *Qualifier, SourceRange R) {
116 Builder.MakeTrivial(Context, Qualifier, R);
120 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
122 Range = SourceRange();
127 Range = Other.getSourceRange();
128 Builder.Adopt(Other);
131 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
132 if (!Builder.getRepresentation())
133 return SourceLocation();
134 return Builder.getTemporary().getLocalBeginLoc();
137 NestedNameSpecifierLoc
138 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
139 if (!Builder.getRepresentation())
140 return NestedNameSpecifierLoc();
142 return Builder.getWithLocInContext(Context);
145 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
146 /// "TheDeclarator" is the declarator that this will be added to.
147 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
148 SourceLocation EllipsisLoc,
152 bool RefQualifierIsLvalueRef,
153 SourceLocation RefQualifierLoc,
154 SourceLocation ConstQualifierLoc,
156 VolatileQualifierLoc,
157 SourceLocation MutableLoc,
158 ExceptionSpecificationType
160 SourceLocation ESpecLoc,
161 ParsedType *Exceptions,
162 SourceRange *ExceptionRanges,
163 unsigned NumExceptions,
165 CachedTokens *ExceptionSpecTokens,
166 SourceLocation LocalRangeBegin,
167 SourceLocation LocalRangeEnd,
168 Declarator &TheDeclarator,
169 ParsedType TrailingReturnType) {
172 I.Loc = LocalRangeBegin;
173 I.EndLoc = LocalRangeEnd;
175 I.Fun.hasPrototype = hasProto;
176 I.Fun.isVariadic = isVariadic;
177 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
178 I.Fun.DeleteArgInfo = false;
179 I.Fun.TypeQuals = TypeQuals;
180 I.Fun.NumArgs = NumArgs;
182 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
183 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
184 I.Fun.ConstQualifierLoc = ConstQualifierLoc.getRawEncoding();
185 I.Fun.VolatileQualifierLoc = VolatileQualifierLoc.getRawEncoding();
186 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
187 I.Fun.ExceptionSpecType = ESpecType;
188 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
189 I.Fun.NumExceptions = 0;
190 I.Fun.Exceptions = 0;
191 I.Fun.NoexceptExpr = 0;
192 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
194 // new[] an argument array if needed.
196 // If the 'InlineParams' in Declarator is unused and big enough, put our
197 // parameter list there (in an effort to avoid new/delete traffic). If it
198 // is already used (consider a function returning a function pointer) or too
199 // small (function taking too many arguments), go to the heap.
200 if (!TheDeclarator.InlineParamsUsed &&
201 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
202 I.Fun.ArgInfo = TheDeclarator.InlineParams;
203 I.Fun.DeleteArgInfo = false;
204 TheDeclarator.InlineParamsUsed = true;
206 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
207 I.Fun.DeleteArgInfo = true;
209 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
212 // Check what exception specification information we should actually store.
214 default: break; // By default, save nothing.
216 // new[] an exception array if needed
218 I.Fun.NumExceptions = NumExceptions;
219 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
220 for (unsigned i = 0; i != NumExceptions; ++i) {
221 I.Fun.Exceptions[i].Ty = Exceptions[i];
222 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
227 case EST_ComputedNoexcept:
228 I.Fun.NoexceptExpr = NoexceptExpr;
232 I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
238 bool Declarator::isDeclarationOfFunction() const {
239 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
240 switch (DeclTypeInfo[i].Kind) {
241 case DeclaratorChunk::Function:
243 case DeclaratorChunk::Paren:
245 case DeclaratorChunk::Pointer:
246 case DeclaratorChunk::Reference:
247 case DeclaratorChunk::Array:
248 case DeclaratorChunk::BlockPointer:
249 case DeclaratorChunk::MemberPointer:
252 llvm_unreachable("Invalid type chunk");
255 switch (DS.getTypeSpecType()) {
275 case TST_unknown_anytype:
276 case TST_unspecified:
283 if (Expr *E = DS.getRepAsExpr())
284 return E->getType()->isFunctionType();
287 case TST_underlyingType:
289 case TST_typeofType: {
290 QualType QT = DS.getRepAsType().get();
294 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
300 return QT->isFunctionType();
304 llvm_unreachable("Invalid TypeSpecType!");
307 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
308 /// declaration specifier includes.
310 unsigned DeclSpec::getParsedSpecifiers() const {
312 if (StorageClassSpec != SCS_unspecified ||
313 SCS_thread_specified)
314 Res |= PQ_StorageClassSpecifier;
316 if (TypeQualifiers != TQ_unspecified)
317 Res |= PQ_TypeQualifier;
319 if (hasTypeSpecifier())
320 Res |= PQ_TypeSpecifier;
322 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
323 Res |= PQ_FunctionSpecifier;
327 template <class T> static bool BadSpecifier(T TNew, T TPrev,
328 const char *&PrevSpec,
330 PrevSpec = DeclSpec::getSpecifierName(TPrev);
331 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
332 : diag::err_invalid_decl_spec_combination);
336 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
338 case DeclSpec::SCS_unspecified: return "unspecified";
339 case DeclSpec::SCS_typedef: return "typedef";
340 case DeclSpec::SCS_extern: return "extern";
341 case DeclSpec::SCS_static: return "static";
342 case DeclSpec::SCS_auto: return "auto";
343 case DeclSpec::SCS_register: return "register";
344 case DeclSpec::SCS_private_extern: return "__private_extern__";
345 case DeclSpec::SCS_mutable: return "mutable";
347 llvm_unreachable("Unknown typespec!");
350 const char *DeclSpec::getSpecifierName(TSW W) {
352 case TSW_unspecified: return "unspecified";
353 case TSW_short: return "short";
354 case TSW_long: return "long";
355 case TSW_longlong: return "long long";
357 llvm_unreachable("Unknown typespec!");
360 const char *DeclSpec::getSpecifierName(TSC C) {
362 case TSC_unspecified: return "unspecified";
363 case TSC_imaginary: return "imaginary";
364 case TSC_complex: return "complex";
366 llvm_unreachable("Unknown typespec!");
370 const char *DeclSpec::getSpecifierName(TSS S) {
372 case TSS_unspecified: return "unspecified";
373 case TSS_signed: return "signed";
374 case TSS_unsigned: return "unsigned";
376 llvm_unreachable("Unknown typespec!");
379 const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
381 case DeclSpec::TST_unspecified: return "unspecified";
382 case DeclSpec::TST_void: return "void";
383 case DeclSpec::TST_char: return "char";
384 case DeclSpec::TST_wchar: return "wchar_t";
385 case DeclSpec::TST_char16: return "char16_t";
386 case DeclSpec::TST_char32: return "char32_t";
387 case DeclSpec::TST_int: return "int";
388 case DeclSpec::TST_int128: return "__int128";
389 case DeclSpec::TST_half: return "half";
390 case DeclSpec::TST_float: return "float";
391 case DeclSpec::TST_double: return "double";
392 case DeclSpec::TST_bool: return "_Bool";
393 case DeclSpec::TST_decimal32: return "_Decimal32";
394 case DeclSpec::TST_decimal64: return "_Decimal64";
395 case DeclSpec::TST_decimal128: return "_Decimal128";
396 case DeclSpec::TST_enum: return "enum";
397 case DeclSpec::TST_class: return "class";
398 case DeclSpec::TST_union: return "union";
399 case DeclSpec::TST_struct: return "struct";
400 case DeclSpec::TST_typename: return "type-name";
401 case DeclSpec::TST_typeofType:
402 case DeclSpec::TST_typeofExpr: return "typeof";
403 case DeclSpec::TST_auto: return "auto";
404 case DeclSpec::TST_decltype: return "(decltype)";
405 case DeclSpec::TST_underlyingType: return "__underlying_type";
406 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
407 case DeclSpec::TST_atomic: return "_Atomic";
408 case DeclSpec::TST_error: return "(error)";
410 llvm_unreachable("Unknown typespec!");
413 const char *DeclSpec::getSpecifierName(TQ T) {
415 case DeclSpec::TQ_unspecified: return "unspecified";
416 case DeclSpec::TQ_const: return "const";
417 case DeclSpec::TQ_restrict: return "restrict";
418 case DeclSpec::TQ_volatile: return "volatile";
420 llvm_unreachable("Unknown typespec!");
423 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
424 const char *&PrevSpec,
426 // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class
427 // specifiers are not supported."
428 // It seems sensible to prohibit private_extern too
429 // The cl_clang_storage_class_specifiers extension enables support for
430 // these storage-class specifiers.
431 if (S.getLangOpts().OpenCL &&
432 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
435 case SCS_private_extern:
439 DiagID = diag::err_not_opencl_storage_class_specifier;
440 PrevSpec = getSpecifierName(SC);
447 if (StorageClassSpec != SCS_unspecified) {
448 // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
449 bool isInvalid = true;
450 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
452 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
453 if (StorageClassSpec == SCS_auto) {
454 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
456 assert(!isInvalid && "auto SCS -> TST recovery failed");
460 // Changing storage class is allowed only if the previous one
461 // was the 'extern' that is part of a linkage specification and
462 // the new storage class is 'typedef'.
464 !(SCS_extern_in_linkage_spec &&
465 StorageClassSpec == SCS_extern &&
467 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
469 StorageClassSpec = SC;
470 StorageClassSpecLoc = Loc;
471 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
475 bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
476 const char *&PrevSpec,
478 if (SCS_thread_specified) {
479 PrevSpec = "__thread";
480 DiagID = diag::ext_duplicate_declspec;
483 SCS_thread_specified = true;
488 /// These methods set the specified attribute of the DeclSpec, but return true
489 /// and ignore the request if invalid (e.g. "extern" then "auto" is
491 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
492 const char *&PrevSpec,
494 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
495 // for 'long long' we will keep the source location of the first 'long'.
496 if (TypeSpecWidth == TSW_unspecified)
498 // Allow turning long -> long long.
499 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
500 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
502 if (TypeAltiVecVector && !TypeAltiVecBool &&
503 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
504 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
505 DiagID = diag::warn_vector_long_decl_spec_combination;
511 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
512 const char *&PrevSpec,
514 if (TypeSpecComplex != TSC_unspecified)
515 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
521 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
522 const char *&PrevSpec,
524 if (TypeSpecSign != TSS_unspecified)
525 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
531 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
532 const char *&PrevSpec,
535 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
538 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
539 SourceLocation TagNameLoc,
540 const char *&PrevSpec,
543 assert(isTypeRep(T) && "T does not store a type");
544 assert(Rep && "no type provided!");
545 if (TypeSpecType != TST_unspecified) {
546 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
547 DiagID = diag::err_invalid_decl_spec_combination;
553 TSTNameLoc = TagNameLoc;
554 TypeSpecOwned = false;
558 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
559 const char *&PrevSpec,
562 assert(isExprRep(T) && "T does not store an expr");
563 assert(Rep && "no expression provided!");
564 if (TypeSpecType != TST_unspecified) {
565 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
566 DiagID = diag::err_invalid_decl_spec_combination;
573 TypeSpecOwned = false;
577 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
578 const char *&PrevSpec,
580 Decl *Rep, bool Owned) {
581 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
584 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
585 SourceLocation TagNameLoc,
586 const char *&PrevSpec,
588 Decl *Rep, bool Owned) {
589 assert(isDeclRep(T) && "T does not store a decl");
590 // Unlike the other cases, we don't assert that we actually get a decl.
592 if (TypeSpecType != TST_unspecified) {
593 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
594 DiagID = diag::err_invalid_decl_spec_combination;
600 TSTNameLoc = TagNameLoc;
601 TypeSpecOwned = Owned;
605 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
606 const char *&PrevSpec,
608 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
609 "rep required for these type-spec kinds!");
610 if (TypeSpecType != TST_unspecified) {
611 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
612 DiagID = diag::err_invalid_decl_spec_combination;
617 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
618 TypeAltiVecBool = true;
622 TypeSpecOwned = false;
623 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
624 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
625 DiagID = diag::err_invalid_vector_decl_spec;
631 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
632 const char *&PrevSpec, unsigned &DiagID) {
633 if (TypeSpecType != TST_unspecified) {
634 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
635 DiagID = diag::err_invalid_vector_decl_spec_combination;
638 TypeAltiVecVector = isAltiVecVector;
643 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
644 const char *&PrevSpec, unsigned &DiagID) {
645 if (!TypeAltiVecVector || TypeAltiVecPixel ||
646 (TypeSpecType != TST_unspecified)) {
647 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
648 DiagID = diag::err_invalid_pixel_decl_spec_combination;
651 TypeAltiVecPixel = isAltiVecPixel;
657 bool DeclSpec::SetTypeSpecError() {
658 TypeSpecType = TST_error;
659 TypeSpecOwned = false;
660 TSTLoc = SourceLocation();
661 TSTNameLoc = SourceLocation();
665 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
666 unsigned &DiagID, const LangOptions &Lang) {
667 // Duplicates turn into warnings pre-C99.
668 if ((TypeQualifiers & T) && !Lang.C99)
669 return BadSpecifier(T, T, PrevSpec, DiagID);
673 default: llvm_unreachable("Unknown type qualifier!");
674 case TQ_const: TQ_constLoc = Loc; break;
675 case TQ_restrict: TQ_restrictLoc = Loc; break;
676 case TQ_volatile: TQ_volatileLoc = Loc; break;
681 bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
683 // 'inline inline' is ok.
684 FS_inline_specified = true;
689 bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
691 // 'virtual virtual' is ok.
692 FS_virtual_specified = true;
697 bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
699 // 'explicit explicit' is ok.
700 FS_explicit_specified = true;
701 FS_explicitLoc = Loc;
705 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
707 if (Friend_specified) {
709 DiagID = diag::ext_duplicate_declspec;
713 Friend_specified = true;
718 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
720 if (isModulePrivateSpecified()) {
721 PrevSpec = "__module_private__";
722 DiagID = diag::ext_duplicate_declspec;
726 ModulePrivateLoc = Loc;
730 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
732 // 'constexpr constexpr' is ok.
733 Constexpr_specified = true;
738 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
740 SourceLocation *ProtoLocs,
741 SourceLocation LAngleLoc) {
743 ProtocolQualifiers = new Decl*[NP];
744 ProtocolLocs = new SourceLocation[NP];
745 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
746 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
747 NumProtocolQualifiers = NP;
748 ProtocolLAngleLoc = LAngleLoc;
751 void DeclSpec::SaveWrittenBuiltinSpecs() {
752 writtenBS.Sign = getTypeSpecSign();
753 writtenBS.Width = getTypeSpecWidth();
754 writtenBS.Type = getTypeSpecType();
755 // Search the list of attributes for the presence of a mode attribute.
756 writtenBS.ModeAttr = false;
757 AttributeList* attrs = getAttributes().getList();
759 if (attrs->getKind() == AttributeList::AT_mode) {
760 writtenBS.ModeAttr = true;
763 attrs = attrs->getNext();
767 void DeclSpec::SaveStorageSpecifierAsWritten() {
768 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
769 // If 'extern' is part of a linkage specification,
770 // then it is not a storage class "as written".
771 StorageClassSpecAsWritten = SCS_unspecified;
773 StorageClassSpecAsWritten = StorageClassSpec;
776 /// Finish - This does final analysis of the declspec, rejecting things like
777 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
778 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
779 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
780 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
781 // Before possibly changing their values, save specs as written.
782 SaveWrittenBuiltinSpecs();
783 SaveStorageSpecifierAsWritten();
785 // Check the type specifier components first.
787 // Validate and finalize AltiVec vector declspec.
788 if (TypeAltiVecVector) {
789 if (TypeAltiVecBool) {
790 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
791 if (TypeSpecSign != TSS_unspecified) {
792 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
793 << getSpecifierName((TSS)TypeSpecSign);
796 // Only char/int are valid with vector bool. (PIM 2.1)
797 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
798 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
799 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
800 << (TypeAltiVecPixel ? "__pixel" :
801 getSpecifierName((TST)TypeSpecType));
804 // Only 'short' is valid with vector bool. (PIM 2.1)
805 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
806 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
807 << getSpecifierName((TSW)TypeSpecWidth);
809 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
810 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
811 (TypeSpecWidth != TSW_unspecified))
812 TypeSpecSign = TSS_unsigned;
815 if (TypeAltiVecPixel) {
816 //TODO: perform validation
817 TypeSpecType = TST_int;
818 TypeSpecSign = TSS_unsigned;
819 TypeSpecWidth = TSW_short;
820 TypeSpecOwned = false;
824 // signed/unsigned are only valid with int/char/wchar_t.
825 if (TypeSpecSign != TSS_unspecified) {
826 if (TypeSpecType == TST_unspecified)
827 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
828 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
829 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
830 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
831 << getSpecifierName((TST)TypeSpecType);
832 // signed double -> double.
833 TypeSpecSign = TSS_unspecified;
837 // Validate the width of the type.
838 switch (TypeSpecWidth) {
839 case TSW_unspecified: break;
840 case TSW_short: // short int
841 case TSW_longlong: // long long int
842 if (TypeSpecType == TST_unspecified)
843 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
844 else if (TypeSpecType != TST_int) {
846 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
847 : diag::err_invalid_longlong_spec)
848 << getSpecifierName((TST)TypeSpecType);
849 TypeSpecType = TST_int;
850 TypeSpecOwned = false;
853 case TSW_long: // long double, long int
854 if (TypeSpecType == TST_unspecified)
855 TypeSpecType = TST_int; // long -> long int.
856 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
857 Diag(D, TSWLoc, diag::err_invalid_long_spec)
858 << getSpecifierName((TST)TypeSpecType);
859 TypeSpecType = TST_int;
860 TypeSpecOwned = false;
865 // TODO: if the implementation does not implement _Complex or _Imaginary,
866 // disallow their use. Need information about the backend.
867 if (TypeSpecComplex != TSC_unspecified) {
868 if (TypeSpecType == TST_unspecified) {
869 Diag(D, TSCLoc, diag::ext_plain_complex)
870 << FixItHint::CreateInsertion(
871 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
873 TypeSpecType = TST_double; // _Complex -> _Complex double.
874 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
875 // Note that this intentionally doesn't include _Complex _Bool.
876 if (!PP.getLangOpts().CPlusPlus)
877 Diag(D, TSTLoc, diag::ext_integer_complex);
878 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
879 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
880 << getSpecifierName((TST)TypeSpecType);
881 TypeSpecComplex = TSC_unspecified;
885 // If no type specifier was provided and we're parsing a language where
886 // the type specifier is not optional, but we got 'auto' as a storage
887 // class specifier, then assume this is an attempt to use C++0x's 'auto'
889 // FIXME: Does Microsoft really support implicit int in C++?
890 if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().MicrosoftExt &&
891 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
892 TypeSpecType = TST_auto;
893 StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
894 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
895 StorageClassSpecLoc = SourceLocation();
897 // Diagnose if we've recovered from an ill-formed 'auto' storage class
898 // specifier in a pre-C++0x dialect of C++.
899 if (!PP.getLangOpts().CPlusPlus0x && TypeSpecType == TST_auto)
900 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
901 if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus0x &&
902 StorageClassSpec == SCS_auto)
903 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
904 << FixItHint::CreateRemoval(StorageClassSpecLoc);
905 if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
906 Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
907 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
908 if (Constexpr_specified)
909 Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
911 // C++ [class.friend]p6:
912 // No storage-class-specifier shall appear in the decl-specifier-seq
913 // of a friend declaration.
914 if (isFriendSpecified() && getStorageClassSpec()) {
915 DeclSpec::SCS SC = getStorageClassSpec();
916 const char *SpecName = getSpecifierName(SC);
918 SourceLocation SCLoc = getStorageClassSpecLoc();
919 SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
921 Diag(D, SCLoc, diag::err_friend_storage_spec)
923 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
925 ClearStorageClassSpecs();
928 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
930 // Okay, now we can infer the real type.
932 // TODO: return "auto function" and other bad things based on the real type.
934 // 'data definition has no type or storage class'?
937 bool DeclSpec::isMissingDeclaratorOk() {
938 TST tst = getTypeSpecType();
939 return isDeclRep(tst) && getRepAsDecl() != 0 &&
940 StorageClassSpec != DeclSpec::SCS_typedef;
943 void UnqualifiedId::clear() {
944 Kind = IK_Identifier;
946 StartLocation = SourceLocation();
947 EndLocation = SourceLocation();
950 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
951 OverloadedOperatorKind Op,
952 SourceLocation SymbolLocations[3]) {
953 Kind = IK_OperatorFunctionId;
954 StartLocation = OperatorLoc;
955 EndLocation = OperatorLoc;
956 OperatorFunctionId.Operator = Op;
957 for (unsigned I = 0; I != 3; ++I) {
958 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
960 if (SymbolLocations[I].isValid())
961 EndLocation = SymbolLocations[I];
965 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
966 const char *&PrevSpec) {
969 if (Specifiers & VS) {
970 PrevSpec = getSpecifierName(VS);
977 default: llvm_unreachable("Unknown specifier!");
978 case VS_Override: VS_overrideLoc = Loc; break;
979 case VS_Final: VS_finalLoc = Loc; break;
985 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
987 default: llvm_unreachable("Unknown specifier");
988 case VS_Override: return "override";
989 case VS_Final: return "final";