blob: 0a4e036e439981def1ddeb80fa0eac06a89c521b [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 Gregor9b3064b2009-04-01 22:41:11 +000016#include "clang/Lex/Preprocessor.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/LangOptions.h"
Chris Lattner5af2f352009-01-20 19:11:22 +000018#include "llvm/ADT/STLExtras.h"
John McCall32d335e2009-08-03 18:47:27 +000019#include "llvm/Support/ErrorHandling.h"
Douglas Gregore4e5b052009-03-19 00:18:19 +000020#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Chris Lattner254be6a2008-11-22 08:32:36 +000023
24static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
25 SourceManager &SrcMgr, unsigned DiagID) {
26 return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID);
27}
28
Chris Lattner5af2f352009-01-20 19:11:22 +000029/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
30/// "TheDeclarator" is the declarator that this will be added to.
31DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +000032 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000033 ParamInfo *ArgInfo,
34 unsigned NumArgs,
35 unsigned TypeQuals,
Sebastian Redl7dc81342009-04-29 17:30:04 +000036 bool hasExceptionSpec,
Sebastian Redl3cc97262009-05-31 11:47:27 +000037 SourceLocation ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000038 bool hasAnyExceptionSpec,
39 ActionBase::TypeTy **Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +000040 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +000041 unsigned NumExceptions,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000042 SourceLocation LPLoc,
43 SourceLocation RPLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000044 Declarator &TheDeclarator) {
45 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +000046 I.Kind = Function;
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000047 I.Loc = LPLoc;
48 I.EndLoc = RPLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +000049 I.Fun.hasPrototype = hasProto;
50 I.Fun.isVariadic = isVariadic;
51 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
52 I.Fun.DeleteArgInfo = false;
53 I.Fun.TypeQuals = TypeQuals;
54 I.Fun.NumArgs = NumArgs;
55 I.Fun.ArgInfo = 0;
56 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +000057 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000058 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
59 I.Fun.NumExceptions = NumExceptions;
60 I.Fun.Exceptions = 0;
61
Chris Lattner5af2f352009-01-20 19:11:22 +000062 // new[] an argument array if needed.
63 if (NumArgs) {
64 // If the 'InlineParams' in Declarator is unused and big enough, put our
65 // parameter list there (in an effort to avoid new/delete traffic). If it
66 // is already used (consider a function returning a function pointer) or too
67 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +000068 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +000069 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
70 I.Fun.ArgInfo = TheDeclarator.InlineParams;
71 I.Fun.DeleteArgInfo = false;
72 TheDeclarator.InlineParamsUsed = true;
73 } else {
74 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
75 I.Fun.DeleteArgInfo = true;
76 }
77 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
78 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000079 // new[] an exception array if needed
80 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +000081 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
82 for (unsigned i = 0; i != NumExceptions; ++i) {
83 I.Fun.Exceptions[i].Ty = Exceptions[i];
84 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
85 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000086 }
Chris Lattner5af2f352009-01-20 19:11:22 +000087 return I;
88}
Chris Lattner254be6a2008-11-22 08:32:36 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +000091/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +000092///
93unsigned DeclSpec::getParsedSpecifiers() const {
94 unsigned Res = 0;
95 if (StorageClassSpec != SCS_unspecified ||
96 SCS_thread_specified)
97 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +000098
Reid Spencer5f016e22007-07-11 17:01:13 +000099 if (TypeQualifiers != TQ_unspecified)
100 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 if (hasTypeSpecifier())
103 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregorb48fe382008-10-31 09:07:45 +0000105 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 Res |= PQ_FunctionSpecifier;
107 return Res;
108}
109
John McCallfec54012009-08-03 20:12:06 +0000110template <class T> static bool BadSpecifier(T TNew, T TPrev,
111 const char *&PrevSpec,
112 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000113 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000114 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
115 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000116 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000117}
John McCall32d335e2009-08-03 18:47:27 +0000118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
120 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 case DeclSpec::SCS_unspecified: return "unspecified";
122 case DeclSpec::SCS_typedef: return "typedef";
123 case DeclSpec::SCS_extern: return "extern";
124 case DeclSpec::SCS_static: return "static";
125 case DeclSpec::SCS_auto: return "auto";
126 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000127 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000128 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
John McCall32d335e2009-08-03 18:47:27 +0000130 llvm::llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000131}
132
John McCall32d335e2009-08-03 18:47:27 +0000133const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000135 case TSW_unspecified: return "unspecified";
136 case TSW_short: return "short";
137 case TSW_long: return "long";
138 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 }
John McCall32d335e2009-08-03 18:47:27 +0000140 llvm::llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000141}
142
John McCall32d335e2009-08-03 18:47:27 +0000143const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000145 case TSC_unspecified: return "unspecified";
146 case TSC_imaginary: return "imaginary";
147 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
John McCall32d335e2009-08-03 18:47:27 +0000149 llvm::llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}
151
152
John McCall32d335e2009-08-03 18:47:27 +0000153const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000155 case TSS_unspecified: return "unspecified";
156 case TSS_signed: return "signed";
157 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 }
John McCall32d335e2009-08-03 18:47:27 +0000159 llvm::llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000160}
161
162const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
163 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 case DeclSpec::TST_unspecified: return "unspecified";
165 case DeclSpec::TST_void: return "void";
166 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000167 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000168 case DeclSpec::TST_char16: return "char16_t";
169 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 case DeclSpec::TST_int: return "int";
171 case DeclSpec::TST_float: return "float";
172 case DeclSpec::TST_double: return "double";
173 case DeclSpec::TST_bool: return "_Bool";
174 case DeclSpec::TST_decimal32: return "_Decimal32";
175 case DeclSpec::TST_decimal64: return "_Decimal64";
176 case DeclSpec::TST_decimal128: return "_Decimal128";
177 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000178 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 case DeclSpec::TST_union: return "union";
180 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000181 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000182 case DeclSpec::TST_typeofType:
183 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000184 case DeclSpec::TST_auto: return "auto";
185 case DeclSpec::TST_decltype: return "(decltype)";
186 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 }
John McCall32d335e2009-08-03 18:47:27 +0000188 llvm::llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000189}
190
John McCall32d335e2009-08-03 18:47:27 +0000191const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000193 case DeclSpec::TQ_unspecified: return "unspecified";
194 case DeclSpec::TQ_const: return "const";
195 case DeclSpec::TQ_restrict: return "restrict";
196 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
John McCall32d335e2009-08-03 18:47:27 +0000198 llvm::llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000199}
200
201bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000202 const char *&PrevSpec,
203 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 if (StorageClassSpec != SCS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000205 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 StorageClassSpec = S;
207 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000208 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 return false;
210}
211
Mike Stump1eb44332009-09-09 15:08:12 +0000212bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000213 const char *&PrevSpec,
214 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 if (SCS_thread_specified) {
216 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000217 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 return true;
219 }
220 SCS_thread_specified = true;
221 SCS_threadLoc = Loc;
222 return false;
223}
224
225
226/// These methods set the specified attribute of the DeclSpec, but return true
227/// and ignore the request if invalid (e.g. "extern" then "auto" is
228/// specified).
229bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000230 const char *&PrevSpec,
231 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 if (TypeSpecWidth != TSW_unspecified &&
233 // Allow turning long -> long long.
234 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000235 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 TypeSpecWidth = W;
237 TSWLoc = Loc;
238 return false;
239}
240
Mike Stump1eb44332009-09-09 15:08:12 +0000241bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000242 const char *&PrevSpec,
243 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000245 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 TypeSpecComplex = C;
247 TSCLoc = Loc;
248 return false;
249}
250
Mike Stump1eb44332009-09-09 15:08:12 +0000251bool DeclSpec::SetTypeSpecSign(TSS S, 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 (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000255 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 TypeSpecSign = S;
257 TSSLoc = Loc;
258 return false;
259}
260
261bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000262 const char *&PrevSpec,
263 unsigned &DiagID,
264 void *Rep, bool Owned) {
265 if (TypeSpecType != TST_unspecified) {
266 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
267 DiagID = diag::err_invalid_decl_spec_combination;
268 return true;
269 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 TypeSpecType = T;
271 TypeRep = Rep;
272 TSTLoc = Loc;
Douglas Gregor402abb52009-05-28 23:31:59 +0000273 TypeSpecOwned = Owned;
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 return false;
275}
276
Douglas Gregorddc29e12009-02-06 22:42:48 +0000277bool DeclSpec::SetTypeSpecError() {
278 TypeSpecType = TST_error;
279 TypeRep = 0;
280 TSTLoc = SourceLocation();
281 return false;
282}
283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000285 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // Duplicates turn into warnings pre-C99.
287 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000288 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 switch (T) {
292 default: assert(0 && "Unknown type qualifier!");
293 case TQ_const: TQ_constLoc = Loc; break;
294 case TQ_restrict: TQ_restrictLoc = Loc; break;
295 case TQ_volatile: TQ_volatileLoc = Loc; break;
296 }
297 return false;
298}
299
John McCallfec54012009-08-03 20:12:06 +0000300bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
301 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // 'inline inline' is ok.
303 FS_inline_specified = true;
304 FS_inlineLoc = Loc;
305 return false;
306}
307
John McCallfec54012009-08-03 20:12:06 +0000308bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
309 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000310 // 'virtual virtual' is ok.
311 FS_virtual_specified = true;
312 FS_virtualLoc = Loc;
313 return false;
314}
315
John McCallfec54012009-08-03 20:12:06 +0000316bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
317 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000318 // 'explicit explicit' is ok.
319 FS_explicit_specified = true;
320 FS_explicitLoc = Loc;
321 return false;
322}
323
John McCallfec54012009-08-03 20:12:06 +0000324bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
325 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000326 if (Friend_specified) {
327 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000328 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000329 return true;
330 }
John McCallfec54012009-08-03 20:12:06 +0000331
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000332 Friend_specified = true;
333 FriendLoc = Loc;
334 return false;
335}
Reid Spencer5f016e22007-07-11 17:01:13 +0000336
Sebastian Redl2ac67232009-11-05 15:47:02 +0000337bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
338 unsigned &DiagID) {
339 // 'constexpr constexpr' is ok.
340 Constexpr_specified = true;
341 ConstexprLoc = Loc;
342 return false;
343}
344
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000345void DeclSpec::setProtocolQualifiers(const ActionBase::DeclPtrTy *Protos,
346 unsigned NP,
347 SourceLocation *ProtoLocs,
348 SourceLocation LAngleLoc) {
349 if (NP == 0) return;
350 ProtocolQualifiers = new ActionBase::DeclPtrTy[NP];
351 ProtocolLocs = new SourceLocation[NP];
352 memcpy((void*)ProtocolQualifiers, Protos, sizeof(ActionBase::DeclPtrTy)*NP);
353 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
354 NumProtocolQualifiers = NP;
355 ProtocolLAngleLoc = LAngleLoc;
356}
357
Reid Spencer5f016e22007-07-11 17:01:13 +0000358/// Finish - This does final analysis of the declspec, rejecting things like
359/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
360/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
361/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000362void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // Check the type specifier components first.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000364 SourceManager &SrcMgr = PP.getSourceManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000365
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000366 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 if (TypeSpecSign != TSS_unspecified) {
368 if (TypeSpecType == TST_unspecified)
369 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000370 else if (TypeSpecType != TST_int &&
371 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000372 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
373 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 // signed double -> double.
375 TypeSpecSign = TSS_unspecified;
376 }
377 }
378
379 // Validate the width of the type.
380 switch (TypeSpecWidth) {
381 case TSW_unspecified: break;
382 case TSW_short: // short int
383 case TSW_longlong: // long long int
384 if (TypeSpecType == TST_unspecified)
385 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
386 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000387 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000389 : diag::err_invalid_longlong_spec)
390 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 TypeSpecType = TST_int;
392 }
393 break;
394 case TSW_long: // long double, long int
395 if (TypeSpecType == TST_unspecified)
396 TypeSpecType = TST_int; // long -> long int.
397 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000398 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
399 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 TypeSpecType = TST_int;
401 }
402 break;
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 // TODO: if the implementation does not implement _Complex or _Imaginary,
406 // disallow their use. Need information about the backend.
407 if (TypeSpecComplex != TSC_unspecified) {
408 if (TypeSpecType == TST_unspecified) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000409 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
410 << CodeModificationHint::CreateInsertion(
411 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
412 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 TypeSpecType = TST_double; // _Complex -> _Complex double.
414 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
415 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000416 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000418 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
419 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 TypeSpecComplex = TSC_unspecified;
421 }
422 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000423
John McCall67d1a672009-08-06 02:15:43 +0000424 // C++ [class.friend]p6:
425 // No storage-class-specifier shall appear in the decl-specifier-seq
426 // of a friend declaration.
427 if (isFriendSpecified() && getStorageClassSpec()) {
428 DeclSpec::SCS SC = getStorageClassSpec();
429 const char *SpecName = getSpecifierName(SC);
430
431 SourceLocation SCLoc = getStorageClassSpecLoc();
432 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
433
434 Diag(D, SCLoc, SrcMgr, diag::err_friend_storage_spec)
435 << SpecName
436 << CodeModificationHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
437
438 ClearStorageClassSpecs();
439 }
440
441
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 // 'data definition has no type or storage class'?
447}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000448
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000449bool DeclSpec::isMissingDeclaratorOk() {
450 TST tst = getTypeSpecType();
451 return (tst == TST_union
452 || tst == TST_struct
453 || tst == TST_class
454 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000455 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000456}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000457
458void UnqualifiedId::clear() {
459 if (Kind == IK_TemplateId)
460 TemplateId->Destroy();
461
462 Kind = IK_Identifier;
463 Identifier = 0;
464 StartLocation = SourceLocation();
465 EndLocation = SourceLocation();
466}
467
468void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
469 OverloadedOperatorKind Op,
470 SourceLocation SymbolLocations[3]) {
471 Kind = IK_OperatorFunctionId;
472 StartLocation = OperatorLoc;
473 EndLocation = OperatorLoc;
474 OperatorFunctionId.Operator = Op;
475 for (unsigned I = 0; I != 3; ++I) {
476 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
477
478 if (SymbolLocations[I].isValid())
479 EndLocation = SymbolLocations[I];
480 }
481}