blob: bc289ec42c99e029170ae2e7009d1eb4e6e64f18 [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
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
15#include "clang/Sema/DeclSpec.h"
16#include "clang/Sema/ParsedTemplate.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,
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000026 unsigned DiagID) {
27 return D.Report(Loc, DiagID);
Chris Lattner254be6a2008-11-22 08:32:36 +000028}
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.
John McCall7f040a92010-12-24 02:08:15 +000049DeclaratorChunk DeclaratorChunk::getFunction(const ParsedAttributes &attrs,
50 bool hasProto, bool isVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +000051 SourceLocation EllipsisLoc,
Chris Lattner5af2f352009-01-20 19:11:22 +000052 ParamInfo *ArgInfo,
53 unsigned NumArgs,
54 unsigned TypeQuals,
Douglas Gregor83f51722011-01-26 03:43:54 +000055 bool RefQualifierIsLvalueRef,
56 SourceLocation RefQualifierLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000057 bool hasExceptionSpec,
Sebastian Redl3cc97262009-05-31 11:47:27 +000058 SourceLocation ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +000059 bool hasAnyExceptionSpec,
John McCallb3d87482010-08-24 05:47:05 +000060 ParsedType *Exceptions,
Sebastian Redlef65f062009-05-29 18:02:33 +000061 SourceRange *ExceptionRanges,
Sebastian Redl7dc81342009-04-29 17:30:04 +000062 unsigned NumExceptions,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000063 SourceLocation LPLoc,
64 SourceLocation RPLoc,
Douglas Gregordab60ad2010-10-01 18:44:50 +000065 Declarator &TheDeclarator,
66 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +000067 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +000068 I.Kind = Function;
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000069 I.Loc = LPLoc;
70 I.EndLoc = RPLoc;
John McCall7f040a92010-12-24 02:08:15 +000071 I.Fun.AttrList = attrs.getList();
Sebastian Redl7dc81342009-04-29 17:30:04 +000072 I.Fun.hasPrototype = hasProto;
73 I.Fun.isVariadic = isVariadic;
74 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
75 I.Fun.DeleteArgInfo = false;
76 I.Fun.TypeQuals = TypeQuals;
77 I.Fun.NumArgs = NumArgs;
78 I.Fun.ArgInfo = 0;
Douglas Gregor83f51722011-01-26 03:43:54 +000079 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
80 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000081 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +000082 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000083 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
84 I.Fun.NumExceptions = NumExceptions;
85 I.Fun.Exceptions = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +000086 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +000087
Chris Lattner5af2f352009-01-20 19:11:22 +000088 // new[] an argument array if needed.
89 if (NumArgs) {
90 // If the 'InlineParams' in Declarator is unused and big enough, put our
91 // parameter list there (in an effort to avoid new/delete traffic). If it
92 // is already used (consider a function returning a function pointer) or too
93 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +000094 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +000095 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
96 I.Fun.ArgInfo = TheDeclarator.InlineParams;
97 I.Fun.DeleteArgInfo = false;
98 TheDeclarator.InlineParamsUsed = true;
99 } else {
100 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
101 I.Fun.DeleteArgInfo = true;
102 }
103 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
104 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000105 // new[] an exception array if needed
106 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +0000107 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
108 for (unsigned i = 0; i != NumExceptions; ++i) {
109 I.Fun.Exceptions[i].Ty = Exceptions[i];
110 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
111 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000112 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000113 return I;
114}
Chris Lattner254be6a2008-11-22 08:32:36 +0000115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000117/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000118///
119unsigned DeclSpec::getParsedSpecifiers() const {
120 unsigned Res = 0;
121 if (StorageClassSpec != SCS_unspecified ||
122 SCS_thread_specified)
123 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 if (TypeQualifiers != TQ_unspecified)
126 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 if (hasTypeSpecifier())
129 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregorb48fe382008-10-31 09:07:45 +0000131 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 Res |= PQ_FunctionSpecifier;
133 return Res;
134}
135
John McCallfec54012009-08-03 20:12:06 +0000136template <class T> static bool BadSpecifier(T TNew, T TPrev,
137 const char *&PrevSpec,
138 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000139 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000140 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
141 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000142 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000143}
John McCall32d335e2009-08-03 18:47:27 +0000144
Reid Spencer5f016e22007-07-11 17:01:13 +0000145const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
146 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 case DeclSpec::SCS_unspecified: return "unspecified";
148 case DeclSpec::SCS_typedef: return "typedef";
149 case DeclSpec::SCS_extern: return "extern";
150 case DeclSpec::SCS_static: return "static";
151 case DeclSpec::SCS_auto: return "auto";
152 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000153 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000154 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000156 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
John McCall32d335e2009-08-03 18:47:27 +0000159const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000161 case TSW_unspecified: return "unspecified";
162 case TSW_short: return "short";
163 case TSW_long: return "long";
164 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000166 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
John McCall32d335e2009-08-03 18:47:27 +0000169const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000171 case TSC_unspecified: return "unspecified";
172 case TSC_imaginary: return "imaginary";
173 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000175 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
178
John McCall32d335e2009-08-03 18:47:27 +0000179const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000181 case TSS_unspecified: return "unspecified";
182 case TSS_signed: return "signed";
183 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000185 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000186}
187
188const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
189 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 case DeclSpec::TST_unspecified: return "unspecified";
191 case DeclSpec::TST_void: return "void";
192 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000193 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000194 case DeclSpec::TST_char16: return "char16_t";
195 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 case DeclSpec::TST_int: return "int";
197 case DeclSpec::TST_float: return "float";
198 case DeclSpec::TST_double: return "double";
199 case DeclSpec::TST_bool: return "_Bool";
200 case DeclSpec::TST_decimal32: return "_Decimal32";
201 case DeclSpec::TST_decimal64: return "_Decimal64";
202 case DeclSpec::TST_decimal128: return "_Decimal128";
203 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000204 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 case DeclSpec::TST_union: return "union";
206 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000207 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000208 case DeclSpec::TST_typeofType:
209 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000210 case DeclSpec::TST_auto: return "auto";
211 case DeclSpec::TST_decltype: return "(decltype)";
212 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000214 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000215}
216
John McCall32d335e2009-08-03 18:47:27 +0000217const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000219 case DeclSpec::TQ_unspecified: return "unspecified";
220 case DeclSpec::TQ_const: return "const";
221 case DeclSpec::TQ_restrict: return "restrict";
222 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000224 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000225}
226
227bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000228 const char *&PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +0000229 unsigned &DiagID,
230 const LangOptions &Lang) {
231 // OpenCL prohibits extern, auto, register, and static
232 // It seems sensible to prohibit private_extern too
233 if (Lang.OpenCL) {
234 switch (S) {
235 case SCS_extern:
236 case SCS_private_extern:
237 case SCS_auto:
238 case SCS_register:
239 case SCS_static:
240 DiagID = diag::err_not_opencl_storage_class_specifier;
241 PrevSpec = getSpecifierName(S);
242 return true;
243 default:
244 break;
245 }
246 }
247
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000248 if (StorageClassSpec != SCS_unspecified) {
249 // Changing storage class is allowed only if the previous one
250 // was the 'extern' that is part of a linkage specification and
251 // the new storage class is 'typedef'.
252 if (!(SCS_extern_in_linkage_spec &&
253 StorageClassSpec == SCS_extern &&
254 S == SCS_typedef))
255 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
256 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 StorageClassSpec = S;
258 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000259 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 return false;
261}
262
Mike Stump1eb44332009-09-09 15:08:12 +0000263bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000264 const char *&PrevSpec,
265 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 if (SCS_thread_specified) {
267 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000268 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 return true;
270 }
271 SCS_thread_specified = true;
272 SCS_threadLoc = Loc;
273 return false;
274}
275
Reid Spencer5f016e22007-07-11 17:01:13 +0000276/// These methods set the specified attribute of the DeclSpec, but return true
277/// and ignore the request if invalid (e.g. "extern" then "auto" is
278/// specified).
279bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000280 const char *&PrevSpec,
281 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 if (TypeSpecWidth != TSW_unspecified &&
283 // Allow turning long -> long long.
284 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000285 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 TypeSpecWidth = W;
287 TSWLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000288 if (TypeAltiVecVector && !TypeAltiVecBool &&
289 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000290 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
291 DiagID = diag::warn_vector_long_decl_spec_combination;
292 return true;
293 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 return false;
295}
296
Mike Stump1eb44332009-09-09 15:08:12 +0000297bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000298 const char *&PrevSpec,
299 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000301 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 TypeSpecComplex = C;
303 TSCLoc = Loc;
304 return false;
305}
306
Mike Stump1eb44332009-09-09 15:08:12 +0000307bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000308 const char *&PrevSpec,
309 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000311 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 TypeSpecSign = S;
313 TSSLoc = Loc;
314 return false;
315}
316
317bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000318 const char *&PrevSpec,
319 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000320 ParsedType Rep) {
321 assert(isTypeRep(T) && "T does not store a type");
322 assert(Rep && "no type provided!");
323 if (TypeSpecType != TST_unspecified) {
324 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
325 DiagID = diag::err_invalid_decl_spec_combination;
326 return true;
327 }
328 TypeSpecType = T;
329 TypeRep = Rep;
330 TSTLoc = Loc;
331 TypeSpecOwned = false;
332 return false;
333}
334
335bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
336 const char *&PrevSpec,
337 unsigned &DiagID,
338 Expr *Rep) {
339 assert(isExprRep(T) && "T does not store an expr");
340 assert(Rep && "no expression provided!");
341 if (TypeSpecType != TST_unspecified) {
342 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
343 DiagID = diag::err_invalid_decl_spec_combination;
344 return true;
345 }
346 TypeSpecType = T;
347 ExprRep = Rep;
348 TSTLoc = Loc;
349 TypeSpecOwned = false;
350 return false;
351}
352
353bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
354 const char *&PrevSpec,
355 unsigned &DiagID,
356 Decl *Rep, bool Owned) {
357 assert(isDeclRep(T) && "T does not store a decl");
358 // Unlike the other cases, we don't assert that we actually get a decl.
359
360 if (TypeSpecType != TST_unspecified) {
361 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
362 DiagID = diag::err_invalid_decl_spec_combination;
363 return true;
364 }
365 TypeSpecType = T;
366 DeclRep = Rep;
367 TSTLoc = Loc;
368 TypeSpecOwned = Owned;
369 return false;
370}
371
372bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
373 const char *&PrevSpec,
374 unsigned &DiagID) {
375 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
376 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000377 if (TypeSpecType != TST_unspecified) {
378 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
379 DiagID = diag::err_invalid_decl_spec_combination;
380 return true;
381 }
Chris Lattner788b0fd2010-06-23 06:00:24 +0000382 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
383 TypeAltiVecBool = true;
384 TSTLoc = Loc;
385 return false;
386 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 TypeSpecType = T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 TSTLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000389 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000390 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000391 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000392 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000393 return true;
394 }
395 return false;
396}
397
398bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
399 const char *&PrevSpec, unsigned &DiagID) {
400 if (TypeSpecType != TST_unspecified) {
401 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
402 DiagID = diag::err_invalid_vector_decl_spec_combination;
403 return true;
404 }
405 TypeAltiVecVector = isAltiVecVector;
406 AltiVecLoc = Loc;
407 return false;
408}
409
410bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
411 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000412 if (!TypeAltiVecVector || TypeAltiVecPixel ||
413 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000414 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
415 DiagID = diag::err_invalid_pixel_decl_spec_combination;
416 return true;
417 }
John Thompson82287d12010-02-05 00:12:22 +0000418 TypeAltiVecPixel = isAltiVecPixel;
419 TSTLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 return false;
421}
422
Douglas Gregorddc29e12009-02-06 22:42:48 +0000423bool DeclSpec::SetTypeSpecError() {
424 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000425 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000426 TSTLoc = SourceLocation();
427 return false;
428}
429
Reid Spencer5f016e22007-07-11 17:01:13 +0000430bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000431 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 // Duplicates turn into warnings pre-C99.
433 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000434 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 switch (T) {
438 default: assert(0 && "Unknown type qualifier!");
439 case TQ_const: TQ_constLoc = Loc; break;
440 case TQ_restrict: TQ_restrictLoc = Loc; break;
441 case TQ_volatile: TQ_volatileLoc = Loc; break;
442 }
443 return false;
444}
445
John McCallfec54012009-08-03 20:12:06 +0000446bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
447 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 // 'inline inline' is ok.
449 FS_inline_specified = true;
450 FS_inlineLoc = Loc;
451 return false;
452}
453
John McCallfec54012009-08-03 20:12:06 +0000454bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
455 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000456 // 'virtual virtual' is ok.
457 FS_virtual_specified = true;
458 FS_virtualLoc = Loc;
459 return false;
460}
461
John McCallfec54012009-08-03 20:12:06 +0000462bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
463 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000464 // 'explicit explicit' is ok.
465 FS_explicit_specified = true;
466 FS_explicitLoc = Loc;
467 return false;
468}
469
John McCallfec54012009-08-03 20:12:06 +0000470bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
471 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000472 if (Friend_specified) {
473 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000474 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000475 return true;
476 }
John McCallfec54012009-08-03 20:12:06 +0000477
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000478 Friend_specified = true;
479 FriendLoc = Loc;
480 return false;
481}
Reid Spencer5f016e22007-07-11 17:01:13 +0000482
Sebastian Redl2ac67232009-11-05 15:47:02 +0000483bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
484 unsigned &DiagID) {
485 // 'constexpr constexpr' is ok.
486 Constexpr_specified = true;
487 ConstexprLoc = Loc;
488 return false;
489}
490
John McCalld226f652010-08-21 09:40:31 +0000491void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000492 unsigned NP,
493 SourceLocation *ProtoLocs,
494 SourceLocation LAngleLoc) {
495 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000496 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000497 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000498 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000499 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
500 NumProtocolQualifiers = NP;
501 ProtocolLAngleLoc = LAngleLoc;
502}
503
Douglas Gregorddf889a2010-01-18 18:04:31 +0000504void DeclSpec::SaveWrittenBuiltinSpecs() {
505 writtenBS.Sign = getTypeSpecSign();
506 writtenBS.Width = getTypeSpecWidth();
507 writtenBS.Type = getTypeSpecType();
508 // Search the list of attributes for the presence of a mode attribute.
509 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000510 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000511 while (attrs) {
512 if (attrs->getKind() == AttributeList::AT_mode) {
513 writtenBS.ModeAttr = true;
514 break;
515 }
516 attrs = attrs->getNext();
517 }
518}
519
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000520void DeclSpec::SaveStorageSpecifierAsWritten() {
521 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
522 // If 'extern' is part of a linkage specification,
523 // then it is not a storage class "as written".
524 StorageClassSpecAsWritten = SCS_unspecified;
525 else
526 StorageClassSpecAsWritten = StorageClassSpec;
527}
528
Reid Spencer5f016e22007-07-11 17:01:13 +0000529/// Finish - This does final analysis of the declspec, rejecting things like
530/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
531/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
532/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000533void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000534 // Before possibly changing their values, save specs as written.
535 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000536 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000537
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 // Check the type specifier components first.
539
Chris Lattner788b0fd2010-06-23 06:00:24 +0000540 // Validate and finalize AltiVec vector declspec.
541 if (TypeAltiVecVector) {
542 if (TypeAltiVecBool) {
543 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
544 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000545 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000546 << getSpecifierName((TSS)TypeSpecSign);
547 }
548
549 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000550 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
551 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000552 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000553 << (TypeAltiVecPixel ? "__pixel" :
554 getSpecifierName((TST)TypeSpecType));
555 }
556
557 // Only 'short' is valid with vector bool. (PIM 2.1)
558 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000559 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000560 << getSpecifierName((TSW)TypeSpecWidth);
561
562 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
563 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
564 (TypeSpecWidth != TSW_unspecified))
565 TypeSpecSign = TSS_unsigned;
566 }
567
568 if (TypeAltiVecPixel) {
569 //TODO: perform validation
570 TypeSpecType = TST_int;
571 TypeSpecSign = TSS_unsigned;
572 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000573 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000574 }
575 }
576
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000577 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 if (TypeSpecSign != TSS_unspecified) {
579 if (TypeSpecType == TST_unspecified)
580 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000581 else if (TypeSpecType != TST_int &&
582 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000583 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000584 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 // signed double -> double.
586 TypeSpecSign = TSS_unspecified;
587 }
588 }
589
590 // Validate the width of the type.
591 switch (TypeSpecWidth) {
592 case TSW_unspecified: break;
593 case TSW_short: // short int
594 case TSW_longlong: // long long int
595 if (TypeSpecType == TST_unspecified)
596 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
597 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000598 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000600 : diag::err_invalid_longlong_spec)
601 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000603 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 }
605 break;
606 case TSW_long: // long double, long int
607 if (TypeSpecType == TST_unspecified)
608 TypeSpecType = TST_int; // long -> long int.
609 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000610 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000611 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000613 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 }
615 break;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 // TODO: if the implementation does not implement _Complex or _Imaginary,
619 // disallow their use. Need information about the backend.
620 if (TypeSpecComplex != TSC_unspecified) {
621 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000622 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000623 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000624 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
625 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 TypeSpecType = TST_double; // _Complex -> _Complex double.
627 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
628 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000629 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000631 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000632 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 TypeSpecComplex = TSC_unspecified;
634 }
635 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000636
John McCall67d1a672009-08-06 02:15:43 +0000637 // C++ [class.friend]p6:
638 // No storage-class-specifier shall appear in the decl-specifier-seq
639 // of a friend declaration.
640 if (isFriendSpecified() && getStorageClassSpec()) {
641 DeclSpec::SCS SC = getStorageClassSpec();
642 const char *SpecName = getSpecifierName(SC);
643
644 SourceLocation SCLoc = getStorageClassSpecLoc();
645 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
646
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000647 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000648 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000649 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000650
651 ClearStorageClassSpecs();
652 }
653
John McCall9e46b8c2010-08-26 17:22:34 +0000654 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 // 'data definition has no type or storage class'?
661}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000662
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000663bool DeclSpec::isMissingDeclaratorOk() {
664 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000665 return isDeclRep(tst) && getRepAsDecl() != 0 &&
666 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000667}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000668
669void UnqualifiedId::clear() {
670 if (Kind == IK_TemplateId)
671 TemplateId->Destroy();
672
673 Kind = IK_Identifier;
674 Identifier = 0;
675 StartLocation = SourceLocation();
676 EndLocation = SourceLocation();
677}
678
679void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
680 OverloadedOperatorKind Op,
681 SourceLocation SymbolLocations[3]) {
682 Kind = IK_OperatorFunctionId;
683 StartLocation = OperatorLoc;
684 EndLocation = OperatorLoc;
685 OperatorFunctionId.Operator = Op;
686 for (unsigned I = 0; I != 3; ++I) {
687 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
688
689 if (SymbolLocations[I].isValid())
690 EndLocation = SymbolLocations[I];
691 }
692}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000693
Anders Carlssoncc54d592011-01-22 16:56:46 +0000694bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000695 const char *&PrevSpec) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000696 if (Specifiers & VS) {
697 PrevSpec = getSpecifierName(VS);
698 return true;
699 }
700
701 Specifiers |= VS;
702
703 switch (VS) {
704 default: assert(0 && "Unknown specifier!");
705 case VS_Override: VS_overrideLoc = Loc; break;
706 case VS_Final: VS_finalLoc = Loc; break;
707 case VS_New: VS_newLoc = Loc; break;
708 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000709
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000710 return false;
711}
712
Anders Carlssoncc54d592011-01-22 16:56:46 +0000713const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000714 switch (VS) {
715 default: assert(0 && "Unknown specifier");
716 case VS_Override: return "override";
717 case VS_Final: return "final";
718 case VS_New: return "new";
719 }
720}
Anders Carlsson46127a92011-01-22 15:58:16 +0000721
Anders Carlssoncc54d592011-01-22 16:56:46 +0000722bool ClassVirtSpecifiers::SetSpecifier(Specifier CVS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000723 const char *&PrevSpec) {
724 if (Specifiers & CVS) {
725 PrevSpec = getSpecifierName(CVS);
726 return true;
727 }
728
729 Specifiers |= CVS;
730
731 switch (CVS) {
732 default: assert(0 && "Unknown specifier!");
733 case CVS_Final: CVS_finalLoc = Loc; break;
734 case CVS_Explicit: CVS_explicitLoc = Loc; break;
735 }
736
737 return false;
738}
739
Anders Carlssoncc54d592011-01-22 16:56:46 +0000740const char *ClassVirtSpecifiers::getSpecifierName(Specifier CVS) {
Anders Carlsson46127a92011-01-22 15:58:16 +0000741 switch (CVS) {
742 default: assert(0 && "Unknown specifier");
743 case CVS_Final: return "final";
744 case CVS_Explicit: return "explicit";
745 }
746}
747