DeclSpec.cpp revision 33e4e70c8c0a17e0ccb7465d96556b077a68ecb1
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/ParsedTemplate.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/LangOptions.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/Support/ErrorHandling.h"
21#include <cstring>
22using namespace clang;
23
24
25static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
26                              unsigned DiagID) {
27  return D.Report(Loc, DiagID);
28}
29
30
31void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
32  assert(TemplateId && "NULL template-id annotation?");
33  Kind = IK_TemplateId;
34  this->TemplateId = TemplateId;
35  StartLocation = TemplateId->TemplateNameLoc;
36  EndLocation = TemplateId->RAngleLoc;
37}
38
39void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
40  assert(TemplateId && "NULL template-id annotation?");
41  Kind = IK_ConstructorTemplateId;
42  this->TemplateId = TemplateId;
43  StartLocation = TemplateId->TemplateNameLoc;
44  EndLocation = TemplateId->RAngleLoc;
45}
46
47/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
48/// "TheDeclarator" is the declarator that this will be added to.
49DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
50                                             SourceLocation EllipsisLoc,
51                                             ParamInfo *ArgInfo,
52                                             unsigned NumArgs,
53                                             unsigned TypeQuals,
54                                             bool hasExceptionSpec,
55                                             SourceLocation ThrowLoc,
56                                             bool hasAnyExceptionSpec,
57                                             ParsedType *Exceptions,
58                                             SourceRange *ExceptionRanges,
59                                             unsigned NumExceptions,
60                                             SourceLocation LPLoc,
61                                             SourceLocation RPLoc,
62                                             Declarator &TheDeclarator,
63                                             ParsedType TrailingReturnType) {
64  DeclaratorChunk I;
65  I.Kind                 = Function;
66  I.Loc                  = LPLoc;
67  I.EndLoc               = RPLoc;
68  I.Fun.hasPrototype     = hasProto;
69  I.Fun.isVariadic       = isVariadic;
70  I.Fun.EllipsisLoc      = EllipsisLoc.getRawEncoding();
71  I.Fun.DeleteArgInfo    = false;
72  I.Fun.TypeQuals        = TypeQuals;
73  I.Fun.NumArgs          = NumArgs;
74  I.Fun.ArgInfo          = 0;
75  I.Fun.hasExceptionSpec = hasExceptionSpec;
76  I.Fun.ThrowLoc         = ThrowLoc.getRawEncoding();
77  I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
78  I.Fun.NumExceptions    = NumExceptions;
79  I.Fun.Exceptions       = 0;
80  I.Fun.TrailingReturnType   = TrailingReturnType.getAsOpaquePtr();
81
82  // new[] an argument array if needed.
83  if (NumArgs) {
84    // If the 'InlineParams' in Declarator is unused and big enough, put our
85    // parameter list there (in an effort to avoid new/delete traffic).  If it
86    // is already used (consider a function returning a function pointer) or too
87    // small (function taking too many arguments), go to the heap.
88    if (!TheDeclarator.InlineParamsUsed &&
89        NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
90      I.Fun.ArgInfo = TheDeclarator.InlineParams;
91      I.Fun.DeleteArgInfo = false;
92      TheDeclarator.InlineParamsUsed = true;
93    } else {
94      I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
95      I.Fun.DeleteArgInfo = true;
96    }
97    memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
98  }
99  // new[] an exception array if needed
100  if (NumExceptions) {
101    I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
102    for (unsigned i = 0; i != NumExceptions; ++i) {
103      I.Fun.Exceptions[i].Ty = Exceptions[i];
104      I.Fun.Exceptions[i].Range = ExceptionRanges[i];
105    }
106  }
107  return I;
108}
109
110/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
111/// declaration specifier includes.
112///
113unsigned DeclSpec::getParsedSpecifiers() const {
114  unsigned Res = 0;
115  if (StorageClassSpec != SCS_unspecified ||
116      SCS_thread_specified)
117    Res |= PQ_StorageClassSpecifier;
118
119  if (TypeQualifiers != TQ_unspecified)
120    Res |= PQ_TypeQualifier;
121
122  if (hasTypeSpecifier())
123    Res |= PQ_TypeSpecifier;
124
125  if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
126    Res |= PQ_FunctionSpecifier;
127  return Res;
128}
129
130template <class T> static bool BadSpecifier(T TNew, T TPrev,
131                                            const char *&PrevSpec,
132                                            unsigned &DiagID) {
133  PrevSpec = DeclSpec::getSpecifierName(TPrev);
134  DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
135            : diag::err_invalid_decl_spec_combination);
136  return true;
137}
138
139const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
140  switch (S) {
141  case DeclSpec::SCS_unspecified: return "unspecified";
142  case DeclSpec::SCS_typedef:     return "typedef";
143  case DeclSpec::SCS_extern:      return "extern";
144  case DeclSpec::SCS_static:      return "static";
145  case DeclSpec::SCS_auto:        return "auto";
146  case DeclSpec::SCS_register:    return "register";
147  case DeclSpec::SCS_private_extern: return "__private_extern__";
148  case DeclSpec::SCS_mutable:     return "mutable";
149  }
150  llvm_unreachable("Unknown typespec!");
151}
152
153const char *DeclSpec::getSpecifierName(TSW W) {
154  switch (W) {
155  case TSW_unspecified: return "unspecified";
156  case TSW_short:       return "short";
157  case TSW_long:        return "long";
158  case TSW_longlong:    return "long long";
159  }
160  llvm_unreachable("Unknown typespec!");
161}
162
163const char *DeclSpec::getSpecifierName(TSC C) {
164  switch (C) {
165  case TSC_unspecified: return "unspecified";
166  case TSC_imaginary:   return "imaginary";
167  case TSC_complex:     return "complex";
168  }
169  llvm_unreachable("Unknown typespec!");
170}
171
172
173const char *DeclSpec::getSpecifierName(TSS S) {
174  switch (S) {
175  case TSS_unspecified: return "unspecified";
176  case TSS_signed:      return "signed";
177  case TSS_unsigned:    return "unsigned";
178  }
179  llvm_unreachable("Unknown typespec!");
180}
181
182const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
183  switch (T) {
184  case DeclSpec::TST_unspecified: return "unspecified";
185  case DeclSpec::TST_void:        return "void";
186  case DeclSpec::TST_char:        return "char";
187  case DeclSpec::TST_wchar:       return "wchar_t";
188  case DeclSpec::TST_char16:      return "char16_t";
189  case DeclSpec::TST_char32:      return "char32_t";
190  case DeclSpec::TST_int:         return "int";
191  case DeclSpec::TST_float:       return "float";
192  case DeclSpec::TST_double:      return "double";
193  case DeclSpec::TST_bool:        return "_Bool";
194  case DeclSpec::TST_decimal32:   return "_Decimal32";
195  case DeclSpec::TST_decimal64:   return "_Decimal64";
196  case DeclSpec::TST_decimal128:  return "_Decimal128";
197  case DeclSpec::TST_enum:        return "enum";
198  case DeclSpec::TST_class:       return "class";
199  case DeclSpec::TST_union:       return "union";
200  case DeclSpec::TST_struct:      return "struct";
201  case DeclSpec::TST_typename:    return "type-name";
202  case DeclSpec::TST_typeofType:
203  case DeclSpec::TST_typeofExpr:  return "typeof";
204  case DeclSpec::TST_auto:        return "auto";
205  case DeclSpec::TST_decltype:    return "(decltype)";
206  case DeclSpec::TST_error:       return "(error)";
207  }
208  llvm_unreachable("Unknown typespec!");
209}
210
211const char *DeclSpec::getSpecifierName(TQ T) {
212  switch (T) {
213  case DeclSpec::TQ_unspecified: return "unspecified";
214  case DeclSpec::TQ_const:       return "const";
215  case DeclSpec::TQ_restrict:    return "restrict";
216  case DeclSpec::TQ_volatile:    return "volatile";
217  }
218  llvm_unreachable("Unknown typespec!");
219}
220
221bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
222                                   const char *&PrevSpec,
223                                   unsigned &DiagID) {
224  if (StorageClassSpec != SCS_unspecified) {
225    // Changing storage class is allowed only if the previous one
226    // was the 'extern' that is part of a linkage specification and
227    // the new storage class is 'typedef'.
228    if (!(SCS_extern_in_linkage_spec &&
229          StorageClassSpec == SCS_extern &&
230          S == SCS_typedef))
231      return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
232  }
233  StorageClassSpec = S;
234  StorageClassSpecLoc = Loc;
235  assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
236  return false;
237}
238
239bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
240                                         const char *&PrevSpec,
241                                         unsigned &DiagID) {
242  if (SCS_thread_specified) {
243    PrevSpec = "__thread";
244    DiagID = diag::ext_duplicate_declspec;
245    return true;
246  }
247  SCS_thread_specified = true;
248  SCS_threadLoc = Loc;
249  return false;
250}
251
252/// These methods set the specified attribute of the DeclSpec, but return true
253/// and ignore the request if invalid (e.g. "extern" then "auto" is
254/// specified).
255bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
256                                const char *&PrevSpec,
257                                unsigned &DiagID) {
258  if (TypeSpecWidth != TSW_unspecified &&
259      // Allow turning long -> long long.
260      (W != TSW_longlong || TypeSpecWidth != TSW_long))
261    return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
262  TypeSpecWidth = W;
263  TSWLoc = Loc;
264  if (TypeAltiVecVector && !TypeAltiVecBool &&
265      ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
266    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
267    DiagID = diag::warn_vector_long_decl_spec_combination;
268    return true;
269  }
270  return false;
271}
272
273bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
274                                  const char *&PrevSpec,
275                                  unsigned &DiagID) {
276  if (TypeSpecComplex != TSC_unspecified)
277    return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
278  TypeSpecComplex = C;
279  TSCLoc = Loc;
280  return false;
281}
282
283bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
284                               const char *&PrevSpec,
285                               unsigned &DiagID) {
286  if (TypeSpecSign != TSS_unspecified)
287    return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
288  TypeSpecSign = S;
289  TSSLoc = Loc;
290  return false;
291}
292
293bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
294                               const char *&PrevSpec,
295                               unsigned &DiagID,
296                               ParsedType Rep) {
297  assert(isTypeRep(T) && "T does not store a type");
298  assert(Rep && "no type provided!");
299  if (TypeSpecType != TST_unspecified) {
300    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
301    DiagID = diag::err_invalid_decl_spec_combination;
302    return true;
303  }
304  TypeSpecType = T;
305  TypeRep = Rep;
306  TSTLoc = Loc;
307  TypeSpecOwned = false;
308  return false;
309}
310
311bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
312                               const char *&PrevSpec,
313                               unsigned &DiagID,
314                               Expr *Rep) {
315  assert(isExprRep(T) && "T does not store an expr");
316  assert(Rep && "no expression provided!");
317  if (TypeSpecType != TST_unspecified) {
318    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
319    DiagID = diag::err_invalid_decl_spec_combination;
320    return true;
321  }
322  TypeSpecType = T;
323  ExprRep = Rep;
324  TSTLoc = Loc;
325  TypeSpecOwned = false;
326  return false;
327}
328
329bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
330                               const char *&PrevSpec,
331                               unsigned &DiagID,
332                               Decl *Rep, bool Owned) {
333  assert(isDeclRep(T) && "T does not store a decl");
334  // Unlike the other cases, we don't assert that we actually get a decl.
335
336  if (TypeSpecType != TST_unspecified) {
337    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
338    DiagID = diag::err_invalid_decl_spec_combination;
339    return true;
340  }
341  TypeSpecType = T;
342  DeclRep = Rep;
343  TSTLoc = Loc;
344  TypeSpecOwned = Owned;
345  return false;
346}
347
348bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
349                               const char *&PrevSpec,
350                               unsigned &DiagID) {
351  assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
352         "rep required for these type-spec kinds!");
353  if (TypeSpecType != TST_unspecified) {
354    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
355    DiagID = diag::err_invalid_decl_spec_combination;
356    return true;
357  }
358  if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
359    TypeAltiVecBool = true;
360    TSTLoc = Loc;
361    return false;
362  }
363  TypeSpecType = T;
364  TSTLoc = Loc;
365  TypeSpecOwned = false;
366  if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
367    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
368    DiagID = diag::err_invalid_vector_decl_spec;
369    return true;
370  }
371  return false;
372}
373
374bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
375                          const char *&PrevSpec, unsigned &DiagID) {
376  if (TypeSpecType != TST_unspecified) {
377    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
378    DiagID = diag::err_invalid_vector_decl_spec_combination;
379    return true;
380  }
381  TypeAltiVecVector = isAltiVecVector;
382  AltiVecLoc = Loc;
383  return false;
384}
385
386bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
387                          const char *&PrevSpec, unsigned &DiagID) {
388  if (!TypeAltiVecVector || TypeAltiVecPixel ||
389      (TypeSpecType != TST_unspecified)) {
390    PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
391    DiagID = diag::err_invalid_pixel_decl_spec_combination;
392    return true;
393  }
394  TypeAltiVecPixel = isAltiVecPixel;
395  TSTLoc = Loc;
396  return false;
397}
398
399bool DeclSpec::SetTypeSpecError() {
400  TypeSpecType = TST_error;
401  TypeSpecOwned = false;
402  TSTLoc = SourceLocation();
403  return false;
404}
405
406bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
407                           unsigned &DiagID, const LangOptions &Lang) {
408  // Duplicates turn into warnings pre-C99.
409  if ((TypeQualifiers & T) && !Lang.C99)
410    return BadSpecifier(T, T, PrevSpec, DiagID);
411  TypeQualifiers |= T;
412
413  switch (T) {
414  default: assert(0 && "Unknown type qualifier!");
415  case TQ_const:    TQ_constLoc = Loc; break;
416  case TQ_restrict: TQ_restrictLoc = Loc; break;
417  case TQ_volatile: TQ_volatileLoc = Loc; break;
418  }
419  return false;
420}
421
422bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
423                                     unsigned &DiagID) {
424  // 'inline inline' is ok.
425  FS_inline_specified = true;
426  FS_inlineLoc = Loc;
427  return false;
428}
429
430bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
431                                      unsigned &DiagID) {
432  // 'virtual virtual' is ok.
433  FS_virtual_specified = true;
434  FS_virtualLoc = Loc;
435  return false;
436}
437
438bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
439                                       unsigned &DiagID) {
440  // 'explicit explicit' is ok.
441  FS_explicit_specified = true;
442  FS_explicitLoc = Loc;
443  return false;
444}
445
446bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
447                             unsigned &DiagID) {
448  if (Friend_specified) {
449    PrevSpec = "friend";
450    DiagID = diag::ext_duplicate_declspec;
451    return true;
452  }
453
454  Friend_specified = true;
455  FriendLoc = Loc;
456  return false;
457}
458
459bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
460                                unsigned &DiagID) {
461  // 'constexpr constexpr' is ok.
462  Constexpr_specified = true;
463  ConstexprLoc = Loc;
464  return false;
465}
466
467void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
468                                     unsigned NP,
469                                     SourceLocation *ProtoLocs,
470                                     SourceLocation LAngleLoc) {
471  if (NP == 0) return;
472  ProtocolQualifiers = new Decl*[NP];
473  ProtocolLocs = new SourceLocation[NP];
474  memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
475  memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
476  NumProtocolQualifiers = NP;
477  ProtocolLAngleLoc = LAngleLoc;
478}
479
480void DeclSpec::SaveWrittenBuiltinSpecs() {
481  writtenBS.Sign = getTypeSpecSign();
482  writtenBS.Width = getTypeSpecWidth();
483  writtenBS.Type = getTypeSpecType();
484  // Search the list of attributes for the presence of a mode attribute.
485  writtenBS.ModeAttr = false;
486  AttributeList* attrs = getAttributes();
487  while (attrs) {
488    if (attrs->getKind() == AttributeList::AT_mode) {
489      writtenBS.ModeAttr = true;
490      break;
491    }
492    attrs = attrs->getNext();
493  }
494}
495
496void DeclSpec::SaveStorageSpecifierAsWritten() {
497  if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
498    // If 'extern' is part of a linkage specification,
499    // then it is not a storage class "as written".
500    StorageClassSpecAsWritten = SCS_unspecified;
501  else
502    StorageClassSpecAsWritten = StorageClassSpec;
503}
504
505/// Finish - This does final analysis of the declspec, rejecting things like
506/// "_Imaginary" (lacking an FP type).  This returns a diagnostic to issue or
507/// diag::NUM_DIAGNOSTICS if there is no error.  After calling this method,
508/// DeclSpec is guaranteed self-consistent, even if an error occurred.
509void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
510  // Before possibly changing their values, save specs as written.
511  SaveWrittenBuiltinSpecs();
512  SaveStorageSpecifierAsWritten();
513
514  // Check the type specifier components first.
515
516  // Validate and finalize AltiVec vector declspec.
517  if (TypeAltiVecVector) {
518    if (TypeAltiVecBool) {
519      // Sign specifiers are not allowed with vector bool. (PIM 2.1)
520      if (TypeSpecSign != TSS_unspecified) {
521        Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
522          << getSpecifierName((TSS)TypeSpecSign);
523      }
524
525      // Only char/int are valid with vector bool. (PIM 2.1)
526      if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
527           (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
528        Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
529          << (TypeAltiVecPixel ? "__pixel" :
530                                 getSpecifierName((TST)TypeSpecType));
531      }
532
533      // Only 'short' is valid with vector bool. (PIM 2.1)
534      if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
535        Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
536          << getSpecifierName((TSW)TypeSpecWidth);
537
538      // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
539      if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
540          (TypeSpecWidth != TSW_unspecified))
541        TypeSpecSign = TSS_unsigned;
542    }
543
544    if (TypeAltiVecPixel) {
545      //TODO: perform validation
546      TypeSpecType = TST_int;
547      TypeSpecSign = TSS_unsigned;
548      TypeSpecWidth = TSW_short;
549      TypeSpecOwned = false;
550    }
551  }
552
553  // signed/unsigned are only valid with int/char/wchar_t.
554  if (TypeSpecSign != TSS_unspecified) {
555    if (TypeSpecType == TST_unspecified)
556      TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
557    else if (TypeSpecType != TST_int  &&
558             TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
559      Diag(D, TSSLoc, diag::err_invalid_sign_spec)
560        << getSpecifierName((TST)TypeSpecType);
561      // signed double -> double.
562      TypeSpecSign = TSS_unspecified;
563    }
564  }
565
566  // Validate the width of the type.
567  switch (TypeSpecWidth) {
568  case TSW_unspecified: break;
569  case TSW_short:    // short int
570  case TSW_longlong: // long long int
571    if (TypeSpecType == TST_unspecified)
572      TypeSpecType = TST_int; // short -> short int, long long -> long long int.
573    else if (TypeSpecType != TST_int) {
574      Diag(D, TSWLoc,
575           TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
576                                      : diag::err_invalid_longlong_spec)
577        <<  getSpecifierName((TST)TypeSpecType);
578      TypeSpecType = TST_int;
579      TypeSpecOwned = false;
580    }
581    break;
582  case TSW_long:  // long double, long int
583    if (TypeSpecType == TST_unspecified)
584      TypeSpecType = TST_int;  // long -> long int.
585    else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
586      Diag(D, TSWLoc, diag::err_invalid_long_spec)
587        << getSpecifierName((TST)TypeSpecType);
588      TypeSpecType = TST_int;
589      TypeSpecOwned = false;
590    }
591    break;
592  }
593
594  // TODO: if the implementation does not implement _Complex or _Imaginary,
595  // disallow their use.  Need information about the backend.
596  if (TypeSpecComplex != TSC_unspecified) {
597    if (TypeSpecType == TST_unspecified) {
598      Diag(D, TSCLoc, diag::ext_plain_complex)
599        << FixItHint::CreateInsertion(
600                              PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
601                                                 " double");
602      TypeSpecType = TST_double;   // _Complex -> _Complex double.
603    } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
604      // Note that this intentionally doesn't include _Complex _Bool.
605      Diag(D, TSTLoc, diag::ext_integer_complex);
606    } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
607      Diag(D, TSCLoc, diag::err_invalid_complex_spec)
608        << getSpecifierName((TST)TypeSpecType);
609      TypeSpecComplex = TSC_unspecified;
610    }
611  }
612
613  // C++ [class.friend]p6:
614  //   No storage-class-specifier shall appear in the decl-specifier-seq
615  //   of a friend declaration.
616  if (isFriendSpecified() && getStorageClassSpec()) {
617    DeclSpec::SCS SC = getStorageClassSpec();
618    const char *SpecName = getSpecifierName(SC);
619
620    SourceLocation SCLoc = getStorageClassSpecLoc();
621    SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
622
623    Diag(D, SCLoc, diag::err_friend_storage_spec)
624      << SpecName
625      << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
626
627    ClearStorageClassSpecs();
628  }
629
630  assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
631
632  // Okay, now we can infer the real type.
633
634  // TODO: return "auto function" and other bad things based on the real type.
635
636  // 'data definition has no type or storage class'?
637}
638
639bool DeclSpec::isMissingDeclaratorOk() {
640  TST tst = getTypeSpecType();
641  return isDeclRep(tst) && getRepAsDecl() != 0 &&
642    StorageClassSpec != DeclSpec::SCS_typedef;
643}
644
645void UnqualifiedId::clear() {
646  if (Kind == IK_TemplateId)
647    TemplateId->Destroy();
648
649  Kind = IK_Identifier;
650  Identifier = 0;
651  StartLocation = SourceLocation();
652  EndLocation = SourceLocation();
653}
654
655void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
656                                          OverloadedOperatorKind Op,
657                                          SourceLocation SymbolLocations[3]) {
658  Kind = IK_OperatorFunctionId;
659  StartLocation = OperatorLoc;
660  EndLocation = OperatorLoc;
661  OperatorFunctionId.Operator = Op;
662  for (unsigned I = 0; I != 3; ++I) {
663    OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
664
665    if (SymbolLocations[I].isValid())
666      EndLocation = SymbolLocations[I];
667  }
668}
669