blob: 979b76ae984e4d4b746ad1f5440a8f90a7f805d5 [file] [log] [blame]
Chris Lattner289ab7b2006-11-08 06:54:53 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner289ab7b2006-11-08 06:54:53 +000010// This file implements semantic analysis for declaration specifiers.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000011//
12//===----------------------------------------------------------------------===//
13
John McCall8b0666c2010-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 Gregore3e01a22009-04-01 22:41:11 +000017#include "clang/Lex/Preprocessor.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000018#include "clang/Basic/LangOptions.h"
Chris Lattner1ce41ed2009-01-20 19:11:22 +000019#include "llvm/ADT/STLExtras.h"
John McCall898cd0f2009-08-03 18:47:27 +000020#include "llvm/Support/ErrorHandling.h"
Douglas Gregor52537682009-03-19 00:18:19 +000021#include <cstring>
Chris Lattnerb9093cd2006-08-04 04:39:53 +000022using namespace clang;
23
Chris Lattner3b0f3ef2008-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 Gregorb53edfb2009-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 Gregor9de54ea2010-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 Lattner1ce41ed2009-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 Gregor94349fd2009-02-18 07:07:28 +000050 SourceLocation EllipsisLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000051 ParamInfo *ArgInfo,
52 unsigned NumArgs,
53 unsigned TypeQuals,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000054 bool hasExceptionSpec,
Sebastian Redlfb3f1792009-05-31 11:47:27 +000055 SourceLocation ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000056 bool hasAnyExceptionSpec,
John McCallba7bf592010-08-24 05:47:05 +000057 ParsedType *Exceptions,
Sebastian Redld6434562009-05-29 18:02:33 +000058 SourceRange *ExceptionRanges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000059 unsigned NumExceptions,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +000060 SourceLocation LPLoc,
61 SourceLocation RPLoc,
Douglas Gregor7fb25412010-10-01 18:44:50 +000062 Declarator &TheDeclarator,
63 ParsedType TrailingReturnType) {
Chris Lattner1ce41ed2009-01-20 19:11:22 +000064 DeclaratorChunk I;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000065 I.Kind = Function;
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +000066 I.Loc = LPLoc;
67 I.EndLoc = RPLoc;
Sebastian Redl2b9cacb2009-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 Redlfb3f1792009-05-31 11:47:27 +000076 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000077 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
78 I.Fun.NumExceptions = NumExceptions;
79 I.Fun.Exceptions = 0;
Douglas Gregor7fb25412010-10-01 18:44:50 +000080 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000081
Chris Lattner1ce41ed2009-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 Stump11289f42009-09-09 15:08:12 +000088 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner1ce41ed2009-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 Redl2b9cacb2009-04-29 17:30:04 +000099 // new[] an exception array if needed
100 if (NumExceptions) {
Sebastian Redld6434562009-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 Redl2b9cacb2009-04-29 17:30:04 +0000106 }
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000107 return I;
108}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000109
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000110/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +0000111/// declaration specifier includes.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000112///
113unsigned DeclSpec::getParsedSpecifiers() const {
114 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000115 if (StorageClassSpec != SCS_unspecified ||
116 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000117 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +0000118
Chris Lattner4d8f8732006-11-28 05:05:08 +0000119 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000120 Res |= PQ_TypeQualifier;
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattnerf055d432006-11-28 04:28:12 +0000122 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000123 Res |= PQ_TypeSpecifier;
Mike Stump11289f42009-09-09 15:08:12 +0000124
Douglas Gregor61956c42008-10-31 09:07:45 +0000125 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000126 Res |= PQ_FunctionSpecifier;
127 return Res;
128}
129
John McCall49bfce42009-08-03 20:12:06 +0000130template <class T> static bool BadSpecifier(T TNew, T TPrev,
131 const char *&PrevSpec,
132 unsigned &DiagID) {
John McCall898cd0f2009-08-03 18:47:27 +0000133 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCall49bfce42009-08-03 20:12:06 +0000134 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
135 : diag::err_invalid_decl_spec_combination);
John McCall898cd0f2009-08-03 18:47:27 +0000136 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000137}
John McCall898cd0f2009-08-03 18:47:27 +0000138
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000139const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000140 switch (S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +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 Friedmand5c0eed2009-04-19 20:27:55 +0000147 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000148 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000149 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000150 llvm_unreachable("Unknown typespec!");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000151}
152
John McCall898cd0f2009-08-03 18:47:27 +0000153const char *DeclSpec::getSpecifierName(TSW W) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000154 switch (W) {
John McCall898cd0f2009-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";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000159 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000160 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000161}
162
John McCall898cd0f2009-08-03 18:47:27 +0000163const char *DeclSpec::getSpecifierName(TSC C) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000164 switch (C) {
John McCall898cd0f2009-08-03 18:47:27 +0000165 case TSC_unspecified: return "unspecified";
166 case TSC_imaginary: return "imaginary";
167 case TSC_complex: return "complex";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000168 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000169 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000170}
171
172
John McCall898cd0f2009-08-03 18:47:27 +0000173const char *DeclSpec::getSpecifierName(TSS S) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000174 switch (S) {
John McCall898cd0f2009-08-03 18:47:27 +0000175 case TSS_unspecified: return "unspecified";
176 case TSS_signed: return "signed";
177 case TSS_unsigned: return "unsigned";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000178 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000179 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000180}
181
Chris Lattner69680ea2007-01-23 04:34:43 +0000182const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000183 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000184 case DeclSpec::TST_unspecified: return "unspecified";
185 case DeclSpec::TST_void: return "void";
186 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000187 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000188 case DeclSpec::TST_char16: return "char16_t";
189 case DeclSpec::TST_char32: return "char32_t";
Chris Lattner839713c2006-08-04 06:15:52 +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";
Chris Lattnerda72c822006-08-13 22:16:42 +0000197 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000198 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000199 case DeclSpec::TST_union: return "union";
200 case DeclSpec::TST_struct: return "struct";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000201 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000202 case DeclSpec::TST_typeofType:
203 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall898cd0f2009-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)";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000207 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000208 llvm_unreachable("Unknown typespec!");
Chris Lattner839713c2006-08-04 06:15:52 +0000209}
210
John McCall898cd0f2009-08-03 18:47:27 +0000211const char *DeclSpec::getSpecifierName(TQ T) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000212 switch (T) {
John McCall898cd0f2009-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";
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000217 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000218 llvm_unreachable("Unknown typespec!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000219}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000220
Chris Lattner4d8f8732006-11-28 05:05:08 +0000221bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000222 const char *&PrevSpec,
223 unsigned &DiagID) {
Abramo Bagnaraed5b6892010-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 }
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000233 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000234 StorageClassSpecLoc = Loc;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000235 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000236 return false;
237}
238
Mike Stump11289f42009-09-09 15:08:12 +0000239bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000240 const char *&PrevSpec,
241 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000242 if (SCS_thread_specified) {
243 PrevSpec = "__thread";
John McCall49bfce42009-08-03 20:12:06 +0000244 DiagID = diag::ext_duplicate_declspec;
Chris Lattner353f5742006-11-28 04:50:12 +0000245 return true;
246 }
247 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000248 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000249 return false;
250}
251
Chris Lattnerb9093cd2006-08-04 04:39:53 +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).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000255bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000256 const char *&PrevSpec,
257 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000258 if (TypeSpecWidth != TSW_unspecified &&
259 // Allow turning long -> long long.
260 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCall49bfce42009-08-03 20:12:06 +0000261 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000262 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000263 TSWLoc = Loc;
Chris Lattner37141f42010-06-23 06:00:24 +0000264 if (TypeAltiVecVector && !TypeAltiVecBool &&
265 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson22334602010-02-05 00:12:22 +0000266 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
267 DiagID = diag::warn_vector_long_decl_spec_combination;
268 return true;
269 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000270 return false;
271}
272
Mike Stump11289f42009-09-09 15:08:12 +0000273bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000274 const char *&PrevSpec,
275 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000276 if (TypeSpecComplex != TSC_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000277 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000278 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000279 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000280 return false;
281}
282
Mike Stump11289f42009-09-09 15:08:12 +0000283bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000284 const char *&PrevSpec,
285 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000286 if (TypeSpecSign != TSS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000287 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000288 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000289 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000290 return false;
291}
292
Chris Lattnerb20e8942006-11-28 05:30:29 +0000293bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000294 const char *&PrevSpec,
295 unsigned &DiagID,
John McCallba7bf592010-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 McCall49bfce42009-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 Lattner37141f42010-06-23 06:00:24 +0000358 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
359 TypeAltiVecBool = true;
360 TSTLoc = Loc;
361 return false;
362 }
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000363 TypeSpecType = T;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000364 TSTLoc = Loc;
John McCallba7bf592010-08-24 05:47:05 +0000365 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000366 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson22334602010-02-05 00:12:22 +0000367 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner37141f42010-06-23 06:00:24 +0000368 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson22334602010-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 Lattner37141f42010-06-23 06:00:24 +0000388 if (!TypeAltiVecVector || TypeAltiVecPixel ||
389 (TypeSpecType != TST_unspecified)) {
John Thompson22334602010-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 Thompson22334602010-02-05 00:12:22 +0000394 TypeAltiVecPixel = isAltiVecPixel;
395 TSTLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000396 return false;
397}
398
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000399bool DeclSpec::SetTypeSpecError() {
400 TypeSpecType = TST_error;
John McCalla3707cc2010-08-26 17:22:34 +0000401 TypeSpecOwned = false;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000402 TSTLoc = SourceLocation();
403 return false;
404}
405
Chris Lattner60809f52006-11-28 05:18:46 +0000406bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000407 unsigned &DiagID, const LangOptions &Lang) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000408 // Duplicates turn into warnings pre-C99.
409 if ((TypeQualifiers & T) && !Lang.C99)
John McCall49bfce42009-08-03 20:12:06 +0000410 return BadSpecifier(T, T, PrevSpec, DiagID);
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000411 TypeQualifiers |= T;
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattner60809f52006-11-28 05:18:46 +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 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000419 return false;
420}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000421
John McCall49bfce42009-08-03 20:12:06 +0000422bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
423 unsigned &DiagID) {
Chris Lattnera925dc62006-11-28 04:33:46 +0000424 // 'inline inline' is ok.
425 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000426 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000427 return false;
428}
429
John McCall49bfce42009-08-03 20:12:06 +0000430bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
431 unsigned &DiagID) {
Douglas Gregor61956c42008-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 McCall49bfce42009-08-03 20:12:06 +0000438bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
439 unsigned &DiagID) {
Douglas Gregor61956c42008-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 McCall49bfce42009-08-03 20:12:06 +0000446bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
447 unsigned &DiagID) {
Anders Carlssoncd8db412009-05-06 04:46:28 +0000448 if (Friend_specified) {
449 PrevSpec = "friend";
John McCall49bfce42009-08-03 20:12:06 +0000450 DiagID = diag::ext_duplicate_declspec;
Anders Carlssoncd8db412009-05-06 04:46:28 +0000451 return true;
452 }
John McCall49bfce42009-08-03 20:12:06 +0000453
Anders Carlssoncd8db412009-05-06 04:46:28 +0000454 Friend_specified = true;
455 FriendLoc = Loc;
456 return false;
457}
Chris Lattnera925dc62006-11-28 04:33:46 +0000458
Sebastian Redl39c2a8b2009-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 McCall48871652010-08-21 09:40:31 +0000467void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000468 unsigned NP,
469 SourceLocation *ProtoLocs,
470 SourceLocation LAngleLoc) {
471 if (NP == 0) return;
John McCall48871652010-08-21 09:40:31 +0000472 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000473 ProtocolLocs = new SourceLocation[NP];
John McCall48871652010-08-21 09:40:31 +0000474 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000475 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
476 NumProtocolQualifiers = NP;
477 ProtocolLAngleLoc = LAngleLoc;
478}
479
Douglas Gregorc9b7a592010-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 Bagnaraed5b6892010-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
Chris Lattnerb9093cd2006-08-04 04:39:53 +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 Gregore3e01a22009-04-01 22:41:11 +0000509void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000510 // Before possibly changing their values, save specs as written.
511 SaveWrittenBuiltinSpecs();
Douglas Gregorc4df4072010-04-19 22:54:31 +0000512 SaveStorageSpecifierAsWritten();
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000513
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000514 // Check the type specifier components first.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000515 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000516
Chris Lattner37141f42010-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 Sandsd3e231e2010-06-23 19:34:52 +0000527 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
528 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Chris Lattner37141f42010-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 McCalla3707cc2010-08-26 17:22:34 +0000550 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000551 }
552 }
553
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000554 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000555 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000556 if (TypeSpecType == TST_unspecified)
557 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000558 else if (TypeSpecType != TST_int &&
559 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000560 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
561 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000562 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000563 TypeSpecSign = TSS_unspecified;
564 }
565 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000566
Chris Lattner839713c2006-08-04 06:15:52 +0000567 // 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
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000572 if (TypeSpecType == TST_unspecified)
573 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
574 else if (TypeSpecType != TST_int) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000575 Diag(D, TSWLoc, SrcMgr,
Chris Lattner36982e42007-05-16 17:49:37 +0000576 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000577 : diag::err_invalid_longlong_spec)
578 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000579 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000580 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000581 }
582 break;
583 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000584 if (TypeSpecType == TST_unspecified)
585 TypeSpecType = TST_int; // long -> long int.
586 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000587 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
588 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000589 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000590 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000591 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000592 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000593 }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner0e894622006-08-13 19:58:17 +0000595 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000596 // disallow their use. Need information about the backend.
597 if (TypeSpecComplex != TSC_unspecified) {
598 if (TypeSpecType == TST_unspecified) {
Douglas Gregore3e01a22009-04-01 22:41:11 +0000599 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
Douglas Gregora771f462010-03-31 17:46:05 +0000600 << FixItHint::CreateInsertion(
Douglas Gregore3e01a22009-04-01 22:41:11 +0000601 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
602 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000603 TypeSpecType = TST_double; // _Complex -> _Complex double.
604 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000605 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000606 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000607 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000608 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
609 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000610 TypeSpecComplex = TSC_unspecified;
611 }
612 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000613
John McCall07e91c02009-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 Gregora771f462010-03-31 17:46:05 +0000626 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall07e91c02009-08-06 02:15:43 +0000627
628 ClearStorageClassSpecs();
629 }
630
John McCalla3707cc2010-08-26 17:22:34 +0000631 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
632
Chris Lattner8e90ef62006-08-05 03:30:45 +0000633 // Okay, now we can infer the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner0e894622006-08-13 19:58:17 +0000635 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000636
Chris Lattner1ae23292006-08-04 06:42:31 +0000637 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000638}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000639
Sebastian Redla2b5e312008-12-28 15:28:59 +0000640bool DeclSpec::isMissingDeclaratorOk() {
641 TST tst = getTypeSpecType();
John McCallba7bf592010-08-24 05:47:05 +0000642 return isDeclRep(tst) && getRepAsDecl() != 0 &&
643 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +0000644}
Douglas Gregor7861a802009-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}