• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
15 #include "clang/Sema/DeclSpec.h"
16 #include "clang/Sema/LocInfoType.h"
17 #include "clang/Sema/ParsedTemplate.h"
18 #include "clang/Sema/SemaDiagnostic.h"
19 #include "clang/Sema/Sema.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/Expr.h"
22 #include "clang/AST/NestedNameSpecifier.h"
23 #include "clang/AST/TypeLoc.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Basic/LangOptions.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include <cstring>
29 using namespace clang;
30 
31 
Diag(DiagnosticsEngine & D,SourceLocation Loc,unsigned DiagID)32 static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
33                               unsigned DiagID) {
34   return D.Report(Loc, DiagID);
35 }
36 
37 
setTemplateId(TemplateIdAnnotation * TemplateId)38 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
39   assert(TemplateId && "NULL template-id annotation?");
40   Kind = IK_TemplateId;
41   this->TemplateId = TemplateId;
42   StartLocation = TemplateId->TemplateNameLoc;
43   EndLocation = TemplateId->RAngleLoc;
44 }
45 
setConstructorTemplateId(TemplateIdAnnotation * TemplateId)46 void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
47   assert(TemplateId && "NULL template-id annotation?");
48   Kind = IK_ConstructorTemplateId;
49   this->TemplateId = TemplateId;
50   StartLocation = TemplateId->TemplateNameLoc;
51   EndLocation = TemplateId->RAngleLoc;
52 }
53 
Extend(ASTContext & Context,SourceLocation TemplateKWLoc,TypeLoc TL,SourceLocation ColonColonLoc)54 void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
55                           TypeLoc TL, SourceLocation ColonColonLoc) {
56   Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
57   if (Range.getBegin().isInvalid())
58     Range.setBegin(TL.getBeginLoc());
59   Range.setEnd(ColonColonLoc);
60 
61   assert(Range == Builder.getSourceRange() &&
62          "NestedNameSpecifierLoc range computation incorrect");
63 }
64 
Extend(ASTContext & Context,IdentifierInfo * Identifier,SourceLocation IdentifierLoc,SourceLocation ColonColonLoc)65 void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
66                           SourceLocation IdentifierLoc,
67                           SourceLocation ColonColonLoc) {
68   Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
69 
70   if (Range.getBegin().isInvalid())
71     Range.setBegin(IdentifierLoc);
72   Range.setEnd(ColonColonLoc);
73 
74   assert(Range == Builder.getSourceRange() &&
75          "NestedNameSpecifierLoc range computation incorrect");
76 }
77 
Extend(ASTContext & Context,NamespaceDecl * Namespace,SourceLocation NamespaceLoc,SourceLocation ColonColonLoc)78 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
79                           SourceLocation NamespaceLoc,
80                           SourceLocation ColonColonLoc) {
81   Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
82 
83   if (Range.getBegin().isInvalid())
84     Range.setBegin(NamespaceLoc);
85   Range.setEnd(ColonColonLoc);
86 
87   assert(Range == Builder.getSourceRange() &&
88          "NestedNameSpecifierLoc range computation incorrect");
89 }
90 
Extend(ASTContext & Context,NamespaceAliasDecl * Alias,SourceLocation AliasLoc,SourceLocation ColonColonLoc)91 void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
92                           SourceLocation AliasLoc,
93                           SourceLocation ColonColonLoc) {
94   Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
95 
96   if (Range.getBegin().isInvalid())
97     Range.setBegin(AliasLoc);
98   Range.setEnd(ColonColonLoc);
99 
100   assert(Range == Builder.getSourceRange() &&
101          "NestedNameSpecifierLoc range computation incorrect");
102 }
103 
MakeGlobal(ASTContext & Context,SourceLocation ColonColonLoc)104 void CXXScopeSpec::MakeGlobal(ASTContext &Context,
105                               SourceLocation ColonColonLoc) {
106   Builder.MakeGlobal(Context, ColonColonLoc);
107 
108   Range = SourceRange(ColonColonLoc);
109 
110   assert(Range == Builder.getSourceRange() &&
111          "NestedNameSpecifierLoc range computation incorrect");
112 }
113 
MakeTrivial(ASTContext & Context,NestedNameSpecifier * Qualifier,SourceRange R)114 void CXXScopeSpec::MakeTrivial(ASTContext &Context,
115                                NestedNameSpecifier *Qualifier, SourceRange R) {
116   Builder.MakeTrivial(Context, Qualifier, R);
117   Range = R;
118 }
119 
Adopt(NestedNameSpecifierLoc Other)120 void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
121   if (!Other) {
122     Range = SourceRange();
123     Builder.Clear();
124     return;
125   }
126 
127   Range = Other.getSourceRange();
128   Builder.Adopt(Other);
129 }
130 
getLastQualifierNameLoc() const131 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
132   if (!Builder.getRepresentation())
133     return SourceLocation();
134   return Builder.getTemporary().getLocalBeginLoc();
135 }
136 
137 NestedNameSpecifierLoc
getWithLocInContext(ASTContext & Context) const138 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
139   if (!Builder.getRepresentation())
140     return NestedNameSpecifierLoc();
141 
142   return Builder.getWithLocInContext(Context);
143 }
144 
145 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
146 /// "TheDeclarator" is the declarator that this will be added to.
getFunction(bool hasProto,bool isVariadic,SourceLocation EllipsisLoc,ParamInfo * ArgInfo,unsigned NumArgs,unsigned TypeQuals,bool RefQualifierIsLvalueRef,SourceLocation RefQualifierLoc,SourceLocation ConstQualifierLoc,SourceLocation VolatileQualifierLoc,SourceLocation MutableLoc,ExceptionSpecificationType ESpecType,SourceLocation ESpecLoc,ParsedType * Exceptions,SourceRange * ExceptionRanges,unsigned NumExceptions,Expr * NoexceptExpr,CachedTokens * ExceptionSpecTokens,SourceLocation LocalRangeBegin,SourceLocation LocalRangeEnd,Declarator & TheDeclarator,ParsedType TrailingReturnType)147 DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
148                                              SourceLocation EllipsisLoc,
149                                              ParamInfo *ArgInfo,
150                                              unsigned NumArgs,
151                                              unsigned TypeQuals,
152                                              bool RefQualifierIsLvalueRef,
153                                              SourceLocation RefQualifierLoc,
154                                              SourceLocation ConstQualifierLoc,
155                                              SourceLocation
156                                                  VolatileQualifierLoc,
157                                              SourceLocation MutableLoc,
158                                              ExceptionSpecificationType
159                                                  ESpecType,
160                                              SourceLocation ESpecLoc,
161                                              ParsedType *Exceptions,
162                                              SourceRange *ExceptionRanges,
163                                              unsigned NumExceptions,
164                                              Expr *NoexceptExpr,
165                                              CachedTokens *ExceptionSpecTokens,
166                                              SourceLocation LocalRangeBegin,
167                                              SourceLocation LocalRangeEnd,
168                                              Declarator &TheDeclarator,
169                                              ParsedType TrailingReturnType) {
170   DeclaratorChunk I;
171   I.Kind                        = Function;
172   I.Loc                         = LocalRangeBegin;
173   I.EndLoc                      = LocalRangeEnd;
174   I.Fun.AttrList                = 0;
175   I.Fun.hasPrototype            = hasProto;
176   I.Fun.isVariadic              = isVariadic;
177   I.Fun.EllipsisLoc             = EllipsisLoc.getRawEncoding();
178   I.Fun.DeleteArgInfo           = false;
179   I.Fun.TypeQuals               = TypeQuals;
180   I.Fun.NumArgs                 = NumArgs;
181   I.Fun.ArgInfo                 = 0;
182   I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
183   I.Fun.RefQualifierLoc         = RefQualifierLoc.getRawEncoding();
184   I.Fun.ConstQualifierLoc       = ConstQualifierLoc.getRawEncoding();
185   I.Fun.VolatileQualifierLoc    = VolatileQualifierLoc.getRawEncoding();
186   I.Fun.MutableLoc              = MutableLoc.getRawEncoding();
187   I.Fun.ExceptionSpecType       = ESpecType;
188   I.Fun.ExceptionSpecLoc        = ESpecLoc.getRawEncoding();
189   I.Fun.NumExceptions           = 0;
190   I.Fun.Exceptions              = 0;
191   I.Fun.NoexceptExpr            = 0;
192   I.Fun.TrailingReturnType   = TrailingReturnType.getAsOpaquePtr();
193 
194   // new[] an argument array if needed.
195   if (NumArgs) {
196     // If the 'InlineParams' in Declarator is unused and big enough, put our
197     // parameter list there (in an effort to avoid new/delete traffic).  If it
198     // is already used (consider a function returning a function pointer) or too
199     // small (function taking too many arguments), go to the heap.
200     if (!TheDeclarator.InlineParamsUsed &&
201         NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
202       I.Fun.ArgInfo = TheDeclarator.InlineParams;
203       I.Fun.DeleteArgInfo = false;
204       TheDeclarator.InlineParamsUsed = true;
205     } else {
206       I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
207       I.Fun.DeleteArgInfo = true;
208     }
209     memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
210   }
211 
212   // Check what exception specification information we should actually store.
213   switch (ESpecType) {
214   default: break; // By default, save nothing.
215   case EST_Dynamic:
216     // new[] an exception array if needed
217     if (NumExceptions) {
218       I.Fun.NumExceptions = NumExceptions;
219       I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
220       for (unsigned i = 0; i != NumExceptions; ++i) {
221         I.Fun.Exceptions[i].Ty = Exceptions[i];
222         I.Fun.Exceptions[i].Range = ExceptionRanges[i];
223       }
224     }
225     break;
226 
227   case EST_ComputedNoexcept:
228     I.Fun.NoexceptExpr = NoexceptExpr;
229     break;
230 
231   case EST_Delayed:
232     I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
233     break;
234   }
235   return I;
236 }
237 
isDeclarationOfFunction() const238 bool Declarator::isDeclarationOfFunction() const {
239   for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
240     switch (DeclTypeInfo[i].Kind) {
241     case DeclaratorChunk::Function:
242       return true;
243     case DeclaratorChunk::Paren:
244       continue;
245     case DeclaratorChunk::Pointer:
246     case DeclaratorChunk::Reference:
247     case DeclaratorChunk::Array:
248     case DeclaratorChunk::BlockPointer:
249     case DeclaratorChunk::MemberPointer:
250       return false;
251     }
252     llvm_unreachable("Invalid type chunk");
253   }
254 
255   switch (DS.getTypeSpecType()) {
256     case TST_atomic:
257     case TST_auto:
258     case TST_bool:
259     case TST_char:
260     case TST_char16:
261     case TST_char32:
262     case TST_class:
263     case TST_decimal128:
264     case TST_decimal32:
265     case TST_decimal64:
266     case TST_double:
267     case TST_enum:
268     case TST_error:
269     case TST_float:
270     case TST_half:
271     case TST_int:
272     case TST_int128:
273     case TST_struct:
274     case TST_union:
275     case TST_unknown_anytype:
276     case TST_unspecified:
277     case TST_void:
278     case TST_wchar:
279       return false;
280 
281     case TST_decltype:
282     case TST_typeofExpr:
283       if (Expr *E = DS.getRepAsExpr())
284         return E->getType()->isFunctionType();
285       return false;
286 
287     case TST_underlyingType:
288     case TST_typename:
289     case TST_typeofType: {
290       QualType QT = DS.getRepAsType().get();
291       if (QT.isNull())
292         return false;
293 
294       if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
295         QT = LIT->getType();
296 
297       if (QT.isNull())
298         return false;
299 
300       return QT->isFunctionType();
301     }
302   }
303 
304   llvm_unreachable("Invalid TypeSpecType!");
305 }
306 
307 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
308 /// declaration specifier includes.
309 ///
getParsedSpecifiers() const310 unsigned DeclSpec::getParsedSpecifiers() const {
311   unsigned Res = 0;
312   if (StorageClassSpec != SCS_unspecified ||
313       SCS_thread_specified)
314     Res |= PQ_StorageClassSpecifier;
315 
316   if (TypeQualifiers != TQ_unspecified)
317     Res |= PQ_TypeQualifier;
318 
319   if (hasTypeSpecifier())
320     Res |= PQ_TypeSpecifier;
321 
322   if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
323     Res |= PQ_FunctionSpecifier;
324   return Res;
325 }
326 
BadSpecifier(T TNew,T TPrev,const char * & PrevSpec,unsigned & DiagID)327 template <class T> static bool BadSpecifier(T TNew, T TPrev,
328                                             const char *&PrevSpec,
329                                             unsigned &DiagID) {
330   PrevSpec = DeclSpec::getSpecifierName(TPrev);
331   DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
332             : diag::err_invalid_decl_spec_combination);
333   return true;
334 }
335 
getSpecifierName(DeclSpec::SCS S)336 const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
337   switch (S) {
338   case DeclSpec::SCS_unspecified: return "unspecified";
339   case DeclSpec::SCS_typedef:     return "typedef";
340   case DeclSpec::SCS_extern:      return "extern";
341   case DeclSpec::SCS_static:      return "static";
342   case DeclSpec::SCS_auto:        return "auto";
343   case DeclSpec::SCS_register:    return "register";
344   case DeclSpec::SCS_private_extern: return "__private_extern__";
345   case DeclSpec::SCS_mutable:     return "mutable";
346   }
347   llvm_unreachable("Unknown typespec!");
348 }
349 
getSpecifierName(TSW W)350 const char *DeclSpec::getSpecifierName(TSW W) {
351   switch (W) {
352   case TSW_unspecified: return "unspecified";
353   case TSW_short:       return "short";
354   case TSW_long:        return "long";
355   case TSW_longlong:    return "long long";
356   }
357   llvm_unreachable("Unknown typespec!");
358 }
359 
getSpecifierName(TSC C)360 const char *DeclSpec::getSpecifierName(TSC C) {
361   switch (C) {
362   case TSC_unspecified: return "unspecified";
363   case TSC_imaginary:   return "imaginary";
364   case TSC_complex:     return "complex";
365   }
366   llvm_unreachable("Unknown typespec!");
367 }
368 
369 
getSpecifierName(TSS S)370 const char *DeclSpec::getSpecifierName(TSS S) {
371   switch (S) {
372   case TSS_unspecified: return "unspecified";
373   case TSS_signed:      return "signed";
374   case TSS_unsigned:    return "unsigned";
375   }
376   llvm_unreachable("Unknown typespec!");
377 }
378 
getSpecifierName(DeclSpec::TST T)379 const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
380   switch (T) {
381   case DeclSpec::TST_unspecified: return "unspecified";
382   case DeclSpec::TST_void:        return "void";
383   case DeclSpec::TST_char:        return "char";
384   case DeclSpec::TST_wchar:       return "wchar_t";
385   case DeclSpec::TST_char16:      return "char16_t";
386   case DeclSpec::TST_char32:      return "char32_t";
387   case DeclSpec::TST_int:         return "int";
388   case DeclSpec::TST_int128:      return "__int128";
389   case DeclSpec::TST_half:        return "half";
390   case DeclSpec::TST_float:       return "float";
391   case DeclSpec::TST_double:      return "double";
392   case DeclSpec::TST_bool:        return "_Bool";
393   case DeclSpec::TST_decimal32:   return "_Decimal32";
394   case DeclSpec::TST_decimal64:   return "_Decimal64";
395   case DeclSpec::TST_decimal128:  return "_Decimal128";
396   case DeclSpec::TST_enum:        return "enum";
397   case DeclSpec::TST_class:       return "class";
398   case DeclSpec::TST_union:       return "union";
399   case DeclSpec::TST_struct:      return "struct";
400   case DeclSpec::TST_typename:    return "type-name";
401   case DeclSpec::TST_typeofType:
402   case DeclSpec::TST_typeofExpr:  return "typeof";
403   case DeclSpec::TST_auto:        return "auto";
404   case DeclSpec::TST_decltype:    return "(decltype)";
405   case DeclSpec::TST_underlyingType: return "__underlying_type";
406   case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
407   case DeclSpec::TST_atomic: return "_Atomic";
408   case DeclSpec::TST_error:       return "(error)";
409   }
410   llvm_unreachable("Unknown typespec!");
411 }
412 
getSpecifierName(TQ T)413 const char *DeclSpec::getSpecifierName(TQ T) {
414   switch (T) {
415   case DeclSpec::TQ_unspecified: return "unspecified";
416   case DeclSpec::TQ_const:       return "const";
417   case DeclSpec::TQ_restrict:    return "restrict";
418   case DeclSpec::TQ_volatile:    return "volatile";
419   }
420   llvm_unreachable("Unknown typespec!");
421 }
422 
SetStorageClassSpec(Sema & S,SCS SC,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)423 bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
424                                    const char *&PrevSpec,
425                                    unsigned &DiagID) {
426   // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class
427   // specifiers are not supported."
428   // It seems sensible to prohibit private_extern too
429   // The cl_clang_storage_class_specifiers extension enables support for
430   // these storage-class specifiers.
431   if (S.getLangOpts().OpenCL &&
432       !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
433     switch (SC) {
434     case SCS_extern:
435     case SCS_private_extern:
436     case SCS_auto:
437     case SCS_register:
438     case SCS_static:
439       DiagID   = diag::err_not_opencl_storage_class_specifier;
440       PrevSpec = getSpecifierName(SC);
441       return true;
442     default:
443       break;
444     }
445   }
446 
447   if (StorageClassSpec != SCS_unspecified) {
448     // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
449     bool isInvalid = true;
450     if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
451       if (SC == SCS_auto)
452         return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
453       if (StorageClassSpec == SCS_auto) {
454         isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
455                                     PrevSpec, DiagID);
456         assert(!isInvalid && "auto SCS -> TST recovery failed");
457       }
458     }
459 
460     // Changing storage class is allowed only if the previous one
461     // was the 'extern' that is part of a linkage specification and
462     // the new storage class is 'typedef'.
463     if (isInvalid &&
464         !(SCS_extern_in_linkage_spec &&
465           StorageClassSpec == SCS_extern &&
466           SC == SCS_typedef))
467       return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
468   }
469   StorageClassSpec = SC;
470   StorageClassSpecLoc = Loc;
471   assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
472   return false;
473 }
474 
SetStorageClassSpecThread(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)475 bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
476                                          const char *&PrevSpec,
477                                          unsigned &DiagID) {
478   if (SCS_thread_specified) {
479     PrevSpec = "__thread";
480     DiagID = diag::ext_duplicate_declspec;
481     return true;
482   }
483   SCS_thread_specified = true;
484   SCS_threadLoc = Loc;
485   return false;
486 }
487 
488 /// These methods set the specified attribute of the DeclSpec, but return true
489 /// and ignore the request if invalid (e.g. "extern" then "auto" is
490 /// specified).
SetTypeSpecWidth(TSW W,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)491 bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
492                                 const char *&PrevSpec,
493                                 unsigned &DiagID) {
494   // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
495   // for 'long long' we will keep the source location of the first 'long'.
496   if (TypeSpecWidth == TSW_unspecified)
497     TSWLoc = Loc;
498   // Allow turning long -> long long.
499   else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
500     return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
501   TypeSpecWidth = W;
502   if (TypeAltiVecVector && !TypeAltiVecBool &&
503       ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
504     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
505     DiagID = diag::warn_vector_long_decl_spec_combination;
506     return true;
507   }
508   return false;
509 }
510 
SetTypeSpecComplex(TSC C,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)511 bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
512                                   const char *&PrevSpec,
513                                   unsigned &DiagID) {
514   if (TypeSpecComplex != TSC_unspecified)
515     return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
516   TypeSpecComplex = C;
517   TSCLoc = Loc;
518   return false;
519 }
520 
SetTypeSpecSign(TSS S,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)521 bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
522                                const char *&PrevSpec,
523                                unsigned &DiagID) {
524   if (TypeSpecSign != TSS_unspecified)
525     return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
526   TypeSpecSign = S;
527   TSSLoc = Loc;
528   return false;
529 }
530 
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,ParsedType Rep)531 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
532                                const char *&PrevSpec,
533                                unsigned &DiagID,
534                                ParsedType Rep) {
535   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
536 }
537 
SetTypeSpecType(TST T,SourceLocation TagKwLoc,SourceLocation TagNameLoc,const char * & PrevSpec,unsigned & DiagID,ParsedType Rep)538 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
539                                SourceLocation TagNameLoc,
540                                const char *&PrevSpec,
541                                unsigned &DiagID,
542                                ParsedType Rep) {
543   assert(isTypeRep(T) && "T does not store a type");
544   assert(Rep && "no type provided!");
545   if (TypeSpecType != TST_unspecified) {
546     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
547     DiagID = diag::err_invalid_decl_spec_combination;
548     return true;
549   }
550   TypeSpecType = T;
551   TypeRep = Rep;
552   TSTLoc = TagKwLoc;
553   TSTNameLoc = TagNameLoc;
554   TypeSpecOwned = false;
555   return false;
556 }
557 
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,Expr * Rep)558 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
559                                const char *&PrevSpec,
560                                unsigned &DiagID,
561                                Expr *Rep) {
562   assert(isExprRep(T) && "T does not store an expr");
563   assert(Rep && "no expression provided!");
564   if (TypeSpecType != TST_unspecified) {
565     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
566     DiagID = diag::err_invalid_decl_spec_combination;
567     return true;
568   }
569   TypeSpecType = T;
570   ExprRep = Rep;
571   TSTLoc = Loc;
572   TSTNameLoc = Loc;
573   TypeSpecOwned = false;
574   return false;
575 }
576 
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,Decl * Rep,bool Owned)577 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
578                                const char *&PrevSpec,
579                                unsigned &DiagID,
580                                Decl *Rep, bool Owned) {
581   return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
582 }
583 
SetTypeSpecType(TST T,SourceLocation TagKwLoc,SourceLocation TagNameLoc,const char * & PrevSpec,unsigned & DiagID,Decl * Rep,bool Owned)584 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
585                                SourceLocation TagNameLoc,
586                                const char *&PrevSpec,
587                                unsigned &DiagID,
588                                Decl *Rep, bool Owned) {
589   assert(isDeclRep(T) && "T does not store a decl");
590   // Unlike the other cases, we don't assert that we actually get a decl.
591 
592   if (TypeSpecType != TST_unspecified) {
593     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
594     DiagID = diag::err_invalid_decl_spec_combination;
595     return true;
596   }
597   TypeSpecType = T;
598   DeclRep = Rep;
599   TSTLoc = TagKwLoc;
600   TSTNameLoc = TagNameLoc;
601   TypeSpecOwned = Owned;
602   return false;
603 }
604 
SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)605 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
606                                const char *&PrevSpec,
607                                unsigned &DiagID) {
608   assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
609          "rep required for these type-spec kinds!");
610   if (TypeSpecType != TST_unspecified) {
611     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
612     DiagID = diag::err_invalid_decl_spec_combination;
613     return true;
614   }
615   TSTLoc = Loc;
616   TSTNameLoc = Loc;
617   if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
618     TypeAltiVecBool = true;
619     return false;
620   }
621   TypeSpecType = T;
622   TypeSpecOwned = false;
623   if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
624     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
625     DiagID = diag::err_invalid_vector_decl_spec;
626     return true;
627   }
628   return false;
629 }
630 
SetTypeAltiVecVector(bool isAltiVecVector,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)631 bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
632                           const char *&PrevSpec, unsigned &DiagID) {
633   if (TypeSpecType != TST_unspecified) {
634     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
635     DiagID = diag::err_invalid_vector_decl_spec_combination;
636     return true;
637   }
638   TypeAltiVecVector = isAltiVecVector;
639   AltiVecLoc = Loc;
640   return false;
641 }
642 
SetTypeAltiVecPixel(bool isAltiVecPixel,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)643 bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
644                           const char *&PrevSpec, unsigned &DiagID) {
645   if (!TypeAltiVecVector || TypeAltiVecPixel ||
646       (TypeSpecType != TST_unspecified)) {
647     PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
648     DiagID = diag::err_invalid_pixel_decl_spec_combination;
649     return true;
650   }
651   TypeAltiVecPixel = isAltiVecPixel;
652   TSTLoc = Loc;
653   TSTNameLoc = Loc;
654   return false;
655 }
656 
SetTypeSpecError()657 bool DeclSpec::SetTypeSpecError() {
658   TypeSpecType = TST_error;
659   TypeSpecOwned = false;
660   TSTLoc = SourceLocation();
661   TSTNameLoc = SourceLocation();
662   return false;
663 }
664 
SetTypeQual(TQ T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,const LangOptions & Lang)665 bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
666                            unsigned &DiagID, const LangOptions &Lang) {
667   // Duplicates turn into warnings pre-C99.
668   if ((TypeQualifiers & T) && !Lang.C99)
669     return BadSpecifier(T, T, PrevSpec, DiagID);
670   TypeQualifiers |= T;
671 
672   switch (T) {
673   default: llvm_unreachable("Unknown type qualifier!");
674   case TQ_const:    TQ_constLoc = Loc; break;
675   case TQ_restrict: TQ_restrictLoc = Loc; break;
676   case TQ_volatile: TQ_volatileLoc = Loc; break;
677   }
678   return false;
679 }
680 
SetFunctionSpecInline(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)681 bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
682                                      unsigned &DiagID) {
683   // 'inline inline' is ok.
684   FS_inline_specified = true;
685   FS_inlineLoc = Loc;
686   return false;
687 }
688 
SetFunctionSpecVirtual(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)689 bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
690                                       unsigned &DiagID) {
691   // 'virtual virtual' is ok.
692   FS_virtual_specified = true;
693   FS_virtualLoc = Loc;
694   return false;
695 }
696 
SetFunctionSpecExplicit(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)697 bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
698                                        unsigned &DiagID) {
699   // 'explicit explicit' is ok.
700   FS_explicit_specified = true;
701   FS_explicitLoc = Loc;
702   return false;
703 }
704 
SetFriendSpec(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)705 bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
706                              unsigned &DiagID) {
707   if (Friend_specified) {
708     PrevSpec = "friend";
709     DiagID = diag::ext_duplicate_declspec;
710     return true;
711   }
712 
713   Friend_specified = true;
714   FriendLoc = Loc;
715   return false;
716 }
717 
setModulePrivateSpec(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)718 bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
719                                     unsigned &DiagID) {
720   if (isModulePrivateSpecified()) {
721     PrevSpec = "__module_private__";
722     DiagID = diag::ext_duplicate_declspec;
723     return true;
724   }
725 
726   ModulePrivateLoc = Loc;
727   return false;
728 }
729 
SetConstexprSpec(SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID)730 bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
731                                 unsigned &DiagID) {
732   // 'constexpr constexpr' is ok.
733   Constexpr_specified = true;
734   ConstexprLoc = Loc;
735   return false;
736 }
737 
setProtocolQualifiers(Decl * const * Protos,unsigned NP,SourceLocation * ProtoLocs,SourceLocation LAngleLoc)738 void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
739                                      unsigned NP,
740                                      SourceLocation *ProtoLocs,
741                                      SourceLocation LAngleLoc) {
742   if (NP == 0) return;
743   ProtocolQualifiers = new Decl*[NP];
744   ProtocolLocs = new SourceLocation[NP];
745   memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
746   memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
747   NumProtocolQualifiers = NP;
748   ProtocolLAngleLoc = LAngleLoc;
749 }
750 
SaveWrittenBuiltinSpecs()751 void DeclSpec::SaveWrittenBuiltinSpecs() {
752   writtenBS.Sign = getTypeSpecSign();
753   writtenBS.Width = getTypeSpecWidth();
754   writtenBS.Type = getTypeSpecType();
755   // Search the list of attributes for the presence of a mode attribute.
756   writtenBS.ModeAttr = false;
757   AttributeList* attrs = getAttributes().getList();
758   while (attrs) {
759     if (attrs->getKind() == AttributeList::AT_mode) {
760       writtenBS.ModeAttr = true;
761       break;
762     }
763     attrs = attrs->getNext();
764   }
765 }
766 
SaveStorageSpecifierAsWritten()767 void DeclSpec::SaveStorageSpecifierAsWritten() {
768   if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
769     // If 'extern' is part of a linkage specification,
770     // then it is not a storage class "as written".
771     StorageClassSpecAsWritten = SCS_unspecified;
772   else
773     StorageClassSpecAsWritten = StorageClassSpec;
774 }
775 
776 /// Finish - This does final analysis of the declspec, rejecting things like
777 /// "_Imaginary" (lacking an FP type).  This returns a diagnostic to issue or
778 /// diag::NUM_DIAGNOSTICS if there is no error.  After calling this method,
779 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
Finish(DiagnosticsEngine & D,Preprocessor & PP)780 void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
781   // Before possibly changing their values, save specs as written.
782   SaveWrittenBuiltinSpecs();
783   SaveStorageSpecifierAsWritten();
784 
785   // Check the type specifier components first.
786 
787   // Validate and finalize AltiVec vector declspec.
788   if (TypeAltiVecVector) {
789     if (TypeAltiVecBool) {
790       // Sign specifiers are not allowed with vector bool. (PIM 2.1)
791       if (TypeSpecSign != TSS_unspecified) {
792         Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
793           << getSpecifierName((TSS)TypeSpecSign);
794       }
795 
796       // Only char/int are valid with vector bool. (PIM 2.1)
797       if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
798            (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
799         Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
800           << (TypeAltiVecPixel ? "__pixel" :
801                                  getSpecifierName((TST)TypeSpecType));
802       }
803 
804       // Only 'short' is valid with vector bool. (PIM 2.1)
805       if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
806         Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
807           << getSpecifierName((TSW)TypeSpecWidth);
808 
809       // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
810       if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
811           (TypeSpecWidth != TSW_unspecified))
812         TypeSpecSign = TSS_unsigned;
813     }
814 
815     if (TypeAltiVecPixel) {
816       //TODO: perform validation
817       TypeSpecType = TST_int;
818       TypeSpecSign = TSS_unsigned;
819       TypeSpecWidth = TSW_short;
820       TypeSpecOwned = false;
821     }
822   }
823 
824   // signed/unsigned are only valid with int/char/wchar_t.
825   if (TypeSpecSign != TSS_unspecified) {
826     if (TypeSpecType == TST_unspecified)
827       TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
828     else if (TypeSpecType != TST_int  && TypeSpecType != TST_int128 &&
829              TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
830       Diag(D, TSSLoc, diag::err_invalid_sign_spec)
831         << getSpecifierName((TST)TypeSpecType);
832       // signed double -> double.
833       TypeSpecSign = TSS_unspecified;
834     }
835   }
836 
837   // Validate the width of the type.
838   switch (TypeSpecWidth) {
839   case TSW_unspecified: break;
840   case TSW_short:    // short int
841   case TSW_longlong: // long long int
842     if (TypeSpecType == TST_unspecified)
843       TypeSpecType = TST_int; // short -> short int, long long -> long long int.
844     else if (TypeSpecType != TST_int) {
845       Diag(D, TSWLoc,
846            TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
847                                       : diag::err_invalid_longlong_spec)
848         <<  getSpecifierName((TST)TypeSpecType);
849       TypeSpecType = TST_int;
850       TypeSpecOwned = false;
851     }
852     break;
853   case TSW_long:  // long double, long int
854     if (TypeSpecType == TST_unspecified)
855       TypeSpecType = TST_int;  // long -> long int.
856     else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
857       Diag(D, TSWLoc, diag::err_invalid_long_spec)
858         << getSpecifierName((TST)TypeSpecType);
859       TypeSpecType = TST_int;
860       TypeSpecOwned = false;
861     }
862     break;
863   }
864 
865   // TODO: if the implementation does not implement _Complex or _Imaginary,
866   // disallow their use.  Need information about the backend.
867   if (TypeSpecComplex != TSC_unspecified) {
868     if (TypeSpecType == TST_unspecified) {
869       Diag(D, TSCLoc, diag::ext_plain_complex)
870         << FixItHint::CreateInsertion(
871                               PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
872                                                  " double");
873       TypeSpecType = TST_double;   // _Complex -> _Complex double.
874     } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
875       // Note that this intentionally doesn't include _Complex _Bool.
876       if (!PP.getLangOpts().CPlusPlus)
877         Diag(D, TSTLoc, diag::ext_integer_complex);
878     } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
879       Diag(D, TSCLoc, diag::err_invalid_complex_spec)
880         << getSpecifierName((TST)TypeSpecType);
881       TypeSpecComplex = TSC_unspecified;
882     }
883   }
884 
885   // If no type specifier was provided and we're parsing a language where
886   // the type specifier is not optional, but we got 'auto' as a storage
887   // class specifier, then assume this is an attempt to use C++0x's 'auto'
888   // type specifier.
889   // FIXME: Does Microsoft really support implicit int in C++?
890   if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().MicrosoftExt &&
891       TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
892     TypeSpecType = TST_auto;
893     StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
894     TSTLoc = TSTNameLoc = StorageClassSpecLoc;
895     StorageClassSpecLoc = SourceLocation();
896   }
897   // Diagnose if we've recovered from an ill-formed 'auto' storage class
898   // specifier in a pre-C++0x dialect of C++.
899   if (!PP.getLangOpts().CPlusPlus0x && TypeSpecType == TST_auto)
900     Diag(D, TSTLoc, diag::ext_auto_type_specifier);
901   if (PP.getLangOpts().CPlusPlus && !PP.getLangOpts().CPlusPlus0x &&
902       StorageClassSpec == SCS_auto)
903     Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
904       << FixItHint::CreateRemoval(StorageClassSpecLoc);
905   if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
906     Diag(D, TSTLoc, diag::warn_cxx98_compat_unicode_type)
907       << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
908   if (Constexpr_specified)
909     Diag(D, ConstexprLoc, diag::warn_cxx98_compat_constexpr);
910 
911   // C++ [class.friend]p6:
912   //   No storage-class-specifier shall appear in the decl-specifier-seq
913   //   of a friend declaration.
914   if (isFriendSpecified() && getStorageClassSpec()) {
915     DeclSpec::SCS SC = getStorageClassSpec();
916     const char *SpecName = getSpecifierName(SC);
917 
918     SourceLocation SCLoc = getStorageClassSpecLoc();
919     SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
920 
921     Diag(D, SCLoc, diag::err_friend_storage_spec)
922       << SpecName
923       << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
924 
925     ClearStorageClassSpecs();
926   }
927 
928   assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
929 
930   // Okay, now we can infer the real type.
931 
932   // TODO: return "auto function" and other bad things based on the real type.
933 
934   // 'data definition has no type or storage class'?
935 }
936 
isMissingDeclaratorOk()937 bool DeclSpec::isMissingDeclaratorOk() {
938   TST tst = getTypeSpecType();
939   return isDeclRep(tst) && getRepAsDecl() != 0 &&
940     StorageClassSpec != DeclSpec::SCS_typedef;
941 }
942 
clear()943 void UnqualifiedId::clear() {
944   Kind = IK_Identifier;
945   Identifier = 0;
946   StartLocation = SourceLocation();
947   EndLocation = SourceLocation();
948 }
949 
setOperatorFunctionId(SourceLocation OperatorLoc,OverloadedOperatorKind Op,SourceLocation SymbolLocations[3])950 void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
951                                           OverloadedOperatorKind Op,
952                                           SourceLocation SymbolLocations[3]) {
953   Kind = IK_OperatorFunctionId;
954   StartLocation = OperatorLoc;
955   EndLocation = OperatorLoc;
956   OperatorFunctionId.Operator = Op;
957   for (unsigned I = 0; I != 3; ++I) {
958     OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
959 
960     if (SymbolLocations[I].isValid())
961       EndLocation = SymbolLocations[I];
962   }
963 }
964 
SetSpecifier(Specifier VS,SourceLocation Loc,const char * & PrevSpec)965 bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
966                                   const char *&PrevSpec) {
967   LastLocation = Loc;
968 
969   if (Specifiers & VS) {
970     PrevSpec = getSpecifierName(VS);
971     return true;
972   }
973 
974   Specifiers |= VS;
975 
976   switch (VS) {
977   default: llvm_unreachable("Unknown specifier!");
978   case VS_Override: VS_overrideLoc = Loc; break;
979   case VS_Final:    VS_finalLoc = Loc; break;
980   }
981 
982   return false;
983 }
984 
getSpecifierName(Specifier VS)985 const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
986   switch (VS) {
987   default: llvm_unreachable("Unknown specifier");
988   case VS_Override: return "override";
989   case VS_Final: return "final";
990   }
991 }
992