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