blob: f52d8b98567ae3538be6c2db3a044d95239a4de1 [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
Douglas Gregor0efc2c12010-01-13 17:31:36 +000039void 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
Chris Lattner5af2f352009-01-20 19:11:22 +000047/// 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,
Douglas Gregor965acbb2009-02-18 07:07:28 +000050 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000051 ParamInfo *ArgInfo,
52 unsigned NumArgs,
53 unsigned TypeQuals,
Sebastian Redl7dc81342009-04-29 17:30:04 +000054 bool hasExceptionSpec,
Sebastian Redl3cc97262009-05-31 11:47:27 +000055 SourceLocation ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000056 bool hasAnyExceptionSpec,
57 ActionBase::TypeTy **Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +000058 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +000059 unsigned NumExceptions,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000060 SourceLocation LPLoc,
61 SourceLocation RPLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000062 Declarator &TheDeclarator) {
63 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +000064 I.Kind = Function;
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000065 I.Loc = LPLoc;
66 I.EndLoc = RPLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +000067 I.Fun.hasPrototype = hasProto;
68 I.Fun.isVariadic = isVariadic;
69 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
70 I.Fun.DeleteArgInfo = false;
71 I.Fun.TypeQuals = TypeQuals;
72 I.Fun.NumArgs = NumArgs;
73 I.Fun.ArgInfo = 0;
74 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +000075 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000076 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
77 I.Fun.NumExceptions = NumExceptions;
78 I.Fun.Exceptions = 0;
79
Chris Lattner5af2f352009-01-20 19:11:22 +000080 // new[] an argument array if needed.
81 if (NumArgs) {
82 // If the 'InlineParams' in Declarator is unused and big enough, put our
83 // parameter list there (in an effort to avoid new/delete traffic). If it
84 // is already used (consider a function returning a function pointer) or too
85 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +000086 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +000087 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
88 I.Fun.ArgInfo = TheDeclarator.InlineParams;
89 I.Fun.DeleteArgInfo = false;
90 TheDeclarator.InlineParamsUsed = true;
91 } else {
92 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
93 I.Fun.DeleteArgInfo = true;
94 }
95 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
96 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000097 // new[] an exception array if needed
98 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +000099 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
100 for (unsigned i = 0; i != NumExceptions; ++i) {
101 I.Fun.Exceptions[i].Ty = Exceptions[i];
102 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
103 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000104 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000105 return I;
106}
Chris Lattner254be6a2008-11-22 08:32:36 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000109/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000110///
111unsigned DeclSpec::getParsedSpecifiers() const {
112 unsigned Res = 0;
113 if (StorageClassSpec != SCS_unspecified ||
114 SCS_thread_specified)
115 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 if (TypeQualifiers != TQ_unspecified)
118 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 if (hasTypeSpecifier())
121 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Douglas Gregorb48fe382008-10-31 09:07:45 +0000123 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 Res |= PQ_FunctionSpecifier;
125 return Res;
126}
127
John McCallfec54012009-08-03 20:12:06 +0000128template <class T> static bool BadSpecifier(T TNew, T TPrev,
129 const char *&PrevSpec,
130 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000131 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000132 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
133 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000134 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000135}
John McCall32d335e2009-08-03 18:47:27 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
138 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 case DeclSpec::SCS_unspecified: return "unspecified";
140 case DeclSpec::SCS_typedef: return "typedef";
141 case DeclSpec::SCS_extern: return "extern";
142 case DeclSpec::SCS_static: return "static";
143 case DeclSpec::SCS_auto: return "auto";
144 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000145 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000146 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000148 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
John McCall32d335e2009-08-03 18:47:27 +0000151const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000153 case TSW_unspecified: return "unspecified";
154 case TSW_short: return "short";
155 case TSW_long: return "long";
156 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000158 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000159}
160
John McCall32d335e2009-08-03 18:47:27 +0000161const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000163 case TSC_unspecified: return "unspecified";
164 case TSC_imaginary: return "imaginary";
165 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000167 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000168}
169
170
John McCall32d335e2009-08-03 18:47:27 +0000171const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000173 case TSS_unspecified: return "unspecified";
174 case TSS_signed: return "signed";
175 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000177 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000178}
179
180const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
181 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 case DeclSpec::TST_unspecified: return "unspecified";
183 case DeclSpec::TST_void: return "void";
184 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000185 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000186 case DeclSpec::TST_char16: return "char16_t";
187 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 case DeclSpec::TST_int: return "int";
189 case DeclSpec::TST_float: return "float";
190 case DeclSpec::TST_double: return "double";
191 case DeclSpec::TST_bool: return "_Bool";
192 case DeclSpec::TST_decimal32: return "_Decimal32";
193 case DeclSpec::TST_decimal64: return "_Decimal64";
194 case DeclSpec::TST_decimal128: return "_Decimal128";
195 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000196 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 case DeclSpec::TST_union: return "union";
198 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000199 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000200 case DeclSpec::TST_typeofType:
201 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000202 case DeclSpec::TST_auto: return "auto";
203 case DeclSpec::TST_decltype: return "(decltype)";
204 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000206 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000207}
208
John McCall32d335e2009-08-03 18:47:27 +0000209const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000211 case DeclSpec::TQ_unspecified: return "unspecified";
212 case DeclSpec::TQ_const: return "const";
213 case DeclSpec::TQ_restrict: return "restrict";
214 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000216 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000217}
218
219bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000220 const char *&PrevSpec,
221 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 if (StorageClassSpec != SCS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000223 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 StorageClassSpec = S;
225 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000226 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 return false;
228}
229
Mike Stump1eb44332009-09-09 15:08:12 +0000230bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000231 const char *&PrevSpec,
232 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 if (SCS_thread_specified) {
234 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000235 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 return true;
237 }
238 SCS_thread_specified = true;
239 SCS_threadLoc = Loc;
240 return false;
241}
242
243
244/// These methods set the specified attribute of the DeclSpec, but return true
245/// and ignore the request if invalid (e.g. "extern" then "auto" is
246/// specified).
247bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000248 const char *&PrevSpec,
249 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 if (TypeSpecWidth != TSW_unspecified &&
251 // Allow turning long -> long long.
252 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000253 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 TypeSpecWidth = W;
255 TSWLoc = Loc;
256 return false;
257}
258
Mike Stump1eb44332009-09-09 15:08:12 +0000259bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000260 const char *&PrevSpec,
261 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000263 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 TypeSpecComplex = C;
265 TSCLoc = Loc;
266 return false;
267}
268
Mike Stump1eb44332009-09-09 15:08:12 +0000269bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000270 const char *&PrevSpec,
271 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000273 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 TypeSpecSign = S;
275 TSSLoc = Loc;
276 return false;
277}
278
279bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000280 const char *&PrevSpec,
281 unsigned &DiagID,
282 void *Rep, bool Owned) {
283 if (TypeSpecType != TST_unspecified) {
284 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
285 DiagID = diag::err_invalid_decl_spec_combination;
286 return true;
287 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 TypeSpecType = T;
289 TypeRep = Rep;
290 TSTLoc = Loc;
Douglas Gregor402abb52009-05-28 23:31:59 +0000291 TypeSpecOwned = Owned;
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 return false;
293}
294
Douglas Gregorddc29e12009-02-06 22:42:48 +0000295bool DeclSpec::SetTypeSpecError() {
296 TypeSpecType = TST_error;
297 TypeRep = 0;
298 TSTLoc = SourceLocation();
299 return false;
300}
301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000303 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // Duplicates turn into warnings pre-C99.
305 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000306 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 switch (T) {
310 default: assert(0 && "Unknown type qualifier!");
311 case TQ_const: TQ_constLoc = Loc; break;
312 case TQ_restrict: TQ_restrictLoc = Loc; break;
313 case TQ_volatile: TQ_volatileLoc = Loc; break;
314 }
315 return false;
316}
317
John McCallfec54012009-08-03 20:12:06 +0000318bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
319 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // 'inline inline' is ok.
321 FS_inline_specified = true;
322 FS_inlineLoc = Loc;
323 return false;
324}
325
John McCallfec54012009-08-03 20:12:06 +0000326bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
327 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000328 // 'virtual virtual' is ok.
329 FS_virtual_specified = true;
330 FS_virtualLoc = Loc;
331 return false;
332}
333
John McCallfec54012009-08-03 20:12:06 +0000334bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
335 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000336 // 'explicit explicit' is ok.
337 FS_explicit_specified = true;
338 FS_explicitLoc = Loc;
339 return false;
340}
341
John McCallfec54012009-08-03 20:12:06 +0000342bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
343 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000344 if (Friend_specified) {
345 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000346 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000347 return true;
348 }
John McCallfec54012009-08-03 20:12:06 +0000349
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000350 Friend_specified = true;
351 FriendLoc = Loc;
352 return false;
353}
Reid Spencer5f016e22007-07-11 17:01:13 +0000354
Sebastian Redl2ac67232009-11-05 15:47:02 +0000355bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
356 unsigned &DiagID) {
357 // 'constexpr constexpr' is ok.
358 Constexpr_specified = true;
359 ConstexprLoc = Loc;
360 return false;
361}
362
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000363void DeclSpec::setProtocolQualifiers(const ActionBase::DeclPtrTy *Protos,
364 unsigned NP,
365 SourceLocation *ProtoLocs,
366 SourceLocation LAngleLoc) {
367 if (NP == 0) return;
368 ProtocolQualifiers = new ActionBase::DeclPtrTy[NP];
369 ProtocolLocs = new SourceLocation[NP];
370 memcpy((void*)ProtocolQualifiers, Protos, sizeof(ActionBase::DeclPtrTy)*NP);
371 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
372 NumProtocolQualifiers = NP;
373 ProtocolLAngleLoc = LAngleLoc;
374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// Finish - This does final analysis of the declspec, rejecting things like
377/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
378/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
379/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000380void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 // Check the type specifier components first.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000382 SourceManager &SrcMgr = PP.getSourceManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000383
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000384 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 if (TypeSpecSign != TSS_unspecified) {
386 if (TypeSpecType == TST_unspecified)
387 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000388 else if (TypeSpecType != TST_int &&
389 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000390 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
391 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 // signed double -> double.
393 TypeSpecSign = TSS_unspecified;
394 }
395 }
396
397 // Validate the width of the type.
398 switch (TypeSpecWidth) {
399 case TSW_unspecified: break;
400 case TSW_short: // short int
401 case TSW_longlong: // long long int
402 if (TypeSpecType == TST_unspecified)
403 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
404 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000405 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000407 : diag::err_invalid_longlong_spec)
408 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 TypeSpecType = TST_int;
410 }
411 break;
412 case TSW_long: // long double, long int
413 if (TypeSpecType == TST_unspecified)
414 TypeSpecType = TST_int; // long -> long int.
415 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000416 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
417 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 TypeSpecType = TST_int;
419 }
420 break;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 // TODO: if the implementation does not implement _Complex or _Imaginary,
424 // disallow their use. Need information about the backend.
425 if (TypeSpecComplex != TSC_unspecified) {
426 if (TypeSpecType == TST_unspecified) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000427 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
428 << CodeModificationHint::CreateInsertion(
429 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
430 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 TypeSpecType = TST_double; // _Complex -> _Complex double.
432 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
433 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000434 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000436 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
437 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 TypeSpecComplex = TSC_unspecified;
439 }
440 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000441
John McCall67d1a672009-08-06 02:15:43 +0000442 // C++ [class.friend]p6:
443 // No storage-class-specifier shall appear in the decl-specifier-seq
444 // of a friend declaration.
445 if (isFriendSpecified() && getStorageClassSpec()) {
446 DeclSpec::SCS SC = getStorageClassSpec();
447 const char *SpecName = getSpecifierName(SC);
448
449 SourceLocation SCLoc = getStorageClassSpecLoc();
450 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
451
452 Diag(D, SCLoc, SrcMgr, diag::err_friend_storage_spec)
453 << SpecName
454 << CodeModificationHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
455
456 ClearStorageClassSpecs();
457 }
458
459
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 // 'data definition has no type or storage class'?
465}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000466
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000467bool DeclSpec::isMissingDeclaratorOk() {
468 TST tst = getTypeSpecType();
469 return (tst == TST_union
470 || tst == TST_struct
471 || tst == TST_class
472 || tst == TST_enum
Douglas Gregor4920f1f2009-01-12 22:49:06 +0000473 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000474}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000475
476void UnqualifiedId::clear() {
477 if (Kind == IK_TemplateId)
478 TemplateId->Destroy();
479
480 Kind = IK_Identifier;
481 Identifier = 0;
482 StartLocation = SourceLocation();
483 EndLocation = SourceLocation();
484}
485
486void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
487 OverloadedOperatorKind Op,
488 SourceLocation SymbolLocations[3]) {
489 Kind = IK_OperatorFunctionId;
490 StartLocation = OperatorLoc;
491 EndLocation = OperatorLoc;
492 OperatorFunctionId.Operator = Op;
493 for (unsigned I = 0; I != 3; ++I) {
494 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
495
496 if (SymbolLocations[I].isValid())
497 EndLocation = SymbolLocations[I];
498 }
499}