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