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/AST/ASTContext.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/NestedNameSpecifier.h"
21 #include "clang/AST/TypeLoc.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Basic/LangOptions.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/Support/ErrorHandling.h"
27 using namespace clang;
30 static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
32 return D.Report(Loc, DiagID);
36 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
37 assert(TemplateId && "NULL template-id annotation?");
39 this->TemplateId = TemplateId;
40 StartLocation = TemplateId->TemplateNameLoc;
41 EndLocation = TemplateId->RAngleLoc;
44 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
45 assert(TemplateId && "NULL template-id annotation?");
46 Kind = IK_ConstructorTemplateId;
47 this->TemplateId = TemplateId;
48 StartLocation = TemplateId->TemplateNameLoc;
49 EndLocation = TemplateId->RAngleLoc;
52 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
53 TypeLoc TL, SourceLocation ColonColonLoc) {
54 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
55 if (Range.getBegin().isInvalid())
56 Range.setBegin(TL.getBeginLoc());
57 Range.setEnd(ColonColonLoc);
59 assert(Range == Builder.getSourceRange() &&
60 "NestedNameSpecifierLoc range computation incorrect");
63 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
64 SourceLocation IdentifierLoc,
65 SourceLocation ColonColonLoc) {
66 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
68 if (Range.getBegin().isInvalid())
69 Range.setBegin(IdentifierLoc);
70 Range.setEnd(ColonColonLoc);
72 assert(Range == Builder.getSourceRange() &&
73 "NestedNameSpecifierLoc range computation incorrect");
76 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
77 SourceLocation NamespaceLoc,
78 SourceLocation ColonColonLoc) {
79 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
81 if (Range.getBegin().isInvalid())
82 Range.setBegin(NamespaceLoc);
83 Range.setEnd(ColonColonLoc);
85 assert(Range == Builder.getSourceRange() &&
86 "NestedNameSpecifierLoc range computation incorrect");
89 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
90 SourceLocation AliasLoc,
91 SourceLocation ColonColonLoc) {
92 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
94 if (Range.getBegin().isInvalid())
95 Range.setBegin(AliasLoc);
96 Range.setEnd(ColonColonLoc);
98 assert(Range == Builder.getSourceRange() &&
99 "NestedNameSpecifierLoc range computation incorrect");
102 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
103 SourceLocation ColonColonLoc) {
104 Builder.MakeGlobal(Context, ColonColonLoc);
106 Range = SourceRange(ColonColonLoc);
108 assert(Range == Builder.getSourceRange() &&
109 "NestedNameSpecifierLoc range computation incorrect");
112 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
113 NestedNameSpecifier *Qualifier, SourceRange R) {
114 Builder.MakeTrivial(Context, Qualifier, R);
118 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
120 Range = SourceRange();
125 Range = Other.getSourceRange();
126 Builder.Adopt(Other);
129 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
130 if (!Builder.getRepresentation())
131 return SourceLocation();
132 return Builder.getTemporary().getLocalBeginLoc();
135 NestedNameSpecifierLoc
136 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
137 if (!Builder.getRepresentation())
138 return NestedNameSpecifierLoc();
140 return Builder.getWithLocInContext(Context);
143 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
144 /// "TheDeclarator" is the declarator that this will be added to.
145 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
146 SourceLocation EllipsisLoc,
150 bool RefQualifierIsLvalueRef,
151 SourceLocation RefQualifierLoc,
152 SourceLocation MutableLoc,
153 ExceptionSpecificationType
155 SourceLocation ESpecLoc,
156 ParsedType *Exceptions,
157 SourceRange *ExceptionRanges,
158 unsigned NumExceptions,
160 SourceLocation LocalRangeBegin,
161 SourceLocation LocalRangeEnd,
162 Declarator &TheDeclarator,
163 ParsedType TrailingReturnType) {
166 I.Loc = LocalRangeBegin;
167 I.EndLoc = LocalRangeEnd;
169 I.Fun.hasPrototype = hasProto;
170 I.Fun.isVariadic = isVariadic;
171 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
172 I.Fun.DeleteArgInfo = false;
173 I.Fun.TypeQuals = TypeQuals;
174 I.Fun.NumArgs = NumArgs;
176 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
177 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
178 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
179 I.Fun.ExceptionSpecType = ESpecType;
180 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
181 I.Fun.NumExceptions = 0;
182 I.Fun.Exceptions = 0;
183 I.Fun.NoexceptExpr = 0;
184 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
186 // new[] an argument array if needed.
188 // If the 'InlineParams' in Declarator is unused and big enough, put our
189 // parameter list there (in an effort to avoid new/delete traffic). If it
190 // is already used (consider a function returning a function pointer) or too
191 // small (function taking too many arguments), go to the heap.
192 if (!TheDeclarator.InlineParamsUsed &&
193 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
194 I.Fun.ArgInfo = TheDeclarator.InlineParams;
195 I.Fun.DeleteArgInfo = false;
196 TheDeclarator.InlineParamsUsed = true;
198 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
199 I.Fun.DeleteArgInfo = true;
201 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
204 // Check what exception specification information we should actually store.
206 default: break; // By default, save nothing.
208 // new[] an exception array if needed
210 I.Fun.NumExceptions = NumExceptions;
211 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
212 for (unsigned i = 0; i != NumExceptions; ++i) {
213 I.Fun.Exceptions[i].Ty = Exceptions[i];
214 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
219 case EST_ComputedNoexcept:
220 I.Fun.NoexceptExpr = NoexceptExpr;
226 bool Declarator::isDeclarationOfFunction() const {
227 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
228 switch (DeclTypeInfo[i].Kind) {
229 case DeclaratorChunk::Function:
231 case DeclaratorChunk::Paren:
233 case DeclaratorChunk::Pointer:
234 case DeclaratorChunk::Reference:
235 case DeclaratorChunk::Array:
236 case DeclaratorChunk::BlockPointer:
237 case DeclaratorChunk::MemberPointer:
240 llvm_unreachable("Invalid type chunk");
244 switch (DS.getTypeSpecType()) {
261 case TST_unknown_anytype:
262 case TST_unspecified:
269 if (Expr *E = DS.getRepAsExpr())
270 return E->getType()->isFunctionType();
273 case TST_underlyingType:
275 case TST_typeofType: {
276 QualType QT = DS.getRepAsType().get();
280 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
286 return QT->isFunctionType();
293 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
294 /// declaration specifier includes.
296 unsigned DeclSpec::getParsedSpecifiers() const {
298 if (StorageClassSpec != SCS_unspecified ||
299 SCS_thread_specified)
300 Res |= PQ_StorageClassSpecifier;
302 if (TypeQualifiers != TQ_unspecified)
303 Res |= PQ_TypeQualifier;
305 if (hasTypeSpecifier())
306 Res |= PQ_TypeSpecifier;
308 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
309 Res |= PQ_FunctionSpecifier;
313 template <class T> static bool BadSpecifier(T TNew, T TPrev,
314 const char *&PrevSpec,
316 PrevSpec = DeclSpec::getSpecifierName(TPrev);
317 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
318 : diag::err_invalid_decl_spec_combination);
322 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
324 case DeclSpec::SCS_unspecified: return "unspecified";
325 case DeclSpec::SCS_typedef: return "typedef";
326 case DeclSpec::SCS_extern: return "extern";
327 case DeclSpec::SCS_static: return "static";
328 case DeclSpec::SCS_auto: return "auto";
329 case DeclSpec::SCS_register: return "register";
330 case DeclSpec::SCS_private_extern: return "__private_extern__";
331 case DeclSpec::SCS_mutable: return "mutable";
333 llvm_unreachable("Unknown typespec!");
336 const char *DeclSpec::getSpecifierName(TSW W) {
338 case TSW_unspecified: return "unspecified";
339 case TSW_short: return "short";
340 case TSW_long: return "long";
341 case TSW_longlong: return "long long";
343 llvm_unreachable("Unknown typespec!");
346 const char *DeclSpec::getSpecifierName(TSC C) {
348 case TSC_unspecified: return "unspecified";
349 case TSC_imaginary: return "imaginary";
350 case TSC_complex: return "complex";
352 llvm_unreachable("Unknown typespec!");
356 const char *DeclSpec::getSpecifierName(TSS S) {
358 case TSS_unspecified: return "unspecified";
359 case TSS_signed: return "signed";
360 case TSS_unsigned: return "unsigned";
362 llvm_unreachable("Unknown typespec!");
365 const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
367 case DeclSpec::TST_unspecified: return "unspecified";
368 case DeclSpec::TST_void: return "void";
369 case DeclSpec::TST_char: return "char";
370 case DeclSpec::TST_wchar: return "wchar_t";
371 case DeclSpec::TST_char16: return "char16_t";
372 case DeclSpec::TST_char32: return "char32_t";
373 case DeclSpec::TST_int: return "int";
374 case DeclSpec::TST_float: return "float";
375 case DeclSpec::TST_double: return "double";
376 case DeclSpec::TST_bool: return "_Bool";
377 case DeclSpec::TST_decimal32: return "_Decimal32";
378 case DeclSpec::TST_decimal64: return "_Decimal64";
379 case DeclSpec::TST_decimal128: return "_Decimal128";
380 case DeclSpec::TST_enum: return "enum";
381 case DeclSpec::TST_class: return "class";
382 case DeclSpec::TST_union: return "union";
383 case DeclSpec::TST_struct: return "struct";
384 case DeclSpec::TST_typename: return "type-name";
385 case DeclSpec::TST_typeofType:
386 case DeclSpec::TST_typeofExpr: return "typeof";
387 case DeclSpec::TST_auto: return "auto";
388 case DeclSpec::TST_decltype: return "(decltype)";
389 case DeclSpec::TST_underlyingType: return "__underlying_type";
390 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
391 case DeclSpec::TST_error: return "(error)";
393 llvm_unreachable("Unknown typespec!");
396 const char *DeclSpec::getSpecifierName(TQ T) {
398 case DeclSpec::TQ_unspecified: return "unspecified";
399 case DeclSpec::TQ_const: return "const";
400 case DeclSpec::TQ_restrict: return "restrict";
401 case DeclSpec::TQ_volatile: return "volatile";
403 llvm_unreachable("Unknown typespec!");
406 bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
407 const char *&PrevSpec,
409 const LangOptions &Lang) {
410 // OpenCL prohibits extern, auto, register, and static
411 // It seems sensible to prohibit private_extern too
415 case SCS_private_extern:
419 DiagID = diag::err_not_opencl_storage_class_specifier;
420 PrevSpec = getSpecifierName(S);
427 if (StorageClassSpec != SCS_unspecified) {
428 // Changing storage class is allowed only if the previous one
429 // was the 'extern' that is part of a linkage specification and
430 // the new storage class is 'typedef'.
431 if (!(SCS_extern_in_linkage_spec &&
432 StorageClassSpec == SCS_extern &&
434 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
436 StorageClassSpec = S;
437 StorageClassSpecLoc = Loc;
438 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
442 bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
443 const char *&PrevSpec,
445 if (SCS_thread_specified) {
446 PrevSpec = "__thread";
447 DiagID = diag::ext_duplicate_declspec;
450 SCS_thread_specified = true;
455 /// These methods set the specified attribute of the DeclSpec, but return true
456 /// and ignore the request if invalid (e.g. "extern" then "auto" is
458 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
459 const char *&PrevSpec,
461 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
462 // for 'long long' we will keep the source location of the first 'long'.
463 if (TypeSpecWidth == TSW_unspecified)
465 // Allow turning long -> long long.
466 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
467 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
469 if (TypeAltiVecVector && !TypeAltiVecBool &&
470 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
471 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
472 DiagID = diag::warn_vector_long_decl_spec_combination;
478 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
479 const char *&PrevSpec,
481 if (TypeSpecComplex != TSC_unspecified)
482 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
488 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
489 const char *&PrevSpec,
491 if (TypeSpecSign != TSS_unspecified)
492 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
498 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
499 const char *&PrevSpec,
502 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
505 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
506 SourceLocation TagNameLoc,
507 const char *&PrevSpec,
510 assert(isTypeRep(T) && "T does not store a type");
511 assert(Rep && "no type provided!");
512 if (TypeSpecType != TST_unspecified) {
513 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
514 DiagID = diag::err_invalid_decl_spec_combination;
520 TSTNameLoc = TagNameLoc;
521 TypeSpecOwned = false;
525 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
526 const char *&PrevSpec,
529 assert(isExprRep(T) && "T does not store an expr");
530 assert(Rep && "no expression provided!");
531 if (TypeSpecType != TST_unspecified) {
532 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
533 DiagID = diag::err_invalid_decl_spec_combination;
540 TypeSpecOwned = false;
544 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
545 const char *&PrevSpec,
547 Decl *Rep, bool Owned) {
548 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
551 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
552 SourceLocation TagNameLoc,
553 const char *&PrevSpec,
555 Decl *Rep, bool Owned) {
556 assert(isDeclRep(T) && "T does not store a decl");
557 // Unlike the other cases, we don't assert that we actually get a decl.
559 if (TypeSpecType != TST_unspecified) {
560 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
561 DiagID = diag::err_invalid_decl_spec_combination;
567 TSTNameLoc = TagNameLoc;
568 TypeSpecOwned = Owned;
572 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
573 const char *&PrevSpec,
575 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
576 "rep required for these type-spec kinds!");
577 if (TypeSpecType != TST_unspecified) {
578 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
579 DiagID = diag::err_invalid_decl_spec_combination;
584 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
585 TypeAltiVecBool = true;
589 TypeSpecOwned = false;
590 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
591 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
592 DiagID = diag::err_invalid_vector_decl_spec;
598 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
599 const char *&PrevSpec, unsigned &DiagID) {
600 if (TypeSpecType != TST_unspecified) {
601 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
602 DiagID = diag::err_invalid_vector_decl_spec_combination;
605 TypeAltiVecVector = isAltiVecVector;
610 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
611 const char *&PrevSpec, unsigned &DiagID) {
612 if (!TypeAltiVecVector || TypeAltiVecPixel ||
613 (TypeSpecType != TST_unspecified)) {
614 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
615 DiagID = diag::err_invalid_pixel_decl_spec_combination;
618 TypeAltiVecPixel = isAltiVecPixel;
624 bool DeclSpec::SetTypeSpecError() {
625 TypeSpecType = TST_error;
626 TypeSpecOwned = false;
627 TSTLoc = SourceLocation();
628 TSTNameLoc = SourceLocation();
632 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
633 unsigned &DiagID, const LangOptions &Lang) {
634 // Duplicates turn into warnings pre-C99.
635 if ((TypeQualifiers & T) && !Lang.C99)
636 return BadSpecifier(T, T, PrevSpec, DiagID);
640 default: assert(0 && "Unknown type qualifier!");
641 case TQ_const: TQ_constLoc = Loc; break;
642 case TQ_restrict: TQ_restrictLoc = Loc; break;
643 case TQ_volatile: TQ_volatileLoc = Loc; break;
648 bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
650 // 'inline inline' is ok.
651 FS_inline_specified = true;
656 bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
658 // 'virtual virtual' is ok.
659 FS_virtual_specified = true;
664 bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
666 // 'explicit explicit' is ok.
667 FS_explicit_specified = true;
668 FS_explicitLoc = Loc;
672 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
674 if (Friend_specified) {
676 DiagID = diag::ext_duplicate_declspec;
680 Friend_specified = true;
685 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
687 // 'constexpr constexpr' is ok.
688 Constexpr_specified = true;
693 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
695 SourceLocation *ProtoLocs,
696 SourceLocation LAngleLoc) {
698 ProtocolQualifiers = new Decl*[NP];
699 ProtocolLocs = new SourceLocation[NP];
700 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
701 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
702 NumProtocolQualifiers = NP;
703 ProtocolLAngleLoc = LAngleLoc;
706 void DeclSpec::SaveWrittenBuiltinSpecs() {
707 writtenBS.Sign = getTypeSpecSign();
708 writtenBS.Width = getTypeSpecWidth();
709 writtenBS.Type = getTypeSpecType();
710 // Search the list of attributes for the presence of a mode attribute.
711 writtenBS.ModeAttr = false;
712 AttributeList* attrs = getAttributes().getList();
714 if (attrs->getKind() == AttributeList::AT_mode) {
715 writtenBS.ModeAttr = true;
718 attrs = attrs->getNext();
722 void DeclSpec::SaveStorageSpecifierAsWritten() {
723 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
724 // If 'extern' is part of a linkage specification,
725 // then it is not a storage class "as written".
726 StorageClassSpecAsWritten = SCS_unspecified;
728 StorageClassSpecAsWritten = StorageClassSpec;
731 /// Finish - This does final analysis of the declspec, rejecting things like
732 /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
733 /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
734 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
735 void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
736 // Before possibly changing their values, save specs as written.
737 SaveWrittenBuiltinSpecs();
738 SaveStorageSpecifierAsWritten();
740 // Check the type specifier components first.
742 // Validate and finalize AltiVec vector declspec.
743 if (TypeAltiVecVector) {
744 if (TypeAltiVecBool) {
745 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
746 if (TypeSpecSign != TSS_unspecified) {
747 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
748 << getSpecifierName((TSS)TypeSpecSign);
751 // Only char/int are valid with vector bool. (PIM 2.1)
752 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
753 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
754 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
755 << (TypeAltiVecPixel ? "__pixel" :
756 getSpecifierName((TST)TypeSpecType));
759 // Only 'short' is valid with vector bool. (PIM 2.1)
760 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
761 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
762 << getSpecifierName((TSW)TypeSpecWidth);
764 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
765 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
766 (TypeSpecWidth != TSW_unspecified))
767 TypeSpecSign = TSS_unsigned;
770 if (TypeAltiVecPixel) {
771 //TODO: perform validation
772 TypeSpecType = TST_int;
773 TypeSpecSign = TSS_unsigned;
774 TypeSpecWidth = TSW_short;
775 TypeSpecOwned = false;
779 // signed/unsigned are only valid with int/char/wchar_t.
780 if (TypeSpecSign != TSS_unspecified) {
781 if (TypeSpecType == TST_unspecified)
782 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
783 else if (TypeSpecType != TST_int &&
784 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
785 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
786 << getSpecifierName((TST)TypeSpecType);
787 // signed double -> double.
788 TypeSpecSign = TSS_unspecified;
792 // Validate the width of the type.
793 switch (TypeSpecWidth) {
794 case TSW_unspecified: break;
795 case TSW_short: // short int
796 case TSW_longlong: // long long int
797 if (TypeSpecType == TST_unspecified)
798 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
799 else if (TypeSpecType != TST_int) {
801 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
802 : diag::err_invalid_longlong_spec)
803 << getSpecifierName((TST)TypeSpecType);
804 TypeSpecType = TST_int;
805 TypeSpecOwned = false;
808 case TSW_long: // long double, long int
809 if (TypeSpecType == TST_unspecified)
810 TypeSpecType = TST_int; // long -> long int.
811 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
812 Diag(D, TSWLoc, diag::err_invalid_long_spec)
813 << getSpecifierName((TST)TypeSpecType);
814 TypeSpecType = TST_int;
815 TypeSpecOwned = false;
820 // TODO: if the implementation does not implement _Complex or _Imaginary,
821 // disallow their use. Need information about the backend.
822 if (TypeSpecComplex != TSC_unspecified) {
823 if (TypeSpecType == TST_unspecified) {
824 Diag(D, TSCLoc, diag::ext_plain_complex)
825 << FixItHint::CreateInsertion(
826 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
828 TypeSpecType = TST_double; // _Complex -> _Complex double.
829 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
830 // Note that this intentionally doesn't include _Complex _Bool.
831 Diag(D, TSTLoc, diag::ext_integer_complex);
832 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
833 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
834 << getSpecifierName((TST)TypeSpecType);
835 TypeSpecComplex = TSC_unspecified;
839 // C++ [class.friend]p6:
840 // No storage-class-specifier shall appear in the decl-specifier-seq
841 // of a friend declaration.
842 if (isFriendSpecified() && getStorageClassSpec()) {
843 DeclSpec::SCS SC = getStorageClassSpec();
844 const char *SpecName = getSpecifierName(SC);
846 SourceLocation SCLoc = getStorageClassSpecLoc();
847 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
849 Diag(D, SCLoc, diag::err_friend_storage_spec)
851 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
853 ClearStorageClassSpecs();
856 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
858 // Okay, now we can infer the real type.
860 // TODO: return "auto function" and other bad things based on the real type.
862 // 'data definition has no type or storage class'?
865 bool DeclSpec::isMissingDeclaratorOk() {
866 TST tst = getTypeSpecType();
867 return isDeclRep(tst) && getRepAsDecl() != 0 &&
868 StorageClassSpec != DeclSpec::SCS_typedef;
871 void UnqualifiedId::clear() {
872 Kind = IK_Identifier;
874 StartLocation = SourceLocation();
875 EndLocation = SourceLocation();
878 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
879 OverloadedOperatorKind Op,
880 SourceLocation SymbolLocations[3]) {
881 Kind = IK_OperatorFunctionId;
882 StartLocation = OperatorLoc;
883 EndLocation = OperatorLoc;
884 OperatorFunctionId.Operator = Op;
885 for (unsigned I = 0; I != 3; ++I) {
886 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
888 if (SymbolLocations[I].isValid())
889 EndLocation = SymbolLocations[I];
893 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
894 const char *&PrevSpec) {
897 if (Specifiers & VS) {
898 PrevSpec = getSpecifierName(VS);
905 default: assert(0 && "Unknown specifier!");
906 case VS_Override: VS_overrideLoc = Loc; break;
907 case VS_Final: VS_finalLoc = Loc; break;
913 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
915 default: assert(0 && "Unknown specifier");
916 case VS_Override: return "override";
917 case VS_Final: return "final";