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