blob: 8a35ab70923f904b3af1ae15cbe2805a17469b37 [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,
229 unsigned &DiagID) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000230 if (StorageClassSpec != SCS_unspecified) {
231 // Changing storage class is allowed only if the previous one
232 // was the 'extern' that is part of a linkage specification and
233 // the new storage class is 'typedef'.
234 if (!(SCS_extern_in_linkage_spec &&
235 StorageClassSpec == SCS_extern &&
236 S == SCS_typedef))
237 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
238 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 StorageClassSpec = S;
240 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000241 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 return false;
243}
244
Mike Stump1eb44332009-09-09 15:08:12 +0000245bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000246 const char *&PrevSpec,
247 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 if (SCS_thread_specified) {
249 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000250 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 return true;
252 }
253 SCS_thread_specified = true;
254 SCS_threadLoc = Loc;
255 return false;
256}
257
Reid Spencer5f016e22007-07-11 17:01:13 +0000258/// These methods set the specified attribute of the DeclSpec, but return true
259/// and ignore the request if invalid (e.g. "extern" then "auto" is
260/// specified).
261bool DeclSpec::SetTypeSpecWidth(TSW W, 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 (TypeSpecWidth != TSW_unspecified &&
265 // Allow turning long -> long long.
266 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000267 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 TypeSpecWidth = W;
269 TSWLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000270 if (TypeAltiVecVector && !TypeAltiVecBool &&
271 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000272 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
273 DiagID = diag::warn_vector_long_decl_spec_combination;
274 return true;
275 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 return false;
277}
278
Mike Stump1eb44332009-09-09 15:08:12 +0000279bool DeclSpec::SetTypeSpecComplex(TSC C, 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 (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000283 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 TypeSpecComplex = C;
285 TSCLoc = Loc;
286 return false;
287}
288
Mike Stump1eb44332009-09-09 15:08:12 +0000289bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000290 const char *&PrevSpec,
291 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000293 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 TypeSpecSign = S;
295 TSSLoc = Loc;
296 return false;
297}
298
299bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000300 const char *&PrevSpec,
301 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000302 ParsedType Rep) {
303 assert(isTypeRep(T) && "T does not store a type");
304 assert(Rep && "no type provided!");
305 if (TypeSpecType != TST_unspecified) {
306 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
307 DiagID = diag::err_invalid_decl_spec_combination;
308 return true;
309 }
310 TypeSpecType = T;
311 TypeRep = Rep;
312 TSTLoc = Loc;
313 TypeSpecOwned = false;
314 return false;
315}
316
317bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
318 const char *&PrevSpec,
319 unsigned &DiagID,
320 Expr *Rep) {
321 assert(isExprRep(T) && "T does not store an expr");
322 assert(Rep && "no expression 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 ExprRep = 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 Decl *Rep, bool Owned) {
339 assert(isDeclRep(T) && "T does not store a decl");
340 // Unlike the other cases, we don't assert that we actually get a decl.
341
342 if (TypeSpecType != TST_unspecified) {
343 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
344 DiagID = diag::err_invalid_decl_spec_combination;
345 return true;
346 }
347 TypeSpecType = T;
348 DeclRep = Rep;
349 TSTLoc = Loc;
350 TypeSpecOwned = Owned;
351 return false;
352}
353
354bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
355 const char *&PrevSpec,
356 unsigned &DiagID) {
357 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
358 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000359 if (TypeSpecType != TST_unspecified) {
360 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
361 DiagID = diag::err_invalid_decl_spec_combination;
362 return true;
363 }
Chris Lattner788b0fd2010-06-23 06:00:24 +0000364 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
365 TypeAltiVecBool = true;
366 TSTLoc = Loc;
367 return false;
368 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 TypeSpecType = T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 TSTLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000371 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000372 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000373 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000374 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000375 return true;
376 }
377 return false;
378}
379
380bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
381 const char *&PrevSpec, unsigned &DiagID) {
382 if (TypeSpecType != TST_unspecified) {
383 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
384 DiagID = diag::err_invalid_vector_decl_spec_combination;
385 return true;
386 }
387 TypeAltiVecVector = isAltiVecVector;
388 AltiVecLoc = Loc;
389 return false;
390}
391
392bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
393 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000394 if (!TypeAltiVecVector || TypeAltiVecPixel ||
395 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000396 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
397 DiagID = diag::err_invalid_pixel_decl_spec_combination;
398 return true;
399 }
John Thompson82287d12010-02-05 00:12:22 +0000400 TypeAltiVecPixel = isAltiVecPixel;
401 TSTLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 return false;
403}
404
Douglas Gregorddc29e12009-02-06 22:42:48 +0000405bool DeclSpec::SetTypeSpecError() {
406 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000407 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000408 TSTLoc = SourceLocation();
409 return false;
410}
411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000413 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 // Duplicates turn into warnings pre-C99.
415 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000416 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 switch (T) {
420 default: assert(0 && "Unknown type qualifier!");
421 case TQ_const: TQ_constLoc = Loc; break;
422 case TQ_restrict: TQ_restrictLoc = Loc; break;
423 case TQ_volatile: TQ_volatileLoc = Loc; break;
424 }
425 return false;
426}
427
John McCallfec54012009-08-03 20:12:06 +0000428bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
429 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 // 'inline inline' is ok.
431 FS_inline_specified = true;
432 FS_inlineLoc = Loc;
433 return false;
434}
435
John McCallfec54012009-08-03 20:12:06 +0000436bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
437 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000438 // 'virtual virtual' is ok.
439 FS_virtual_specified = true;
440 FS_virtualLoc = Loc;
441 return false;
442}
443
John McCallfec54012009-08-03 20:12:06 +0000444bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
445 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000446 // 'explicit explicit' is ok.
447 FS_explicit_specified = true;
448 FS_explicitLoc = Loc;
449 return false;
450}
451
John McCallfec54012009-08-03 20:12:06 +0000452bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
453 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000454 if (Friend_specified) {
455 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000456 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000457 return true;
458 }
John McCallfec54012009-08-03 20:12:06 +0000459
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000460 Friend_specified = true;
461 FriendLoc = Loc;
462 return false;
463}
Reid Spencer5f016e22007-07-11 17:01:13 +0000464
Sebastian Redl2ac67232009-11-05 15:47:02 +0000465bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
466 unsigned &DiagID) {
467 // 'constexpr constexpr' is ok.
468 Constexpr_specified = true;
469 ConstexprLoc = Loc;
470 return false;
471}
472
John McCalld226f652010-08-21 09:40:31 +0000473void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000474 unsigned NP,
475 SourceLocation *ProtoLocs,
476 SourceLocation LAngleLoc) {
477 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000478 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000479 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000480 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000481 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
482 NumProtocolQualifiers = NP;
483 ProtocolLAngleLoc = LAngleLoc;
484}
485
Douglas Gregorddf889a2010-01-18 18:04:31 +0000486void DeclSpec::SaveWrittenBuiltinSpecs() {
487 writtenBS.Sign = getTypeSpecSign();
488 writtenBS.Width = getTypeSpecWidth();
489 writtenBS.Type = getTypeSpecType();
490 // Search the list of attributes for the presence of a mode attribute.
491 writtenBS.ModeAttr = false;
John McCall7f040a92010-12-24 02:08:15 +0000492 AttributeList* attrs = getAttributes().getList();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000493 while (attrs) {
494 if (attrs->getKind() == AttributeList::AT_mode) {
495 writtenBS.ModeAttr = true;
496 break;
497 }
498 attrs = attrs->getNext();
499 }
500}
501
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000502void DeclSpec::SaveStorageSpecifierAsWritten() {
503 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
504 // If 'extern' is part of a linkage specification,
505 // then it is not a storage class "as written".
506 StorageClassSpecAsWritten = SCS_unspecified;
507 else
508 StorageClassSpecAsWritten = StorageClassSpec;
509}
510
Reid Spencer5f016e22007-07-11 17:01:13 +0000511/// Finish - This does final analysis of the declspec, rejecting things like
512/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
513/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
514/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000515void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000516 // Before possibly changing their values, save specs as written.
517 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000518 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 // Check the type specifier components first.
521
Chris Lattner788b0fd2010-06-23 06:00:24 +0000522 // Validate and finalize AltiVec vector declspec.
523 if (TypeAltiVecVector) {
524 if (TypeAltiVecBool) {
525 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
526 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000527 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000528 << getSpecifierName((TSS)TypeSpecSign);
529 }
530
531 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000532 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
533 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000534 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000535 << (TypeAltiVecPixel ? "__pixel" :
536 getSpecifierName((TST)TypeSpecType));
537 }
538
539 // Only 'short' is valid with vector bool. (PIM 2.1)
540 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000541 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner788b0fd2010-06-23 06:00:24 +0000542 << getSpecifierName((TSW)TypeSpecWidth);
543
544 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
545 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
546 (TypeSpecWidth != TSW_unspecified))
547 TypeSpecSign = TSS_unsigned;
548 }
549
550 if (TypeAltiVecPixel) {
551 //TODO: perform validation
552 TypeSpecType = TST_int;
553 TypeSpecSign = TSS_unsigned;
554 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000555 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000556 }
557 }
558
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000559 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 if (TypeSpecSign != TSS_unspecified) {
561 if (TypeSpecType == TST_unspecified)
562 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000563 else if (TypeSpecType != TST_int &&
564 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000565 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000566 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 // signed double -> double.
568 TypeSpecSign = TSS_unspecified;
569 }
570 }
571
572 // Validate the width of the type.
573 switch (TypeSpecWidth) {
574 case TSW_unspecified: break;
575 case TSW_short: // short int
576 case TSW_longlong: // long long int
577 if (TypeSpecType == TST_unspecified)
578 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
579 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000580 Diag(D, TSWLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000582 : diag::err_invalid_longlong_spec)
583 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000585 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 }
587 break;
588 case TSW_long: // long double, long int
589 if (TypeSpecType == TST_unspecified)
590 TypeSpecType = TST_int; // long -> long int.
591 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000592 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000593 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000595 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 }
597 break;
598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 // TODO: if the implementation does not implement _Complex or _Imaginary,
601 // disallow their use. Need information about the backend.
602 if (TypeSpecComplex != TSC_unspecified) {
603 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000604 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000605 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000606 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
607 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 TypeSpecType = TST_double; // _Complex -> _Complex double.
609 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
610 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000611 Diag(D, TSTLoc, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000613 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner254be6a2008-11-22 08:32:36 +0000614 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 TypeSpecComplex = TSC_unspecified;
616 }
617 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000618
John McCall67d1a672009-08-06 02:15:43 +0000619 // C++ [class.friend]p6:
620 // No storage-class-specifier shall appear in the decl-specifier-seq
621 // of a friend declaration.
622 if (isFriendSpecified() && getStorageClassSpec()) {
623 DeclSpec::SCS SC = getStorageClassSpec();
624 const char *SpecName = getSpecifierName(SC);
625
626 SourceLocation SCLoc = getStorageClassSpecLoc();
627 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
628
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000629 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall67d1a672009-08-06 02:15:43 +0000630 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000631 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000632
633 ClearStorageClassSpecs();
634 }
635
John McCall9e46b8c2010-08-26 17:22:34 +0000636 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
637
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 // 'data definition has no type or storage class'?
643}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000644
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000645bool DeclSpec::isMissingDeclaratorOk() {
646 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000647 return isDeclRep(tst) && getRepAsDecl() != 0 &&
648 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000649}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000650
651void UnqualifiedId::clear() {
652 if (Kind == IK_TemplateId)
653 TemplateId->Destroy();
654
655 Kind = IK_Identifier;
656 Identifier = 0;
657 StartLocation = SourceLocation();
658 EndLocation = SourceLocation();
659}
660
661void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
662 OverloadedOperatorKind Op,
663 SourceLocation SymbolLocations[3]) {
664 Kind = IK_OperatorFunctionId;
665 StartLocation = OperatorLoc;
666 EndLocation = OperatorLoc;
667 OperatorFunctionId.Operator = Op;
668 for (unsigned I = 0; I != 3; ++I) {
669 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
670
671 if (SymbolLocations[I].isValid())
672 EndLocation = SymbolLocations[I];
673 }
674}
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000675
Anders Carlssoncc54d592011-01-22 16:56:46 +0000676bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000677 const char *&PrevSpec) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000678 if (Specifiers & VS) {
679 PrevSpec = getSpecifierName(VS);
680 return true;
681 }
682
683 Specifiers |= VS;
684
685 switch (VS) {
686 default: assert(0 && "Unknown specifier!");
687 case VS_Override: VS_overrideLoc = Loc; break;
688 case VS_Final: VS_finalLoc = Loc; break;
689 case VS_New: VS_newLoc = Loc; break;
690 }
Anders Carlsson46127a92011-01-22 15:58:16 +0000691
Anders Carlssonb971dbd2011-01-17 03:05:47 +0000692 return false;
693}
694
Anders Carlssoncc54d592011-01-22 16:56:46 +0000695const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssonc46bb7d2011-01-22 15:11:37 +0000696 switch (VS) {
697 default: assert(0 && "Unknown specifier");
698 case VS_Override: return "override";
699 case VS_Final: return "final";
700 case VS_New: return "new";
701 }
702}
Anders Carlsson46127a92011-01-22 15:58:16 +0000703
Anders Carlssoncc54d592011-01-22 16:56:46 +0000704bool ClassVirtSpecifiers::SetSpecifier(Specifier CVS, SourceLocation Loc,
Anders Carlsson46127a92011-01-22 15:58:16 +0000705 const char *&PrevSpec) {
706 if (Specifiers & CVS) {
707 PrevSpec = getSpecifierName(CVS);
708 return true;
709 }
710
711 Specifiers |= CVS;
712
713 switch (CVS) {
714 default: assert(0 && "Unknown specifier!");
715 case CVS_Final: CVS_finalLoc = Loc; break;
716 case CVS_Explicit: CVS_explicitLoc = Loc; break;
717 }
718
719 return false;
720}
721
Anders Carlssoncc54d592011-01-22 16:56:46 +0000722const char *ClassVirtSpecifiers::getSpecifierName(Specifier CVS) {
Anders Carlsson46127a92011-01-22 15:58:16 +0000723 switch (CVS) {
724 default: assert(0 && "Unknown specifier");
725 case CVS_Final: return "final";
726 case CVS_Explicit: return "explicit";
727 }
728}
729