blob: 979b76ae984e4d4b746ad1f5440a8f90a7f805d5 [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,
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,
John McCallb3d87482010-08-24 05:47:05 +000057 ParsedType *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,
Douglas Gregordab60ad2010-10-01 18:44:50 +000062 Declarator &TheDeclarator,
63 ParsedType TrailingReturnType) {
Chris Lattner5af2f352009-01-20 19:11:22 +000064 DeclaratorChunk I;
Sebastian Redl7dc81342009-04-29 17:30:04 +000065 I.Kind = Function;
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +000066 I.Loc = LPLoc;
67 I.EndLoc = RPLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +000068 I.Fun.hasPrototype = hasProto;
69 I.Fun.isVariadic = isVariadic;
70 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
71 I.Fun.DeleteArgInfo = false;
72 I.Fun.TypeQuals = TypeQuals;
73 I.Fun.NumArgs = NumArgs;
74 I.Fun.ArgInfo = 0;
75 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redl3cc97262009-05-31 11:47:27 +000076 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl7dc81342009-04-29 17:30:04 +000077 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
78 I.Fun.NumExceptions = NumExceptions;
79 I.Fun.Exceptions = 0;
Douglas Gregordab60ad2010-10-01 18:44:50 +000080 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl7dc81342009-04-29 17:30:04 +000081
Chris Lattner5af2f352009-01-20 19:11:22 +000082 // new[] an argument array if needed.
83 if (NumArgs) {
84 // If the 'InlineParams' in Declarator is unused and big enough, put our
85 // parameter list there (in an effort to avoid new/delete traffic). If it
86 // is already used (consider a function returning a function pointer) or too
87 // small (function taking too many arguments), go to the heap.
Mike Stump1eb44332009-09-09 15:08:12 +000088 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner5af2f352009-01-20 19:11:22 +000089 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
90 I.Fun.ArgInfo = TheDeclarator.InlineParams;
91 I.Fun.DeleteArgInfo = false;
92 TheDeclarator.InlineParamsUsed = true;
93 } else {
94 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
95 I.Fun.DeleteArgInfo = true;
96 }
97 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
98 }
Sebastian Redl7dc81342009-04-29 17:30:04 +000099 // new[] an exception array if needed
100 if (NumExceptions) {
Sebastian Redlef65f062009-05-29 18:02:33 +0000101 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
102 for (unsigned i = 0; i != NumExceptions; ++i) {
103 I.Fun.Exceptions[i].Ty = Exceptions[i];
104 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
105 }
Sebastian Redl7dc81342009-04-29 17:30:04 +0000106 }
Chris Lattner5af2f352009-01-20 19:11:22 +0000107 return I;
108}
Chris Lattner254be6a2008-11-22 08:32:36 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattner2a327d12009-02-27 18:35:46 +0000111/// declaration specifier includes.
Reid Spencer5f016e22007-07-11 17:01:13 +0000112///
113unsigned DeclSpec::getParsedSpecifiers() const {
114 unsigned Res = 0;
115 if (StorageClassSpec != SCS_unspecified ||
116 SCS_thread_specified)
117 Res |= PQ_StorageClassSpecifier;
Mike Stumpd4204332008-06-19 19:52:46 +0000118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 if (TypeQualifiers != TQ_unspecified)
120 Res |= PQ_TypeQualifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 if (hasTypeSpecifier())
123 Res |= PQ_TypeSpecifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Douglas Gregorb48fe382008-10-31 09:07:45 +0000125 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 Res |= PQ_FunctionSpecifier;
127 return Res;
128}
129
John McCallfec54012009-08-03 20:12:06 +0000130template <class T> static bool BadSpecifier(T TNew, T TPrev,
131 const char *&PrevSpec,
132 unsigned &DiagID) {
John McCall32d335e2009-08-03 18:47:27 +0000133 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCallfec54012009-08-03 20:12:06 +0000134 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
135 : diag::err_invalid_decl_spec_combination);
John McCall32d335e2009-08-03 18:47:27 +0000136 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000137}
John McCall32d335e2009-08-03 18:47:27 +0000138
Reid Spencer5f016e22007-07-11 17:01:13 +0000139const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
140 switch (S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 case DeclSpec::SCS_unspecified: return "unspecified";
142 case DeclSpec::SCS_typedef: return "typedef";
143 case DeclSpec::SCS_extern: return "extern";
144 case DeclSpec::SCS_static: return "static";
145 case DeclSpec::SCS_auto: return "auto";
146 case DeclSpec::SCS_register: return "register";
Eli Friedman63054b32009-04-19 20:27:55 +0000147 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redl669d5d72008-11-14 23:42:31 +0000148 case DeclSpec::SCS_mutable: return "mutable";
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000150 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000151}
152
John McCall32d335e2009-08-03 18:47:27 +0000153const char *DeclSpec::getSpecifierName(TSW W) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 switch (W) {
John McCall32d335e2009-08-03 18:47:27 +0000155 case TSW_unspecified: return "unspecified";
156 case TSW_short: return "short";
157 case TSW_long: return "long";
158 case TSW_longlong: return "long long";
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000160 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000161}
162
John McCall32d335e2009-08-03 18:47:27 +0000163const char *DeclSpec::getSpecifierName(TSC C) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 switch (C) {
John McCall32d335e2009-08-03 18:47:27 +0000165 case TSC_unspecified: return "unspecified";
166 case TSC_imaginary: return "imaginary";
167 case TSC_complex: return "complex";
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000169 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000170}
171
172
John McCall32d335e2009-08-03 18:47:27 +0000173const char *DeclSpec::getSpecifierName(TSS S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 switch (S) {
John McCall32d335e2009-08-03 18:47:27 +0000175 case TSS_unspecified: return "unspecified";
176 case TSS_signed: return "signed";
177 case TSS_unsigned: return "unsigned";
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000179 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
182const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
183 switch (T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 case DeclSpec::TST_unspecified: return "unspecified";
185 case DeclSpec::TST_void: return "void";
186 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000187 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000188 case DeclSpec::TST_char16: return "char16_t";
189 case DeclSpec::TST_char32: return "char32_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 case DeclSpec::TST_int: return "int";
191 case DeclSpec::TST_float: return "float";
192 case DeclSpec::TST_double: return "double";
193 case DeclSpec::TST_bool: return "_Bool";
194 case DeclSpec::TST_decimal32: return "_Decimal32";
195 case DeclSpec::TST_decimal64: return "_Decimal64";
196 case DeclSpec::TST_decimal128: return "_Decimal128";
197 case DeclSpec::TST_enum: return "enum";
Chris Lattner99dc9142008-04-13 18:59:07 +0000198 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 case DeclSpec::TST_union: return "union";
200 case DeclSpec::TST_struct: return "struct";
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000201 case DeclSpec::TST_typename: return "type-name";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000202 case DeclSpec::TST_typeofType:
203 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall32d335e2009-08-03 18:47:27 +0000204 case DeclSpec::TST_auto: return "auto";
205 case DeclSpec::TST_decltype: return "(decltype)";
206 case DeclSpec::TST_error: return "(error)";
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000208 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000209}
210
John McCall32d335e2009-08-03 18:47:27 +0000211const char *DeclSpec::getSpecifierName(TQ T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 switch (T) {
John McCall32d335e2009-08-03 18:47:27 +0000213 case DeclSpec::TQ_unspecified: return "unspecified";
214 case DeclSpec::TQ_const: return "const";
215 case DeclSpec::TQ_restrict: return "restrict";
216 case DeclSpec::TQ_volatile: return "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000218 llvm_unreachable("Unknown typespec!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000219}
220
221bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000222 const char *&PrevSpec,
223 unsigned &DiagID) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000224 if (StorageClassSpec != SCS_unspecified) {
225 // Changing storage class is allowed only if the previous one
226 // was the 'extern' that is part of a linkage specification and
227 // the new storage class is 'typedef'.
228 if (!(SCS_extern_in_linkage_spec &&
229 StorageClassSpec == SCS_extern &&
230 S == SCS_typedef))
231 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
232 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 StorageClassSpec = S;
234 StorageClassSpecLoc = Loc;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000235 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 return false;
237}
238
Mike Stump1eb44332009-09-09 15:08:12 +0000239bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000240 const char *&PrevSpec,
241 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 if (SCS_thread_specified) {
243 PrevSpec = "__thread";
John McCallfec54012009-08-03 20:12:06 +0000244 DiagID = diag::ext_duplicate_declspec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 return true;
246 }
247 SCS_thread_specified = true;
248 SCS_threadLoc = Loc;
249 return false;
250}
251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252/// These methods set the specified attribute of the DeclSpec, but return true
253/// and ignore the request if invalid (e.g. "extern" then "auto" is
254/// specified).
255bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000256 const char *&PrevSpec,
257 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 if (TypeSpecWidth != TSW_unspecified &&
259 // Allow turning long -> long long.
260 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCallfec54012009-08-03 20:12:06 +0000261 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 TypeSpecWidth = W;
263 TSWLoc = Loc;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000264 if (TypeAltiVecVector && !TypeAltiVecBool &&
265 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson82287d12010-02-05 00:12:22 +0000266 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
267 DiagID = diag::warn_vector_long_decl_spec_combination;
268 return true;
269 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 return false;
271}
272
Mike Stump1eb44332009-09-09 15:08:12 +0000273bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000274 const char *&PrevSpec,
275 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 if (TypeSpecComplex != TSC_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000277 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 TypeSpecComplex = C;
279 TSCLoc = Loc;
280 return false;
281}
282
Mike Stump1eb44332009-09-09 15:08:12 +0000283bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000284 const char *&PrevSpec,
285 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 if (TypeSpecSign != TSS_unspecified)
John McCallfec54012009-08-03 20:12:06 +0000287 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 TypeSpecSign = S;
289 TSSLoc = Loc;
290 return false;
291}
292
293bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCallfec54012009-08-03 20:12:06 +0000294 const char *&PrevSpec,
295 unsigned &DiagID,
John McCallb3d87482010-08-24 05:47:05 +0000296 ParsedType Rep) {
297 assert(isTypeRep(T) && "T does not store a type");
298 assert(Rep && "no type provided!");
299 if (TypeSpecType != TST_unspecified) {
300 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
301 DiagID = diag::err_invalid_decl_spec_combination;
302 return true;
303 }
304 TypeSpecType = T;
305 TypeRep = Rep;
306 TSTLoc = Loc;
307 TypeSpecOwned = false;
308 return false;
309}
310
311bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
312 const char *&PrevSpec,
313 unsigned &DiagID,
314 Expr *Rep) {
315 assert(isExprRep(T) && "T does not store an expr");
316 assert(Rep && "no expression provided!");
317 if (TypeSpecType != TST_unspecified) {
318 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
319 DiagID = diag::err_invalid_decl_spec_combination;
320 return true;
321 }
322 TypeSpecType = T;
323 ExprRep = Rep;
324 TSTLoc = Loc;
325 TypeSpecOwned = false;
326 return false;
327}
328
329bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
330 const char *&PrevSpec,
331 unsigned &DiagID,
332 Decl *Rep, bool Owned) {
333 assert(isDeclRep(T) && "T does not store a decl");
334 // Unlike the other cases, we don't assert that we actually get a decl.
335
336 if (TypeSpecType != TST_unspecified) {
337 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
338 DiagID = diag::err_invalid_decl_spec_combination;
339 return true;
340 }
341 TypeSpecType = T;
342 DeclRep = Rep;
343 TSTLoc = Loc;
344 TypeSpecOwned = Owned;
345 return false;
346}
347
348bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
349 const char *&PrevSpec,
350 unsigned &DiagID) {
351 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
352 "rep required for these type-spec kinds!");
John McCallfec54012009-08-03 20:12:06 +0000353 if (TypeSpecType != TST_unspecified) {
354 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
355 DiagID = diag::err_invalid_decl_spec_combination;
356 return true;
357 }
Chris Lattner788b0fd2010-06-23 06:00:24 +0000358 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
359 TypeAltiVecBool = true;
360 TSTLoc = Loc;
361 return false;
362 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 TypeSpecType = T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 TSTLoc = Loc;
John McCallb3d87482010-08-24 05:47:05 +0000365 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000366 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson82287d12010-02-05 00:12:22 +0000367 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner788b0fd2010-06-23 06:00:24 +0000368 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson82287d12010-02-05 00:12:22 +0000369 return true;
370 }
371 return false;
372}
373
374bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
375 const char *&PrevSpec, unsigned &DiagID) {
376 if (TypeSpecType != TST_unspecified) {
377 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
378 DiagID = diag::err_invalid_vector_decl_spec_combination;
379 return true;
380 }
381 TypeAltiVecVector = isAltiVecVector;
382 AltiVecLoc = Loc;
383 return false;
384}
385
386bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
387 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000388 if (!TypeAltiVecVector || TypeAltiVecPixel ||
389 (TypeSpecType != TST_unspecified)) {
John Thompson82287d12010-02-05 00:12:22 +0000390 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
391 DiagID = diag::err_invalid_pixel_decl_spec_combination;
392 return true;
393 }
John Thompson82287d12010-02-05 00:12:22 +0000394 TypeAltiVecPixel = isAltiVecPixel;
395 TSTLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 return false;
397}
398
Douglas Gregorddc29e12009-02-06 22:42:48 +0000399bool DeclSpec::SetTypeSpecError() {
400 TypeSpecType = TST_error;
John McCall9e46b8c2010-08-26 17:22:34 +0000401 TypeSpecOwned = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000402 TSTLoc = SourceLocation();
403 return false;
404}
405
Reid Spencer5f016e22007-07-11 17:01:13 +0000406bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000407 unsigned &DiagID, const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 // Duplicates turn into warnings pre-C99.
409 if ((TypeQualifiers & T) && !Lang.C99)
John McCallfec54012009-08-03 20:12:06 +0000410 return BadSpecifier(T, T, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 TypeQualifiers |= T;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 switch (T) {
414 default: assert(0 && "Unknown type qualifier!");
415 case TQ_const: TQ_constLoc = Loc; break;
416 case TQ_restrict: TQ_restrictLoc = Loc; break;
417 case TQ_volatile: TQ_volatileLoc = Loc; break;
418 }
419 return false;
420}
421
John McCallfec54012009-08-03 20:12:06 +0000422bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
423 unsigned &DiagID) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 // 'inline inline' is ok.
425 FS_inline_specified = true;
426 FS_inlineLoc = Loc;
427 return false;
428}
429
John McCallfec54012009-08-03 20:12:06 +0000430bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
431 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000432 // 'virtual virtual' is ok.
433 FS_virtual_specified = true;
434 FS_virtualLoc = Loc;
435 return false;
436}
437
John McCallfec54012009-08-03 20:12:06 +0000438bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
439 unsigned &DiagID) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000440 // 'explicit explicit' is ok.
441 FS_explicit_specified = true;
442 FS_explicitLoc = Loc;
443 return false;
444}
445
John McCallfec54012009-08-03 20:12:06 +0000446bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
447 unsigned &DiagID) {
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000448 if (Friend_specified) {
449 PrevSpec = "friend";
John McCallfec54012009-08-03 20:12:06 +0000450 DiagID = diag::ext_duplicate_declspec;
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000451 return true;
452 }
John McCallfec54012009-08-03 20:12:06 +0000453
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000454 Friend_specified = true;
455 FriendLoc = Loc;
456 return false;
457}
Reid Spencer5f016e22007-07-11 17:01:13 +0000458
Sebastian Redl2ac67232009-11-05 15:47:02 +0000459bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
460 unsigned &DiagID) {
461 // 'constexpr constexpr' is ok.
462 Constexpr_specified = true;
463 ConstexprLoc = Loc;
464 return false;
465}
466
John McCalld226f652010-08-21 09:40:31 +0000467void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000468 unsigned NP,
469 SourceLocation *ProtoLocs,
470 SourceLocation LAngleLoc) {
471 if (NP == 0) return;
John McCalld226f652010-08-21 09:40:31 +0000472 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000473 ProtocolLocs = new SourceLocation[NP];
John McCalld226f652010-08-21 09:40:31 +0000474 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidise3a535b2009-09-29 19:42:11 +0000475 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
476 NumProtocolQualifiers = NP;
477 ProtocolLAngleLoc = LAngleLoc;
478}
479
Douglas Gregorddf889a2010-01-18 18:04:31 +0000480void DeclSpec::SaveWrittenBuiltinSpecs() {
481 writtenBS.Sign = getTypeSpecSign();
482 writtenBS.Width = getTypeSpecWidth();
483 writtenBS.Type = getTypeSpecType();
484 // Search the list of attributes for the presence of a mode attribute.
485 writtenBS.ModeAttr = false;
486 AttributeList* attrs = getAttributes();
487 while (attrs) {
488 if (attrs->getKind() == AttributeList::AT_mode) {
489 writtenBS.ModeAttr = true;
490 break;
491 }
492 attrs = attrs->getNext();
493 }
494}
495
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000496void DeclSpec::SaveStorageSpecifierAsWritten() {
497 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
498 // If 'extern' is part of a linkage specification,
499 // then it is not a storage class "as written".
500 StorageClassSpecAsWritten = SCS_unspecified;
501 else
502 StorageClassSpecAsWritten = StorageClassSpec;
503}
504
Reid Spencer5f016e22007-07-11 17:01:13 +0000505/// Finish - This does final analysis of the declspec, rejecting things like
506/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
507/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
508/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000509void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000510 // Before possibly changing their values, save specs as written.
511 SaveWrittenBuiltinSpecs();
Douglas Gregor16573fa2010-04-19 22:54:31 +0000512 SaveStorageSpecifierAsWritten();
Douglas Gregorddf889a2010-01-18 18:04:31 +0000513
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 // Check the type specifier components first.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000515 SourceManager &SrcMgr = PP.getSourceManager();
Reid Spencer5f016e22007-07-11 17:01:13 +0000516
Chris Lattner788b0fd2010-06-23 06:00:24 +0000517 // Validate and finalize AltiVec vector declspec.
518 if (TypeAltiVecVector) {
519 if (TypeAltiVecBool) {
520 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
521 if (TypeSpecSign != TSS_unspecified) {
522 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_vector_bool_decl_spec)
523 << getSpecifierName((TSS)TypeSpecSign);
524 }
525
526 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sands2e964a922010-06-23 19:34:52 +0000527 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
528 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Chris Lattner788b0fd2010-06-23 06:00:24 +0000529 Diag(D, TSTLoc, SrcMgr, diag::err_invalid_vector_bool_decl_spec)
530 << (TypeAltiVecPixel ? "__pixel" :
531 getSpecifierName((TST)TypeSpecType));
532 }
533
534 // Only 'short' is valid with vector bool. (PIM 2.1)
535 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
536 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_vector_bool_decl_spec)
537 << getSpecifierName((TSW)TypeSpecWidth);
538
539 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
540 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
541 (TypeSpecWidth != TSW_unspecified))
542 TypeSpecSign = TSS_unsigned;
543 }
544
545 if (TypeAltiVecPixel) {
546 //TODO: perform validation
547 TypeSpecType = TST_int;
548 TypeSpecSign = TSS_unsigned;
549 TypeSpecWidth = TSW_short;
John McCall9e46b8c2010-08-26 17:22:34 +0000550 TypeSpecOwned = false;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000551 }
552 }
553
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000554 // signed/unsigned are only valid with int/char/wchar_t.
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 if (TypeSpecSign != TSS_unspecified) {
556 if (TypeSpecType == TST_unspecified)
557 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000558 else if (TypeSpecType != TST_int &&
559 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000560 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
561 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 // signed double -> double.
563 TypeSpecSign = TSS_unspecified;
564 }
565 }
566
567 // Validate the width of the type.
568 switch (TypeSpecWidth) {
569 case TSW_unspecified: break;
570 case TSW_short: // short int
571 case TSW_longlong: // long long int
572 if (TypeSpecType == TST_unspecified)
573 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
574 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000575 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner254be6a2008-11-22 08:32:36 +0000577 : diag::err_invalid_longlong_spec)
578 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000580 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 }
582 break;
583 case TSW_long: // long double, long int
584 if (TypeSpecType == TST_unspecified)
585 TypeSpecType = TST_int; // long -> long int.
586 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000587 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
588 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 TypeSpecType = TST_int;
John McCall9e46b8c2010-08-26 17:22:34 +0000590 TypeSpecOwned = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 }
592 break;
593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 // TODO: if the implementation does not implement _Complex or _Imaginary,
596 // disallow their use. Need information about the backend.
597 if (TypeSpecComplex != TSC_unspecified) {
598 if (TypeSpecType == TST_unspecified) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000599 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
Douglas Gregor849b2432010-03-31 17:46:05 +0000600 << FixItHint::CreateInsertion(
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000601 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
602 " double");
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 TypeSpecType = TST_double; // _Complex -> _Complex double.
604 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
605 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000606 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner254be6a2008-11-22 08:32:36 +0000608 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
609 << getSpecifierName((TST)TypeSpecType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 TypeSpecComplex = TSC_unspecified;
611 }
612 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000613
John McCall67d1a672009-08-06 02:15:43 +0000614 // C++ [class.friend]p6:
615 // No storage-class-specifier shall appear in the decl-specifier-seq
616 // of a friend declaration.
617 if (isFriendSpecified() && getStorageClassSpec()) {
618 DeclSpec::SCS SC = getStorageClassSpec();
619 const char *SpecName = getSpecifierName(SC);
620
621 SourceLocation SCLoc = getStorageClassSpecLoc();
622 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
623
624 Diag(D, SCLoc, SrcMgr, diag::err_friend_storage_spec)
625 << SpecName
Douglas Gregor849b2432010-03-31 17:46:05 +0000626 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall67d1a672009-08-06 02:15:43 +0000627
628 ClearStorageClassSpecs();
629 }
630
John McCall9e46b8c2010-08-26 17:22:34 +0000631 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
632
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 // Okay, now we can infer the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 // 'data definition has no type or storage class'?
638}
Daniel Dunbare4858a62008-08-11 03:45:03 +0000639
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000640bool DeclSpec::isMissingDeclaratorOk() {
641 TST tst = getTypeSpecType();
John McCallb3d87482010-08-24 05:47:05 +0000642 return isDeclRep(tst) && getRepAsDecl() != 0 &&
643 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla4ed0d82008-12-28 15:28:59 +0000644}
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000645
646void UnqualifiedId::clear() {
647 if (Kind == IK_TemplateId)
648 TemplateId->Destroy();
649
650 Kind = IK_Identifier;
651 Identifier = 0;
652 StartLocation = SourceLocation();
653 EndLocation = SourceLocation();
654}
655
656void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
657 OverloadedOperatorKind Op,
658 SourceLocation SymbolLocations[3]) {
659 Kind = IK_OperatorFunctionId;
660 StartLocation = OperatorLoc;
661 EndLocation = OperatorLoc;
662 OperatorFunctionId.Operator = Op;
663 for (unsigned I = 0; I != 3; ++I) {
664 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
665
666 if (SymbolLocations[I].isValid())
667 EndLocation = SymbolLocations[I];
668 }
669}