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 DeclSpec::hasTagDefinition() const {
355   if (!TypeSpecOwned)
356     return false;
357   return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
358 }
359 
360 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
361 /// declaration specifier includes.
362 ///
363 unsigned DeclSpec::getParsedSpecifiers() const {
364   unsigned Res = 0;
365   if (StorageClassSpec != SCS_unspecified ||
366       ThreadStorageClassSpec != TSCS_unspecified)
367     Res |= PQ_StorageClassSpecifier;
368 
369   if (TypeQualifiers != TQ_unspecified)
370     Res |= PQ_TypeQualifier;
371 
372   if (hasTypeSpecifier())
373     Res |= PQ_TypeSpecifier;
374 
375   if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified ||
376       FS_noreturn_specified || FS_forceinline_specified)
377     Res |= PQ_FunctionSpecifier;
378   return Res;
379 }
380 
381 template <class T> static bool BadSpecifier(T TNew, T TPrev,
382                                             const char *&PrevSpec,
383                                             unsigned &DiagID,
384                                             bool IsExtension = true) {
385   PrevSpec = DeclSpec::getSpecifierName(TPrev);
386   if (TNew != TPrev)
387     DiagID = diag::err_invalid_decl_spec_combination;
388   else
389     DiagID = IsExtension ? diag::ext_duplicate_declspec :
390                            diag::warn_duplicate_declspec;
391   return true;
392 }
393 
394 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
395   switch (S) {
396   case DeclSpec::SCS_unspecified: return "unspecified";
397   case DeclSpec::SCS_typedef:     return "typedef";
398   case DeclSpec::SCS_extern:      return "extern";
399   case DeclSpec::SCS_static:      return "static";
400   case DeclSpec::SCS_auto:        return "auto";
401   case DeclSpec::SCS_register:    return "register";
402   case DeclSpec::SCS_private_extern: return "__private_extern__";
403   case DeclSpec::SCS_mutable:     return "mutable";
404   }
405   llvm_unreachable("Unknown typespec!");
406 }
407 
408 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
409   switch (S) {
410   case DeclSpec::TSCS_unspecified:   return "unspecified";
411   case DeclSpec::TSCS___thread:      return "__thread";
412   case DeclSpec::TSCS_thread_local:  return "thread_local";
413   case DeclSpec::TSCS__Thread_local: return "_Thread_local";
414   }
415   llvm_unreachable("Unknown typespec!");
416 }
417 
418 const char *DeclSpec::getSpecifierName(TSW W) {
419   switch (W) {
420   case TSW_unspecified: return "unspecified";
421   case TSW_short:       return "short";
422   case TSW_long:        return "long";
423   case TSW_longlong:    return "long long";
424   }
425   llvm_unreachable("Unknown typespec!");
426 }
427 
428 const char *DeclSpec::getSpecifierName(TSC C) {
429   switch (C) {
430   case TSC_unspecified: return "unspecified";
431   case TSC_imaginary:   return "imaginary";
432   case TSC_complex:     return "complex";
433   }
434   llvm_unreachable("Unknown typespec!");
435 }
436 
437 
438 const char *DeclSpec::getSpecifierName(TSS S) {
439   switch (S) {
440   case TSS_unspecified: return "unspecified";
441   case TSS_signed:      return "signed";
442   case TSS_unsigned:    return "unsigned";
443   }
444   llvm_unreachable("Unknown typespec!");
445 }
446 
447 const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
448                                        const PrintingPolicy &Policy) {
449   switch (T) {
450   case DeclSpec::TST_unspecified: return "unspecified";
451   case DeclSpec::TST_void:        return "void";
452   case DeclSpec::TST_char:        return "char";
453   case DeclSpec::TST_wchar:       return Policy.MSWChar ? "__wchar_t" : "wchar_t";
454   case DeclSpec::TST_char16:      return "char16_t";
455   case DeclSpec::TST_char32:      return "char32_t";
456   case DeclSpec::TST_int:         return "int";
457   case DeclSpec::TST_int128:      return "__int128";
458   case DeclSpec::TST_half:        return "half";
459   case DeclSpec::TST_float:       return "float";
460   case DeclSpec::TST_double:      return "double";
461   case DeclSpec::TST_bool:        return Policy.Bool ? "bool" : "_Bool";
462   case DeclSpec::TST_decimal32:   return "_Decimal32";
463   case DeclSpec::TST_decimal64:   return "_Decimal64";
464   case DeclSpec::TST_decimal128:  return "_Decimal128";
465   case DeclSpec::TST_enum:        return "enum";
466   case DeclSpec::TST_class:       return "class";
467   case DeclSpec::TST_union:       return "union";
468   case DeclSpec::TST_struct:      return "struct";
469   case DeclSpec::TST_interface:   return "__interface";
470   case DeclSpec::TST_typename:    return "type-name";
471   case DeclSpec::TST_typeofType:
472   case DeclSpec::TST_typeofExpr:  return "typeof";
473   case DeclSpec::TST_auto:        return "auto";
474   case DeclSpec::TST_decltype:    return "(decltype)";
475   case DeclSpec::TST_decltype_auto: return "decltype(auto)";
476   case DeclSpec::TST_underlyingType: return "__underlying_type";
477   case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
478   case DeclSpec::TST_atomic: return "_Atomic";
479   case DeclSpec::TST_error:       return "(error)";
480   }
481   llvm_unreachable("Unknown typespec!");
482 }
483 
484 const char *DeclSpec::getSpecifierName(TQ T) {
485   switch (T) {
486   case DeclSpec::TQ_unspecified: return "unspecified";
487   case DeclSpec::TQ_const:       return "const";
488   case DeclSpec::TQ_restrict:    return "restrict";
489   case DeclSpec::TQ_volatile:    return "volatile";
490   case DeclSpec::TQ_atomic:      return "_Atomic";
491   }
492   llvm_unreachable("Unknown typespec!");
493 }
494 
495 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
496                                    const char *&PrevSpec,
497                                    unsigned &DiagID,
498                                    const PrintingPolicy &Policy) {
499   // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
500   // specifiers are not supported.
501   // It seems sensible to prohibit private_extern too
502   // The cl_clang_storage_class_specifiers extension enables support for
503   // these storage-class specifiers.
504   // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
505   // specifiers are not supported."
506   if (S.getLangOpts().OpenCL &&
507       !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
508     switch (SC) {
509     case SCS_extern:
510     case SCS_private_extern:
511     case SCS_static:
512         if (S.getLangOpts().OpenCLVersion < 120) {
513           DiagID   = diag::err_opencl_unknown_type_specifier;
514           PrevSpec = getSpecifierName(SC);
515           return true;
516         }
517         break;
518     case SCS_auto:
519     case SCS_register:
520       DiagID   = diag::err_opencl_unknown_type_specifier;
521       PrevSpec = getSpecifierName(SC);
522       return true;
523     default:
524       break;
525     }
526   }
527 
528   if (StorageClassSpec != SCS_unspecified) {
529     // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
530     bool isInvalid = true;
531     if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
532       if (SC == SCS_auto)
533         return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy);
534       if (StorageClassSpec == SCS_auto) {
535         isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
536                                     PrevSpec, DiagID, Policy);
537         assert(!isInvalid && "auto SCS -> TST recovery failed");
538       }
539     }
540 
541     // Changing storage class is allowed only if the previous one
542     // was the 'extern' that is part of a linkage specification and
543     // the new storage class is 'typedef'.
544     if (isInvalid &&
545         !(SCS_extern_in_linkage_spec &&
546           StorageClassSpec == SCS_extern &&
547           SC == SCS_typedef))
548       return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
549   }
550   StorageClassSpec = SC;
551   StorageClassSpecLoc = Loc;
552   assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
553   return false;
554 }
555 
556 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
557                                          const char *&PrevSpec,
558                                          unsigned &DiagID) {
559   if (ThreadStorageClassSpec != TSCS_unspecified)
560     return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
561 
562   ThreadStorageClassSpec = TSC;
563   ThreadStorageClassSpecLoc = Loc;
564   return false;
565 }
566 
567 /// These methods set the specified attribute of the DeclSpec, but return true
568 /// and ignore the request if invalid (e.g. "extern" then "auto" is
569 /// specified).
570 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
571                                 const char *&PrevSpec,
572                                 unsigned &DiagID,
573                                 const PrintingPolicy &Policy) {
574   // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
575   // for 'long long' we will keep the source location of the first 'long'.
576   if (TypeSpecWidth == TSW_unspecified)
577     TSWLoc = Loc;
578   // Allow turning long -> long long.
579   else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
580     return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
581   TypeSpecWidth = W;
582   return false;
583 }
584 
585 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
586                                   const char *&PrevSpec,
587                                   unsigned &DiagID) {
588   if (TypeSpecComplex != TSC_unspecified)
589     return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
590   TypeSpecComplex = C;
591   TSCLoc = Loc;
592   return false;
593 }
594 
595 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
596                                const char *&PrevSpec,
597                                unsigned &DiagID) {
598   if (TypeSpecSign != TSS_unspecified)
599     return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
600   TypeSpecSign = S;
601   TSSLoc = Loc;
602   return false;
603 }
604 
605 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
606                                const char *&PrevSpec,
607                                unsigned &DiagID,
608                                ParsedType Rep,
609                                const PrintingPolicy &Policy) {
610   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy);
611 }
612 
613 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
614                                SourceLocation TagNameLoc,
615                                const char *&PrevSpec,
616                                unsigned &DiagID,
617                                ParsedType Rep,
618                                const PrintingPolicy &Policy) {
619   assert(isTypeRep(T) && "T does not store a type");
620   assert(Rep && "no type provided!");
621   if (TypeSpecType != TST_unspecified) {
622     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
623     DiagID = diag::err_invalid_decl_spec_combination;
624     return true;
625   }
626   TypeSpecType = T;
627   TypeRep = Rep;
628   TSTLoc = TagKwLoc;
629   TSTNameLoc = TagNameLoc;
630   TypeSpecOwned = false;
631   return false;
632 }
633 
634 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
635                                const char *&PrevSpec,
636                                unsigned &DiagID,
637                                Expr *Rep,
638                                const PrintingPolicy &Policy) {
639   assert(isExprRep(T) && "T does not store an expr");
640   assert(Rep && "no expression provided!");
641   if (TypeSpecType != TST_unspecified) {
642     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
643     DiagID = diag::err_invalid_decl_spec_combination;
644     return true;
645   }
646   TypeSpecType = T;
647   ExprRep = Rep;
648   TSTLoc = Loc;
649   TSTNameLoc = Loc;
650   TypeSpecOwned = false;
651   return false;
652 }
653 
654 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
655                                const char *&PrevSpec,
656                                unsigned &DiagID,
657                                Decl *Rep, bool Owned,
658                                const PrintingPolicy &Policy) {
659   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy);
660 }
661 
662 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
663                                SourceLocation TagNameLoc,
664                                const char *&PrevSpec,
665                                unsigned &DiagID,
666                                Decl *Rep, bool Owned,
667                                const PrintingPolicy &Policy) {
668   assert(isDeclRep(T) && "T does not store a decl");
669   // Unlike the other cases, we don't assert that we actually get a decl.
670 
671   if (TypeSpecType != TST_unspecified) {
672     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
673     DiagID = diag::err_invalid_decl_spec_combination;
674     return true;
675   }
676   TypeSpecType = T;
677   DeclRep = Rep;
678   TSTLoc = TagKwLoc;
679   TSTNameLoc = TagNameLoc;
680   TypeSpecOwned = Owned && Rep != nullptr;
681   return false;
682 }
683 
684 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
685                                const char *&PrevSpec,
686                                unsigned &DiagID,
687                                const PrintingPolicy &Policy) {
688   assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
689          "rep required for these type-spec kinds!");
690   if (TypeSpecType != TST_unspecified) {
691     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
692     DiagID = diag::err_invalid_decl_spec_combination;
693     return true;
694   }
695   TSTLoc = Loc;
696   TSTNameLoc = Loc;
697   if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
698     TypeAltiVecBool = true;
699     return false;
700   }
701   TypeSpecType = T;
702   TypeSpecOwned = false;
703   return false;
704 }
705 
706 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
707                           const char *&PrevSpec, unsigned &DiagID,
708                           const PrintingPolicy &Policy) {
709   if (TypeSpecType != TST_unspecified) {
710     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
711     DiagID = diag::err_invalid_vector_decl_spec_combination;
712     return true;
713   }
714   TypeAltiVecVector = isAltiVecVector;
715   AltiVecLoc = Loc;
716   return false;
717 }
718 
719 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
720                           const char *&PrevSpec, unsigned &DiagID,
721                           const PrintingPolicy &Policy) {
722   if (!TypeAltiVecVector || TypeAltiVecPixel ||
723       (TypeSpecType != TST_unspecified)) {
724     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
725     DiagID = diag::err_invalid_pixel_decl_spec_combination;
726     return true;
727   }
728   TypeAltiVecPixel = isAltiVecPixel;
729   TSTLoc = Loc;
730   TSTNameLoc = Loc;
731   return false;
732 }
733 
734 bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
735                                   const char *&PrevSpec, unsigned &DiagID,
736                                   const PrintingPolicy &Policy) {
737   if (!TypeAltiVecVector || TypeAltiVecBool ||
738       (TypeSpecType != TST_unspecified)) {
739     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
740     DiagID = diag::err_invalid_vector_bool_decl_spec;
741     return true;
742   }
743   TypeAltiVecBool = isAltiVecBool;
744   TSTLoc = Loc;
745   TSTNameLoc = Loc;
746   return false;
747 }
748 
749 bool DeclSpec::SetTypeSpecError() {
750   TypeSpecType = TST_error;
751   TypeSpecOwned = false;
752   TSTLoc = SourceLocation();
753   TSTNameLoc = SourceLocation();
754   return false;
755 }
756 
757 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
758                            unsigned &DiagID, const LangOptions &Lang) {
759   // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
760   // C++.  However, since this is likely not what the user intended, we will
761   // always warn.  We do not need to set the qualifier's location since we
762   // already have it.
763   if (TypeQualifiers & T) {
764     bool IsExtension = true;
765     if (Lang.C99)
766       IsExtension = false;
767     return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
768   }
769   TypeQualifiers |= T;
770 
771   switch (T) {
772   case TQ_unspecified: break;
773   case TQ_const:    TQ_constLoc = Loc; return false;
774   case TQ_restrict: TQ_restrictLoc = Loc; return false;
775   case TQ_volatile: TQ_volatileLoc = Loc; return false;
776   case TQ_atomic:   TQ_atomicLoc = Loc; return false;
777   }
778 
779   llvm_unreachable("Unknown type qualifier!");
780 }
781 
782 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
783                                      unsigned &DiagID) {
784   // 'inline inline' is ok.  However, since this is likely not what the user
785   // intended, we will always warn, similar to duplicates of type qualifiers.
786   if (FS_inline_specified) {
787     DiagID = diag::warn_duplicate_declspec;
788     PrevSpec = "inline";
789     return true;
790   }
791   FS_inline_specified = true;
792   FS_inlineLoc = Loc;
793   return false;
794 }
795 
796 bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
797                                           unsigned &DiagID) {
798   if (FS_forceinline_specified) {
799     DiagID = diag::warn_duplicate_declspec;
800     PrevSpec = "__forceinline";
801     return true;
802   }
803   FS_forceinline_specified = true;
804   FS_forceinlineLoc = Loc;
805   return false;
806 }
807 
808 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
809                                       const char *&PrevSpec,
810                                       unsigned &DiagID) {
811   // 'virtual virtual' is ok, but warn as this is likely not what the user
812   // intended.
813   if (FS_virtual_specified) {
814     DiagID = diag::warn_duplicate_declspec;
815     PrevSpec = "virtual";
816     return true;
817   }
818   FS_virtual_specified = true;
819   FS_virtualLoc = Loc;
820   return false;
821 }
822 
823 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
824                                        const char *&PrevSpec,
825                                        unsigned &DiagID) {
826   // 'explicit explicit' is ok, but warn as this is likely not what the user
827   // intended.
828   if (FS_explicit_specified) {
829     DiagID = diag::warn_duplicate_declspec;
830     PrevSpec = "explicit";
831     return true;
832   }
833   FS_explicit_specified = true;
834   FS_explicitLoc = Loc;
835   return false;
836 }
837 
838 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
839                                        const char *&PrevSpec,
840                                        unsigned &DiagID) {
841   // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
842   // intended.
843   if (FS_noreturn_specified) {
844     DiagID = diag::warn_duplicate_declspec;
845     PrevSpec = "_Noreturn";
846     return true;
847   }
848   FS_noreturn_specified = true;
849   FS_noreturnLoc = Loc;
850   return false;
851 }
852 
853 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
854                              unsigned &DiagID) {
855   if (Friend_specified) {
856     PrevSpec = "friend";
857     // Keep the later location, so that we can later diagnose ill-formed
858     // declarations like 'friend class X friend;'. Per [class.friend]p3,
859     // 'friend' must be the first token in a friend declaration that is
860     // not a function declaration.
861     FriendLoc = Loc;
862     DiagID = diag::warn_duplicate_declspec;
863     return true;
864   }
865 
866   Friend_specified = true;
867   FriendLoc = Loc;
868   return false;
869 }
870 
871 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
872                                     unsigned &DiagID) {
873   if (isModulePrivateSpecified()) {
874     PrevSpec = "__module_private__";
875     DiagID = diag::ext_duplicate_declspec;
876     return true;
877   }
878 
879   ModulePrivateLoc = Loc;
880   return false;
881 }
882 
883 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
884                                 unsigned &DiagID) {
885   // 'constexpr constexpr' is ok, but warn as this is likely not what the user
886   // intended.
887   if (Constexpr_specified) {
888     DiagID = diag::warn_duplicate_declspec;
889     PrevSpec = "constexpr";
890     return true;
891   }
892   Constexpr_specified = true;
893   ConstexprLoc = Loc;
894   return false;
895 }
896 
897 bool DeclSpec::SetConceptSpec(SourceLocation Loc, const char *&PrevSpec,
898                               unsigned &DiagID) {
899   if (Concept_specified) {
900     DiagID = diag::ext_duplicate_declspec;
901     PrevSpec = "concept";
902     return true;
903   }
904   Concept_specified = true;
905   ConceptLoc = Loc;
906   return false;
907 }
908 
909 void DeclSpec::SaveWrittenBuiltinSpecs() {
910   writtenBS.Sign = getTypeSpecSign();
911   writtenBS.Width = getTypeSpecWidth();
912   writtenBS.Type = getTypeSpecType();
913   // Search the list of attributes for the presence of a mode attribute.
914   writtenBS.ModeAttr = false;
915   AttributeList* attrs = getAttributes().getList();
916   while (attrs) {
917     if (attrs->getKind() == AttributeList::AT_Mode) {
918       writtenBS.ModeAttr = true;
919       break;
920     }
921     attrs = attrs->getNext();
922   }
923 }
924 
925 /// Finish - This does final analysis of the declspec, rejecting things like
926 /// "_Imaginary" (lacking an FP type).  This returns a diagnostic to issue or
927 /// diag::NUM_DIAGNOSTICS if there is no error.  After calling this method,
928 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
929 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP, const PrintingPolicy &Policy) {
930   // Before possibly changing their values, save specs as written.
931   SaveWrittenBuiltinSpecs();
932 
933   // Check the type specifier components first.
934 
935   // If decltype(auto) is used, no other type specifiers are permitted.
936   if (TypeSpecType == TST_decltype_auto &&
937       (TypeSpecWidth != TSW_unspecified ||
938        TypeSpecComplex != TSC_unspecified ||
939        TypeSpecSign != TSS_unspecified ||
940        TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
941        TypeQualifiers)) {
942     const unsigned NumLocs = 8;
943     SourceLocation ExtraLocs[NumLocs] = {
944       TSWLoc, TSCLoc, TSSLoc, AltiVecLoc,
945       TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc
946     };
947     FixItHint Hints[NumLocs];
948     SourceLocation FirstLoc;
949     for (unsigned I = 0; I != NumLocs; ++I) {
950       if (!ExtraLocs[I].isInvalid()) {
951         if (FirstLoc.isInvalid() ||
952             PP.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
953                                                             FirstLoc))
954           FirstLoc = ExtraLocs[I];
955         Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
956       }
957     }
958     TypeSpecWidth = TSW_unspecified;
959     TypeSpecComplex = TSC_unspecified;
960     TypeSpecSign = TSS_unspecified;
961     TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
962     TypeQualifiers = 0;
963     Diag(D, TSTLoc, diag::err_decltype_auto_cannot_be_combined)
964       << Hints[0] << Hints[1] << Hints[2] << Hints[3]
965       << Hints[4] << Hints[5] << Hints[6] << Hints[7];
966   }
967 
968   // Validate and finalize AltiVec vector declspec.
969   if (TypeAltiVecVector) {
970     if (TypeAltiVecBool) {
971       // Sign specifiers are not allowed with vector bool. (PIM 2.1)
972       if (TypeSpecSign != TSS_unspecified) {
973         Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
974           << getSpecifierName((TSS)TypeSpecSign);
975       }
976 
977       // Only char/int are valid with vector bool. (PIM 2.1)
978       if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
979            (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
980         Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
981           << (TypeAltiVecPixel ? "__pixel" :
982                                  getSpecifierName((TST)TypeSpecType, Policy));
983       }
984 
985       // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
986       if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short) &&
987           (TypeSpecWidth != TSW_longlong))
988         Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
989           << getSpecifierName((TSW)TypeSpecWidth);
990 
991       // vector bool long long requires VSX support or ZVector.
992       if ((TypeSpecWidth == TSW_longlong) &&
993           (!PP.getTargetInfo().hasFeature("vsx")) &&
994           (!PP.getTargetInfo().hasFeature("power8-vector")) &&
995           !PP.getLangOpts().ZVector)
996         Diag(D, TSTLoc, diag::err_invalid_vector_long_long_decl_spec);
997 
998       // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
999       if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1000           (TypeSpecWidth != TSW_unspecified))
1001         TypeSpecSign = TSS_unsigned;
1002     } else if (TypeSpecType == TST_double) {
1003       // vector long double and vector long long double are never allowed.
1004       // vector double is OK for Power7 and later, and ZVector.
1005       if (TypeSpecWidth == TSW_long || TypeSpecWidth == TSW_longlong)
1006         Diag(D, TSWLoc, diag::err_invalid_vector_long_double_decl_spec);
1007       else if (!PP.getTargetInfo().hasFeature("vsx") &&
1008                !PP.getLangOpts().ZVector)
1009         Diag(D, TSTLoc, diag::err_invalid_vector_double_decl_spec);
1010     } else if (TypeSpecType == TST_float) {
1011       // vector float is unsupported for ZVector.
1012       if (PP.getLangOpts().ZVector)
1013         Diag(D, TSTLoc, diag::err_invalid_vector_float_decl_spec);
1014     } else if (TypeSpecWidth == TSW_long) {
1015       // vector long is unsupported for ZVector and deprecated for AltiVec.
1016       if (PP.getLangOpts().ZVector)
1017         Diag(D, TSWLoc, diag::err_invalid_vector_long_decl_spec);
1018       else
1019         Diag(D, TSWLoc, diag::warn_vector_long_decl_spec_combination)
1020           << getSpecifierName((TST)TypeSpecType, Policy);
1021     }
1022 
1023     if (TypeAltiVecPixel) {
1024       //TODO: perform validation
1025       TypeSpecType = TST_int;
1026       TypeSpecSign = TSS_unsigned;
1027       TypeSpecWidth = TSW_short;
1028       TypeSpecOwned = false;
1029     }
1030   }
1031 
1032   // signed/unsigned are only valid with int/char/wchar_t.
1033   if (TypeSpecSign != TSS_unspecified) {
1034     if (TypeSpecType == TST_unspecified)
1035       TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
1036     else if (TypeSpecType != TST_int  && TypeSpecType != TST_int128 &&
1037              TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
1038       Diag(D, TSSLoc, diag::err_invalid_sign_spec)
1039         << getSpecifierName((TST)TypeSpecType, Policy);
1040       // signed double -> double.
1041       TypeSpecSign = TSS_unspecified;
1042     }
1043   }
1044 
1045   // Validate the width of the type.
1046   switch (TypeSpecWidth) {
1047   case TSW_unspecified: break;
1048   case TSW_short:    // short int
1049   case TSW_longlong: // long long int
1050     if (TypeSpecType == TST_unspecified)
1051       TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1052     else if (TypeSpecType != TST_int) {
1053       Diag(D, TSWLoc,
1054            TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
1055                                       : diag::err_invalid_longlong_spec)
1056         <<  getSpecifierName((TST)TypeSpecType, Policy);
1057       TypeSpecType = TST_int;
1058       TypeSpecOwned = false;
1059     }
1060     break;
1061   case TSW_long:  // long double, long int
1062     if (TypeSpecType == TST_unspecified)
1063       TypeSpecType = TST_int;  // long -> long int.
1064     else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
1065       Diag(D, TSWLoc, diag::err_invalid_long_spec)
1066         << getSpecifierName((TST)TypeSpecType, Policy);
1067       TypeSpecType = TST_int;
1068       TypeSpecOwned = false;
1069     }
1070     break;
1071   }
1072 
1073   // TODO: if the implementation does not implement _Complex or _Imaginary,
1074   // disallow their use.  Need information about the backend.
1075   if (TypeSpecComplex != TSC_unspecified) {
1076     if (TypeSpecType == TST_unspecified) {
1077       Diag(D, TSCLoc, diag::ext_plain_complex)
1078         << FixItHint::CreateInsertion(
1079                               PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
1080                                                  " double");
1081       TypeSpecType = TST_double;   // _Complex -> _Complex double.
1082     } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1083       // Note that this intentionally doesn't include _Complex _Bool.
1084       if (!PP.getLangOpts().CPlusPlus)
1085         Diag(D, TSTLoc, diag::ext_integer_complex);
1086     } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
1087       Diag(D, TSCLoc, diag::err_invalid_complex_spec)
1088         << getSpecifierName((TST)TypeSpecType, Policy);
1089       TypeSpecComplex = TSC_unspecified;
1090     }
1091   }
1092 
1093   // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1094   // _Thread_local can only appear with the 'static' and 'extern' storage class
1095   // specifiers. We also allow __private_extern__ as an extension.
1096   if (ThreadStorageClassSpec != TSCS_unspecified) {
1097     switch (StorageClassSpec) {
1098     case SCS_unspecified:
1099     case SCS_extern:
1100     case SCS_private_extern:
1101     case SCS_static:
1102       break;
1103     default:
1104       if (PP.getSourceManager().isBeforeInTranslationUnit(
1105             getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
1106         Diag(D, getStorageClassSpecLoc(),
1107              diag::err_invalid_decl_spec_combination)
1108           << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1109           << SourceRange(getThreadStorageClassSpecLoc());
1110       else
1111         Diag(D, getThreadStorageClassSpecLoc(),
1112              diag::err_invalid_decl_spec_combination)
1113           << DeclSpec::getSpecifierName(getStorageClassSpec())
1114           << SourceRange(getStorageClassSpecLoc());
1115       // Discard the thread storage class specifier to recover.
1116       ThreadStorageClassSpec = TSCS_unspecified;
1117       ThreadStorageClassSpecLoc = SourceLocation();
1118     }
1119   }
1120 
1121   // If no type specifier was provided and we're parsing a language where
1122   // the type specifier is not optional, but we got 'auto' as a storage
1123   // class specifier, then assume this is an attempt to use C++0x's 'auto'
1124   // type specifier.
1125   if (PP.getLangOpts().CPlusPlus &&
1126       TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1127     TypeSpecType = TST_auto;
1128     StorageClassSpec = SCS_unspecified;
1129     TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1130     StorageClassSpecLoc = SourceLocation();
1131   }
1132   // Diagnose if we've recovered from an ill-formed 'auto' storage class
1133   // specifier in a pre-C++11 dialect of C++.
1134   if (!PP.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1135     Diag(D, TSTLoc, diag::ext_auto_type_specifier);
1136   if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus11 &&
1137       StorageClassSpec == SCS_auto)
1138     Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
1139       << FixItHint::CreateRemoval(StorageClassSpecLoc);
1140   if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1141     Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
1142       << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1143   if (Constexpr_specified)
1144     Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
1145 
1146   // C++ [class.friend]p6:
1147   //   No storage-class-specifier shall appear in the decl-specifier-seq
1148   //   of a friend declaration.
1149   if (isFriendSpecified() &&
1150       (getStorageClassSpec() || getThreadStorageClassSpec())) {
1151     SmallString<32> SpecName;
1152     SourceLocation SCLoc;
1153     FixItHint StorageHint, ThreadHint;
1154 
1155     if (DeclSpec::SCS SC = getStorageClassSpec()) {
1156       SpecName = getSpecifierName(SC);
1157       SCLoc = getStorageClassSpecLoc();
1158       StorageHint = FixItHint::CreateRemoval(SCLoc);
1159     }
1160 
1161     if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1162       if (!SpecName.empty()) SpecName += " ";
1163       SpecName += getSpecifierName(TSC);
1164       SCLoc = getThreadStorageClassSpecLoc();
1165       ThreadHint = FixItHint::CreateRemoval(SCLoc);
1166     }
1167 
1168     Diag(D, SCLoc, diag::err_friend_decl_spec)
1169       << SpecName << StorageHint << ThreadHint;
1170 
1171     ClearStorageClassSpecs();
1172   }
1173 
1174   // C++11 [dcl.fct.spec]p5:
1175   //   The virtual specifier shall be used only in the initial
1176   //   declaration of a non-static class member function;
1177   // C++11 [dcl.fct.spec]p6:
1178   //   The explicit specifier shall be used only in the declaration of
1179   //   a constructor or conversion function within its class
1180   //   definition;
1181   if (isFriendSpecified() && (isVirtualSpecified() || isExplicitSpecified())) {
1182     StringRef Keyword;
1183     SourceLocation SCLoc;
1184 
1185     if (isVirtualSpecified()) {
1186       Keyword = "virtual";
1187       SCLoc = getVirtualSpecLoc();
1188     } else {
1189       Keyword = "explicit";
1190       SCLoc = getExplicitSpecLoc();
1191     }
1192 
1193     FixItHint Hint = FixItHint::CreateRemoval(SCLoc);
1194     Diag(D, SCLoc, diag::err_friend_decl_spec)
1195       << Keyword << Hint;
1196 
1197     FS_virtual_specified = FS_explicit_specified = false;
1198     FS_virtualLoc = FS_explicitLoc = SourceLocation();
1199   }
1200 
1201   assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1202 
1203   // Okay, now we can infer the real type.
1204 
1205   // TODO: return "auto function" and other bad things based on the real type.
1206 
1207   // 'data definition has no type or storage class'?
1208 }
1209 
1210 bool DeclSpec::isMissingDeclaratorOk() {
1211   TST tst = getTypeSpecType();
1212   return isDeclRep(tst) && getRepAsDecl() != nullptr &&
1213     StorageClassSpec != DeclSpec::SCS_typedef;
1214 }
1215 
1216 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1217                                           OverloadedOperatorKind Op,
1218                                           SourceLocation SymbolLocations[3]) {
1219   Kind = IK_OperatorFunctionId;
1220   StartLocation = OperatorLoc;
1221   EndLocation = OperatorLoc;
1222   OperatorFunctionId.Operator = Op;
1223   for (unsigned I = 0; I != 3; ++I) {
1224     OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
1225 
1226     if (SymbolLocations[I].isValid())
1227       EndLocation = SymbolLocations[I];
1228   }
1229 }
1230 
1231 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1232                                   const char *&PrevSpec) {
1233   if (!FirstLocation.isValid())
1234     FirstLocation = Loc;
1235   LastLocation = Loc;
1236   LastSpecifier = VS;
1237 
1238   if (Specifiers & VS) {
1239     PrevSpec = getSpecifierName(VS);
1240     return true;
1241   }
1242 
1243   Specifiers |= VS;
1244 
1245   switch (VS) {
1246   default: llvm_unreachable("Unknown specifier!");
1247   case VS_Override: VS_overrideLoc = Loc; break;
1248   case VS_Sealed:
1249   case VS_Final:    VS_finalLoc = Loc; break;
1250   }
1251 
1252   return false;
1253 }
1254 
1255 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1256   switch (VS) {
1257   default: llvm_unreachable("Unknown specifier");
1258   case VS_Override: return "override";
1259   case VS_Final: return "final";
1260   case VS_Sealed: return "sealed";
1261   }
1262 }
1263