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