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