blob: 4cd8fe887bb1499b03870fb65bc075fb25b9018c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for declaration specifiers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/DeclSpec.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000016#include "clang/Parse/Template.h"
Douglas Gregor9b3064b2009-04-01 22:41:11 +000017#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000019#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000020#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000021#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Chris Lattner254be6a2008-11-22 08:32:36 +000024
25static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
26 SourceManager &SrcMgr, unsigned DiagID) {
27 return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID);
28}
29
Douglas Gregor314b97f2009-11-10 19:49:08 +000030
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
Chris Lattner5af2f352009-01-20 19:11:22 +000039/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
40/// "TheDeclarator" is the declarator that this will be added to.
41DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +000042 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000043 ParamInfo *ArgInfo,
44 unsigned NumArgs,
45 unsigned TypeQuals,
Sebastian Redl7dc81342009-04-29 17:30:04 +000046 bool hasExceptionSpec,
Sebastian Redl3cc97262009-05-31 11:47:27 +000047 SourceLocation ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000048 bool hasAnyExceptionSpec,
49 ActionBase::TypeTy **Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +000050 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +000051 unsigned NumExceptions,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000052 SourceLocation LPLoc,
53 SourceLocation RPLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000054 Declarator &TheDeclarator) {
55 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +000056 I.Kind = Function;
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000057 I.Loc = LPLoc;
58 I.EndLoc = RPLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +000059 I.Fun.hasPrototype = hasProto;
60 I.Fun.isVariadic = isVariadic;
61 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
62 I.Fun.DeleteArgInfo = false;
63 I.Fun.TypeQuals = TypeQuals;
64 I.Fun.NumArgs = NumArgs;
65 I.Fun.ArgInfo = 0;
66 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +000067 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000068 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
69 I.Fun.NumExceptions = NumExceptions;
70 I.Fun.Exceptions = 0;
71
Chris Lattner5af2f352009-01-20 19:11:22 +000072 // new[] an argument array if needed.
73 if (NumArgs) {
74 // If the 'InlineParams' in Declarator is unused and big enough, put our
75 // parameter list there (in an effort to avoid new/delete traffic). If it
76 // is already used (consider a function returning a function pointer) or too
77 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +000078 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +000079 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
80 I.Fun.ArgInfo = TheDeclarator.InlineParams;
81 I.Fun.DeleteArgInfo = false;
82 TheDeclarator.InlineParamsUsed = true;
83 } else {
84 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
85 I.Fun.DeleteArgInfo = true;
86 }
87 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
88 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000089 // new[] an exception array if needed
90 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +000091 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
92 for (unsigned i = 0; i != NumExceptions; ++i) {
93 I.Fun.Exceptions[i].Ty = Exceptions[i];
94 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
95 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000096 }
Chris Lattner5af2f352009-01-20 19:11:22 +000097 return I;
98}
Chris Lattner254be6a2008-11-22 08:32:36 +000099
Reid Spencer5f016e22007-07-11 17:01:13 +0000100/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000101/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000102///
103unsigned DeclSpec::getParsedSpecifiers() const {
104 unsigned Res = 0;
105 if (StorageClassSpec != SCS_unspecified ||
106 SCS_thread_specified)
107 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 if (TypeQualifiers != TQ_unspecified)
110 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 if (hasTypeSpecifier())
113 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Douglas Gregorb48fe382008-10-31 09:07:45 +0000115 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 Res |= PQ_FunctionSpecifier;
117 return Res;
118}
119
John McCallfec54012009-08-03 20:12:06 +0000120template <class T> static bool BadSpecifier(T TNew, T TPrev,
121 const char *&PrevSpec,
122 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000123 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000124 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
125 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000126 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000127}
John McCall32d335e2009-08-03 18:47:27 +0000128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
130 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 case DeclSpec::SCS_unspecified: return "unspecified";
132 case DeclSpec::SCS_typedef: return "typedef";
133 case DeclSpec::SCS_extern: return "extern";
134 case DeclSpec::SCS_static: return "static";
135 case DeclSpec::SCS_auto: return "auto";
136 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000137 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000138 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000140 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000141}
142
John McCall32d335e2009-08-03 18:47:27 +0000143const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000145 case TSW_unspecified: return "unspecified";
146 case TSW_short: return "short";
147 case TSW_long: return "long";
148 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000150 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000151}
152
John McCall32d335e2009-08-03 18:47:27 +0000153const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000155 case TSC_unspecified: return "unspecified";
156 case TSC_imaginary: return "imaginary";
157 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000159 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000160}
161
162
John McCall32d335e2009-08-03 18:47:27 +0000163const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000165 case TSS_unspecified: return "unspecified";
166 case TSS_signed: return "signed";
167 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000169 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000170}
171
172const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
173 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 case DeclSpec::TST_unspecified: return "unspecified";
175 case DeclSpec::TST_void: return "void";
176 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000178 case DeclSpec::TST_char16: return "char16_t";
179 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 case DeclSpec::TST_int: return "int";
181 case DeclSpec::TST_float: return "float";
182 case DeclSpec::TST_double: return "double";
183 case DeclSpec::TST_bool: return "_Bool";
184 case DeclSpec::TST_decimal32: return "_Decimal32";
185 case DeclSpec::TST_decimal64: return "_Decimal64";
186 case DeclSpec::TST_decimal128: return "_Decimal128";
187 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000188 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 case DeclSpec::TST_union: return "union";
190 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000191 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000192 case DeclSpec::TST_typeofType:
193 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000194 case DeclSpec::TST_auto: return "auto";
195 case DeclSpec::TST_decltype: return "(decltype)";
196 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000198 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000199}
200
John McCall32d335e2009-08-03 18:47:27 +0000201const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000203 case DeclSpec::TQ_unspecified: return "unspecified";
204 case DeclSpec::TQ_const: return "const";
205 case DeclSpec::TQ_restrict: return "restrict";
206 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000208 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000209}
210
211bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000212 const char *&PrevSpec,
213 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 if (StorageClassSpec != SCS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000215 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 StorageClassSpec = S;
217 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000218 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 return false;
220}
221
Mike Stump1eb44332009-09-09 15:08:12 +0000222bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000223 const char *&PrevSpec,
224 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 if (SCS_thread_specified) {
226 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000227 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 return true;
229 }
230 SCS_thread_specified = true;
231 SCS_threadLoc = Loc;
232 return false;
233}
234
235
236/// These methods set the specified attribute of the DeclSpec, but return true
237/// and ignore the request if invalid (e.g. "extern" then "auto" is
238/// specified).
239bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000240 const char *&PrevSpec,
241 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 if (TypeSpecWidth != TSW_unspecified &&
243 // Allow turning long -> long long.
244 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000245 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 TypeSpecWidth = W;
247 TSWLoc = Loc;
248 return false;
249}
250
Mike Stump1eb44332009-09-09 15:08:12 +0000251bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000252 const char *&PrevSpec,
253 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000255 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 TypeSpecComplex = C;
257 TSCLoc = Loc;
258 return false;
259}
260
Mike Stump1eb44332009-09-09 15:08:12 +0000261bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000262 const char *&PrevSpec,
263 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000265 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 TypeSpecSign = S;
267 TSSLoc = Loc;
268 return false;
269}
270
271bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000272 const char *&PrevSpec,
273 unsigned &DiagID,
274 void *Rep, bool Owned) {
275 if (TypeSpecType != TST_unspecified) {
276 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
277 DiagID = diag::err_invalid_decl_spec_combination;
278 return true;
279 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 TypeSpecType = T;
281 TypeRep = Rep;
282 TSTLoc = Loc;
Douglas Gregor402abb52009-05-28 23:31:59 +0000283 TypeSpecOwned = Owned;
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 return false;
285}
286
Douglas Gregorddc29e12009-02-06 22:42:48 +0000287bool DeclSpec::SetTypeSpecError() {
288 TypeSpecType = TST_error;
289 TypeRep = 0;
290 TSTLoc = SourceLocation();
291 return false;
292}
293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000295 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 // Duplicates turn into warnings pre-C99.
297 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000298 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 switch (T) {
302 default: assert(0 && "Unknown type qualifier!");
303 case TQ_const: TQ_constLoc = Loc; break;
304 case TQ_restrict: TQ_restrictLoc = Loc; break;
305 case TQ_volatile: TQ_volatileLoc = Loc; break;
306 }
307 return false;
308}
309
John McCallfec54012009-08-03 20:12:06 +0000310bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
311 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // 'inline inline' is ok.
313 FS_inline_specified = true;
314 FS_inlineLoc = Loc;
315 return false;
316}
317
John McCallfec54012009-08-03 20:12:06 +0000318bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
319 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000320 // 'virtual virtual' is ok.
321 FS_virtual_specified = true;
322 FS_virtualLoc = Loc;
323 return false;
324}
325
John McCallfec54012009-08-03 20:12:06 +0000326bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
327 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000328 // 'explicit explicit' is ok.
329 FS_explicit_specified = true;
330 FS_explicitLoc = Loc;
331 return false;
332}
333
John McCallfec54012009-08-03 20:12:06 +0000334bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
335 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000336 if (Friend_specified) {
337 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000338 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000339 return true;
340 }
John McCallfec54012009-08-03 20:12:06 +0000341
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000342 Friend_specified = true;
343 FriendLoc = Loc;
344 return false;
345}
Reid Spencer5f016e22007-07-11 17:01:13 +0000346
Sebastian Redl2ac67232009-11-05 15:47:02 +0000347bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
348 unsigned &DiagID) {
349 // 'constexpr constexpr' is ok.
350 Constexpr_specified = true;
351 ConstexprLoc = Loc;
352 return false;
353}
354
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000355void DeclSpec::setProtocolQualifiers(const ActionBase::DeclPtrTy *Protos,
356 unsigned NP,
357 SourceLocation *ProtoLocs,
358 SourceLocation LAngleLoc) {
359 if (NP == 0) return;
360 ProtocolQualifiers = new ActionBase::DeclPtrTy[NP];
361 ProtocolLocs = new SourceLocation[NP];
362 memcpy((void*)ProtocolQualifiers, Protos, sizeof(ActionBase::DeclPtrTy)*NP);
363 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
364 NumProtocolQualifiers = NP;
365 ProtocolLAngleLoc = LAngleLoc;
366}
367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368/// Finish - This does final analysis of the declspec, rejecting things like
369/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
370/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
371/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000372void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // Check the type specifier components first.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000374 SourceManager &SrcMgr = PP.getSourceManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000375
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000376 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 if (TypeSpecSign != TSS_unspecified) {
378 if (TypeSpecType == TST_unspecified)
379 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000380 else if (TypeSpecType != TST_int &&
381 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000382 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
383 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 // signed double -> double.
385 TypeSpecSign = TSS_unspecified;
386 }
387 }
388
389 // Validate the width of the type.
390 switch (TypeSpecWidth) {
391 case TSW_unspecified: break;
392 case TSW_short: // short int
393 case TSW_longlong: // long long int
394 if (TypeSpecType == TST_unspecified)
395 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
396 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000397 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000399 : diag::err_invalid_longlong_spec)
400 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 TypeSpecType = TST_int;
402 }
403 break;
404 case TSW_long: // long double, long int
405 if (TypeSpecType == TST_unspecified)
406 TypeSpecType = TST_int; // long -> long int.
407 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000408 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
409 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 TypeSpecType = TST_int;
411 }
412 break;
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 // TODO: if the implementation does not implement _Complex or _Imaginary,
416 // disallow their use. Need information about the backend.
417 if (TypeSpecComplex != TSC_unspecified) {
418 if (TypeSpecType == TST_unspecified) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000419 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
420 << CodeModificationHint::CreateInsertion(
421 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
422 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 TypeSpecType = TST_double; // _Complex -> _Complex double.
424 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
425 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000426 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000428 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
429 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 TypeSpecComplex = TSC_unspecified;
431 }
432 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000433
John McCall67d1a672009-08-06 02:15:43 +0000434 // C++ [class.friend]p6:
435 // No storage-class-specifier shall appear in the decl-specifier-seq
436 // of a friend declaration.
437 if (isFriendSpecified() && getStorageClassSpec()) {
438 DeclSpec::SCS SC = getStorageClassSpec();
439 const char *SpecName = getSpecifierName(SC);
440
441 SourceLocation SCLoc = getStorageClassSpecLoc();
442 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
443
444 Diag(D, SCLoc, SrcMgr, diag::err_friend_storage_spec)
445 << SpecName
446 << CodeModificationHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
447
448 ClearStorageClassSpecs();
449 }
450
451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 // 'data definition has no type or storage class'?
457}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000458
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000459bool DeclSpec::isMissingDeclaratorOk() {
460 TST tst = getTypeSpecType();
461 return (tst == TST_union
462 || tst == TST_struct
463 || tst == TST_class
464 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000465 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000466}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000467
468void UnqualifiedId::clear() {
469 if (Kind == IK_TemplateId)
470 TemplateId->Destroy();
471
472 Kind = IK_Identifier;
473 Identifier = 0;
474 StartLocation = SourceLocation();
475 EndLocation = SourceLocation();
476}
477
478void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
479 OverloadedOperatorKind Op,
480 SourceLocation SymbolLocations[3]) {
481 Kind = IK_OperatorFunctionId;
482 StartLocation = OperatorLoc;
483 EndLocation = OperatorLoc;
484 OperatorFunctionId.Operator = Op;
485 for (unsigned I = 0; I != 3; ++I) {
486 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
487
488 if (SymbolLocations[I].isValid())
489 EndLocation = SymbolLocations[I];
490 }
491}