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/Sema.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/NestedNameSpecifier.h"
22 #include "clang/AST/TypeLoc.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Basic/LangOptions.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/ErrorHandling.h"
28 using namespace clang;
31 static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
33 return D.Report(Loc, DiagID);
37 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
38 assert(TemplateId && "NULL template-id annotation?");
40 this->TemplateId = TemplateId;
41 StartLocation = TemplateId->TemplateNameLoc;
42 EndLocation = TemplateId->RAngleLoc;
45 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
46 assert(TemplateId && "NULL template-id annotation?");
47 Kind = IK_ConstructorTemplateId;
48 this->TemplateId = TemplateId;
49 StartLocation = TemplateId->TemplateNameLoc;
50 EndLocation = TemplateId->RAngleLoc;
53 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
54 TypeLoc TL, SourceLocation ColonColonLoc) {
55 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
56 if (Range.getBegin().isInvalid())
57 Range.setBegin(TL.getBeginLoc());
58 Range.setEnd(ColonColonLoc);
60 assert(Range == Builder.getSourceRange() &&
61 "NestedNameSpecifierLoc range computation incorrect");
64 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
65 SourceLocation IdentifierLoc,
66 SourceLocation ColonColonLoc) {
67 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
69 if (Range.getBegin().isInvalid())
70 Range.setBegin(IdentifierLoc);
71 Range.setEnd(ColonColonLoc);
73 assert(Range == Builder.getSourceRange() &&
74 "NestedNameSpecifierLoc range computation incorrect");
77 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
78 SourceLocation NamespaceLoc,
79 SourceLocation ColonColonLoc) {
80 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
82 if (Range.getBegin().isInvalid())
83 Range.setBegin(NamespaceLoc);
84 Range.setEnd(ColonColonLoc);
86 assert(Range == Builder.getSourceRange() &&
87 "NestedNameSpecifierLoc range computation incorrect");
90 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
91 SourceLocation AliasLoc,
92 SourceLocation ColonColonLoc) {
93 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
95 if (Range.getBegin().isInvalid())
96 Range.setBegin(AliasLoc);
97 Range.setEnd(ColonColonLoc);
99 assert(Range == Builder.getSourceRange() &&
100 "NestedNameSpecifierLoc range computation incorrect");
103 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
104 SourceLocation ColonColonLoc) {
105 Builder.MakeGlobal(Context, ColonColonLoc);
107 Range = SourceRange(ColonColonLoc);
109 assert(Range == Builder.getSourceRange() &&
110 "NestedNameSpecifierLoc range computation incorrect");
113 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
114 NestedNameSpecifier *Qualifier, SourceRange R) {
115 Builder.MakeTrivial(Context, Qualifier, R);
119 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
121 Range = SourceRange();
126 Range = Other.getSourceRange();
127 Builder.Adopt(Other);
130 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
131 if (!Builder.getRepresentation())
132 return SourceLocation();
133 return Builder.getTemporary().getLocalBeginLoc();
136 NestedNameSpecifierLoc
137 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
138 if (!Builder.getRepresentation())
139 return NestedNameSpecifierLoc();
141 return Builder.getWithLocInContext(Context);
144 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
145 /// "TheDeclarator" is the declarator that this will be added to.
146 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
147 SourceLocation EllipsisLoc,
151 bool RefQualifierIsLvalueRef,
152 SourceLocation RefQualifierLoc,
153 SourceLocation MutableLoc,
154 ExceptionSpecificationType
156 SourceLocation ESpecLoc,
157 ParsedType *Exceptions,
158 SourceRange *ExceptionRanges,
159 unsigned NumExceptions,
161 SourceLocation LocalRangeBegin,
162 SourceLocation LocalRangeEnd,
163 Declarator &TheDeclarator,
164 ParsedType TrailingReturnType) {
167 I.Loc = LocalRangeBegin;
168 I.EndLoc = LocalRangeEnd;
170 I.Fun.hasPrototype = hasProto;
171 I.Fun.isVariadic = isVariadic;
172 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
173 I.Fun.DeleteArgInfo = false;
174 I.Fun.TypeQuals = TypeQuals;
175 I.Fun.NumArgs = NumArgs;
177 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
178 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
179 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
180 I.Fun.ExceptionSpecType = ESpecType;
181 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
182 I.Fun.NumExceptions = 0;
183 I.Fun.Exceptions = 0;
184 I.Fun.NoexceptExpr = 0;
185 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
187 // new[] an argument array if needed.
189 // If the 'InlineParams' in Declarator is unused and big enough, put our
190 // parameter list there (in an effort to avoid new/delete traffic). If it
191 // is already used (consider a function returning a function pointer) or too
192 // small (function taking too many arguments), go to the heap.
193 if (!TheDeclarator.InlineParamsUsed &&
194 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
195 I.Fun.ArgInfo = TheDeclarator.InlineParams;
196 I.Fun.DeleteArgInfo = false;
197 TheDeclarator.InlineParamsUsed = true;
199 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
200 I.Fun.DeleteArgInfo = true;
202 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
205 // Check what exception specification information we should actually store.
207 default: break; // By default, save nothing.
209 // new[] an exception array if needed
211 I.Fun.NumExceptions = NumExceptions;
212 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
213 for (unsigned i = 0; i != NumExceptions; ++i) {
214 I.Fun.Exceptions[i].Ty = Exceptions[i];
215 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
220 case EST_ComputedNoexcept:
221 I.Fun.NoexceptExpr = NoexceptExpr;
227 bool Declarator::isDeclarationOfFunction() const {
228 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
229 switch (DeclTypeInfo[i].Kind) {
230 case DeclaratorChunk::Function:
232 case DeclaratorChunk::Paren:
234 case DeclaratorChunk::Pointer:
235 case DeclaratorChunk::Reference:
236 case DeclaratorChunk::Array:
237 case DeclaratorChunk::BlockPointer:
238 case DeclaratorChunk::MemberPointer:
241 llvm_unreachable("Invalid type chunk");
245 switch (DS.getTypeSpecType()) {
264 case TST_unknown_anytype:
265 case TST_unspecified:
272 if (Expr *E = DS.getRepAsExpr())
273 return E->getType()->isFunctionType();
276 case TST_underlyingType:
278 case TST_typeofType: {
279 QualType QT = DS.getRepAsType().get();
283 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
289 return QT->isFunctionType();
296 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
297 /// declaration specifier includes.
299 unsigned DeclSpec::getParsedSpecifiers() const {
301 if (StorageClassSpec != SCS_unspecified ||
302 SCS_thread_specified)
303 Res |= PQ_StorageClassSpecifier;
305 if (TypeQualifiers != TQ_unspecified)
306 Res |= PQ_TypeQualifier;
308 if (hasTypeSpecifier())
309 Res |= PQ_TypeSpecifier;
311 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
312 Res |= PQ_FunctionSpecifier;
316 template <class T> static bool BadSpecifier(T TNew, T TPrev,
317 const char *&PrevSpec,
319 PrevSpec = DeclSpec::getSpecifierName(TPrev);
320 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
321 : diag::err_invalid_decl_spec_combination);
325 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
327 case DeclSpec::SCS_unspecified: return "unspecified";
328 case DeclSpec::SCS_typedef: return "typedef";
329 case DeclSpec::SCS_extern: return "extern";
330 case DeclSpec::SCS_static: return "static";
331 case DeclSpec::SCS_auto: return "auto";
332 case DeclSpec::SCS_register: return "register";
333 case DeclSpec::SCS_private_extern: return "__private_extern__";
334 case DeclSpec::SCS_mutable: return "mutable";
336 llvm_unreachable("Unknown typespec!");
339 const char *DeclSpec::getSpecifierName(TSW W) {
341 case TSW_unspecified: return "unspecified";
342 case TSW_short: return "short";
343 case TSW_long: return "long";
344 case TSW_longlong: return "long long";
346 llvm_unreachable("Unknown typespec!");
349 const char *DeclSpec::getSpecifierName(TSC C) {
351 case TSC_unspecified: return "unspecified";
352 case TSC_imaginary: return "imaginary";
353 case TSC_complex: return "complex";
355 llvm_unreachable("Unknown typespec!");
359 const char *DeclSpec::getSpecifierName(TSS S) {
361 case TSS_unspecified: return "unspecified";
362 case TSS_signed: return "signed";
363 case TSS_unsigned: return "unsigned";
365 llvm_unreachable("Unknown typespec!");
368 const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
370 case DeclSpec::TST_unspecified: return "unspecified";
371 case DeclSpec::TST_void: return "void";
372 case DeclSpec::TST_char: return "char";
373 case DeclSpec::TST_wchar: return "wchar_t";
374 case DeclSpec::TST_char16: return "char16_t";
375 case DeclSpec::TST_char32: return "char32_t";
376 case DeclSpec::TST_int: return "int";
377 case DeclSpec::TST_half: return "half";
378 case DeclSpec::TST_float: return "float";
379 case DeclSpec::TST_double: return "double";
380 case DeclSpec::TST_bool: return "_Bool";
381 case DeclSpec::TST_decimal32: return "_Decimal32";
382 case DeclSpec::TST_decimal64: return "_Decimal64";
383 case DeclSpec::TST_decimal128: return "_Decimal128";
384 case DeclSpec::TST_enum: return "enum";
385 case DeclSpec::TST_class: return "class";
386 case DeclSpec::TST_union: return "union";
387 case DeclSpec::TST_struct: return "struct";
388 case DeclSpec::TST_typename: return "type-name";
389 case DeclSpec::TST_typeofType:
390 case DeclSpec::TST_typeofExpr: return "typeof";
391 case DeclSpec::TST_auto: return "auto";
392 case DeclSpec::TST_decltype: return "(decltype)";
393 case DeclSpec::TST_underlyingType: return "__underlying_type";
394 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
395 case DeclSpec::TST_atomic: return "_Atomic";
396 case DeclSpec::TST_error: return "(error)";
398 llvm_unreachable("Unknown typespec!");
401 const char *DeclSpec::getSpecifierName(TQ T) {
403 case DeclSpec::TQ_unspecified: return "unspecified";
404 case DeclSpec::TQ_const: return "const";
405 case DeclSpec::TQ_restrict: return "restrict";
406 case DeclSpec::TQ_volatile: return "volatile";
408 llvm_unreachable("Unknown typespec!");
411 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
412 const char *&PrevSpec,
414 // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class
415 // specifiers are not supported."
416 // It seems sensible to prohibit private_extern too
417 // The cl_clang_storage_class_specifiers extension enables support for
418 // these storage-class specifiers.
419 if (S.getLangOptions().OpenCL &&
420 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
423 case SCS_private_extern:
427 DiagID = diag::err_not_opencl_storage_class_specifier;
428 PrevSpec = getSpecifierName(SC);
435 if (StorageClassSpec != SCS_unspecified) {
436 // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
437 bool isInvalid = true;
438 if (TypeSpecType == TST_unspecified && S.getLangOptions().CPlusPlus) {
440 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
441 if (StorageClassSpec == SCS_auto) {
442 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
444 assert(!isInvalid && "auto SCS -> TST recovery failed");
448 // Changing storage class is allowed only if the previous one
449 // was the 'extern' that is part of a linkage specification and
450 // the new storage class is 'typedef'.
452 !(SCS_extern_in_linkage_spec &&
453 StorageClassSpec == SCS_extern &&
455 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
457 StorageClassSpec = SC;
458 StorageClassSpecLoc = Loc;
459 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
463 bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
464 const char *&PrevSpec,
466 if (SCS_thread_specified) {
467 PrevSpec = "__thread";
468 DiagID = diag::ext_duplicate_declspec;
471 SCS_thread_specified = true;
476 /// These methods set the specified attribute of the DeclSpec, but return true
477 /// and ignore the request if invalid (e.g. "extern" then "auto" is
479 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
480 const char *&PrevSpec,
482 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
483 // for 'long long' we will keep the source location of the first 'long'.
484 if (TypeSpecWidth == TSW_unspecified)
486 // Allow turning long -> long long.
487 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
488 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
490 if (TypeAltiVecVector && !TypeAltiVecBool &&
491 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
492 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
493 DiagID = diag::warn_vector_long_decl_spec_combination;
499 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
500 const char *&PrevSpec,
502 if (TypeSpecComplex != TSC_unspecified)
503 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
509 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
510 const char *&PrevSpec,
512 if (TypeSpecSign != TSS_unspecified)
513 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
519 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
520 const char *&PrevSpec,
523 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
526 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
527 SourceLocation TagNameLoc,
528 const char *&PrevSpec,
531 assert(isTypeRep(T) && "T does not store a type");
532 assert(Rep && "no type provided!");
533 if (TypeSpecType != TST_unspecified) {
534 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
535 DiagID = diag::err_invalid_decl_spec_combination;
541 TSTNameLoc = TagNameLoc;
542 TypeSpecOwned = false;
546 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
547 const char *&PrevSpec,
550 assert(isExprRep(T) && "T does not store an expr");
551 assert(Rep && "no expression provided!");
552 if (TypeSpecType != TST_unspecified) {
553 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
554 DiagID = diag::err_invalid_decl_spec_combination;
561 TypeSpecOwned = false;
565 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
566 const char *&PrevSpec,
568 Decl *Rep, bool Owned) {
569 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
572 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
573 SourceLocation TagNameLoc,
574 const char *&PrevSpec,
576 Decl *Rep, bool Owned) {
577 assert(isDeclRep(T) && "T does not store a decl");
578 // Unlike the other cases, we don't assert that we actually get a decl.
580 if (TypeSpecType != TST_unspecified) {
581 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
582 DiagID = diag::err_invalid_decl_spec_combination;
588 TSTNameLoc = TagNameLoc;
589 TypeSpecOwned = Owned;
593 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
594 const char *&PrevSpec,
596 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
597 "rep required for these type-spec kinds!");
598 if (TypeSpecType != TST_unspecified) {
599 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
600 DiagID = diag::err_invalid_decl_spec_combination;
605 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
606 TypeAltiVecBool = true;
610 TypeSpecOwned = false;
611 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
612 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
613 DiagID = diag::err_invalid_vector_decl_spec;
619 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
620 const char *&PrevSpec, unsigned &DiagID) {
621 if (TypeSpecType != TST_unspecified) {
622 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
623 DiagID = diag::err_invalid_vector_decl_spec_combination;
626 TypeAltiVecVector = isAltiVecVector;
631 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
632 const char *&PrevSpec, unsigned &DiagID) {
633 if (!TypeAltiVecVector || TypeAltiVecPixel ||
634 (TypeSpecType != TST_unspecified)) {
635 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
636 DiagID = diag::err_invalid_pixel_decl_spec_combination;
639 TypeAltiVecPixel = isAltiVecPixel;
645 bool DeclSpec::SetTypeSpecError() {
646 TypeSpecType = TST_error;
647 TypeSpecOwned = false;
648 TSTLoc = SourceLocation();
649 TSTNameLoc = SourceLocation();
653 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
654 unsigned &DiagID, const LangOptions &Lang) {
655 // Duplicates turn into warnings pre-C99.
656 if ((TypeQualifiers & T) && !Lang.C99)
657 return BadSpecifier(T, T, PrevSpec, DiagID);
661 default: llvm_unreachable("Unknown type qualifier!");
662 case TQ_const: TQ_constLoc = Loc; break;
663 case TQ_restrict: TQ_restrictLoc = Loc; break;
664 case TQ_volatile: TQ_volatileLoc = Loc; break;
669 bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
671 // 'inline inline' is ok.
672 FS_inline_specified = true;
677 bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
679 // 'virtual virtual' is ok.
680 FS_virtual_specified = true;
685 bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
687 // 'explicit explicit' is ok.
688 FS_explicit_specified = true;
689 FS_explicitLoc = Loc;
693 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
695 if (Friend_specified) {
697 DiagID = diag::ext_duplicate_declspec;
701 Friend_specified = true;
706 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
708 if (isModulePrivateSpecified()) {
709 PrevSpec = "__module_private__";
710 DiagID = diag::ext_duplicate_declspec;
714 ModulePrivateLoc = Loc;
718 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
720 // 'constexpr constexpr' is ok.
721 Constexpr_specified = true;
726 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
728 SourceLocation *ProtoLocs,
729 SourceLocation LAngleLoc) {
731 ProtocolQualifiers = new Decl*[NP];
732 ProtocolLocs = new SourceLocation[NP];
733 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
734 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
735 NumProtocolQualifiers = NP;
736 ProtocolLAngleLoc = LAngleLoc;
739 void DeclSpec::SaveWrittenBuiltinSpecs() {
740 writtenBS.Sign = getTypeSpecSign();
741 writtenBS.Width = getTypeSpecWidth();
742 writtenBS.Type = getTypeSpecType();
743 // Search the list of attributes for the presence of a mode attribute.
744 writtenBS.ModeAttr = false;
745 AttributeList* attrs = getAttributes().getList();
747 if (attrs->getKind() == AttributeList::AT_mode) {
748 writtenBS.ModeAttr = true;
751 attrs = attrs->getNext();
755 void DeclSpec::SaveStorageSpecifierAsWritten() {
756 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
757 // If 'extern' is part of a linkage specification,
758 // then it is not a storage class "as written".
759 StorageClassSpecAsWritten = SCS_unspecified;
761 StorageClassSpecAsWritten = StorageClassSpec;
764 /// Finish - This does final analysis of the declspec, rejecting things like
765 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
766 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
767 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
768 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
769 // Before possibly changing their values, save specs as written.
770 SaveWrittenBuiltinSpecs();
771 SaveStorageSpecifierAsWritten();
773 // Check the type specifier components first.
775 // Validate and finalize AltiVec vector declspec.
776 if (TypeAltiVecVector) {
777 if (TypeAltiVecBool) {
778 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
779 if (TypeSpecSign != TSS_unspecified) {
780 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
781 << getSpecifierName((TSS)TypeSpecSign);
784 // Only char/int are valid with vector bool. (PIM 2.1)
785 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
786 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
787 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
788 << (TypeAltiVecPixel ? "__pixel" :
789 getSpecifierName((TST)TypeSpecType));
792 // Only 'short' is valid with vector bool. (PIM 2.1)
793 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
794 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
795 << getSpecifierName((TSW)TypeSpecWidth);
797 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
798 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
799 (TypeSpecWidth != TSW_unspecified))
800 TypeSpecSign = TSS_unsigned;
803 if (TypeAltiVecPixel) {
804 //TODO: perform validation
805 TypeSpecType = TST_int;
806 TypeSpecSign = TSS_unsigned;
807 TypeSpecWidth = TSW_short;
808 TypeSpecOwned = false;
812 // signed/unsigned are only valid with int/char/wchar_t.
813 if (TypeSpecSign != TSS_unspecified) {
814 if (TypeSpecType == TST_unspecified)
815 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
816 else if (TypeSpecType != TST_int &&
817 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
818 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
819 << getSpecifierName((TST)TypeSpecType);
820 // signed double -> double.
821 TypeSpecSign = TSS_unspecified;
825 // Validate the width of the type.
826 switch (TypeSpecWidth) {
827 case TSW_unspecified: break;
828 case TSW_short: // short int
829 case TSW_longlong: // long long int
830 if (TypeSpecType == TST_unspecified)
831 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
832 else if (TypeSpecType != TST_int) {
834 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
835 : diag::err_invalid_longlong_spec)
836 << getSpecifierName((TST)TypeSpecType);
837 TypeSpecType = TST_int;
838 TypeSpecOwned = false;
841 case TSW_long: // long double, long int
842 if (TypeSpecType == TST_unspecified)
843 TypeSpecType = TST_int; // long -> long int.
844 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
845 Diag(D, TSWLoc, diag::err_invalid_long_spec)
846 << getSpecifierName((TST)TypeSpecType);
847 TypeSpecType = TST_int;
848 TypeSpecOwned = false;
853 // TODO: if the implementation does not implement _Complex or _Imaginary,
854 // disallow their use. Need information about the backend.
855 if (TypeSpecComplex != TSC_unspecified) {
856 if (TypeSpecType == TST_unspecified) {
857 Diag(D, TSCLoc, diag::ext_plain_complex)
858 << FixItHint::CreateInsertion(
859 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
861 TypeSpecType = TST_double; // _Complex -> _Complex double.
862 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
863 // Note that this intentionally doesn't include _Complex _Bool.
864 Diag(D, TSTLoc, diag::ext_integer_complex);
865 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
866 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
867 << getSpecifierName((TST)TypeSpecType);
868 TypeSpecComplex = TSC_unspecified;
872 // If no type specifier was provided and we're parsing a language where
873 // the type specifier is not optional, but we got 'auto' as a storage
874 // class specifier, then assume this is an attempt to use C++0x's 'auto'
876 // FIXME: Does Microsoft really support implicit int in C++?
877 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().MicrosoftExt &&
878 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
879 TypeSpecType = TST_auto;
880 StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
881 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
882 StorageClassSpecLoc = SourceLocation();
884 // Diagnose if we've recovered from an ill-formed 'auto' storage class
885 // specifier in a pre-C++0x dialect of C++.
886 if (!PP.getLangOptions().CPlusPlus0x && TypeSpecType == TST_auto)
887 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
888 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().CPlusPlus0x &&
889 StorageClassSpec == SCS_auto)
890 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
891 << FixItHint::CreateRemoval(StorageClassSpecLoc);
893 // C++ [class.friend]p6:
894 // No storage-class-specifier shall appear in the decl-specifier-seq
895 // of a friend declaration.
896 if (isFriendSpecified() && getStorageClassSpec()) {
897 DeclSpec::SCS SC = getStorageClassSpec();
898 const char *SpecName = getSpecifierName(SC);
900 SourceLocation SCLoc = getStorageClassSpecLoc();
901 SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
903 Diag(D, SCLoc, diag::err_friend_storage_spec)
905 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
907 ClearStorageClassSpecs();
910 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
912 // Okay, now we can infer the real type.
914 // TODO: return "auto function" and other bad things based on the real type.
916 // 'data definition has no type or storage class'?
919 bool DeclSpec::isMissingDeclaratorOk() {
920 TST tst = getTypeSpecType();
921 return isDeclRep(tst) && getRepAsDecl() != 0 &&
922 StorageClassSpec != DeclSpec::SCS_typedef;
925 void UnqualifiedId::clear() {
926 Kind = IK_Identifier;
928 StartLocation = SourceLocation();
929 EndLocation = SourceLocation();
932 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
933 OverloadedOperatorKind Op,
934 SourceLocation SymbolLocations[3]) {
935 Kind = IK_OperatorFunctionId;
936 StartLocation = OperatorLoc;
937 EndLocation = OperatorLoc;
938 OperatorFunctionId.Operator = Op;
939 for (unsigned I = 0; I != 3; ++I) {
940 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
942 if (SymbolLocations[I].isValid())
943 EndLocation = SymbolLocations[I];
947 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
948 const char *&PrevSpec) {
951 if (Specifiers & VS) {
952 PrevSpec = getSpecifierName(VS);
959 default: llvm_unreachable("Unknown specifier!");
960 case VS_Override: VS_overrideLoc = Loc; break;
961 case VS_Final: VS_finalLoc = Loc; break;
967 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
969 default: llvm_unreachable("Unknown specifier");
970 case VS_Override: return "override";
971 case VS_Final: return "final";