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