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