xref: /minix3/external/bsd/llvm/dist/clang/lib/Sema/DeclSpec.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc //
10*f4a2713aSLionel Sambuc //  This file implements semantic analysis for declaration specifiers.
11*f4a2713aSLionel Sambuc //
12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
15*f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16*f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
17*f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
18*f4a2713aSLionel Sambuc #include "clang/AST/NestedNameSpecifier.h"
19*f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
20*f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
21*f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
22*f4a2713aSLionel Sambuc #include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
23*f4a2713aSLionel Sambuc #include "clang/Sema/LocInfoType.h"
24*f4a2713aSLionel Sambuc #include "clang/Sema/ParsedTemplate.h"
25*f4a2713aSLionel Sambuc #include "clang/Sema/Sema.h"
26*f4a2713aSLionel Sambuc #include "clang/Sema/SemaDiagnostic.h"
27*f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
28*f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
29*f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
30*f4a2713aSLionel Sambuc #include <cstring>
31*f4a2713aSLionel Sambuc using namespace clang;
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
35*f4a2713aSLionel Sambuc                               unsigned DiagID) {
36*f4a2713aSLionel Sambuc   return D.Report(Loc, DiagID);
37*f4a2713aSLionel Sambuc }
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
41*f4a2713aSLionel Sambuc   assert(TemplateId && "NULL template-id annotation?");
42*f4a2713aSLionel Sambuc   Kind = IK_TemplateId;
43*f4a2713aSLionel Sambuc   this->TemplateId = TemplateId;
44*f4a2713aSLionel Sambuc   StartLocation = TemplateId->TemplateNameLoc;
45*f4a2713aSLionel Sambuc   EndLocation = TemplateId->RAngleLoc;
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
49*f4a2713aSLionel Sambuc   assert(TemplateId && "NULL template-id annotation?");
50*f4a2713aSLionel Sambuc   Kind = IK_ConstructorTemplateId;
51*f4a2713aSLionel Sambuc   this->TemplateId = TemplateId;
52*f4a2713aSLionel Sambuc   StartLocation = TemplateId->TemplateNameLoc;
53*f4a2713aSLionel Sambuc   EndLocation = TemplateId->RAngleLoc;
54*f4a2713aSLionel Sambuc }
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
57*f4a2713aSLionel Sambuc                           TypeLoc TL, SourceLocation ColonColonLoc) {
58*f4a2713aSLionel Sambuc   Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
59*f4a2713aSLionel Sambuc   if (Range.getBegin().isInvalid())
60*f4a2713aSLionel Sambuc     Range.setBegin(TL.getBeginLoc());
61*f4a2713aSLionel Sambuc   Range.setEnd(ColonColonLoc);
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc   assert(Range == Builder.getSourceRange() &&
64*f4a2713aSLionel Sambuc          "NestedNameSpecifierLoc range computation incorrect");
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
68*f4a2713aSLionel Sambuc                           SourceLocation IdentifierLoc,
69*f4a2713aSLionel Sambuc                           SourceLocation ColonColonLoc) {
70*f4a2713aSLionel Sambuc   Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc   if (Range.getBegin().isInvalid())
73*f4a2713aSLionel Sambuc     Range.setBegin(IdentifierLoc);
74*f4a2713aSLionel Sambuc   Range.setEnd(ColonColonLoc);
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc   assert(Range == Builder.getSourceRange() &&
77*f4a2713aSLionel Sambuc          "NestedNameSpecifierLoc range computation incorrect");
78*f4a2713aSLionel Sambuc }
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
81*f4a2713aSLionel Sambuc                           SourceLocation NamespaceLoc,
82*f4a2713aSLionel Sambuc                           SourceLocation ColonColonLoc) {
83*f4a2713aSLionel Sambuc   Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc   if (Range.getBegin().isInvalid())
86*f4a2713aSLionel Sambuc     Range.setBegin(NamespaceLoc);
87*f4a2713aSLionel Sambuc   Range.setEnd(ColonColonLoc);
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc   assert(Range == Builder.getSourceRange() &&
90*f4a2713aSLionel Sambuc          "NestedNameSpecifierLoc range computation incorrect");
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
94*f4a2713aSLionel Sambuc                           SourceLocation AliasLoc,
95*f4a2713aSLionel Sambuc                           SourceLocation ColonColonLoc) {
96*f4a2713aSLionel Sambuc   Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
97*f4a2713aSLionel Sambuc 
98*f4a2713aSLionel Sambuc   if (Range.getBegin().isInvalid())
99*f4a2713aSLionel Sambuc     Range.setBegin(AliasLoc);
100*f4a2713aSLionel Sambuc   Range.setEnd(ColonColonLoc);
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc   assert(Range == Builder.getSourceRange() &&
103*f4a2713aSLionel Sambuc          "NestedNameSpecifierLoc range computation incorrect");
104*f4a2713aSLionel Sambuc }
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc void CXXScopeSpec::MakeGlobal(ASTContext &Context,
107*f4a2713aSLionel Sambuc                               SourceLocation ColonColonLoc) {
108*f4a2713aSLionel Sambuc   Builder.MakeGlobal(Context, ColonColonLoc);
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc   Range = SourceRange(ColonColonLoc);
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc   assert(Range == Builder.getSourceRange() &&
113*f4a2713aSLionel Sambuc          "NestedNameSpecifierLoc range computation incorrect");
114*f4a2713aSLionel Sambuc }
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc void CXXScopeSpec::MakeTrivial(ASTContext &Context,
117*f4a2713aSLionel Sambuc                                NestedNameSpecifier *Qualifier, SourceRange R) {
118*f4a2713aSLionel Sambuc   Builder.MakeTrivial(Context, Qualifier, R);
119*f4a2713aSLionel Sambuc   Range = R;
120*f4a2713aSLionel Sambuc }
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
123*f4a2713aSLionel Sambuc   if (!Other) {
124*f4a2713aSLionel Sambuc     Range = SourceRange();
125*f4a2713aSLionel Sambuc     Builder.Clear();
126*f4a2713aSLionel Sambuc     return;
127*f4a2713aSLionel Sambuc   }
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc   Range = Other.getSourceRange();
130*f4a2713aSLionel Sambuc   Builder.Adopt(Other);
131*f4a2713aSLionel Sambuc }
132*f4a2713aSLionel Sambuc 
133*f4a2713aSLionel Sambuc SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
134*f4a2713aSLionel Sambuc   if (!Builder.getRepresentation())
135*f4a2713aSLionel Sambuc     return SourceLocation();
136*f4a2713aSLionel Sambuc   return Builder.getTemporary().getLocalBeginLoc();
137*f4a2713aSLionel Sambuc }
138*f4a2713aSLionel Sambuc 
139*f4a2713aSLionel Sambuc NestedNameSpecifierLoc
140*f4a2713aSLionel Sambuc CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
141*f4a2713aSLionel Sambuc   if (!Builder.getRepresentation())
142*f4a2713aSLionel Sambuc     return NestedNameSpecifierLoc();
143*f4a2713aSLionel Sambuc 
144*f4a2713aSLionel Sambuc   return Builder.getWithLocInContext(Context);
145*f4a2713aSLionel Sambuc }
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
148*f4a2713aSLionel Sambuc /// "TheDeclarator" is the declarator that this will be added to.
149*f4a2713aSLionel Sambuc DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
150*f4a2713aSLionel Sambuc                                              bool isAmbiguous,
151*f4a2713aSLionel Sambuc                                              SourceLocation LParenLoc,
152*f4a2713aSLionel Sambuc                                              ParamInfo *ArgInfo,
153*f4a2713aSLionel Sambuc                                              unsigned NumArgs,
154*f4a2713aSLionel Sambuc                                              SourceLocation EllipsisLoc,
155*f4a2713aSLionel Sambuc                                              SourceLocation RParenLoc,
156*f4a2713aSLionel Sambuc                                              unsigned TypeQuals,
157*f4a2713aSLionel Sambuc                                              bool RefQualifierIsLvalueRef,
158*f4a2713aSLionel Sambuc                                              SourceLocation RefQualifierLoc,
159*f4a2713aSLionel Sambuc                                              SourceLocation ConstQualifierLoc,
160*f4a2713aSLionel Sambuc                                              SourceLocation
161*f4a2713aSLionel Sambuc                                                  VolatileQualifierLoc,
162*f4a2713aSLionel Sambuc                                              SourceLocation MutableLoc,
163*f4a2713aSLionel Sambuc                                              ExceptionSpecificationType
164*f4a2713aSLionel Sambuc                                                  ESpecType,
165*f4a2713aSLionel Sambuc                                              SourceLocation ESpecLoc,
166*f4a2713aSLionel Sambuc                                              ParsedType *Exceptions,
167*f4a2713aSLionel Sambuc                                              SourceRange *ExceptionRanges,
168*f4a2713aSLionel Sambuc                                              unsigned NumExceptions,
169*f4a2713aSLionel Sambuc                                              Expr *NoexceptExpr,
170*f4a2713aSLionel Sambuc                                              SourceLocation LocalRangeBegin,
171*f4a2713aSLionel Sambuc                                              SourceLocation LocalRangeEnd,
172*f4a2713aSLionel Sambuc                                              Declarator &TheDeclarator,
173*f4a2713aSLionel Sambuc                                              TypeResult TrailingReturnType) {
174*f4a2713aSLionel Sambuc   assert(!(TypeQuals & DeclSpec::TQ_atomic) &&
175*f4a2713aSLionel Sambuc          "function cannot have _Atomic qualifier");
176*f4a2713aSLionel Sambuc 
177*f4a2713aSLionel Sambuc   DeclaratorChunk I;
178*f4a2713aSLionel Sambuc   I.Kind                        = Function;
179*f4a2713aSLionel Sambuc   I.Loc                         = LocalRangeBegin;
180*f4a2713aSLionel Sambuc   I.EndLoc                      = LocalRangeEnd;
181*f4a2713aSLionel Sambuc   I.Fun.AttrList                = 0;
182*f4a2713aSLionel Sambuc   I.Fun.hasPrototype            = hasProto;
183*f4a2713aSLionel Sambuc   I.Fun.isVariadic              = EllipsisLoc.isValid();
184*f4a2713aSLionel Sambuc   I.Fun.isAmbiguous             = isAmbiguous;
185*f4a2713aSLionel Sambuc   I.Fun.LParenLoc               = LParenLoc.getRawEncoding();
186*f4a2713aSLionel Sambuc   I.Fun.EllipsisLoc             = EllipsisLoc.getRawEncoding();
187*f4a2713aSLionel Sambuc   I.Fun.RParenLoc               = RParenLoc.getRawEncoding();
188*f4a2713aSLionel Sambuc   I.Fun.DeleteArgInfo           = false;
189*f4a2713aSLionel Sambuc   I.Fun.TypeQuals               = TypeQuals;
190*f4a2713aSLionel Sambuc   I.Fun.NumArgs                 = NumArgs;
191*f4a2713aSLionel Sambuc   I.Fun.ArgInfo                 = 0;
192*f4a2713aSLionel Sambuc   I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
193*f4a2713aSLionel Sambuc   I.Fun.RefQualifierLoc         = RefQualifierLoc.getRawEncoding();
194*f4a2713aSLionel Sambuc   I.Fun.ConstQualifierLoc       = ConstQualifierLoc.getRawEncoding();
195*f4a2713aSLionel Sambuc   I.Fun.VolatileQualifierLoc    = VolatileQualifierLoc.getRawEncoding();
196*f4a2713aSLionel Sambuc   I.Fun.MutableLoc              = MutableLoc.getRawEncoding();
197*f4a2713aSLionel Sambuc   I.Fun.ExceptionSpecType       = ESpecType;
198*f4a2713aSLionel Sambuc   I.Fun.ExceptionSpecLoc        = ESpecLoc.getRawEncoding();
199*f4a2713aSLionel Sambuc   I.Fun.NumExceptions           = 0;
200*f4a2713aSLionel Sambuc   I.Fun.Exceptions              = 0;
201*f4a2713aSLionel Sambuc   I.Fun.NoexceptExpr            = 0;
202*f4a2713aSLionel Sambuc   I.Fun.HasTrailingReturnType   = TrailingReturnType.isUsable() ||
203*f4a2713aSLionel Sambuc                                   TrailingReturnType.isInvalid();
204*f4a2713aSLionel Sambuc   I.Fun.TrailingReturnType      = TrailingReturnType.get();
205*f4a2713aSLionel Sambuc 
206*f4a2713aSLionel Sambuc   // new[] an argument array if needed.
207*f4a2713aSLionel Sambuc   if (NumArgs) {
208*f4a2713aSLionel Sambuc     // If the 'InlineParams' in Declarator is unused and big enough, put our
209*f4a2713aSLionel Sambuc     // parameter list there (in an effort to avoid new/delete traffic).  If it
210*f4a2713aSLionel Sambuc     // is already used (consider a function returning a function pointer) or too
211*f4a2713aSLionel Sambuc     // small (function taking too many arguments), go to the heap.
212*f4a2713aSLionel Sambuc     if (!TheDeclarator.InlineParamsUsed &&
213*f4a2713aSLionel Sambuc         NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
214*f4a2713aSLionel Sambuc       I.Fun.ArgInfo = TheDeclarator.InlineParams;
215*f4a2713aSLionel Sambuc       I.Fun.DeleteArgInfo = false;
216*f4a2713aSLionel Sambuc       TheDeclarator.InlineParamsUsed = true;
217*f4a2713aSLionel Sambuc     } else {
218*f4a2713aSLionel Sambuc       I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
219*f4a2713aSLionel Sambuc       I.Fun.DeleteArgInfo = true;
220*f4a2713aSLionel Sambuc     }
221*f4a2713aSLionel Sambuc     memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
222*f4a2713aSLionel Sambuc   }
223*f4a2713aSLionel Sambuc 
224*f4a2713aSLionel Sambuc   // Check what exception specification information we should actually store.
225*f4a2713aSLionel Sambuc   switch (ESpecType) {
226*f4a2713aSLionel Sambuc   default: break; // By default, save nothing.
227*f4a2713aSLionel Sambuc   case EST_Dynamic:
228*f4a2713aSLionel Sambuc     // new[] an exception array if needed
229*f4a2713aSLionel Sambuc     if (NumExceptions) {
230*f4a2713aSLionel Sambuc       I.Fun.NumExceptions = NumExceptions;
231*f4a2713aSLionel Sambuc       I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
232*f4a2713aSLionel Sambuc       for (unsigned i = 0; i != NumExceptions; ++i) {
233*f4a2713aSLionel Sambuc         I.Fun.Exceptions[i].Ty = Exceptions[i];
234*f4a2713aSLionel Sambuc         I.Fun.Exceptions[i].Range = ExceptionRanges[i];
235*f4a2713aSLionel Sambuc       }
236*f4a2713aSLionel Sambuc     }
237*f4a2713aSLionel Sambuc     break;
238*f4a2713aSLionel Sambuc 
239*f4a2713aSLionel Sambuc   case EST_ComputedNoexcept:
240*f4a2713aSLionel Sambuc     I.Fun.NoexceptExpr = NoexceptExpr;
241*f4a2713aSLionel Sambuc     break;
242*f4a2713aSLionel Sambuc   }
243*f4a2713aSLionel Sambuc   return I;
244*f4a2713aSLionel Sambuc }
245*f4a2713aSLionel Sambuc 
246*f4a2713aSLionel Sambuc bool Declarator::isDeclarationOfFunction() const {
247*f4a2713aSLionel Sambuc   for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
248*f4a2713aSLionel Sambuc     switch (DeclTypeInfo[i].Kind) {
249*f4a2713aSLionel Sambuc     case DeclaratorChunk::Function:
250*f4a2713aSLionel Sambuc       return true;
251*f4a2713aSLionel Sambuc     case DeclaratorChunk::Paren:
252*f4a2713aSLionel Sambuc       continue;
253*f4a2713aSLionel Sambuc     case DeclaratorChunk::Pointer:
254*f4a2713aSLionel Sambuc     case DeclaratorChunk::Reference:
255*f4a2713aSLionel Sambuc     case DeclaratorChunk::Array:
256*f4a2713aSLionel Sambuc     case DeclaratorChunk::BlockPointer:
257*f4a2713aSLionel Sambuc     case DeclaratorChunk::MemberPointer:
258*f4a2713aSLionel Sambuc       return false;
259*f4a2713aSLionel Sambuc     }
260*f4a2713aSLionel Sambuc     llvm_unreachable("Invalid type chunk");
261*f4a2713aSLionel Sambuc   }
262*f4a2713aSLionel Sambuc 
263*f4a2713aSLionel Sambuc   switch (DS.getTypeSpecType()) {
264*f4a2713aSLionel Sambuc     case TST_atomic:
265*f4a2713aSLionel Sambuc     case TST_auto:
266*f4a2713aSLionel Sambuc     case TST_bool:
267*f4a2713aSLionel Sambuc     case TST_char:
268*f4a2713aSLionel Sambuc     case TST_char16:
269*f4a2713aSLionel Sambuc     case TST_char32:
270*f4a2713aSLionel Sambuc     case TST_class:
271*f4a2713aSLionel Sambuc     case TST_decimal128:
272*f4a2713aSLionel Sambuc     case TST_decimal32:
273*f4a2713aSLionel Sambuc     case TST_decimal64:
274*f4a2713aSLionel Sambuc     case TST_double:
275*f4a2713aSLionel Sambuc     case TST_enum:
276*f4a2713aSLionel Sambuc     case TST_error:
277*f4a2713aSLionel Sambuc     case TST_float:
278*f4a2713aSLionel Sambuc     case TST_half:
279*f4a2713aSLionel Sambuc     case TST_int:
280*f4a2713aSLionel Sambuc     case TST_int128:
281*f4a2713aSLionel Sambuc     case TST_struct:
282*f4a2713aSLionel Sambuc     case TST_interface:
283*f4a2713aSLionel Sambuc     case TST_union:
284*f4a2713aSLionel Sambuc     case TST_unknown_anytype:
285*f4a2713aSLionel Sambuc     case TST_unspecified:
286*f4a2713aSLionel Sambuc     case TST_void:
287*f4a2713aSLionel Sambuc     case TST_wchar:
288*f4a2713aSLionel Sambuc     case TST_image1d_t:
289*f4a2713aSLionel Sambuc     case TST_image1d_array_t:
290*f4a2713aSLionel Sambuc     case TST_image1d_buffer_t:
291*f4a2713aSLionel Sambuc     case TST_image2d_t:
292*f4a2713aSLionel Sambuc     case TST_image2d_array_t:
293*f4a2713aSLionel Sambuc     case TST_image3d_t:
294*f4a2713aSLionel Sambuc     case TST_sampler_t:
295*f4a2713aSLionel Sambuc     case TST_event_t:
296*f4a2713aSLionel Sambuc       return false;
297*f4a2713aSLionel Sambuc 
298*f4a2713aSLionel Sambuc     case TST_decltype_auto:
299*f4a2713aSLionel Sambuc       // This must have an initializer, so can't be a function declaration,
300*f4a2713aSLionel Sambuc       // even if the initializer has function type.
301*f4a2713aSLionel Sambuc       return false;
302*f4a2713aSLionel Sambuc 
303*f4a2713aSLionel Sambuc     case TST_decltype:
304*f4a2713aSLionel Sambuc     case TST_typeofExpr:
305*f4a2713aSLionel Sambuc       if (Expr *E = DS.getRepAsExpr())
306*f4a2713aSLionel Sambuc         return E->getType()->isFunctionType();
307*f4a2713aSLionel Sambuc       return false;
308*f4a2713aSLionel Sambuc 
309*f4a2713aSLionel Sambuc     case TST_underlyingType:
310*f4a2713aSLionel Sambuc     case TST_typename:
311*f4a2713aSLionel Sambuc     case TST_typeofType: {
312*f4a2713aSLionel Sambuc       QualType QT = DS.getRepAsType().get();
313*f4a2713aSLionel Sambuc       if (QT.isNull())
314*f4a2713aSLionel Sambuc         return false;
315*f4a2713aSLionel Sambuc 
316*f4a2713aSLionel Sambuc       if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
317*f4a2713aSLionel Sambuc         QT = LIT->getType();
318*f4a2713aSLionel Sambuc 
319*f4a2713aSLionel Sambuc       if (QT.isNull())
320*f4a2713aSLionel Sambuc         return false;
321*f4a2713aSLionel Sambuc 
322*f4a2713aSLionel Sambuc       return QT->isFunctionType();
323*f4a2713aSLionel Sambuc     }
324*f4a2713aSLionel Sambuc   }
325*f4a2713aSLionel Sambuc 
326*f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TypeSpecType!");
327*f4a2713aSLionel Sambuc }
328*f4a2713aSLionel Sambuc 
329*f4a2713aSLionel Sambuc bool Declarator::isStaticMember() {
330*f4a2713aSLionel Sambuc   assert(getContext() == MemberContext);
331*f4a2713aSLionel Sambuc   return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
332*f4a2713aSLionel Sambuc          CXXMethodDecl::isStaticOverloadedOperator(
333*f4a2713aSLionel Sambuc              getName().OperatorFunctionId.Operator);
334*f4a2713aSLionel Sambuc }
335*f4a2713aSLionel Sambuc 
336*f4a2713aSLionel Sambuc bool DeclSpec::hasTagDefinition() const {
337*f4a2713aSLionel Sambuc   if (!TypeSpecOwned)
338*f4a2713aSLionel Sambuc     return false;
339*f4a2713aSLionel Sambuc   return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
340*f4a2713aSLionel Sambuc }
341*f4a2713aSLionel Sambuc 
342*f4a2713aSLionel Sambuc /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
343*f4a2713aSLionel Sambuc /// declaration specifier includes.
344*f4a2713aSLionel Sambuc ///
345*f4a2713aSLionel Sambuc unsigned DeclSpec::getParsedSpecifiers() const {
346*f4a2713aSLionel Sambuc   unsigned Res = 0;
347*f4a2713aSLionel Sambuc   if (StorageClassSpec != SCS_unspecified ||
348*f4a2713aSLionel Sambuc       ThreadStorageClassSpec != TSCS_unspecified)
349*f4a2713aSLionel Sambuc     Res |= PQ_StorageClassSpecifier;
350*f4a2713aSLionel Sambuc 
351*f4a2713aSLionel Sambuc   if (TypeQualifiers != TQ_unspecified)
352*f4a2713aSLionel Sambuc     Res |= PQ_TypeQualifier;
353*f4a2713aSLionel Sambuc 
354*f4a2713aSLionel Sambuc   if (hasTypeSpecifier())
355*f4a2713aSLionel Sambuc     Res |= PQ_TypeSpecifier;
356*f4a2713aSLionel Sambuc 
357*f4a2713aSLionel Sambuc   if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified ||
358*f4a2713aSLionel Sambuc       FS_noreturn_specified || FS_forceinline_specified)
359*f4a2713aSLionel Sambuc     Res |= PQ_FunctionSpecifier;
360*f4a2713aSLionel Sambuc   return Res;
361*f4a2713aSLionel Sambuc }
362*f4a2713aSLionel Sambuc 
363*f4a2713aSLionel Sambuc template <class T> static bool BadSpecifier(T TNew, T TPrev,
364*f4a2713aSLionel Sambuc                                             const char *&PrevSpec,
365*f4a2713aSLionel Sambuc                                             unsigned &DiagID,
366*f4a2713aSLionel Sambuc                                             bool IsExtension = true) {
367*f4a2713aSLionel Sambuc   PrevSpec = DeclSpec::getSpecifierName(TPrev);
368*f4a2713aSLionel Sambuc   if (TNew != TPrev)
369*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_decl_spec_combination;
370*f4a2713aSLionel Sambuc   else
371*f4a2713aSLionel Sambuc     DiagID = IsExtension ? diag::ext_duplicate_declspec :
372*f4a2713aSLionel Sambuc                            diag::warn_duplicate_declspec;
373*f4a2713aSLionel Sambuc   return true;
374*f4a2713aSLionel Sambuc }
375*f4a2713aSLionel Sambuc 
376*f4a2713aSLionel Sambuc const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
377*f4a2713aSLionel Sambuc   switch (S) {
378*f4a2713aSLionel Sambuc   case DeclSpec::SCS_unspecified: return "unspecified";
379*f4a2713aSLionel Sambuc   case DeclSpec::SCS_typedef:     return "typedef";
380*f4a2713aSLionel Sambuc   case DeclSpec::SCS_extern:      return "extern";
381*f4a2713aSLionel Sambuc   case DeclSpec::SCS_static:      return "static";
382*f4a2713aSLionel Sambuc   case DeclSpec::SCS_auto:        return "auto";
383*f4a2713aSLionel Sambuc   case DeclSpec::SCS_register:    return "register";
384*f4a2713aSLionel Sambuc   case DeclSpec::SCS_private_extern: return "__private_extern__";
385*f4a2713aSLionel Sambuc   case DeclSpec::SCS_mutable:     return "mutable";
386*f4a2713aSLionel Sambuc   }
387*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown typespec!");
388*f4a2713aSLionel Sambuc }
389*f4a2713aSLionel Sambuc 
390*f4a2713aSLionel Sambuc const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
391*f4a2713aSLionel Sambuc   switch (S) {
392*f4a2713aSLionel Sambuc   case DeclSpec::TSCS_unspecified:   return "unspecified";
393*f4a2713aSLionel Sambuc   case DeclSpec::TSCS___thread:      return "__thread";
394*f4a2713aSLionel Sambuc   case DeclSpec::TSCS_thread_local:  return "thread_local";
395*f4a2713aSLionel Sambuc   case DeclSpec::TSCS__Thread_local: return "_Thread_local";
396*f4a2713aSLionel Sambuc   }
397*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown typespec!");
398*f4a2713aSLionel Sambuc }
399*f4a2713aSLionel Sambuc 
400*f4a2713aSLionel Sambuc const char *DeclSpec::getSpecifierName(TSW W) {
401*f4a2713aSLionel Sambuc   switch (W) {
402*f4a2713aSLionel Sambuc   case TSW_unspecified: return "unspecified";
403*f4a2713aSLionel Sambuc   case TSW_short:       return "short";
404*f4a2713aSLionel Sambuc   case TSW_long:        return "long";
405*f4a2713aSLionel Sambuc   case TSW_longlong:    return "long long";
406*f4a2713aSLionel Sambuc   }
407*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown typespec!");
408*f4a2713aSLionel Sambuc }
409*f4a2713aSLionel Sambuc 
410*f4a2713aSLionel Sambuc const char *DeclSpec::getSpecifierName(TSC C) {
411*f4a2713aSLionel Sambuc   switch (C) {
412*f4a2713aSLionel Sambuc   case TSC_unspecified: return "unspecified";
413*f4a2713aSLionel Sambuc   case TSC_imaginary:   return "imaginary";
414*f4a2713aSLionel Sambuc   case TSC_complex:     return "complex";
415*f4a2713aSLionel Sambuc   }
416*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown typespec!");
417*f4a2713aSLionel Sambuc }
418*f4a2713aSLionel Sambuc 
419*f4a2713aSLionel Sambuc 
420*f4a2713aSLionel Sambuc const char *DeclSpec::getSpecifierName(TSS S) {
421*f4a2713aSLionel Sambuc   switch (S) {
422*f4a2713aSLionel Sambuc   case TSS_unspecified: return "unspecified";
423*f4a2713aSLionel Sambuc   case TSS_signed:      return "signed";
424*f4a2713aSLionel Sambuc   case TSS_unsigned:    return "unsigned";
425*f4a2713aSLionel Sambuc   }
426*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown typespec!");
427*f4a2713aSLionel Sambuc }
428*f4a2713aSLionel Sambuc 
429*f4a2713aSLionel Sambuc const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
430*f4a2713aSLionel Sambuc   switch (T) {
431*f4a2713aSLionel Sambuc   case DeclSpec::TST_unspecified: return "unspecified";
432*f4a2713aSLionel Sambuc   case DeclSpec::TST_void:        return "void";
433*f4a2713aSLionel Sambuc   case DeclSpec::TST_char:        return "char";
434*f4a2713aSLionel Sambuc   case DeclSpec::TST_wchar:       return "wchar_t";
435*f4a2713aSLionel Sambuc   case DeclSpec::TST_char16:      return "char16_t";
436*f4a2713aSLionel Sambuc   case DeclSpec::TST_char32:      return "char32_t";
437*f4a2713aSLionel Sambuc   case DeclSpec::TST_int:         return "int";
438*f4a2713aSLionel Sambuc   case DeclSpec::TST_int128:      return "__int128";
439*f4a2713aSLionel Sambuc   case DeclSpec::TST_half:        return "half";
440*f4a2713aSLionel Sambuc   case DeclSpec::TST_float:       return "float";
441*f4a2713aSLionel Sambuc   case DeclSpec::TST_double:      return "double";
442*f4a2713aSLionel Sambuc   case DeclSpec::TST_bool:        return "_Bool";
443*f4a2713aSLionel Sambuc   case DeclSpec::TST_decimal32:   return "_Decimal32";
444*f4a2713aSLionel Sambuc   case DeclSpec::TST_decimal64:   return "_Decimal64";
445*f4a2713aSLionel Sambuc   case DeclSpec::TST_decimal128:  return "_Decimal128";
446*f4a2713aSLionel Sambuc   case DeclSpec::TST_enum:        return "enum";
447*f4a2713aSLionel Sambuc   case DeclSpec::TST_class:       return "class";
448*f4a2713aSLionel Sambuc   case DeclSpec::TST_union:       return "union";
449*f4a2713aSLionel Sambuc   case DeclSpec::TST_struct:      return "struct";
450*f4a2713aSLionel Sambuc   case DeclSpec::TST_interface:   return "__interface";
451*f4a2713aSLionel Sambuc   case DeclSpec::TST_typename:    return "type-name";
452*f4a2713aSLionel Sambuc   case DeclSpec::TST_typeofType:
453*f4a2713aSLionel Sambuc   case DeclSpec::TST_typeofExpr:  return "typeof";
454*f4a2713aSLionel Sambuc   case DeclSpec::TST_auto:        return "auto";
455*f4a2713aSLionel Sambuc   case DeclSpec::TST_decltype:    return "(decltype)";
456*f4a2713aSLionel Sambuc   case DeclSpec::TST_decltype_auto: return "decltype(auto)";
457*f4a2713aSLionel Sambuc   case DeclSpec::TST_underlyingType: return "__underlying_type";
458*f4a2713aSLionel Sambuc   case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
459*f4a2713aSLionel Sambuc   case DeclSpec::TST_atomic: return "_Atomic";
460*f4a2713aSLionel Sambuc   case DeclSpec::TST_image1d_t:   return "image1d_t";
461*f4a2713aSLionel Sambuc   case DeclSpec::TST_image1d_array_t: return "image1d_array_t";
462*f4a2713aSLionel Sambuc   case DeclSpec::TST_image1d_buffer_t: return "image1d_buffer_t";
463*f4a2713aSLionel Sambuc   case DeclSpec::TST_image2d_t:   return "image2d_t";
464*f4a2713aSLionel Sambuc   case DeclSpec::TST_image2d_array_t: return "image2d_array_t";
465*f4a2713aSLionel Sambuc   case DeclSpec::TST_image3d_t:   return "image3d_t";
466*f4a2713aSLionel Sambuc   case DeclSpec::TST_sampler_t:   return "sampler_t";
467*f4a2713aSLionel Sambuc   case DeclSpec::TST_event_t:     return "event_t";
468*f4a2713aSLionel Sambuc   case DeclSpec::TST_error:       return "(error)";
469*f4a2713aSLionel Sambuc   }
470*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown typespec!");
471*f4a2713aSLionel Sambuc }
472*f4a2713aSLionel Sambuc 
473*f4a2713aSLionel Sambuc const char *DeclSpec::getSpecifierName(TQ T) {
474*f4a2713aSLionel Sambuc   switch (T) {
475*f4a2713aSLionel Sambuc   case DeclSpec::TQ_unspecified: return "unspecified";
476*f4a2713aSLionel Sambuc   case DeclSpec::TQ_const:       return "const";
477*f4a2713aSLionel Sambuc   case DeclSpec::TQ_restrict:    return "restrict";
478*f4a2713aSLionel Sambuc   case DeclSpec::TQ_volatile:    return "volatile";
479*f4a2713aSLionel Sambuc   case DeclSpec::TQ_atomic:      return "_Atomic";
480*f4a2713aSLionel Sambuc   }
481*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown typespec!");
482*f4a2713aSLionel Sambuc }
483*f4a2713aSLionel Sambuc 
484*f4a2713aSLionel Sambuc bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
485*f4a2713aSLionel Sambuc                                    const char *&PrevSpec,
486*f4a2713aSLionel Sambuc                                    unsigned &DiagID) {
487*f4a2713aSLionel Sambuc   // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
488*f4a2713aSLionel Sambuc   // specifiers are not supported.
489*f4a2713aSLionel Sambuc   // It seems sensible to prohibit private_extern too
490*f4a2713aSLionel Sambuc   // The cl_clang_storage_class_specifiers extension enables support for
491*f4a2713aSLionel Sambuc   // these storage-class specifiers.
492*f4a2713aSLionel Sambuc   // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
493*f4a2713aSLionel Sambuc   // specifiers are not supported."
494*f4a2713aSLionel Sambuc   if (S.getLangOpts().OpenCL &&
495*f4a2713aSLionel Sambuc       !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
496*f4a2713aSLionel Sambuc     switch (SC) {
497*f4a2713aSLionel Sambuc     case SCS_extern:
498*f4a2713aSLionel Sambuc     case SCS_private_extern:
499*f4a2713aSLionel Sambuc     case SCS_static:
500*f4a2713aSLionel Sambuc         if (S.getLangOpts().OpenCLVersion < 120) {
501*f4a2713aSLionel Sambuc           DiagID   = diag::err_not_opencl_storage_class_specifier;
502*f4a2713aSLionel Sambuc           PrevSpec = getSpecifierName(SC);
503*f4a2713aSLionel Sambuc           return true;
504*f4a2713aSLionel Sambuc         }
505*f4a2713aSLionel Sambuc         break;
506*f4a2713aSLionel Sambuc     case SCS_auto:
507*f4a2713aSLionel Sambuc     case SCS_register:
508*f4a2713aSLionel Sambuc       DiagID   = diag::err_not_opencl_storage_class_specifier;
509*f4a2713aSLionel Sambuc       PrevSpec = getSpecifierName(SC);
510*f4a2713aSLionel Sambuc       return true;
511*f4a2713aSLionel Sambuc     default:
512*f4a2713aSLionel Sambuc       break;
513*f4a2713aSLionel Sambuc     }
514*f4a2713aSLionel Sambuc   }
515*f4a2713aSLionel Sambuc 
516*f4a2713aSLionel Sambuc   if (StorageClassSpec != SCS_unspecified) {
517*f4a2713aSLionel Sambuc     // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
518*f4a2713aSLionel Sambuc     bool isInvalid = true;
519*f4a2713aSLionel Sambuc     if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
520*f4a2713aSLionel Sambuc       if (SC == SCS_auto)
521*f4a2713aSLionel Sambuc         return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
522*f4a2713aSLionel Sambuc       if (StorageClassSpec == SCS_auto) {
523*f4a2713aSLionel Sambuc         isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
524*f4a2713aSLionel Sambuc                                     PrevSpec, DiagID);
525*f4a2713aSLionel Sambuc         assert(!isInvalid && "auto SCS -> TST recovery failed");
526*f4a2713aSLionel Sambuc       }
527*f4a2713aSLionel Sambuc     }
528*f4a2713aSLionel Sambuc 
529*f4a2713aSLionel Sambuc     // Changing storage class is allowed only if the previous one
530*f4a2713aSLionel Sambuc     // was the 'extern' that is part of a linkage specification and
531*f4a2713aSLionel Sambuc     // the new storage class is 'typedef'.
532*f4a2713aSLionel Sambuc     if (isInvalid &&
533*f4a2713aSLionel Sambuc         !(SCS_extern_in_linkage_spec &&
534*f4a2713aSLionel Sambuc           StorageClassSpec == SCS_extern &&
535*f4a2713aSLionel Sambuc           SC == SCS_typedef))
536*f4a2713aSLionel Sambuc       return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
537*f4a2713aSLionel Sambuc   }
538*f4a2713aSLionel Sambuc   StorageClassSpec = SC;
539*f4a2713aSLionel Sambuc   StorageClassSpecLoc = Loc;
540*f4a2713aSLionel Sambuc   assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
541*f4a2713aSLionel Sambuc   return false;
542*f4a2713aSLionel Sambuc }
543*f4a2713aSLionel Sambuc 
544*f4a2713aSLionel Sambuc bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
545*f4a2713aSLionel Sambuc                                          const char *&PrevSpec,
546*f4a2713aSLionel Sambuc                                          unsigned &DiagID) {
547*f4a2713aSLionel Sambuc   if (ThreadStorageClassSpec != TSCS_unspecified)
548*f4a2713aSLionel Sambuc     return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
549*f4a2713aSLionel Sambuc 
550*f4a2713aSLionel Sambuc   ThreadStorageClassSpec = TSC;
551*f4a2713aSLionel Sambuc   ThreadStorageClassSpecLoc = Loc;
552*f4a2713aSLionel Sambuc   return false;
553*f4a2713aSLionel Sambuc }
554*f4a2713aSLionel Sambuc 
555*f4a2713aSLionel Sambuc /// These methods set the specified attribute of the DeclSpec, but return true
556*f4a2713aSLionel Sambuc /// and ignore the request if invalid (e.g. "extern" then "auto" is
557*f4a2713aSLionel Sambuc /// specified).
558*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
559*f4a2713aSLionel Sambuc                                 const char *&PrevSpec,
560*f4a2713aSLionel Sambuc                                 unsigned &DiagID) {
561*f4a2713aSLionel Sambuc   // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
562*f4a2713aSLionel Sambuc   // for 'long long' we will keep the source location of the first 'long'.
563*f4a2713aSLionel Sambuc   if (TypeSpecWidth == TSW_unspecified)
564*f4a2713aSLionel Sambuc     TSWLoc = Loc;
565*f4a2713aSLionel Sambuc   // Allow turning long -> long long.
566*f4a2713aSLionel Sambuc   else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
567*f4a2713aSLionel Sambuc     return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
568*f4a2713aSLionel Sambuc   TypeSpecWidth = W;
569*f4a2713aSLionel Sambuc   if (TypeAltiVecVector && !TypeAltiVecBool &&
570*f4a2713aSLionel Sambuc       ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
571*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
572*f4a2713aSLionel Sambuc     DiagID = diag::warn_vector_long_decl_spec_combination;
573*f4a2713aSLionel Sambuc     return true;
574*f4a2713aSLionel Sambuc   }
575*f4a2713aSLionel Sambuc   return false;
576*f4a2713aSLionel Sambuc }
577*f4a2713aSLionel Sambuc 
578*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
579*f4a2713aSLionel Sambuc                                   const char *&PrevSpec,
580*f4a2713aSLionel Sambuc                                   unsigned &DiagID) {
581*f4a2713aSLionel Sambuc   if (TypeSpecComplex != TSC_unspecified)
582*f4a2713aSLionel Sambuc     return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
583*f4a2713aSLionel Sambuc   TypeSpecComplex = C;
584*f4a2713aSLionel Sambuc   TSCLoc = Loc;
585*f4a2713aSLionel Sambuc   return false;
586*f4a2713aSLionel Sambuc }
587*f4a2713aSLionel Sambuc 
588*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
589*f4a2713aSLionel Sambuc                                const char *&PrevSpec,
590*f4a2713aSLionel Sambuc                                unsigned &DiagID) {
591*f4a2713aSLionel Sambuc   if (TypeSpecSign != TSS_unspecified)
592*f4a2713aSLionel Sambuc     return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
593*f4a2713aSLionel Sambuc   TypeSpecSign = S;
594*f4a2713aSLionel Sambuc   TSSLoc = Loc;
595*f4a2713aSLionel Sambuc   return false;
596*f4a2713aSLionel Sambuc }
597*f4a2713aSLionel Sambuc 
598*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
599*f4a2713aSLionel Sambuc                                const char *&PrevSpec,
600*f4a2713aSLionel Sambuc                                unsigned &DiagID,
601*f4a2713aSLionel Sambuc                                ParsedType Rep) {
602*f4a2713aSLionel Sambuc   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
603*f4a2713aSLionel Sambuc }
604*f4a2713aSLionel Sambuc 
605*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
606*f4a2713aSLionel Sambuc                                SourceLocation TagNameLoc,
607*f4a2713aSLionel Sambuc                                const char *&PrevSpec,
608*f4a2713aSLionel Sambuc                                unsigned &DiagID,
609*f4a2713aSLionel Sambuc                                ParsedType Rep) {
610*f4a2713aSLionel Sambuc   assert(isTypeRep(T) && "T does not store a type");
611*f4a2713aSLionel Sambuc   assert(Rep && "no type provided!");
612*f4a2713aSLionel Sambuc   if (TypeSpecType != TST_unspecified) {
613*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
614*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_decl_spec_combination;
615*f4a2713aSLionel Sambuc     return true;
616*f4a2713aSLionel Sambuc   }
617*f4a2713aSLionel Sambuc   TypeSpecType = T;
618*f4a2713aSLionel Sambuc   TypeRep = Rep;
619*f4a2713aSLionel Sambuc   TSTLoc = TagKwLoc;
620*f4a2713aSLionel Sambuc   TSTNameLoc = TagNameLoc;
621*f4a2713aSLionel Sambuc   TypeSpecOwned = false;
622*f4a2713aSLionel Sambuc   return false;
623*f4a2713aSLionel Sambuc }
624*f4a2713aSLionel Sambuc 
625*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
626*f4a2713aSLionel Sambuc                                const char *&PrevSpec,
627*f4a2713aSLionel Sambuc                                unsigned &DiagID,
628*f4a2713aSLionel Sambuc                                Expr *Rep) {
629*f4a2713aSLionel Sambuc   assert(isExprRep(T) && "T does not store an expr");
630*f4a2713aSLionel Sambuc   assert(Rep && "no expression provided!");
631*f4a2713aSLionel Sambuc   if (TypeSpecType != TST_unspecified) {
632*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
633*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_decl_spec_combination;
634*f4a2713aSLionel Sambuc     return true;
635*f4a2713aSLionel Sambuc   }
636*f4a2713aSLionel Sambuc   TypeSpecType = T;
637*f4a2713aSLionel Sambuc   ExprRep = Rep;
638*f4a2713aSLionel Sambuc   TSTLoc = Loc;
639*f4a2713aSLionel Sambuc   TSTNameLoc = Loc;
640*f4a2713aSLionel Sambuc   TypeSpecOwned = false;
641*f4a2713aSLionel Sambuc   return false;
642*f4a2713aSLionel Sambuc }
643*f4a2713aSLionel Sambuc 
644*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
645*f4a2713aSLionel Sambuc                                const char *&PrevSpec,
646*f4a2713aSLionel Sambuc                                unsigned &DiagID,
647*f4a2713aSLionel Sambuc                                Decl *Rep, bool Owned) {
648*f4a2713aSLionel Sambuc   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
649*f4a2713aSLionel Sambuc }
650*f4a2713aSLionel Sambuc 
651*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
652*f4a2713aSLionel Sambuc                                SourceLocation TagNameLoc,
653*f4a2713aSLionel Sambuc                                const char *&PrevSpec,
654*f4a2713aSLionel Sambuc                                unsigned &DiagID,
655*f4a2713aSLionel Sambuc                                Decl *Rep, bool Owned) {
656*f4a2713aSLionel Sambuc   assert(isDeclRep(T) && "T does not store a decl");
657*f4a2713aSLionel Sambuc   // Unlike the other cases, we don't assert that we actually get a decl.
658*f4a2713aSLionel Sambuc 
659*f4a2713aSLionel Sambuc   if (TypeSpecType != TST_unspecified) {
660*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
661*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_decl_spec_combination;
662*f4a2713aSLionel Sambuc     return true;
663*f4a2713aSLionel Sambuc   }
664*f4a2713aSLionel Sambuc   TypeSpecType = T;
665*f4a2713aSLionel Sambuc   DeclRep = Rep;
666*f4a2713aSLionel Sambuc   TSTLoc = TagKwLoc;
667*f4a2713aSLionel Sambuc   TSTNameLoc = TagNameLoc;
668*f4a2713aSLionel Sambuc   TypeSpecOwned = Owned && Rep != 0;
669*f4a2713aSLionel Sambuc   return false;
670*f4a2713aSLionel Sambuc }
671*f4a2713aSLionel Sambuc 
672*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
673*f4a2713aSLionel Sambuc                                const char *&PrevSpec,
674*f4a2713aSLionel Sambuc                                unsigned &DiagID) {
675*f4a2713aSLionel Sambuc   assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
676*f4a2713aSLionel Sambuc          "rep required for these type-spec kinds!");
677*f4a2713aSLionel Sambuc   if (TypeSpecType != TST_unspecified) {
678*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
679*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_decl_spec_combination;
680*f4a2713aSLionel Sambuc     return true;
681*f4a2713aSLionel Sambuc   }
682*f4a2713aSLionel Sambuc   TSTLoc = Loc;
683*f4a2713aSLionel Sambuc   TSTNameLoc = Loc;
684*f4a2713aSLionel Sambuc   if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
685*f4a2713aSLionel Sambuc     TypeAltiVecBool = true;
686*f4a2713aSLionel Sambuc     return false;
687*f4a2713aSLionel Sambuc   }
688*f4a2713aSLionel Sambuc   TypeSpecType = T;
689*f4a2713aSLionel Sambuc   TypeSpecOwned = false;
690*f4a2713aSLionel Sambuc   if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
691*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
692*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_vector_decl_spec;
693*f4a2713aSLionel Sambuc     return true;
694*f4a2713aSLionel Sambuc   }
695*f4a2713aSLionel Sambuc   return false;
696*f4a2713aSLionel Sambuc }
697*f4a2713aSLionel Sambuc 
698*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
699*f4a2713aSLionel Sambuc                           const char *&PrevSpec, unsigned &DiagID) {
700*f4a2713aSLionel Sambuc   if (TypeSpecType != TST_unspecified) {
701*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
702*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_vector_decl_spec_combination;
703*f4a2713aSLionel Sambuc     return true;
704*f4a2713aSLionel Sambuc   }
705*f4a2713aSLionel Sambuc   TypeAltiVecVector = isAltiVecVector;
706*f4a2713aSLionel Sambuc   AltiVecLoc = Loc;
707*f4a2713aSLionel Sambuc   return false;
708*f4a2713aSLionel Sambuc }
709*f4a2713aSLionel Sambuc 
710*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
711*f4a2713aSLionel Sambuc                           const char *&PrevSpec, unsigned &DiagID) {
712*f4a2713aSLionel Sambuc   if (!TypeAltiVecVector || TypeAltiVecPixel ||
713*f4a2713aSLionel Sambuc       (TypeSpecType != TST_unspecified)) {
714*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
715*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_pixel_decl_spec_combination;
716*f4a2713aSLionel Sambuc     return true;
717*f4a2713aSLionel Sambuc   }
718*f4a2713aSLionel Sambuc   TypeAltiVecPixel = isAltiVecPixel;
719*f4a2713aSLionel Sambuc   TSTLoc = Loc;
720*f4a2713aSLionel Sambuc   TSTNameLoc = Loc;
721*f4a2713aSLionel Sambuc   return false;
722*f4a2713aSLionel Sambuc }
723*f4a2713aSLionel Sambuc 
724*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
725*f4a2713aSLionel Sambuc                           const char *&PrevSpec, unsigned &DiagID) {
726*f4a2713aSLionel Sambuc   if (!TypeAltiVecVector || TypeAltiVecBool ||
727*f4a2713aSLionel Sambuc       (TypeSpecType != TST_unspecified)) {
728*f4a2713aSLionel Sambuc     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
729*f4a2713aSLionel Sambuc     DiagID = diag::err_invalid_vector_bool_decl_spec;
730*f4a2713aSLionel Sambuc     return true;
731*f4a2713aSLionel Sambuc   }
732*f4a2713aSLionel Sambuc   TypeAltiVecBool = isAltiVecBool;
733*f4a2713aSLionel Sambuc   TSTLoc = Loc;
734*f4a2713aSLionel Sambuc   TSTNameLoc = Loc;
735*f4a2713aSLionel Sambuc   return false;
736*f4a2713aSLionel Sambuc }
737*f4a2713aSLionel Sambuc 
738*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeSpecError() {
739*f4a2713aSLionel Sambuc   TypeSpecType = TST_error;
740*f4a2713aSLionel Sambuc   TypeSpecOwned = false;
741*f4a2713aSLionel Sambuc   TSTLoc = SourceLocation();
742*f4a2713aSLionel Sambuc   TSTNameLoc = SourceLocation();
743*f4a2713aSLionel Sambuc   return false;
744*f4a2713aSLionel Sambuc }
745*f4a2713aSLionel Sambuc 
746*f4a2713aSLionel Sambuc bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
747*f4a2713aSLionel Sambuc                            unsigned &DiagID, const LangOptions &Lang) {
748*f4a2713aSLionel Sambuc   // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
749*f4a2713aSLionel Sambuc   // C++.  However, since this is likely not what the user intended, we will
750*f4a2713aSLionel Sambuc   // always warn.  We do not need to set the qualifier's location since we
751*f4a2713aSLionel Sambuc   // already have it.
752*f4a2713aSLionel Sambuc   if (TypeQualifiers & T) {
753*f4a2713aSLionel Sambuc     bool IsExtension = true;
754*f4a2713aSLionel Sambuc     if (Lang.C99)
755*f4a2713aSLionel Sambuc       IsExtension = false;
756*f4a2713aSLionel Sambuc     return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
757*f4a2713aSLionel Sambuc   }
758*f4a2713aSLionel Sambuc   TypeQualifiers |= T;
759*f4a2713aSLionel Sambuc 
760*f4a2713aSLionel Sambuc   switch (T) {
761*f4a2713aSLionel Sambuc   case TQ_unspecified: break;
762*f4a2713aSLionel Sambuc   case TQ_const:    TQ_constLoc = Loc; return false;
763*f4a2713aSLionel Sambuc   case TQ_restrict: TQ_restrictLoc = Loc; return false;
764*f4a2713aSLionel Sambuc   case TQ_volatile: TQ_volatileLoc = Loc; return false;
765*f4a2713aSLionel Sambuc   case TQ_atomic:   TQ_atomicLoc = Loc; return false;
766*f4a2713aSLionel Sambuc   }
767*f4a2713aSLionel Sambuc 
768*f4a2713aSLionel Sambuc   llvm_unreachable("Unknown type qualifier!");
769*f4a2713aSLionel Sambuc }
770*f4a2713aSLionel Sambuc 
771*f4a2713aSLionel Sambuc bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
772*f4a2713aSLionel Sambuc                                      unsigned &DiagID) {
773*f4a2713aSLionel Sambuc   // 'inline inline' is ok.  However, since this is likely not what the user
774*f4a2713aSLionel Sambuc   // intended, we will always warn, similar to duplicates of type qualifiers.
775*f4a2713aSLionel Sambuc   if (FS_inline_specified) {
776*f4a2713aSLionel Sambuc     DiagID = diag::warn_duplicate_declspec;
777*f4a2713aSLionel Sambuc     PrevSpec = "inline";
778*f4a2713aSLionel Sambuc     return true;
779*f4a2713aSLionel Sambuc   }
780*f4a2713aSLionel Sambuc   FS_inline_specified = true;
781*f4a2713aSLionel Sambuc   FS_inlineLoc = Loc;
782*f4a2713aSLionel Sambuc   return false;
783*f4a2713aSLionel Sambuc }
784*f4a2713aSLionel Sambuc 
785*f4a2713aSLionel Sambuc bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
786*f4a2713aSLionel Sambuc                                           unsigned &DiagID) {
787*f4a2713aSLionel Sambuc   if (FS_forceinline_specified) {
788*f4a2713aSLionel Sambuc     DiagID = diag::warn_duplicate_declspec;
789*f4a2713aSLionel Sambuc     PrevSpec = "__forceinline";
790*f4a2713aSLionel Sambuc     return true;
791*f4a2713aSLionel Sambuc   }
792*f4a2713aSLionel Sambuc   FS_forceinline_specified = true;
793*f4a2713aSLionel Sambuc   FS_forceinlineLoc = Loc;
794*f4a2713aSLionel Sambuc   return false;
795*f4a2713aSLionel Sambuc }
796*f4a2713aSLionel Sambuc 
797*f4a2713aSLionel Sambuc bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
798*f4a2713aSLionel Sambuc                                       const char *&PrevSpec,
799*f4a2713aSLionel Sambuc                                       unsigned &DiagID) {
800*f4a2713aSLionel Sambuc   // 'virtual virtual' is ok, but warn as this is likely not what the user
801*f4a2713aSLionel Sambuc   // intended.
802*f4a2713aSLionel Sambuc   if (FS_virtual_specified) {
803*f4a2713aSLionel Sambuc     DiagID = diag::warn_duplicate_declspec;
804*f4a2713aSLionel Sambuc     PrevSpec = "virtual";
805*f4a2713aSLionel Sambuc     return true;
806*f4a2713aSLionel Sambuc   }
807*f4a2713aSLionel Sambuc   FS_virtual_specified = true;
808*f4a2713aSLionel Sambuc   FS_virtualLoc = Loc;
809*f4a2713aSLionel Sambuc   return false;
810*f4a2713aSLionel Sambuc }
811*f4a2713aSLionel Sambuc 
812*f4a2713aSLionel Sambuc bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
813*f4a2713aSLionel Sambuc                                        const char *&PrevSpec,
814*f4a2713aSLionel Sambuc                                        unsigned &DiagID) {
815*f4a2713aSLionel Sambuc   // 'explicit explicit' is ok, but warn as this is likely not what the user
816*f4a2713aSLionel Sambuc   // intended.
817*f4a2713aSLionel Sambuc   if (FS_explicit_specified) {
818*f4a2713aSLionel Sambuc     DiagID = diag::warn_duplicate_declspec;
819*f4a2713aSLionel Sambuc     PrevSpec = "explicit";
820*f4a2713aSLionel Sambuc     return true;
821*f4a2713aSLionel Sambuc   }
822*f4a2713aSLionel Sambuc   FS_explicit_specified = true;
823*f4a2713aSLionel Sambuc   FS_explicitLoc = Loc;
824*f4a2713aSLionel Sambuc   return false;
825*f4a2713aSLionel Sambuc }
826*f4a2713aSLionel Sambuc 
827*f4a2713aSLionel Sambuc bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
828*f4a2713aSLionel Sambuc                                        const char *&PrevSpec,
829*f4a2713aSLionel Sambuc                                        unsigned &DiagID) {
830*f4a2713aSLionel Sambuc   // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
831*f4a2713aSLionel Sambuc   // intended.
832*f4a2713aSLionel Sambuc   if (FS_noreturn_specified) {
833*f4a2713aSLionel Sambuc     DiagID = diag::warn_duplicate_declspec;
834*f4a2713aSLionel Sambuc     PrevSpec = "_Noreturn";
835*f4a2713aSLionel Sambuc     return true;
836*f4a2713aSLionel Sambuc   }
837*f4a2713aSLionel Sambuc   FS_noreturn_specified = true;
838*f4a2713aSLionel Sambuc   FS_noreturnLoc = Loc;
839*f4a2713aSLionel Sambuc   return false;
840*f4a2713aSLionel Sambuc }
841*f4a2713aSLionel Sambuc 
842*f4a2713aSLionel Sambuc bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
843*f4a2713aSLionel Sambuc                              unsigned &DiagID) {
844*f4a2713aSLionel Sambuc   if (Friend_specified) {
845*f4a2713aSLionel Sambuc     PrevSpec = "friend";
846*f4a2713aSLionel Sambuc     DiagID = diag::ext_duplicate_declspec;
847*f4a2713aSLionel Sambuc     return true;
848*f4a2713aSLionel Sambuc   }
849*f4a2713aSLionel Sambuc 
850*f4a2713aSLionel Sambuc   Friend_specified = true;
851*f4a2713aSLionel Sambuc   FriendLoc = Loc;
852*f4a2713aSLionel Sambuc   return false;
853*f4a2713aSLionel Sambuc }
854*f4a2713aSLionel Sambuc 
855*f4a2713aSLionel Sambuc bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
856*f4a2713aSLionel Sambuc                                     unsigned &DiagID) {
857*f4a2713aSLionel Sambuc   if (isModulePrivateSpecified()) {
858*f4a2713aSLionel Sambuc     PrevSpec = "__module_private__";
859*f4a2713aSLionel Sambuc     DiagID = diag::ext_duplicate_declspec;
860*f4a2713aSLionel Sambuc     return true;
861*f4a2713aSLionel Sambuc   }
862*f4a2713aSLionel Sambuc 
863*f4a2713aSLionel Sambuc   ModulePrivateLoc = Loc;
864*f4a2713aSLionel Sambuc   return false;
865*f4a2713aSLionel Sambuc }
866*f4a2713aSLionel Sambuc 
867*f4a2713aSLionel Sambuc bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
868*f4a2713aSLionel Sambuc                                 unsigned &DiagID) {
869*f4a2713aSLionel Sambuc   // 'constexpr constexpr' is ok.
870*f4a2713aSLionel Sambuc   Constexpr_specified = true;
871*f4a2713aSLionel Sambuc   ConstexprLoc = Loc;
872*f4a2713aSLionel Sambuc   return false;
873*f4a2713aSLionel Sambuc }
874*f4a2713aSLionel Sambuc 
875*f4a2713aSLionel Sambuc void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
876*f4a2713aSLionel Sambuc                                      unsigned NP,
877*f4a2713aSLionel Sambuc                                      SourceLocation *ProtoLocs,
878*f4a2713aSLionel Sambuc                                      SourceLocation LAngleLoc) {
879*f4a2713aSLionel Sambuc   if (NP == 0) return;
880*f4a2713aSLionel Sambuc   Decl **ProtoQuals = new Decl*[NP];
881*f4a2713aSLionel Sambuc   memcpy(ProtoQuals, Protos, sizeof(Decl*)*NP);
882*f4a2713aSLionel Sambuc   ProtocolQualifiers = ProtoQuals;
883*f4a2713aSLionel Sambuc   ProtocolLocs = new SourceLocation[NP];
884*f4a2713aSLionel Sambuc   memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
885*f4a2713aSLionel Sambuc   NumProtocolQualifiers = NP;
886*f4a2713aSLionel Sambuc   ProtocolLAngleLoc = LAngleLoc;
887*f4a2713aSLionel Sambuc }
888*f4a2713aSLionel Sambuc 
889*f4a2713aSLionel Sambuc void DeclSpec::SaveWrittenBuiltinSpecs() {
890*f4a2713aSLionel Sambuc   writtenBS.Sign = getTypeSpecSign();
891*f4a2713aSLionel Sambuc   writtenBS.Width = getTypeSpecWidth();
892*f4a2713aSLionel Sambuc   writtenBS.Type = getTypeSpecType();
893*f4a2713aSLionel Sambuc   // Search the list of attributes for the presence of a mode attribute.
894*f4a2713aSLionel Sambuc   writtenBS.ModeAttr = false;
895*f4a2713aSLionel Sambuc   AttributeList* attrs = getAttributes().getList();
896*f4a2713aSLionel Sambuc   while (attrs) {
897*f4a2713aSLionel Sambuc     if (attrs->getKind() == AttributeList::AT_Mode) {
898*f4a2713aSLionel Sambuc       writtenBS.ModeAttr = true;
899*f4a2713aSLionel Sambuc       break;
900*f4a2713aSLionel Sambuc     }
901*f4a2713aSLionel Sambuc     attrs = attrs->getNext();
902*f4a2713aSLionel Sambuc   }
903*f4a2713aSLionel Sambuc }
904*f4a2713aSLionel Sambuc 
905*f4a2713aSLionel Sambuc /// Finish - This does final analysis of the declspec, rejecting things like
906*f4a2713aSLionel Sambuc /// "_Imaginary" (lacking an FP type).  This returns a diagnostic to issue or
907*f4a2713aSLionel Sambuc /// diag::NUM_DIAGNOSTICS if there is no error.  After calling this method,
908*f4a2713aSLionel Sambuc /// DeclSpec is guaranteed self-consistent, even if an error occurred.
909*f4a2713aSLionel Sambuc void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
910*f4a2713aSLionel Sambuc   // Before possibly changing their values, save specs as written.
911*f4a2713aSLionel Sambuc   SaveWrittenBuiltinSpecs();
912*f4a2713aSLionel Sambuc 
913*f4a2713aSLionel Sambuc   // Check the type specifier components first.
914*f4a2713aSLionel Sambuc 
915*f4a2713aSLionel Sambuc   // If decltype(auto) is used, no other type specifiers are permitted.
916*f4a2713aSLionel Sambuc   if (TypeSpecType == TST_decltype_auto &&
917*f4a2713aSLionel Sambuc       (TypeSpecWidth != TSW_unspecified ||
918*f4a2713aSLionel Sambuc        TypeSpecComplex != TSC_unspecified ||
919*f4a2713aSLionel Sambuc        TypeSpecSign != TSS_unspecified ||
920*f4a2713aSLionel Sambuc        TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
921*f4a2713aSLionel Sambuc        TypeQualifiers)) {
922*f4a2713aSLionel Sambuc     const unsigned NumLocs = 8;
923*f4a2713aSLionel Sambuc     SourceLocation ExtraLocs[NumLocs] = {
924*f4a2713aSLionel Sambuc       TSWLoc, TSCLoc, TSSLoc, AltiVecLoc,
925*f4a2713aSLionel Sambuc       TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc
926*f4a2713aSLionel Sambuc     };
927*f4a2713aSLionel Sambuc     FixItHint Hints[NumLocs];
928*f4a2713aSLionel Sambuc     SourceLocation FirstLoc;
929*f4a2713aSLionel Sambuc     for (unsigned I = 0; I != NumLocs; ++I) {
930*f4a2713aSLionel Sambuc       if (!ExtraLocs[I].isInvalid()) {
931*f4a2713aSLionel Sambuc         if (FirstLoc.isInvalid() ||
932*f4a2713aSLionel Sambuc             PP.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
933*f4a2713aSLionel Sambuc                                                             FirstLoc))
934*f4a2713aSLionel Sambuc           FirstLoc = ExtraLocs[I];
935*f4a2713aSLionel Sambuc         Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
936*f4a2713aSLionel Sambuc       }
937*f4a2713aSLionel Sambuc     }
938*f4a2713aSLionel Sambuc     TypeSpecWidth = TSW_unspecified;
939*f4a2713aSLionel Sambuc     TypeSpecComplex = TSC_unspecified;
940*f4a2713aSLionel Sambuc     TypeSpecSign = TSS_unspecified;
941*f4a2713aSLionel Sambuc     TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
942*f4a2713aSLionel Sambuc     TypeQualifiers = 0;
943*f4a2713aSLionel Sambuc     Diag(D, TSTLoc, diag::err_decltype_auto_cannot_be_combined)
944*f4a2713aSLionel Sambuc       << Hints[0] << Hints[1] << Hints[2] << Hints[3]
945*f4a2713aSLionel Sambuc       << Hints[4] << Hints[5] << Hints[6] << Hints[7];
946*f4a2713aSLionel Sambuc   }
947*f4a2713aSLionel Sambuc 
948*f4a2713aSLionel Sambuc   // Validate and finalize AltiVec vector declspec.
949*f4a2713aSLionel Sambuc   if (TypeAltiVecVector) {
950*f4a2713aSLionel Sambuc     if (TypeAltiVecBool) {
951*f4a2713aSLionel Sambuc       // Sign specifiers are not allowed with vector bool. (PIM 2.1)
952*f4a2713aSLionel Sambuc       if (TypeSpecSign != TSS_unspecified) {
953*f4a2713aSLionel Sambuc         Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
954*f4a2713aSLionel Sambuc           << getSpecifierName((TSS)TypeSpecSign);
955*f4a2713aSLionel Sambuc       }
956*f4a2713aSLionel Sambuc 
957*f4a2713aSLionel Sambuc       // Only char/int are valid with vector bool. (PIM 2.1)
958*f4a2713aSLionel Sambuc       if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
959*f4a2713aSLionel Sambuc            (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
960*f4a2713aSLionel Sambuc         Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
961*f4a2713aSLionel Sambuc           << (TypeAltiVecPixel ? "__pixel" :
962*f4a2713aSLionel Sambuc                                  getSpecifierName((TST)TypeSpecType));
963*f4a2713aSLionel Sambuc       }
964*f4a2713aSLionel Sambuc 
965*f4a2713aSLionel Sambuc       // Only 'short' is valid with vector bool. (PIM 2.1)
966*f4a2713aSLionel Sambuc       if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
967*f4a2713aSLionel Sambuc         Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
968*f4a2713aSLionel Sambuc           << getSpecifierName((TSW)TypeSpecWidth);
969*f4a2713aSLionel Sambuc 
970*f4a2713aSLionel Sambuc       // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
971*f4a2713aSLionel Sambuc       if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
972*f4a2713aSLionel Sambuc           (TypeSpecWidth != TSW_unspecified))
973*f4a2713aSLionel Sambuc         TypeSpecSign = TSS_unsigned;
974*f4a2713aSLionel Sambuc     }
975*f4a2713aSLionel Sambuc 
976*f4a2713aSLionel Sambuc     if (TypeAltiVecPixel) {
977*f4a2713aSLionel Sambuc       //TODO: perform validation
978*f4a2713aSLionel Sambuc       TypeSpecType = TST_int;
979*f4a2713aSLionel Sambuc       TypeSpecSign = TSS_unsigned;
980*f4a2713aSLionel Sambuc       TypeSpecWidth = TSW_short;
981*f4a2713aSLionel Sambuc       TypeSpecOwned = false;
982*f4a2713aSLionel Sambuc     }
983*f4a2713aSLionel Sambuc   }
984*f4a2713aSLionel Sambuc 
985*f4a2713aSLionel Sambuc   // signed/unsigned are only valid with int/char/wchar_t.
986*f4a2713aSLionel Sambuc   if (TypeSpecSign != TSS_unspecified) {
987*f4a2713aSLionel Sambuc     if (TypeSpecType == TST_unspecified)
988*f4a2713aSLionel Sambuc       TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
989*f4a2713aSLionel Sambuc     else if (TypeSpecType != TST_int  && TypeSpecType != TST_int128 &&
990*f4a2713aSLionel Sambuc              TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
991*f4a2713aSLionel Sambuc       Diag(D, TSSLoc, diag::err_invalid_sign_spec)
992*f4a2713aSLionel Sambuc         << getSpecifierName((TST)TypeSpecType);
993*f4a2713aSLionel Sambuc       // signed double -> double.
994*f4a2713aSLionel Sambuc       TypeSpecSign = TSS_unspecified;
995*f4a2713aSLionel Sambuc     }
996*f4a2713aSLionel Sambuc   }
997*f4a2713aSLionel Sambuc 
998*f4a2713aSLionel Sambuc   // Validate the width of the type.
999*f4a2713aSLionel Sambuc   switch (TypeSpecWidth) {
1000*f4a2713aSLionel Sambuc   case TSW_unspecified: break;
1001*f4a2713aSLionel Sambuc   case TSW_short:    // short int
1002*f4a2713aSLionel Sambuc   case TSW_longlong: // long long int
1003*f4a2713aSLionel Sambuc     if (TypeSpecType == TST_unspecified)
1004*f4a2713aSLionel Sambuc       TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1005*f4a2713aSLionel Sambuc     else if (TypeSpecType != TST_int) {
1006*f4a2713aSLionel Sambuc       Diag(D, TSWLoc,
1007*f4a2713aSLionel Sambuc            TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
1008*f4a2713aSLionel Sambuc                                       : diag::err_invalid_longlong_spec)
1009*f4a2713aSLionel Sambuc         <<  getSpecifierName((TST)TypeSpecType);
1010*f4a2713aSLionel Sambuc       TypeSpecType = TST_int;
1011*f4a2713aSLionel Sambuc       TypeSpecOwned = false;
1012*f4a2713aSLionel Sambuc     }
1013*f4a2713aSLionel Sambuc     break;
1014*f4a2713aSLionel Sambuc   case TSW_long:  // long double, long int
1015*f4a2713aSLionel Sambuc     if (TypeSpecType == TST_unspecified)
1016*f4a2713aSLionel Sambuc       TypeSpecType = TST_int;  // long -> long int.
1017*f4a2713aSLionel Sambuc     else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
1018*f4a2713aSLionel Sambuc       Diag(D, TSWLoc, diag::err_invalid_long_spec)
1019*f4a2713aSLionel Sambuc         << getSpecifierName((TST)TypeSpecType);
1020*f4a2713aSLionel Sambuc       TypeSpecType = TST_int;
1021*f4a2713aSLionel Sambuc       TypeSpecOwned = false;
1022*f4a2713aSLionel Sambuc     }
1023*f4a2713aSLionel Sambuc     break;
1024*f4a2713aSLionel Sambuc   }
1025*f4a2713aSLionel Sambuc 
1026*f4a2713aSLionel Sambuc   // TODO: if the implementation does not implement _Complex or _Imaginary,
1027*f4a2713aSLionel Sambuc   // disallow their use.  Need information about the backend.
1028*f4a2713aSLionel Sambuc   if (TypeSpecComplex != TSC_unspecified) {
1029*f4a2713aSLionel Sambuc     if (TypeSpecType == TST_unspecified) {
1030*f4a2713aSLionel Sambuc       Diag(D, TSCLoc, diag::ext_plain_complex)
1031*f4a2713aSLionel Sambuc         << FixItHint::CreateInsertion(
1032*f4a2713aSLionel Sambuc                               PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
1033*f4a2713aSLionel Sambuc                                                  " double");
1034*f4a2713aSLionel Sambuc       TypeSpecType = TST_double;   // _Complex -> _Complex double.
1035*f4a2713aSLionel Sambuc     } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1036*f4a2713aSLionel Sambuc       // Note that this intentionally doesn't include _Complex _Bool.
1037*f4a2713aSLionel Sambuc       if (!PP.getLangOpts().CPlusPlus)
1038*f4a2713aSLionel Sambuc         Diag(D, TSTLoc, diag::ext_integer_complex);
1039*f4a2713aSLionel Sambuc     } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
1040*f4a2713aSLionel Sambuc       Diag(D, TSCLoc, diag::err_invalid_complex_spec)
1041*f4a2713aSLionel Sambuc         << getSpecifierName((TST)TypeSpecType);
1042*f4a2713aSLionel Sambuc       TypeSpecComplex = TSC_unspecified;
1043*f4a2713aSLionel Sambuc     }
1044*f4a2713aSLionel Sambuc   }
1045*f4a2713aSLionel Sambuc 
1046*f4a2713aSLionel Sambuc   // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1047*f4a2713aSLionel Sambuc   // _Thread_local can only appear with the 'static' and 'extern' storage class
1048*f4a2713aSLionel Sambuc   // specifiers. We also allow __private_extern__ as an extension.
1049*f4a2713aSLionel Sambuc   if (ThreadStorageClassSpec != TSCS_unspecified) {
1050*f4a2713aSLionel Sambuc     switch (StorageClassSpec) {
1051*f4a2713aSLionel Sambuc     case SCS_unspecified:
1052*f4a2713aSLionel Sambuc     case SCS_extern:
1053*f4a2713aSLionel Sambuc     case SCS_private_extern:
1054*f4a2713aSLionel Sambuc     case SCS_static:
1055*f4a2713aSLionel Sambuc       break;
1056*f4a2713aSLionel Sambuc     default:
1057*f4a2713aSLionel Sambuc       if (PP.getSourceManager().isBeforeInTranslationUnit(
1058*f4a2713aSLionel Sambuc             getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
1059*f4a2713aSLionel Sambuc         Diag(D, getStorageClassSpecLoc(),
1060*f4a2713aSLionel Sambuc              diag::err_invalid_decl_spec_combination)
1061*f4a2713aSLionel Sambuc           << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1062*f4a2713aSLionel Sambuc           << SourceRange(getThreadStorageClassSpecLoc());
1063*f4a2713aSLionel Sambuc       else
1064*f4a2713aSLionel Sambuc         Diag(D, getThreadStorageClassSpecLoc(),
1065*f4a2713aSLionel Sambuc              diag::err_invalid_decl_spec_combination)
1066*f4a2713aSLionel Sambuc           << DeclSpec::getSpecifierName(getStorageClassSpec())
1067*f4a2713aSLionel Sambuc           << SourceRange(getStorageClassSpecLoc());
1068*f4a2713aSLionel Sambuc       // Discard the thread storage class specifier to recover.
1069*f4a2713aSLionel Sambuc       ThreadStorageClassSpec = TSCS_unspecified;
1070*f4a2713aSLionel Sambuc       ThreadStorageClassSpecLoc = SourceLocation();
1071*f4a2713aSLionel Sambuc     }
1072*f4a2713aSLionel Sambuc   }
1073*f4a2713aSLionel Sambuc 
1074*f4a2713aSLionel Sambuc   // If no type specifier was provided and we're parsing a language where
1075*f4a2713aSLionel Sambuc   // the type specifier is not optional, but we got 'auto' as a storage
1076*f4a2713aSLionel Sambuc   // class specifier, then assume this is an attempt to use C++0x's 'auto'
1077*f4a2713aSLionel Sambuc   // type specifier.
1078*f4a2713aSLionel Sambuc   if (PP.getLangOpts().CPlusPlus &&
1079*f4a2713aSLionel Sambuc       TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1080*f4a2713aSLionel Sambuc     TypeSpecType = TST_auto;
1081*f4a2713aSLionel Sambuc     StorageClassSpec = SCS_unspecified;
1082*f4a2713aSLionel Sambuc     TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1083*f4a2713aSLionel Sambuc     StorageClassSpecLoc = SourceLocation();
1084*f4a2713aSLionel Sambuc   }
1085*f4a2713aSLionel Sambuc   // Diagnose if we've recovered from an ill-formed 'auto' storage class
1086*f4a2713aSLionel Sambuc   // specifier in a pre-C++11 dialect of C++.
1087*f4a2713aSLionel Sambuc   if (!PP.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1088*f4a2713aSLionel Sambuc     Diag(D, TSTLoc, diag::ext_auto_type_specifier);
1089*f4a2713aSLionel Sambuc   if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus11 &&
1090*f4a2713aSLionel Sambuc       StorageClassSpec == SCS_auto)
1091*f4a2713aSLionel Sambuc     Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
1092*f4a2713aSLionel Sambuc       << FixItHint::CreateRemoval(StorageClassSpecLoc);
1093*f4a2713aSLionel Sambuc   if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1094*f4a2713aSLionel Sambuc     Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
1095*f4a2713aSLionel Sambuc       << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1096*f4a2713aSLionel Sambuc   if (Constexpr_specified)
1097*f4a2713aSLionel Sambuc     Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
1098*f4a2713aSLionel Sambuc 
1099*f4a2713aSLionel Sambuc   // C++ [class.friend]p6:
1100*f4a2713aSLionel Sambuc   //   No storage-class-specifier shall appear in the decl-specifier-seq
1101*f4a2713aSLionel Sambuc   //   of a friend declaration.
1102*f4a2713aSLionel Sambuc   if (isFriendSpecified() &&
1103*f4a2713aSLionel Sambuc       (getStorageClassSpec() || getThreadStorageClassSpec())) {
1104*f4a2713aSLionel Sambuc     SmallString<32> SpecName;
1105*f4a2713aSLionel Sambuc     SourceLocation SCLoc;
1106*f4a2713aSLionel Sambuc     FixItHint StorageHint, ThreadHint;
1107*f4a2713aSLionel Sambuc 
1108*f4a2713aSLionel Sambuc     if (DeclSpec::SCS SC = getStorageClassSpec()) {
1109*f4a2713aSLionel Sambuc       SpecName = getSpecifierName(SC);
1110*f4a2713aSLionel Sambuc       SCLoc = getStorageClassSpecLoc();
1111*f4a2713aSLionel Sambuc       StorageHint = FixItHint::CreateRemoval(SCLoc);
1112*f4a2713aSLionel Sambuc     }
1113*f4a2713aSLionel Sambuc 
1114*f4a2713aSLionel Sambuc     if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1115*f4a2713aSLionel Sambuc       if (!SpecName.empty()) SpecName += " ";
1116*f4a2713aSLionel Sambuc       SpecName += getSpecifierName(TSC);
1117*f4a2713aSLionel Sambuc       SCLoc = getThreadStorageClassSpecLoc();
1118*f4a2713aSLionel Sambuc       ThreadHint = FixItHint::CreateRemoval(SCLoc);
1119*f4a2713aSLionel Sambuc     }
1120*f4a2713aSLionel Sambuc 
1121*f4a2713aSLionel Sambuc     Diag(D, SCLoc, diag::err_friend_storage_spec)
1122*f4a2713aSLionel Sambuc       << SpecName << StorageHint << ThreadHint;
1123*f4a2713aSLionel Sambuc 
1124*f4a2713aSLionel Sambuc     ClearStorageClassSpecs();
1125*f4a2713aSLionel Sambuc   }
1126*f4a2713aSLionel Sambuc 
1127*f4a2713aSLionel Sambuc   assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1128*f4a2713aSLionel Sambuc 
1129*f4a2713aSLionel Sambuc   // Okay, now we can infer the real type.
1130*f4a2713aSLionel Sambuc 
1131*f4a2713aSLionel Sambuc   // TODO: return "auto function" and other bad things based on the real type.
1132*f4a2713aSLionel Sambuc 
1133*f4a2713aSLionel Sambuc   // 'data definition has no type or storage class'?
1134*f4a2713aSLionel Sambuc }
1135*f4a2713aSLionel Sambuc 
1136*f4a2713aSLionel Sambuc bool DeclSpec::isMissingDeclaratorOk() {
1137*f4a2713aSLionel Sambuc   TST tst = getTypeSpecType();
1138*f4a2713aSLionel Sambuc   return isDeclRep(tst) && getRepAsDecl() != 0 &&
1139*f4a2713aSLionel Sambuc     StorageClassSpec != DeclSpec::SCS_typedef;
1140*f4a2713aSLionel Sambuc }
1141*f4a2713aSLionel Sambuc 
1142*f4a2713aSLionel Sambuc void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1143*f4a2713aSLionel Sambuc                                           OverloadedOperatorKind Op,
1144*f4a2713aSLionel Sambuc                                           SourceLocation SymbolLocations[3]) {
1145*f4a2713aSLionel Sambuc   Kind = IK_OperatorFunctionId;
1146*f4a2713aSLionel Sambuc   StartLocation = OperatorLoc;
1147*f4a2713aSLionel Sambuc   EndLocation = OperatorLoc;
1148*f4a2713aSLionel Sambuc   OperatorFunctionId.Operator = Op;
1149*f4a2713aSLionel Sambuc   for (unsigned I = 0; I != 3; ++I) {
1150*f4a2713aSLionel Sambuc     OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
1151*f4a2713aSLionel Sambuc 
1152*f4a2713aSLionel Sambuc     if (SymbolLocations[I].isValid())
1153*f4a2713aSLionel Sambuc       EndLocation = SymbolLocations[I];
1154*f4a2713aSLionel Sambuc   }
1155*f4a2713aSLionel Sambuc }
1156*f4a2713aSLionel Sambuc 
1157*f4a2713aSLionel Sambuc bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1158*f4a2713aSLionel Sambuc                                   const char *&PrevSpec) {
1159*f4a2713aSLionel Sambuc   LastLocation = Loc;
1160*f4a2713aSLionel Sambuc 
1161*f4a2713aSLionel Sambuc   if (Specifiers & VS) {
1162*f4a2713aSLionel Sambuc     PrevSpec = getSpecifierName(VS);
1163*f4a2713aSLionel Sambuc     return true;
1164*f4a2713aSLionel Sambuc   }
1165*f4a2713aSLionel Sambuc 
1166*f4a2713aSLionel Sambuc   Specifiers |= VS;
1167*f4a2713aSLionel Sambuc 
1168*f4a2713aSLionel Sambuc   switch (VS) {
1169*f4a2713aSLionel Sambuc   default: llvm_unreachable("Unknown specifier!");
1170*f4a2713aSLionel Sambuc   case VS_Override: VS_overrideLoc = Loc; break;
1171*f4a2713aSLionel Sambuc   case VS_Sealed:
1172*f4a2713aSLionel Sambuc   case VS_Final:    VS_finalLoc = Loc; break;
1173*f4a2713aSLionel Sambuc   }
1174*f4a2713aSLionel Sambuc 
1175*f4a2713aSLionel Sambuc   return false;
1176*f4a2713aSLionel Sambuc }
1177*f4a2713aSLionel Sambuc 
1178*f4a2713aSLionel Sambuc const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1179*f4a2713aSLionel Sambuc   switch (VS) {
1180*f4a2713aSLionel Sambuc   default: llvm_unreachable("Unknown specifier");
1181*f4a2713aSLionel Sambuc   case VS_Override: return "override";
1182*f4a2713aSLionel Sambuc   case VS_Final: return "final";
1183*f4a2713aSLionel Sambuc   case VS_Sealed: return "sealed";
1184*f4a2713aSLionel Sambuc   }
1185*f4a2713aSLionel Sambuc }
1186