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