1 //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements semantic analysis for declaration specifiers.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Sema/DeclSpec.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/NestedNameSpecifier.h"
19 #include "clang/AST/TypeLoc.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
23 #include "clang/Sema/LocInfoType.h"
24 #include "clang/Sema/ParsedTemplate.h"
25 #include "clang/Sema/Sema.h"
26 #include "clang/Sema/SemaDiagnostic.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include <cstring>
31 using namespace clang;
32 
33 
34 static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
35                               unsigned DiagID) {
36   return D.Report(Loc, DiagID);
37 }
38 
39 
40 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
41   assert(TemplateId && "NULL template-id annotation?");
42   Kind = IK_TemplateId;
43   this->TemplateId = TemplateId;
44   StartLocation = TemplateId->TemplateNameLoc;
45   EndLocation = TemplateId->RAngleLoc;
46 }
47 
48 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
49   assert(TemplateId && "NULL template-id annotation?");
50   Kind = IK_ConstructorTemplateId;
51   this->TemplateId = TemplateId;
52   StartLocation = TemplateId->TemplateNameLoc;
53   EndLocation = TemplateId->RAngleLoc;
54 }
55 
56 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
57                           TypeLoc TL, SourceLocation ColonColonLoc) {
58   Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
59   if (Range.getBegin().isInvalid())
60     Range.setBegin(TL.getBeginLoc());
61   Range.setEnd(ColonColonLoc);
62 
63   assert(Range == Builder.getSourceRange() &&
64          "NestedNameSpecifierLoc range computation incorrect");
65 }
66 
67 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
68                           SourceLocation IdentifierLoc,
69                           SourceLocation ColonColonLoc) {
70   Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
71 
72   if (Range.getBegin().isInvalid())
73     Range.setBegin(IdentifierLoc);
74   Range.setEnd(ColonColonLoc);
75 
76   assert(Range == Builder.getSourceRange() &&
77          "NestedNameSpecifierLoc range computation incorrect");
78 }
79 
80 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
81                           SourceLocation NamespaceLoc,
82                           SourceLocation ColonColonLoc) {
83   Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
84 
85   if (Range.getBegin().isInvalid())
86     Range.setBegin(NamespaceLoc);
87   Range.setEnd(ColonColonLoc);
88 
89   assert(Range == Builder.getSourceRange() &&
90          "NestedNameSpecifierLoc range computation incorrect");
91 }
92 
93 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
94                           SourceLocation AliasLoc,
95                           SourceLocation ColonColonLoc) {
96   Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
97 
98   if (Range.getBegin().isInvalid())
99     Range.setBegin(AliasLoc);
100   Range.setEnd(ColonColonLoc);
101 
102   assert(Range == Builder.getSourceRange() &&
103          "NestedNameSpecifierLoc range computation incorrect");
104 }
105 
106 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
107                               SourceLocation ColonColonLoc) {
108   Builder.MakeGlobal(Context, ColonColonLoc);
109 
110   Range = SourceRange(ColonColonLoc);
111 
112   assert(Range == Builder.getSourceRange() &&
113          "NestedNameSpecifierLoc range computation incorrect");
114 }
115 
116 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
117                                NestedNameSpecifier *Qualifier, SourceRange R) {
118   Builder.MakeTrivial(Context, Qualifier, R);
119   Range = R;
120 }
121 
122 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
123   if (!Other) {
124     Range = SourceRange();
125     Builder.Clear();
126     return;
127   }
128 
129   Range = Other.getSourceRange();
130   Builder.Adopt(Other);
131 }
132 
133 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
134   if (!Builder.getRepresentation())
135     return SourceLocation();
136   return Builder.getTemporary().getLocalBeginLoc();
137 }
138 
139 NestedNameSpecifierLoc
140 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
141   if (!Builder.getRepresentation())
142     return NestedNameSpecifierLoc();
143 
144   return Builder.getWithLocInContext(Context);
145 }
146 
147 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
148 /// "TheDeclarator" is the declarator that this will be added to.
149 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
150                                              bool isAmbiguous,
151                                              SourceLocation LParenLoc,
152                                              ParamInfo *Params,
153                                              unsigned NumParams,
154                                              SourceLocation EllipsisLoc,
155                                              SourceLocation RParenLoc,
156                                              unsigned TypeQuals,
157                                              bool RefQualifierIsLvalueRef,
158                                              SourceLocation RefQualifierLoc,
159                                              SourceLocation ConstQualifierLoc,
160                                              SourceLocation
161                                                  VolatileQualifierLoc,
162                                              SourceLocation MutableLoc,
163                                              ExceptionSpecificationType
164                                                  ESpecType,
165                                              SourceLocation ESpecLoc,
166                                              ParsedType *Exceptions,
167                                              SourceRange *ExceptionRanges,
168                                              unsigned NumExceptions,
169                                              Expr *NoexceptExpr,
170                                              SourceLocation LocalRangeBegin,
171                                              SourceLocation LocalRangeEnd,
172                                              Declarator &TheDeclarator,
173                                              TypeResult TrailingReturnType) {
174   assert(!(TypeQuals & DeclSpec::TQ_atomic) &&
175          "function cannot have _Atomic qualifier");
176 
177   DeclaratorChunk I;
178   I.Kind                        = Function;
179   I.Loc                         = LocalRangeBegin;
180   I.EndLoc                      = LocalRangeEnd;
181   I.Fun.AttrList                = nullptr;
182   I.Fun.hasPrototype            = hasProto;
183   I.Fun.isVariadic              = EllipsisLoc.isValid();
184   I.Fun.isAmbiguous             = isAmbiguous;
185   I.Fun.LParenLoc               = LParenLoc.getRawEncoding();
186   I.Fun.EllipsisLoc             = EllipsisLoc.getRawEncoding();
187   I.Fun.RParenLoc               = RParenLoc.getRawEncoding();
188   I.Fun.DeleteParams            = false;
189   I.Fun.TypeQuals               = TypeQuals;
190   I.Fun.NumParams               = NumParams;
191   I.Fun.Params                  = nullptr;
192   I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
193   I.Fun.RefQualifierLoc         = RefQualifierLoc.getRawEncoding();
194   I.Fun.ConstQualifierLoc       = ConstQualifierLoc.getRawEncoding();
195   I.Fun.VolatileQualifierLoc    = VolatileQualifierLoc.getRawEncoding();
196   I.Fun.MutableLoc              = MutableLoc.getRawEncoding();
197   I.Fun.ExceptionSpecType       = ESpecType;
198   I.Fun.ExceptionSpecLoc        = ESpecLoc.getRawEncoding();
199   I.Fun.NumExceptions           = 0;
200   I.Fun.Exceptions              = nullptr;
201   I.Fun.NoexceptExpr            = nullptr;
202   I.Fun.HasTrailingReturnType   = TrailingReturnType.isUsable() ||
203                                   TrailingReturnType.isInvalid();
204   I.Fun.TrailingReturnType      = TrailingReturnType.get();
205 
206   // new[] a parameter array if needed.
207   if (NumParams) {
208     // If the 'InlineParams' in Declarator is unused and big enough, put our
209     // parameter list there (in an effort to avoid new/delete traffic).  If it
210     // is already used (consider a function returning a function pointer) or too
211     // small (function with too many parameters), go to the heap.
212     if (!TheDeclarator.InlineParamsUsed &&
213         NumParams <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
214       I.Fun.Params = TheDeclarator.InlineParams;
215       I.Fun.DeleteParams = false;
216       TheDeclarator.InlineParamsUsed = true;
217     } else {
218       I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams];
219       I.Fun.DeleteParams = true;
220     }
221     memcpy(I.Fun.Params, Params, sizeof(Params[0]) * NumParams);
222   }
223 
224   // Check what exception specification information we should actually store.
225   switch (ESpecType) {
226   default: break; // By default, save nothing.
227   case EST_Dynamic:
228     // new[] an exception array if needed
229     if (NumExceptions) {
230       I.Fun.NumExceptions = NumExceptions;
231       I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
232       for (unsigned i = 0; i != NumExceptions; ++i) {
233         I.Fun.Exceptions[i].Ty = Exceptions[i];
234         I.Fun.Exceptions[i].Range = ExceptionRanges[i];
235       }
236     }
237     break;
238 
239   case EST_ComputedNoexcept:
240     I.Fun.NoexceptExpr = NoexceptExpr;
241     break;
242   }
243   return I;
244 }
245 
246 bool Declarator::isDeclarationOfFunction() const {
247   for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
248     switch (DeclTypeInfo[i].Kind) {
249     case DeclaratorChunk::Function:
250       return true;
251     case DeclaratorChunk::Paren:
252       continue;
253     case DeclaratorChunk::Pointer:
254     case DeclaratorChunk::Reference:
255     case DeclaratorChunk::Array:
256     case DeclaratorChunk::BlockPointer:
257     case DeclaratorChunk::MemberPointer:
258       return false;
259     }
260     llvm_unreachable("Invalid type chunk");
261   }
262 
263   switch (DS.getTypeSpecType()) {
264     case TST_atomic:
265     case TST_auto:
266     case TST_bool:
267     case TST_char:
268     case TST_char16:
269     case TST_char32:
270     case TST_class:
271     case TST_decimal128:
272     case TST_decimal32:
273     case TST_decimal64:
274     case TST_double:
275     case TST_enum:
276     case TST_error:
277     case TST_float:
278     case TST_half:
279     case TST_int:
280     case TST_int128:
281     case TST_struct:
282     case TST_interface:
283     case TST_union:
284     case TST_unknown_anytype:
285     case TST_unspecified:
286     case TST_void:
287     case TST_wchar:
288       return false;
289 
290     case TST_decltype_auto:
291       // This must have an initializer, so can't be a function declaration,
292       // even if the initializer has function type.
293       return false;
294 
295     case TST_decltype:
296     case TST_typeofExpr:
297       if (Expr *E = DS.getRepAsExpr())
298         return E->getType()->isFunctionType();
299       return false;
300 
301     case TST_underlyingType:
302     case TST_typename:
303     case TST_typeofType: {
304       QualType QT = DS.getRepAsType().get();
305       if (QT.isNull())
306         return false;
307 
308       if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
309         QT = LIT->getType();
310 
311       if (QT.isNull())
312         return false;
313 
314       return QT->isFunctionType();
315     }
316   }
317 
318   llvm_unreachable("Invalid TypeSpecType!");
319 }
320 
321 bool Declarator::isStaticMember() {
322   assert(getContext() == MemberContext);
323   return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
324          CXXMethodDecl::isStaticOverloadedOperator(
325              getName().OperatorFunctionId.Operator);
326 }
327 
328 bool DeclSpec::hasTagDefinition() const {
329   if (!TypeSpecOwned)
330     return false;
331   return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
332 }
333 
334 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
335 /// declaration specifier includes.
336 ///
337 unsigned DeclSpec::getParsedSpecifiers() const {
338   unsigned Res = 0;
339   if (StorageClassSpec != SCS_unspecified ||
340       ThreadStorageClassSpec != TSCS_unspecified)
341     Res |= PQ_StorageClassSpecifier;
342 
343   if (TypeQualifiers != TQ_unspecified)
344     Res |= PQ_TypeQualifier;
345 
346   if (hasTypeSpecifier())
347     Res |= PQ_TypeSpecifier;
348 
349   if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified ||
350       FS_noreturn_specified || FS_forceinline_specified)
351     Res |= PQ_FunctionSpecifier;
352   return Res;
353 }
354 
355 template <class T> static bool BadSpecifier(T TNew, T TPrev,
356                                             const char *&PrevSpec,
357                                             unsigned &DiagID,
358                                             bool IsExtension = true) {
359   PrevSpec = DeclSpec::getSpecifierName(TPrev);
360   if (TNew != TPrev)
361     DiagID = diag::err_invalid_decl_spec_combination;
362   else
363     DiagID = IsExtension ? diag::ext_duplicate_declspec :
364                            diag::warn_duplicate_declspec;
365   return true;
366 }
367 
368 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
369   switch (S) {
370   case DeclSpec::SCS_unspecified: return "unspecified";
371   case DeclSpec::SCS_typedef:     return "typedef";
372   case DeclSpec::SCS_extern:      return "extern";
373   case DeclSpec::SCS_static:      return "static";
374   case DeclSpec::SCS_auto:        return "auto";
375   case DeclSpec::SCS_register:    return "register";
376   case DeclSpec::SCS_private_extern: return "__private_extern__";
377   case DeclSpec::SCS_mutable:     return "mutable";
378   }
379   llvm_unreachable("Unknown typespec!");
380 }
381 
382 const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
383   switch (S) {
384   case DeclSpec::TSCS_unspecified:   return "unspecified";
385   case DeclSpec::TSCS___thread:      return "__thread";
386   case DeclSpec::TSCS_thread_local:  return "thread_local";
387   case DeclSpec::TSCS__Thread_local: return "_Thread_local";
388   }
389   llvm_unreachable("Unknown typespec!");
390 }
391 
392 const char *DeclSpec::getSpecifierName(TSW W) {
393   switch (W) {
394   case TSW_unspecified: return "unspecified";
395   case TSW_short:       return "short";
396   case TSW_long:        return "long";
397   case TSW_longlong:    return "long long";
398   }
399   llvm_unreachable("Unknown typespec!");
400 }
401 
402 const char *DeclSpec::getSpecifierName(TSC C) {
403   switch (C) {
404   case TSC_unspecified: return "unspecified";
405   case TSC_imaginary:   return "imaginary";
406   case TSC_complex:     return "complex";
407   }
408   llvm_unreachable("Unknown typespec!");
409 }
410 
411 
412 const char *DeclSpec::getSpecifierName(TSS S) {
413   switch (S) {
414   case TSS_unspecified: return "unspecified";
415   case TSS_signed:      return "signed";
416   case TSS_unsigned:    return "unsigned";
417   }
418   llvm_unreachable("Unknown typespec!");
419 }
420 
421 const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
422                                        const PrintingPolicy &Policy) {
423   switch (T) {
424   case DeclSpec::TST_unspecified: return "unspecified";
425   case DeclSpec::TST_void:        return "void";
426   case DeclSpec::TST_char:        return "char";
427   case DeclSpec::TST_wchar:       return Policy.MSWChar ? "__wchar_t" : "wchar_t";
428   case DeclSpec::TST_char16:      return "char16_t";
429   case DeclSpec::TST_char32:      return "char32_t";
430   case DeclSpec::TST_int:         return "int";
431   case DeclSpec::TST_int128:      return "__int128";
432   case DeclSpec::TST_half:        return "half";
433   case DeclSpec::TST_float:       return "float";
434   case DeclSpec::TST_double:      return "double";
435   case DeclSpec::TST_bool:        return Policy.Bool ? "bool" : "_Bool";
436   case DeclSpec::TST_decimal32:   return "_Decimal32";
437   case DeclSpec::TST_decimal64:   return "_Decimal64";
438   case DeclSpec::TST_decimal128:  return "_Decimal128";
439   case DeclSpec::TST_enum:        return "enum";
440   case DeclSpec::TST_class:       return "class";
441   case DeclSpec::TST_union:       return "union";
442   case DeclSpec::TST_struct:      return "struct";
443   case DeclSpec::TST_interface:   return "__interface";
444   case DeclSpec::TST_typename:    return "type-name";
445   case DeclSpec::TST_typeofType:
446   case DeclSpec::TST_typeofExpr:  return "typeof";
447   case DeclSpec::TST_auto:        return "auto";
448   case DeclSpec::TST_decltype:    return "(decltype)";
449   case DeclSpec::TST_decltype_auto: return "decltype(auto)";
450   case DeclSpec::TST_underlyingType: return "__underlying_type";
451   case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
452   case DeclSpec::TST_atomic: return "_Atomic";
453   case DeclSpec::TST_error:       return "(error)";
454   }
455   llvm_unreachable("Unknown typespec!");
456 }
457 
458 const char *DeclSpec::getSpecifierName(TQ T) {
459   switch (T) {
460   case DeclSpec::TQ_unspecified: return "unspecified";
461   case DeclSpec::TQ_const:       return "const";
462   case DeclSpec::TQ_restrict:    return "restrict";
463   case DeclSpec::TQ_volatile:    return "volatile";
464   case DeclSpec::TQ_atomic:      return "_Atomic";
465   }
466   llvm_unreachable("Unknown typespec!");
467 }
468 
469 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
470                                    const char *&PrevSpec,
471                                    unsigned &DiagID,
472                                    const PrintingPolicy &Policy) {
473   // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
474   // specifiers are not supported.
475   // It seems sensible to prohibit private_extern too
476   // The cl_clang_storage_class_specifiers extension enables support for
477   // these storage-class specifiers.
478   // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
479   // specifiers are not supported."
480   if (S.getLangOpts().OpenCL &&
481       !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
482     switch (SC) {
483     case SCS_extern:
484     case SCS_private_extern:
485     case SCS_static:
486         if (S.getLangOpts().OpenCLVersion < 120) {
487           DiagID   = diag::err_not_opencl_storage_class_specifier;
488           PrevSpec = getSpecifierName(SC);
489           return true;
490         }
491         break;
492     case SCS_auto:
493     case SCS_register:
494       DiagID   = diag::err_not_opencl_storage_class_specifier;
495       PrevSpec = getSpecifierName(SC);
496       return true;
497     default:
498       break;
499     }
500   }
501 
502   if (StorageClassSpec != SCS_unspecified) {
503     // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
504     bool isInvalid = true;
505     if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
506       if (SC == SCS_auto)
507         return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy);
508       if (StorageClassSpec == SCS_auto) {
509         isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
510                                     PrevSpec, DiagID, Policy);
511         assert(!isInvalid && "auto SCS -> TST recovery failed");
512       }
513     }
514 
515     // Changing storage class is allowed only if the previous one
516     // was the 'extern' that is part of a linkage specification and
517     // the new storage class is 'typedef'.
518     if (isInvalid &&
519         !(SCS_extern_in_linkage_spec &&
520           StorageClassSpec == SCS_extern &&
521           SC == SCS_typedef))
522       return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
523   }
524   StorageClassSpec = SC;
525   StorageClassSpecLoc = Loc;
526   assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
527   return false;
528 }
529 
530 bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
531                                          const char *&PrevSpec,
532                                          unsigned &DiagID) {
533   if (ThreadStorageClassSpec != TSCS_unspecified)
534     return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
535 
536   ThreadStorageClassSpec = TSC;
537   ThreadStorageClassSpecLoc = Loc;
538   return false;
539 }
540 
541 /// These methods set the specified attribute of the DeclSpec, but return true
542 /// and ignore the request if invalid (e.g. "extern" then "auto" is
543 /// specified).
544 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
545                                 const char *&PrevSpec,
546                                 unsigned &DiagID,
547                                 const PrintingPolicy &Policy) {
548   // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
549   // for 'long long' we will keep the source location of the first 'long'.
550   if (TypeSpecWidth == TSW_unspecified)
551     TSWLoc = Loc;
552   // Allow turning long -> long long.
553   else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
554     return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
555   TypeSpecWidth = W;
556   return false;
557 }
558 
559 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
560                                   const char *&PrevSpec,
561                                   unsigned &DiagID) {
562   if (TypeSpecComplex != TSC_unspecified)
563     return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
564   TypeSpecComplex = C;
565   TSCLoc = Loc;
566   return false;
567 }
568 
569 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
570                                const char *&PrevSpec,
571                                unsigned &DiagID) {
572   if (TypeSpecSign != TSS_unspecified)
573     return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
574   TypeSpecSign = S;
575   TSSLoc = Loc;
576   return false;
577 }
578 
579 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
580                                const char *&PrevSpec,
581                                unsigned &DiagID,
582                                ParsedType Rep,
583                                const PrintingPolicy &Policy) {
584   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy);
585 }
586 
587 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
588                                SourceLocation TagNameLoc,
589                                const char *&PrevSpec,
590                                unsigned &DiagID,
591                                ParsedType Rep,
592                                const PrintingPolicy &Policy) {
593   assert(isTypeRep(T) && "T does not store a type");
594   assert(Rep && "no type provided!");
595   if (TypeSpecType != TST_unspecified) {
596     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
597     DiagID = diag::err_invalid_decl_spec_combination;
598     return true;
599   }
600   TypeSpecType = T;
601   TypeRep = Rep;
602   TSTLoc = TagKwLoc;
603   TSTNameLoc = TagNameLoc;
604   TypeSpecOwned = false;
605   return false;
606 }
607 
608 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
609                                const char *&PrevSpec,
610                                unsigned &DiagID,
611                                Expr *Rep,
612                                const PrintingPolicy &Policy) {
613   assert(isExprRep(T) && "T does not store an expr");
614   assert(Rep && "no expression provided!");
615   if (TypeSpecType != TST_unspecified) {
616     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
617     DiagID = diag::err_invalid_decl_spec_combination;
618     return true;
619   }
620   TypeSpecType = T;
621   ExprRep = Rep;
622   TSTLoc = Loc;
623   TSTNameLoc = Loc;
624   TypeSpecOwned = false;
625   return false;
626 }
627 
628 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
629                                const char *&PrevSpec,
630                                unsigned &DiagID,
631                                Decl *Rep, bool Owned,
632                                const PrintingPolicy &Policy) {
633   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy);
634 }
635 
636 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
637                                SourceLocation TagNameLoc,
638                                const char *&PrevSpec,
639                                unsigned &DiagID,
640                                Decl *Rep, bool Owned,
641                                const PrintingPolicy &Policy) {
642   assert(isDeclRep(T) && "T does not store a decl");
643   // Unlike the other cases, we don't assert that we actually get a decl.
644 
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   DeclRep = Rep;
652   TSTLoc = TagKwLoc;
653   TSTNameLoc = TagNameLoc;
654   TypeSpecOwned = Owned && Rep != nullptr;
655   return false;
656 }
657 
658 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
659                                const char *&PrevSpec,
660                                unsigned &DiagID,
661                                const PrintingPolicy &Policy) {
662   assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
663          "rep required for these type-spec kinds!");
664   if (TypeSpecType != TST_unspecified) {
665     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
666     DiagID = diag::err_invalid_decl_spec_combination;
667     return true;
668   }
669   TSTLoc = Loc;
670   TSTNameLoc = Loc;
671   if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
672     TypeAltiVecBool = true;
673     return false;
674   }
675   TypeSpecType = T;
676   TypeSpecOwned = false;
677   if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
678     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
679     DiagID = diag::err_invalid_vector_decl_spec;
680     return true;
681   }
682   return false;
683 }
684 
685 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
686                           const char *&PrevSpec, unsigned &DiagID,
687                           const PrintingPolicy &Policy) {
688   if (TypeSpecType != TST_unspecified) {
689     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
690     DiagID = diag::err_invalid_vector_decl_spec_combination;
691     return true;
692   }
693   TypeAltiVecVector = isAltiVecVector;
694   AltiVecLoc = Loc;
695   return false;
696 }
697 
698 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
699                           const char *&PrevSpec, unsigned &DiagID,
700                           const PrintingPolicy &Policy) {
701   if (!TypeAltiVecVector || TypeAltiVecPixel ||
702       (TypeSpecType != TST_unspecified)) {
703     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
704     DiagID = diag::err_invalid_pixel_decl_spec_combination;
705     return true;
706   }
707   TypeAltiVecPixel = isAltiVecPixel;
708   TSTLoc = Loc;
709   TSTNameLoc = Loc;
710   return false;
711 }
712 
713 bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
714                                   const char *&PrevSpec, unsigned &DiagID,
715                                   const PrintingPolicy &Policy) {
716   if (!TypeAltiVecVector || TypeAltiVecBool ||
717       (TypeSpecType != TST_unspecified)) {
718     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
719     DiagID = diag::err_invalid_vector_bool_decl_spec;
720     return true;
721   }
722   TypeAltiVecBool = isAltiVecBool;
723   TSTLoc = Loc;
724   TSTNameLoc = Loc;
725   return false;
726 }
727 
728 bool DeclSpec::SetTypeSpecError() {
729   TypeSpecType = TST_error;
730   TypeSpecOwned = false;
731   TSTLoc = SourceLocation();
732   TSTNameLoc = SourceLocation();
733   return false;
734 }
735 
736 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
737                            unsigned &DiagID, const LangOptions &Lang) {
738   // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
739   // C++.  However, since this is likely not what the user intended, we will
740   // always warn.  We do not need to set the qualifier's location since we
741   // already have it.
742   if (TypeQualifiers & T) {
743     bool IsExtension = true;
744     if (Lang.C99)
745       IsExtension = false;
746     return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
747   }
748   TypeQualifiers |= T;
749 
750   switch (T) {
751   case TQ_unspecified: break;
752   case TQ_const:    TQ_constLoc = Loc; return false;
753   case TQ_restrict: TQ_restrictLoc = Loc; return false;
754   case TQ_volatile: TQ_volatileLoc = Loc; return false;
755   case TQ_atomic:   TQ_atomicLoc = Loc; return false;
756   }
757 
758   llvm_unreachable("Unknown type qualifier!");
759 }
760 
761 bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
762                                      unsigned &DiagID) {
763   // 'inline inline' is ok.  However, since this is likely not what the user
764   // intended, we will always warn, similar to duplicates of type qualifiers.
765   if (FS_inline_specified) {
766     DiagID = diag::warn_duplicate_declspec;
767     PrevSpec = "inline";
768     return true;
769   }
770   FS_inline_specified = true;
771   FS_inlineLoc = Loc;
772   return false;
773 }
774 
775 bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
776                                           unsigned &DiagID) {
777   if (FS_forceinline_specified) {
778     DiagID = diag::warn_duplicate_declspec;
779     PrevSpec = "__forceinline";
780     return true;
781   }
782   FS_forceinline_specified = true;
783   FS_forceinlineLoc = Loc;
784   return false;
785 }
786 
787 bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
788                                       const char *&PrevSpec,
789                                       unsigned &DiagID) {
790   // 'virtual virtual' is ok, but warn as this is likely not what the user
791   // intended.
792   if (FS_virtual_specified) {
793     DiagID = diag::warn_duplicate_declspec;
794     PrevSpec = "virtual";
795     return true;
796   }
797   FS_virtual_specified = true;
798   FS_virtualLoc = Loc;
799   return false;
800 }
801 
802 bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
803                                        const char *&PrevSpec,
804                                        unsigned &DiagID) {
805   // 'explicit explicit' is ok, but warn as this is likely not what the user
806   // intended.
807   if (FS_explicit_specified) {
808     DiagID = diag::warn_duplicate_declspec;
809     PrevSpec = "explicit";
810     return true;
811   }
812   FS_explicit_specified = true;
813   FS_explicitLoc = Loc;
814   return false;
815 }
816 
817 bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
818                                        const char *&PrevSpec,
819                                        unsigned &DiagID) {
820   // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
821   // intended.
822   if (FS_noreturn_specified) {
823     DiagID = diag::warn_duplicate_declspec;
824     PrevSpec = "_Noreturn";
825     return true;
826   }
827   FS_noreturn_specified = true;
828   FS_noreturnLoc = Loc;
829   return false;
830 }
831 
832 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
833                              unsigned &DiagID) {
834   if (Friend_specified) {
835     PrevSpec = "friend";
836     // Keep the later location, so that we can later diagnose ill-formed
837     // declarations like 'friend class X friend;'. Per [class.friend]p3,
838     // 'friend' must be the first token in a friend declaration that is
839     // not a function declaration.
840     FriendLoc = Loc;
841     DiagID = diag::warn_duplicate_declspec;
842     return true;
843   }
844 
845   Friend_specified = true;
846   FriendLoc = Loc;
847   return false;
848 }
849 
850 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
851                                     unsigned &DiagID) {
852   if (isModulePrivateSpecified()) {
853     PrevSpec = "__module_private__";
854     DiagID = diag::ext_duplicate_declspec;
855     return true;
856   }
857 
858   ModulePrivateLoc = Loc;
859   return false;
860 }
861 
862 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
863                                 unsigned &DiagID) {
864   // 'constexpr constexpr' is ok, but warn as this is likely not what the user
865   // intended.
866   if (Constexpr_specified) {
867     DiagID = diag::warn_duplicate_declspec;
868     PrevSpec = "constexpr";
869     return true;
870   }
871   Constexpr_specified = true;
872   ConstexprLoc = Loc;
873   return false;
874 }
875 
876 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
877                                      unsigned NP,
878                                      SourceLocation *ProtoLocs,
879                                      SourceLocation LAngleLoc) {
880   if (NP == 0) return;
881   Decl **ProtoQuals = new Decl*[NP];
882   memcpy(ProtoQuals, Protos, sizeof(Decl*)*NP);
883   ProtocolQualifiers = ProtoQuals;
884   ProtocolLocs = new SourceLocation[NP];
885   memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
886   NumProtocolQualifiers = NP;
887   ProtocolLAngleLoc = LAngleLoc;
888 }
889 
890 void DeclSpec::SaveWrittenBuiltinSpecs() {
891   writtenBS.Sign = getTypeSpecSign();
892   writtenBS.Width = getTypeSpecWidth();
893   writtenBS.Type = getTypeSpecType();
894   // Search the list of attributes for the presence of a mode attribute.
895   writtenBS.ModeAttr = false;
896   AttributeList* attrs = getAttributes().getList();
897   while (attrs) {
898     if (attrs->getKind() == AttributeList::AT_Mode) {
899       writtenBS.ModeAttr = true;
900       break;
901     }
902     attrs = attrs->getNext();
903   }
904 }
905 
906 /// Finish - This does final analysis of the declspec, rejecting things like
907 /// "_Imaginary" (lacking an FP type).  This returns a diagnostic to issue or
908 /// diag::NUM_DIAGNOSTICS if there is no error.  After calling this method,
909 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
910 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP, const PrintingPolicy &Policy) {
911   // Before possibly changing their values, save specs as written.
912   SaveWrittenBuiltinSpecs();
913 
914   // Check the type specifier components first.
915 
916   // If decltype(auto) is used, no other type specifiers are permitted.
917   if (TypeSpecType == TST_decltype_auto &&
918       (TypeSpecWidth != TSW_unspecified ||
919        TypeSpecComplex != TSC_unspecified ||
920        TypeSpecSign != TSS_unspecified ||
921        TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
922        TypeQualifiers)) {
923     const unsigned NumLocs = 8;
924     SourceLocation ExtraLocs[NumLocs] = {
925       TSWLoc, TSCLoc, TSSLoc, AltiVecLoc,
926       TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc
927     };
928     FixItHint Hints[NumLocs];
929     SourceLocation FirstLoc;
930     for (unsigned I = 0; I != NumLocs; ++I) {
931       if (!ExtraLocs[I].isInvalid()) {
932         if (FirstLoc.isInvalid() ||
933             PP.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
934                                                             FirstLoc))
935           FirstLoc = ExtraLocs[I];
936         Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
937       }
938     }
939     TypeSpecWidth = TSW_unspecified;
940     TypeSpecComplex = TSC_unspecified;
941     TypeSpecSign = TSS_unspecified;
942     TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
943     TypeQualifiers = 0;
944     Diag(D, TSTLoc, diag::err_decltype_auto_cannot_be_combined)
945       << Hints[0] << Hints[1] << Hints[2] << Hints[3]
946       << Hints[4] << Hints[5] << Hints[6] << Hints[7];
947   }
948 
949   // Validate and finalize AltiVec vector declspec.
950   if (TypeAltiVecVector) {
951     if (TypeAltiVecBool) {
952       // Sign specifiers are not allowed with vector bool. (PIM 2.1)
953       if (TypeSpecSign != TSS_unspecified) {
954         Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
955           << getSpecifierName((TSS)TypeSpecSign);
956       }
957 
958       // Only char/int are valid with vector bool. (PIM 2.1)
959       if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
960            (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
961         Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
962           << (TypeAltiVecPixel ? "__pixel" :
963                                  getSpecifierName((TST)TypeSpecType, Policy));
964       }
965 
966       // Only 'short' is valid with vector bool. (PIM 2.1)
967       if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
968         Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
969           << getSpecifierName((TSW)TypeSpecWidth);
970 
971       // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
972       if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
973           (TypeSpecWidth != TSW_unspecified))
974         TypeSpecSign = TSS_unsigned;
975     } else if (TypeSpecWidth == TSW_long) {
976       Diag(D, TSWLoc, diag::warn_vector_long_decl_spec_combination)
977         << getSpecifierName((TST)TypeSpecType, Policy);
978     }
979 
980     if (TypeAltiVecPixel) {
981       //TODO: perform validation
982       TypeSpecType = TST_int;
983       TypeSpecSign = TSS_unsigned;
984       TypeSpecWidth = TSW_short;
985       TypeSpecOwned = false;
986     }
987   }
988 
989   // signed/unsigned are only valid with int/char/wchar_t.
990   if (TypeSpecSign != TSS_unspecified) {
991     if (TypeSpecType == TST_unspecified)
992       TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
993     else if (TypeSpecType != TST_int  && TypeSpecType != TST_int128 &&
994              TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
995       Diag(D, TSSLoc, diag::err_invalid_sign_spec)
996         << getSpecifierName((TST)TypeSpecType, Policy);
997       // signed double -> double.
998       TypeSpecSign = TSS_unspecified;
999     }
1000   }
1001 
1002   // Validate the width of the type.
1003   switch (TypeSpecWidth) {
1004   case TSW_unspecified: break;
1005   case TSW_short:    // short int
1006   case TSW_longlong: // long long int
1007     if (TypeSpecType == TST_unspecified)
1008       TypeSpecType = TST_int; // short -> short int, long long -> long long int.
1009     else if (TypeSpecType != TST_int) {
1010       Diag(D, TSWLoc,
1011            TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
1012                                       : diag::err_invalid_longlong_spec)
1013         <<  getSpecifierName((TST)TypeSpecType, Policy);
1014       TypeSpecType = TST_int;
1015       TypeSpecOwned = false;
1016     }
1017     break;
1018   case TSW_long:  // long double, long int
1019     if (TypeSpecType == TST_unspecified)
1020       TypeSpecType = TST_int;  // long -> long int.
1021     else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
1022       Diag(D, TSWLoc, diag::err_invalid_long_spec)
1023         << getSpecifierName((TST)TypeSpecType, Policy);
1024       TypeSpecType = TST_int;
1025       TypeSpecOwned = false;
1026     }
1027     break;
1028   }
1029 
1030   // TODO: if the implementation does not implement _Complex or _Imaginary,
1031   // disallow their use.  Need information about the backend.
1032   if (TypeSpecComplex != TSC_unspecified) {
1033     if (TypeSpecType == TST_unspecified) {
1034       Diag(D, TSCLoc, diag::ext_plain_complex)
1035         << FixItHint::CreateInsertion(
1036                               PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
1037                                                  " double");
1038       TypeSpecType = TST_double;   // _Complex -> _Complex double.
1039     } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
1040       // Note that this intentionally doesn't include _Complex _Bool.
1041       if (!PP.getLangOpts().CPlusPlus)
1042         Diag(D, TSTLoc, diag::ext_integer_complex);
1043     } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
1044       Diag(D, TSCLoc, diag::err_invalid_complex_spec)
1045         << getSpecifierName((TST)TypeSpecType, Policy);
1046       TypeSpecComplex = TSC_unspecified;
1047     }
1048   }
1049 
1050   // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1051   // _Thread_local can only appear with the 'static' and 'extern' storage class
1052   // specifiers. We also allow __private_extern__ as an extension.
1053   if (ThreadStorageClassSpec != TSCS_unspecified) {
1054     switch (StorageClassSpec) {
1055     case SCS_unspecified:
1056     case SCS_extern:
1057     case SCS_private_extern:
1058     case SCS_static:
1059       break;
1060     default:
1061       if (PP.getSourceManager().isBeforeInTranslationUnit(
1062             getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
1063         Diag(D, getStorageClassSpecLoc(),
1064              diag::err_invalid_decl_spec_combination)
1065           << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1066           << SourceRange(getThreadStorageClassSpecLoc());
1067       else
1068         Diag(D, getThreadStorageClassSpecLoc(),
1069              diag::err_invalid_decl_spec_combination)
1070           << DeclSpec::getSpecifierName(getStorageClassSpec())
1071           << SourceRange(getStorageClassSpecLoc());
1072       // Discard the thread storage class specifier to recover.
1073       ThreadStorageClassSpec = TSCS_unspecified;
1074       ThreadStorageClassSpecLoc = SourceLocation();
1075     }
1076   }
1077 
1078   // If no type specifier was provided and we're parsing a language where
1079   // the type specifier is not optional, but we got 'auto' as a storage
1080   // class specifier, then assume this is an attempt to use C++0x's 'auto'
1081   // type specifier.
1082   if (PP.getLangOpts().CPlusPlus &&
1083       TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1084     TypeSpecType = TST_auto;
1085     StorageClassSpec = SCS_unspecified;
1086     TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1087     StorageClassSpecLoc = SourceLocation();
1088   }
1089   // Diagnose if we've recovered from an ill-formed 'auto' storage class
1090   // specifier in a pre-C++11 dialect of C++.
1091   if (!PP.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1092     Diag(D, TSTLoc, diag::ext_auto_type_specifier);
1093   if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus11 &&
1094       StorageClassSpec == SCS_auto)
1095     Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
1096       << FixItHint::CreateRemoval(StorageClassSpecLoc);
1097   if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
1098     Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
1099       << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
1100   if (Constexpr_specified)
1101     Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
1102 
1103   // C++ [class.friend]p6:
1104   //   No storage-class-specifier shall appear in the decl-specifier-seq
1105   //   of a friend declaration.
1106   if (isFriendSpecified() &&
1107       (getStorageClassSpec() || getThreadStorageClassSpec())) {
1108     SmallString<32> SpecName;
1109     SourceLocation SCLoc;
1110     FixItHint StorageHint, ThreadHint;
1111 
1112     if (DeclSpec::SCS SC = getStorageClassSpec()) {
1113       SpecName = getSpecifierName(SC);
1114       SCLoc = getStorageClassSpecLoc();
1115       StorageHint = FixItHint::CreateRemoval(SCLoc);
1116     }
1117 
1118     if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1119       if (!SpecName.empty()) SpecName += " ";
1120       SpecName += getSpecifierName(TSC);
1121       SCLoc = getThreadStorageClassSpecLoc();
1122       ThreadHint = FixItHint::CreateRemoval(SCLoc);
1123     }
1124 
1125     Diag(D, SCLoc, diag::err_friend_decl_spec)
1126       << SpecName << StorageHint << ThreadHint;
1127 
1128     ClearStorageClassSpecs();
1129   }
1130 
1131   // C++11 [dcl.fct.spec]p5:
1132   //   The virtual specifier shall be used only in the initial
1133   //   declaration of a non-static class member function;
1134   // C++11 [dcl.fct.spec]p6:
1135   //   The explicit specifier shall be used only in the declaration of
1136   //   a constructor or conversion function within its class
1137   //   definition;
1138   if (isFriendSpecified() && (isVirtualSpecified() || isExplicitSpecified())) {
1139     StringRef Keyword;
1140     SourceLocation SCLoc;
1141 
1142     if (isVirtualSpecified()) {
1143       Keyword = "virtual";
1144       SCLoc = getVirtualSpecLoc();
1145     } else {
1146       Keyword = "explicit";
1147       SCLoc = getExplicitSpecLoc();
1148     }
1149 
1150     FixItHint Hint = FixItHint::CreateRemoval(SCLoc);
1151     Diag(D, SCLoc, diag::err_friend_decl_spec)
1152       << Keyword << Hint;
1153 
1154     FS_virtual_specified = FS_explicit_specified = false;
1155     FS_virtualLoc = FS_explicitLoc = SourceLocation();
1156   }
1157 
1158   assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
1159 
1160   // Okay, now we can infer the real type.
1161 
1162   // TODO: return "auto function" and other bad things based on the real type.
1163 
1164   // 'data definition has no type or storage class'?
1165 }
1166 
1167 bool DeclSpec::isMissingDeclaratorOk() {
1168   TST tst = getTypeSpecType();
1169   return isDeclRep(tst) && getRepAsDecl() != nullptr &&
1170     StorageClassSpec != DeclSpec::SCS_typedef;
1171 }
1172 
1173 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
1174                                           OverloadedOperatorKind Op,
1175                                           SourceLocation SymbolLocations[3]) {
1176   Kind = IK_OperatorFunctionId;
1177   StartLocation = OperatorLoc;
1178   EndLocation = OperatorLoc;
1179   OperatorFunctionId.Operator = Op;
1180   for (unsigned I = 0; I != 3; ++I) {
1181     OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
1182 
1183     if (SymbolLocations[I].isValid())
1184       EndLocation = SymbolLocations[I];
1185   }
1186 }
1187 
1188 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
1189                                   const char *&PrevSpec) {
1190   LastLocation = Loc;
1191 
1192   if (Specifiers & VS) {
1193     PrevSpec = getSpecifierName(VS);
1194     return true;
1195   }
1196 
1197   Specifiers |= VS;
1198 
1199   switch (VS) {
1200   default: llvm_unreachable("Unknown specifier!");
1201   case VS_Override: VS_overrideLoc = Loc; break;
1202   case VS_Sealed:
1203   case VS_Final:    VS_finalLoc = Loc; break;
1204   }
1205 
1206   return false;
1207 }
1208 
1209 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
1210   switch (VS) {
1211   default: llvm_unreachable("Unknown specifier");
1212   case VS_Override: return "override";
1213   case VS_Final: return "final";
1214   case VS_Sealed: return "sealed";
1215   }
1216 }
1217