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 SourceLocation LocalRangeBegin,
166 SourceLocation LocalRangeEnd,
167 Declarator &TheDeclarator,
168 ParsedType TrailingReturnType) {
171 I.Loc = LocalRangeBegin;
172 I.EndLoc = LocalRangeEnd;
174 I.Fun.hasPrototype = hasProto;
175 I.Fun.isVariadic = isVariadic;
176 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
177 I.Fun.DeleteArgInfo = false;
178 I.Fun.TypeQuals = TypeQuals;
179 I.Fun.NumArgs = NumArgs;
181 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
182 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
183 I.Fun.ConstQualifierLoc = ConstQualifierLoc.getRawEncoding();
184 I.Fun.VolatileQualifierLoc = VolatileQualifierLoc.getRawEncoding();
185 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
186 I.Fun.ExceptionSpecType = ESpecType;
187 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
188 I.Fun.NumExceptions = 0;
189 I.Fun.Exceptions = 0;
190 I.Fun.NoexceptExpr = 0;
191 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
193 // new[] an argument array if needed.
195 // If the 'InlineParams' in Declarator is unused and big enough, put our
196 // parameter list there (in an effort to avoid new/delete traffic). If it
197 // is already used (consider a function returning a function pointer) or too
198 // small (function taking too many arguments), go to the heap.
199 if (!TheDeclarator.InlineParamsUsed &&
200 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
201 I.Fun.ArgInfo = TheDeclarator.InlineParams;
202 I.Fun.DeleteArgInfo = false;
203 TheDeclarator.InlineParamsUsed = true;
205 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
206 I.Fun.DeleteArgInfo = true;
208 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
211 // Check what exception specification information we should actually store.
213 default: break; // By default, save nothing.
215 // new[] an exception array if needed
217 I.Fun.NumExceptions = NumExceptions;
218 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
219 for (unsigned i = 0; i != NumExceptions; ++i) {
220 I.Fun.Exceptions[i].Ty = Exceptions[i];
221 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
226 case EST_ComputedNoexcept:
227 I.Fun.NoexceptExpr = NoexceptExpr;
233 bool Declarator::isDeclarationOfFunction() const {
234 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
235 switch (DeclTypeInfo[i].Kind) {
236 case DeclaratorChunk::Function:
238 case DeclaratorChunk::Paren:
240 case DeclaratorChunk::Pointer:
241 case DeclaratorChunk::Reference:
242 case DeclaratorChunk::Array:
243 case DeclaratorChunk::BlockPointer:
244 case DeclaratorChunk::MemberPointer:
247 llvm_unreachable("Invalid type chunk");
250 switch (DS.getTypeSpecType()) {
270 case TST_unknown_anytype:
271 case TST_unspecified:
278 if (Expr *E = DS.getRepAsExpr())
279 return E->getType()->isFunctionType();
282 case TST_underlyingType:
284 case TST_typeofType: {
285 QualType QT = DS.getRepAsType().get();
289 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
295 return QT->isFunctionType();
299 llvm_unreachable("Invalid TypeSpecType!");
302 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
303 /// declaration specifier includes.
305 unsigned DeclSpec::getParsedSpecifiers() const {
307 if (StorageClassSpec != SCS_unspecified ||
308 SCS_thread_specified)
309 Res |= PQ_StorageClassSpecifier;
311 if (TypeQualifiers != TQ_unspecified)
312 Res |= PQ_TypeQualifier;
314 if (hasTypeSpecifier())
315 Res |= PQ_TypeSpecifier;
317 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
318 Res |= PQ_FunctionSpecifier;
322 template <class T> static bool BadSpecifier(T TNew, T TPrev,
323 const char *&PrevSpec,
325 PrevSpec = DeclSpec::getSpecifierName(TPrev);
326 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
327 : diag::err_invalid_decl_spec_combination);
331 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
333 case DeclSpec::SCS_unspecified: return "unspecified";
334 case DeclSpec::SCS_typedef: return "typedef";
335 case DeclSpec::SCS_extern: return "extern";
336 case DeclSpec::SCS_static: return "static";
337 case DeclSpec::SCS_auto: return "auto";
338 case DeclSpec::SCS_register: return "register";
339 case DeclSpec::SCS_private_extern: return "__private_extern__";
340 case DeclSpec::SCS_mutable: return "mutable";
342 llvm_unreachable("Unknown typespec!");
345 const char *DeclSpec::getSpecifierName(TSW W) {
347 case TSW_unspecified: return "unspecified";
348 case TSW_short: return "short";
349 case TSW_long: return "long";
350 case TSW_longlong: return "long long";
352 llvm_unreachable("Unknown typespec!");
355 const char *DeclSpec::getSpecifierName(TSC C) {
357 case TSC_unspecified: return "unspecified";
358 case TSC_imaginary: return "imaginary";
359 case TSC_complex: return "complex";
361 llvm_unreachable("Unknown typespec!");
365 const char *DeclSpec::getSpecifierName(TSS S) {
367 case TSS_unspecified: return "unspecified";
368 case TSS_signed: return "signed";
369 case TSS_unsigned: return "unsigned";
371 llvm_unreachable("Unknown typespec!");
374 const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
376 case DeclSpec::TST_unspecified: return "unspecified";
377 case DeclSpec::TST_void: return "void";
378 case DeclSpec::TST_char: return "char";
379 case DeclSpec::TST_wchar: return "wchar_t";
380 case DeclSpec::TST_char16: return "char16_t";
381 case DeclSpec::TST_char32: return "char32_t";
382 case DeclSpec::TST_int: return "int";
383 case DeclSpec::TST_int128: return "__int128";
384 case DeclSpec::TST_half: return "half";
385 case DeclSpec::TST_float: return "float";
386 case DeclSpec::TST_double: return "double";
387 case DeclSpec::TST_bool: return "_Bool";
388 case DeclSpec::TST_decimal32: return "_Decimal32";
389 case DeclSpec::TST_decimal64: return "_Decimal64";
390 case DeclSpec::TST_decimal128: return "_Decimal128";
391 case DeclSpec::TST_enum: return "enum";
392 case DeclSpec::TST_class: return "class";
393 case DeclSpec::TST_union: return "union";
394 case DeclSpec::TST_struct: return "struct";
395 case DeclSpec::TST_typename: return "type-name";
396 case DeclSpec::TST_typeofType:
397 case DeclSpec::TST_typeofExpr: return "typeof";
398 case DeclSpec::TST_auto: return "auto";
399 case DeclSpec::TST_decltype: return "(decltype)";
400 case DeclSpec::TST_underlyingType: return "__underlying_type";
401 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
402 case DeclSpec::TST_atomic: return "_Atomic";
403 case DeclSpec::TST_error: return "(error)";
405 llvm_unreachable("Unknown typespec!");
408 const char *DeclSpec::getSpecifierName(TQ T) {
410 case DeclSpec::TQ_unspecified: return "unspecified";
411 case DeclSpec::TQ_const: return "const";
412 case DeclSpec::TQ_restrict: return "restrict";
413 case DeclSpec::TQ_volatile: return "volatile";
415 llvm_unreachable("Unknown typespec!");
418 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
419 const char *&PrevSpec,
421 // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class
422 // specifiers are not supported."
423 // It seems sensible to prohibit private_extern too
424 // The cl_clang_storage_class_specifiers extension enables support for
425 // these storage-class specifiers.
426 if (S.getLangOpts().OpenCL &&
427 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
430 case SCS_private_extern:
434 DiagID = diag::err_not_opencl_storage_class_specifier;
435 PrevSpec = getSpecifierName(SC);
442 if (StorageClassSpec != SCS_unspecified) {
443 // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
444 bool isInvalid = true;
445 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
447 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
448 if (StorageClassSpec == SCS_auto) {
449 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
451 assert(!isInvalid && "auto SCS -> TST recovery failed");
455 // Changing storage class is allowed only if the previous one
456 // was the 'extern' that is part of a linkage specification and
457 // the new storage class is 'typedef'.
459 !(SCS_extern_in_linkage_spec &&
460 StorageClassSpec == SCS_extern &&
462 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
464 StorageClassSpec = SC;
465 StorageClassSpecLoc = Loc;
466 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
470 bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
471 const char *&PrevSpec,
473 if (SCS_thread_specified) {
474 PrevSpec = "__thread";
475 DiagID = diag::ext_duplicate_declspec;
478 SCS_thread_specified = true;
483 /// These methods set the specified attribute of the DeclSpec, but return true
484 /// and ignore the request if invalid (e.g. "extern" then "auto" is
486 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
487 const char *&PrevSpec,
489 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
490 // for 'long long' we will keep the source location of the first 'long'.
491 if (TypeSpecWidth == TSW_unspecified)
493 // Allow turning long -> long long.
494 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
495 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
497 if (TypeAltiVecVector && !TypeAltiVecBool &&
498 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
499 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
500 DiagID = diag::warn_vector_long_decl_spec_combination;
506 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
507 const char *&PrevSpec,
509 if (TypeSpecComplex != TSC_unspecified)
510 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
516 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
517 const char *&PrevSpec,
519 if (TypeSpecSign != TSS_unspecified)
520 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
526 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
527 const char *&PrevSpec,
530 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
533 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
534 SourceLocation TagNameLoc,
535 const char *&PrevSpec,
538 assert(isTypeRep(T) && "T does not store a type");
539 assert(Rep && "no type provided!");
540 if (TypeSpecType != TST_unspecified) {
541 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
542 DiagID = diag::err_invalid_decl_spec_combination;
548 TSTNameLoc = TagNameLoc;
549 TypeSpecOwned = false;
553 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
554 const char *&PrevSpec,
557 assert(isExprRep(T) && "T does not store an expr");
558 assert(Rep && "no expression provided!");
559 if (TypeSpecType != TST_unspecified) {
560 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
561 DiagID = diag::err_invalid_decl_spec_combination;
568 TypeSpecOwned = false;
572 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
573 const char *&PrevSpec,
575 Decl *Rep, bool Owned) {
576 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
579 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
580 SourceLocation TagNameLoc,
581 const char *&PrevSpec,
583 Decl *Rep, bool Owned) {
584 assert(isDeclRep(T) && "T does not store a decl");
585 // Unlike the other cases, we don't assert that we actually get a decl.
587 if (TypeSpecType != TST_unspecified) {
588 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
589 DiagID = diag::err_invalid_decl_spec_combination;
595 TSTNameLoc = TagNameLoc;
596 TypeSpecOwned = Owned;
600 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
601 const char *&PrevSpec,
603 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
604 "rep required for these type-spec kinds!");
605 if (TypeSpecType != TST_unspecified) {
606 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
607 DiagID = diag::err_invalid_decl_spec_combination;
612 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
613 TypeAltiVecBool = true;
617 TypeSpecOwned = false;
618 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
619 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
620 DiagID = diag::err_invalid_vector_decl_spec;
626 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
627 const char *&PrevSpec, unsigned &DiagID) {
628 if (TypeSpecType != TST_unspecified) {
629 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
630 DiagID = diag::err_invalid_vector_decl_spec_combination;
633 TypeAltiVecVector = isAltiVecVector;
638 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
639 const char *&PrevSpec, unsigned &DiagID) {
640 if (!TypeAltiVecVector || TypeAltiVecPixel ||
641 (TypeSpecType != TST_unspecified)) {
642 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
643 DiagID = diag::err_invalid_pixel_decl_spec_combination;
646 TypeAltiVecPixel = isAltiVecPixel;
652 bool DeclSpec::SetTypeSpecError() {
653 TypeSpecType = TST_error;
654 TypeSpecOwned = false;
655 TSTLoc = SourceLocation();
656 TSTNameLoc = SourceLocation();
660 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
661 unsigned &DiagID, const LangOptions &Lang) {
662 // Duplicates turn into warnings pre-C99.
663 if ((TypeQualifiers & T) && !Lang.C99)
664 return BadSpecifier(T, T, PrevSpec, DiagID);
668 default: llvm_unreachable("Unknown type qualifier!");
669 case TQ_const: TQ_constLoc = Loc; break;
670 case TQ_restrict: TQ_restrictLoc = Loc; break;
671 case TQ_volatile: TQ_volatileLoc = Loc; break;
676 bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
678 // 'inline inline' is ok.
679 FS_inline_specified = true;
684 bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
686 // 'virtual virtual' is ok.
687 FS_virtual_specified = true;
692 bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
694 // 'explicit explicit' is ok.
695 FS_explicit_specified = true;
696 FS_explicitLoc = Loc;
700 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
702 if (Friend_specified) {
704 DiagID = diag::ext_duplicate_declspec;
708 Friend_specified = true;
713 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
715 if (isModulePrivateSpecified()) {
716 PrevSpec = "__module_private__";
717 DiagID = diag::ext_duplicate_declspec;
721 ModulePrivateLoc = Loc;
725 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
727 // 'constexpr constexpr' is ok.
728 Constexpr_specified = true;
733 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
735 SourceLocation *ProtoLocs,
736 SourceLocation LAngleLoc) {
738 ProtocolQualifiers = new Decl*[NP];
739 ProtocolLocs = new SourceLocation[NP];
740 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
741 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
742 NumProtocolQualifiers = NP;
743 ProtocolLAngleLoc = LAngleLoc;
746 void DeclSpec::SaveWrittenBuiltinSpecs() {
747 writtenBS.Sign = getTypeSpecSign();
748 writtenBS.Width = getTypeSpecWidth();
749 writtenBS.Type = getTypeSpecType();
750 // Search the list of attributes for the presence of a mode attribute.
751 writtenBS.ModeAttr = false;
752 AttributeList* attrs = getAttributes().getList();
754 if (attrs->getKind() == AttributeList::AT_mode) {
755 writtenBS.ModeAttr = true;
758 attrs = attrs->getNext();
762 void DeclSpec::SaveStorageSpecifierAsWritten() {
763 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
764 // If 'extern' is part of a linkage specification,
765 // then it is not a storage class "as written".
766 StorageClassSpecAsWritten = SCS_unspecified;
768 StorageClassSpecAsWritten = StorageClassSpec;
771 /// Finish - This does final analysis of the declspec, rejecting things like
772 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
773 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
774 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
775 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
776 // Before possibly changing their values, save specs as written.
777 SaveWrittenBuiltinSpecs();
778 SaveStorageSpecifierAsWritten();
780 // Check the type specifier components first.
782 // Validate and finalize AltiVec vector declspec.
783 if (TypeAltiVecVector) {
784 if (TypeAltiVecBool) {
785 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
786 if (TypeSpecSign != TSS_unspecified) {
787 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
788 << getSpecifierName((TSS)TypeSpecSign);
791 // Only char/int are valid with vector bool. (PIM 2.1)
792 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
793 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
794 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
795 << (TypeAltiVecPixel ? "__pixel" :
796 getSpecifierName((TST)TypeSpecType));
799 // Only 'short' is valid with vector bool. (PIM 2.1)
800 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
801 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
802 << getSpecifierName((TSW)TypeSpecWidth);
804 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
805 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
806 (TypeSpecWidth != TSW_unspecified))
807 TypeSpecSign = TSS_unsigned;
810 if (TypeAltiVecPixel) {
811 //TODO: perform validation
812 TypeSpecType = TST_int;
813 TypeSpecSign = TSS_unsigned;
814 TypeSpecWidth = TSW_short;
815 TypeSpecOwned = false;
819 // signed/unsigned are only valid with int/char/wchar_t.
820 if (TypeSpecSign != TSS_unspecified) {
821 if (TypeSpecType == TST_unspecified)
822 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
823 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
824 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
825 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
826 << getSpecifierName((TST)TypeSpecType);
827 // signed double -> double.
828 TypeSpecSign = TSS_unspecified;
832 // Validate the width of the type.
833 switch (TypeSpecWidth) {
834 case TSW_unspecified: break;
835 case TSW_short: // short int
836 case TSW_longlong: // long long int
837 if (TypeSpecType == TST_unspecified)
838 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
839 else if (TypeSpecType != TST_int) {
841 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
842 : diag::err_invalid_longlong_spec)
843 << getSpecifierName((TST)TypeSpecType);
844 TypeSpecType = TST_int;
845 TypeSpecOwned = false;
848 case TSW_long: // long double, long int
849 if (TypeSpecType == TST_unspecified)
850 TypeSpecType = TST_int; // long -> long int.
851 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
852 Diag(D, TSWLoc, diag::err_invalid_long_spec)
853 << getSpecifierName((TST)TypeSpecType);
854 TypeSpecType = TST_int;
855 TypeSpecOwned = false;
860 // TODO: if the implementation does not implement _Complex or _Imaginary,
861 // disallow their use. Need information about the backend.
862 if (TypeSpecComplex != TSC_unspecified) {
863 if (TypeSpecType == TST_unspecified) {
864 Diag(D, TSCLoc, diag::ext_plain_complex)
865 << FixItHint::CreateInsertion(
866 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
868 TypeSpecType = TST_double; // _Complex -> _Complex double.
869 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
870 // Note that this intentionally doesn't include _Complex _Bool.
871 if (!PP.getLangOpts().CPlusPlus)
872 Diag(D, TSTLoc, diag::ext_integer_complex);
873 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
874 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
875 << getSpecifierName((TST)TypeSpecType);
876 TypeSpecComplex = TSC_unspecified;
880 // If no type specifier was provided and we're parsing a language where
881 // the type specifier is not optional, but we got 'auto' as a storage
882 // class specifier, then assume this is an attempt to use C++0x's 'auto'
884 // FIXME: Does Microsoft really support implicit int in C++?
885 if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().MicrosoftExt &&
886 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
887 TypeSpecType = TST_auto;
888 StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
889 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
890 StorageClassSpecLoc = SourceLocation();
892 // Diagnose if we've recovered from an ill-formed 'auto' storage class
893 // specifier in a pre-C++0x dialect of C++.
894 if (!PP.getLangOpts().CPlusPlus0x && TypeSpecType == TST_auto)
895 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
896 if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus0x &&
897 StorageClassSpec == SCS_auto)
898 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
899 << FixItHint::CreateRemoval(StorageClassSpecLoc);
900 if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
901 Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
902 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
903 if (Constexpr_specified)
904 Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
906 // C++ [class.friend]p6:
907 // No storage-class-specifier shall appear in the decl-specifier-seq
908 // of a friend declaration.
909 if (isFriendSpecified() && getStorageClassSpec()) {
910 DeclSpec::SCS SC = getStorageClassSpec();
911 const char *SpecName = getSpecifierName(SC);
913 SourceLocation SCLoc = getStorageClassSpecLoc();
914 SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
916 Diag(D, SCLoc, diag::err_friend_storage_spec)
918 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
920 ClearStorageClassSpecs();
923 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
925 // Okay, now we can infer the real type.
927 // TODO: return "auto function" and other bad things based on the real type.
929 // 'data definition has no type or storage class'?
932 bool DeclSpec::isMissingDeclaratorOk() {
933 TST tst = getTypeSpecType();
934 return isDeclRep(tst) && getRepAsDecl() != 0 &&
935 StorageClassSpec != DeclSpec::SCS_typedef;
938 void UnqualifiedId::clear() {
939 Kind = IK_Identifier;
941 StartLocation = SourceLocation();
942 EndLocation = SourceLocation();
945 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
946 OverloadedOperatorKind Op,
947 SourceLocation SymbolLocations[3]) {
948 Kind = IK_OperatorFunctionId;
949 StartLocation = OperatorLoc;
950 EndLocation = OperatorLoc;
951 OperatorFunctionId.Operator = Op;
952 for (unsigned I = 0; I != 3; ++I) {
953 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
955 if (SymbolLocations[I].isValid())
956 EndLocation = SymbolLocations[I];
960 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
961 const char *&PrevSpec) {
964 if (Specifiers & VS) {
965 PrevSpec = getSpecifierName(VS);
972 default: llvm_unreachable("Unknown specifier!");
973 case VS_Override: VS_overrideLoc = Loc; break;
974 case VS_Final: VS_finalLoc = Loc; break;
980 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
982 default: llvm_unreachable("Unknown specifier");
983 case VS_Override: return "override";
984 case VS_Final: return "final";