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