blob: b46e8af9db868095111773cbce877241809a70fa [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,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000062 Declarator &TheDeclarator) {
63 DeclaratorChunk I;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000064 I.Kind = Function;
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +000065 I.Loc = LPLoc;
66 I.EndLoc = RPLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000067 I.Fun.hasPrototype = hasProto;
68 I.Fun.isVariadic = isVariadic;
69 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
70 I.Fun.DeleteArgInfo = false;
71 I.Fun.TypeQuals = TypeQuals;
72 I.Fun.NumArgs = NumArgs;
73 I.Fun.ArgInfo = 0;
74 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redlfb3f1792009-05-31 11:47:27 +000075 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000076 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
77 I.Fun.NumExceptions = NumExceptions;
78 I.Fun.Exceptions = 0;
79
Chris Lattner1ce41ed2009-01-20 19:11:22 +000080 // new[] an argument array if needed.
81 if (NumArgs) {
82 // If the 'InlineParams' in Declarator is unused and big enough, put our
83 // parameter list there (in an effort to avoid new/delete traffic). If it
84 // is already used (consider a function returning a function pointer) or too
85 // small (function taking too many arguments), go to the heap.
Mike Stump11289f42009-09-09 15:08:12 +000086 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner1ce41ed2009-01-20 19:11:22 +000087 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
88 I.Fun.ArgInfo = TheDeclarator.InlineParams;
89 I.Fun.DeleteArgInfo = false;
90 TheDeclarator.InlineParamsUsed = true;
91 } else {
92 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
93 I.Fun.DeleteArgInfo = true;
94 }
95 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
96 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000097 // new[] an exception array if needed
98 if (NumExceptions) {
Sebastian Redld6434562009-05-29 18:02:33 +000099 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
100 for (unsigned i = 0; i != NumExceptions; ++i) {
101 I.Fun.Exceptions[i].Ty = Exceptions[i];
102 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
103 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000104 }
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000105 return I;
106}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000107
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000108/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +0000109/// declaration specifier includes.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000110///
111unsigned DeclSpec::getParsedSpecifiers() const {
112 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000113 if (StorageClassSpec != SCS_unspecified ||
114 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000115 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +0000116
Chris Lattner4d8f8732006-11-28 05:05:08 +0000117 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000118 Res |= PQ_TypeQualifier;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattnerf055d432006-11-28 04:28:12 +0000120 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000121 Res |= PQ_TypeSpecifier;
Mike Stump11289f42009-09-09 15:08:12 +0000122
Douglas Gregor61956c42008-10-31 09:07:45 +0000123 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000124 Res |= PQ_FunctionSpecifier;
125 return Res;
126}
127
John McCall49bfce42009-08-03 20:12:06 +0000128template <class T> static bool BadSpecifier(T TNew, T TPrev,
129 const char *&PrevSpec,
130 unsigned &DiagID) {
John McCall898cd0f2009-08-03 18:47:27 +0000131 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCall49bfce42009-08-03 20:12:06 +0000132 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
133 : diag::err_invalid_decl_spec_combination);
John McCall898cd0f2009-08-03 18:47:27 +0000134 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000135}
John McCall898cd0f2009-08-03 18:47:27 +0000136
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000137const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000138 switch (S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000139 case DeclSpec::SCS_unspecified: return "unspecified";
140 case DeclSpec::SCS_typedef: return "typedef";
141 case DeclSpec::SCS_extern: return "extern";
142 case DeclSpec::SCS_static: return "static";
143 case DeclSpec::SCS_auto: return "auto";
144 case DeclSpec::SCS_register: return "register";
Eli Friedmand5c0eed2009-04-19 20:27:55 +0000145 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000146 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000147 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000148 llvm_unreachable("Unknown typespec!");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000149}
150
John McCall898cd0f2009-08-03 18:47:27 +0000151const char *DeclSpec::getSpecifierName(TSW W) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000152 switch (W) {
John McCall898cd0f2009-08-03 18:47:27 +0000153 case TSW_unspecified: return "unspecified";
154 case TSW_short: return "short";
155 case TSW_long: return "long";
156 case TSW_longlong: return "long long";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000157 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000158 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000159}
160
John McCall898cd0f2009-08-03 18:47:27 +0000161const char *DeclSpec::getSpecifierName(TSC C) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000162 switch (C) {
John McCall898cd0f2009-08-03 18:47:27 +0000163 case TSC_unspecified: return "unspecified";
164 case TSC_imaginary: return "imaginary";
165 case TSC_complex: return "complex";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000166 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000167 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000168}
169
170
John McCall898cd0f2009-08-03 18:47:27 +0000171const char *DeclSpec::getSpecifierName(TSS S) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000172 switch (S) {
John McCall898cd0f2009-08-03 18:47:27 +0000173 case TSS_unspecified: return "unspecified";
174 case TSS_signed: return "signed";
175 case TSS_unsigned: return "unsigned";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000176 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000177 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000178}
179
Chris Lattner69680ea2007-01-23 04:34:43 +0000180const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000181 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000182 case DeclSpec::TST_unspecified: return "unspecified";
183 case DeclSpec::TST_void: return "void";
184 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000185 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000186 case DeclSpec::TST_char16: return "char16_t";
187 case DeclSpec::TST_char32: return "char32_t";
Chris Lattner839713c2006-08-04 06:15:52 +0000188 case DeclSpec::TST_int: return "int";
189 case DeclSpec::TST_float: return "float";
190 case DeclSpec::TST_double: return "double";
191 case DeclSpec::TST_bool: return "_Bool";
192 case DeclSpec::TST_decimal32: return "_Decimal32";
193 case DeclSpec::TST_decimal64: return "_Decimal64";
194 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +0000195 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000196 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000197 case DeclSpec::TST_union: return "union";
198 case DeclSpec::TST_struct: return "struct";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000199 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000200 case DeclSpec::TST_typeofType:
201 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall898cd0f2009-08-03 18:47:27 +0000202 case DeclSpec::TST_auto: return "auto";
203 case DeclSpec::TST_decltype: return "(decltype)";
204 case DeclSpec::TST_error: return "(error)";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000205 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000206 llvm_unreachable("Unknown typespec!");
Chris Lattner839713c2006-08-04 06:15:52 +0000207}
208
John McCall898cd0f2009-08-03 18:47:27 +0000209const char *DeclSpec::getSpecifierName(TQ T) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000210 switch (T) {
John McCall898cd0f2009-08-03 18:47:27 +0000211 case DeclSpec::TQ_unspecified: return "unspecified";
212 case DeclSpec::TQ_const: return "const";
213 case DeclSpec::TQ_restrict: return "restrict";
214 case DeclSpec::TQ_volatile: return "volatile";
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000215 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000216 llvm_unreachable("Unknown typespec!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000217}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000218
Chris Lattner4d8f8732006-11-28 05:05:08 +0000219bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000220 const char *&PrevSpec,
221 unsigned &DiagID) {
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000222 if (StorageClassSpec != SCS_unspecified) {
223 // Changing storage class is allowed only if the previous one
224 // was the 'extern' that is part of a linkage specification and
225 // the new storage class is 'typedef'.
226 if (!(SCS_extern_in_linkage_spec &&
227 StorageClassSpec == SCS_extern &&
228 S == SCS_typedef))
229 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
230 }
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000231 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000232 StorageClassSpecLoc = Loc;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000233 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000234 return false;
235}
236
Mike Stump11289f42009-09-09 15:08:12 +0000237bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000238 const char *&PrevSpec,
239 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000240 if (SCS_thread_specified) {
241 PrevSpec = "__thread";
John McCall49bfce42009-08-03 20:12:06 +0000242 DiagID = diag::ext_duplicate_declspec;
Chris Lattner353f5742006-11-28 04:50:12 +0000243 return true;
244 }
245 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000246 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000247 return false;
248}
249
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000250/// These methods set the specified attribute of the DeclSpec, but return true
251/// and ignore the request if invalid (e.g. "extern" then "auto" is
252/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000253bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000254 const char *&PrevSpec,
255 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000256 if (TypeSpecWidth != TSW_unspecified &&
257 // Allow turning long -> long long.
258 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCall49bfce42009-08-03 20:12:06 +0000259 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000260 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000261 TSWLoc = Loc;
Chris Lattner37141f42010-06-23 06:00:24 +0000262 if (TypeAltiVecVector && !TypeAltiVecBool &&
263 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson22334602010-02-05 00:12:22 +0000264 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
265 DiagID = diag::warn_vector_long_decl_spec_combination;
266 return true;
267 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000268 return false;
269}
270
Mike Stump11289f42009-09-09 15:08:12 +0000271bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000272 const char *&PrevSpec,
273 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000274 if (TypeSpecComplex != TSC_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000275 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000276 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000277 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000278 return false;
279}
280
Mike Stump11289f42009-09-09 15:08:12 +0000281bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000282 const char *&PrevSpec,
283 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000284 if (TypeSpecSign != TSS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000285 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000286 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000287 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000288 return false;
289}
290
Chris Lattnerb20e8942006-11-28 05:30:29 +0000291bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000292 const char *&PrevSpec,
293 unsigned &DiagID,
John McCallba7bf592010-08-24 05:47:05 +0000294 ParsedType Rep) {
295 assert(isTypeRep(T) && "T does not store a type");
296 assert(Rep && "no type provided!");
297 if (TypeSpecType != TST_unspecified) {
298 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
299 DiagID = diag::err_invalid_decl_spec_combination;
300 return true;
301 }
302 TypeSpecType = T;
303 TypeRep = Rep;
304 TSTLoc = Loc;
305 TypeSpecOwned = false;
306 return false;
307}
308
309bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
310 const char *&PrevSpec,
311 unsigned &DiagID,
312 Expr *Rep) {
313 assert(isExprRep(T) && "T does not store an expr");
314 assert(Rep && "no expression provided!");
315 if (TypeSpecType != TST_unspecified) {
316 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
317 DiagID = diag::err_invalid_decl_spec_combination;
318 return true;
319 }
320 TypeSpecType = T;
321 ExprRep = Rep;
322 TSTLoc = Loc;
323 TypeSpecOwned = false;
324 return false;
325}
326
327bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
328 const char *&PrevSpec,
329 unsigned &DiagID,
330 Decl *Rep, bool Owned) {
331 assert(isDeclRep(T) && "T does not store a decl");
332 // Unlike the other cases, we don't assert that we actually get a decl.
333
334 if (TypeSpecType != TST_unspecified) {
335 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
336 DiagID = diag::err_invalid_decl_spec_combination;
337 return true;
338 }
339 TypeSpecType = T;
340 DeclRep = Rep;
341 TSTLoc = Loc;
342 TypeSpecOwned = Owned;
343 return false;
344}
345
346bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
347 const char *&PrevSpec,
348 unsigned &DiagID) {
349 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
350 "rep required for these type-spec kinds!");
John McCall49bfce42009-08-03 20:12:06 +0000351 if (TypeSpecType != TST_unspecified) {
352 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
353 DiagID = diag::err_invalid_decl_spec_combination;
354 return true;
355 }
Chris Lattner37141f42010-06-23 06:00:24 +0000356 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
357 TypeAltiVecBool = true;
358 TSTLoc = Loc;
359 return false;
360 }
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000361 TypeSpecType = T;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000362 TSTLoc = Loc;
John McCallba7bf592010-08-24 05:47:05 +0000363 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000364 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson22334602010-02-05 00:12:22 +0000365 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner37141f42010-06-23 06:00:24 +0000366 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson22334602010-02-05 00:12:22 +0000367 return true;
368 }
369 return false;
370}
371
372bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
373 const char *&PrevSpec, unsigned &DiagID) {
374 if (TypeSpecType != TST_unspecified) {
375 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
376 DiagID = diag::err_invalid_vector_decl_spec_combination;
377 return true;
378 }
379 TypeAltiVecVector = isAltiVecVector;
380 AltiVecLoc = Loc;
381 return false;
382}
383
384bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
385 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner37141f42010-06-23 06:00:24 +0000386 if (!TypeAltiVecVector || TypeAltiVecPixel ||
387 (TypeSpecType != TST_unspecified)) {
John Thompson22334602010-02-05 00:12:22 +0000388 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
389 DiagID = diag::err_invalid_pixel_decl_spec_combination;
390 return true;
391 }
John Thompson22334602010-02-05 00:12:22 +0000392 TypeAltiVecPixel = isAltiVecPixel;
393 TSTLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000394 return false;
395}
396
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000397bool DeclSpec::SetTypeSpecError() {
398 TypeSpecType = TST_error;
John McCalla3707cc2010-08-26 17:22:34 +0000399 TypeSpecOwned = false;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000400 TSTLoc = SourceLocation();
401 return false;
402}
403
Chris Lattner60809f52006-11-28 05:18:46 +0000404bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000405 unsigned &DiagID, const LangOptions &Lang) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000406 // Duplicates turn into warnings pre-C99.
407 if ((TypeQualifiers & T) && !Lang.C99)
John McCall49bfce42009-08-03 20:12:06 +0000408 return BadSpecifier(T, T, PrevSpec, DiagID);
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000409 TypeQualifiers |= T;
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattner60809f52006-11-28 05:18:46 +0000411 switch (T) {
412 default: assert(0 && "Unknown type qualifier!");
413 case TQ_const: TQ_constLoc = Loc; break;
414 case TQ_restrict: TQ_restrictLoc = Loc; break;
415 case TQ_volatile: TQ_volatileLoc = Loc; break;
416 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000417 return false;
418}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000419
John McCall49bfce42009-08-03 20:12:06 +0000420bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
421 unsigned &DiagID) {
Chris Lattnera925dc62006-11-28 04:33:46 +0000422 // 'inline inline' is ok.
423 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000424 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000425 return false;
426}
427
John McCall49bfce42009-08-03 20:12:06 +0000428bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
429 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000430 // 'virtual virtual' is ok.
431 FS_virtual_specified = true;
432 FS_virtualLoc = Loc;
433 return false;
434}
435
John McCall49bfce42009-08-03 20:12:06 +0000436bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
437 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000438 // 'explicit explicit' is ok.
439 FS_explicit_specified = true;
440 FS_explicitLoc = Loc;
441 return false;
442}
443
John McCall49bfce42009-08-03 20:12:06 +0000444bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
445 unsigned &DiagID) {
Anders Carlssoncd8db412009-05-06 04:46:28 +0000446 if (Friend_specified) {
447 PrevSpec = "friend";
John McCall49bfce42009-08-03 20:12:06 +0000448 DiagID = diag::ext_duplicate_declspec;
Anders Carlssoncd8db412009-05-06 04:46:28 +0000449 return true;
450 }
John McCall49bfce42009-08-03 20:12:06 +0000451
Anders Carlssoncd8db412009-05-06 04:46:28 +0000452 Friend_specified = true;
453 FriendLoc = Loc;
454 return false;
455}
Chris Lattnera925dc62006-11-28 04:33:46 +0000456
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000457bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
458 unsigned &DiagID) {
459 // 'constexpr constexpr' is ok.
460 Constexpr_specified = true;
461 ConstexprLoc = Loc;
462 return false;
463}
464
John McCall48871652010-08-21 09:40:31 +0000465void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000466 unsigned NP,
467 SourceLocation *ProtoLocs,
468 SourceLocation LAngleLoc) {
469 if (NP == 0) return;
John McCall48871652010-08-21 09:40:31 +0000470 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000471 ProtocolLocs = new SourceLocation[NP];
John McCall48871652010-08-21 09:40:31 +0000472 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000473 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
474 NumProtocolQualifiers = NP;
475 ProtocolLAngleLoc = LAngleLoc;
476}
477
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000478void DeclSpec::SaveWrittenBuiltinSpecs() {
479 writtenBS.Sign = getTypeSpecSign();
480 writtenBS.Width = getTypeSpecWidth();
481 writtenBS.Type = getTypeSpecType();
482 // Search the list of attributes for the presence of a mode attribute.
483 writtenBS.ModeAttr = false;
484 AttributeList* attrs = getAttributes();
485 while (attrs) {
486 if (attrs->getKind() == AttributeList::AT_mode) {
487 writtenBS.ModeAttr = true;
488 break;
489 }
490 attrs = attrs->getNext();
491 }
492}
493
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000494void DeclSpec::SaveStorageSpecifierAsWritten() {
495 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
496 // If 'extern' is part of a linkage specification,
497 // then it is not a storage class "as written".
498 StorageClassSpecAsWritten = SCS_unspecified;
499 else
500 StorageClassSpecAsWritten = StorageClassSpec;
501}
502
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000503/// Finish - This does final analysis of the declspec, rejecting things like
504/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
505/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
506/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000507void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000508 // Before possibly changing their values, save specs as written.
509 SaveWrittenBuiltinSpecs();
Douglas Gregorc4df4072010-04-19 22:54:31 +0000510 SaveStorageSpecifierAsWritten();
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000511
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000512 // Check the type specifier components first.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000513 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000514
Chris Lattner37141f42010-06-23 06:00:24 +0000515 // Validate and finalize AltiVec vector declspec.
516 if (TypeAltiVecVector) {
517 if (TypeAltiVecBool) {
518 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
519 if (TypeSpecSign != TSS_unspecified) {
520 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_vector_bool_decl_spec)
521 << getSpecifierName((TSS)TypeSpecSign);
522 }
523
524 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sandsd3e231e2010-06-23 19:34:52 +0000525 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
526 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Chris Lattner37141f42010-06-23 06:00:24 +0000527 Diag(D, TSTLoc, SrcMgr, diag::err_invalid_vector_bool_decl_spec)
528 << (TypeAltiVecPixel ? "__pixel" :
529 getSpecifierName((TST)TypeSpecType));
530 }
531
532 // Only 'short' is valid with vector bool. (PIM 2.1)
533 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
534 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_vector_bool_decl_spec)
535 << getSpecifierName((TSW)TypeSpecWidth);
536
537 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
538 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
539 (TypeSpecWidth != TSW_unspecified))
540 TypeSpecSign = TSS_unsigned;
541 }
542
543 if (TypeAltiVecPixel) {
544 //TODO: perform validation
545 TypeSpecType = TST_int;
546 TypeSpecSign = TSS_unsigned;
547 TypeSpecWidth = TSW_short;
John McCalla3707cc2010-08-26 17:22:34 +0000548 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000549 }
550 }
551
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000552 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000553 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000554 if (TypeSpecType == TST_unspecified)
555 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000556 else if (TypeSpecType != TST_int &&
557 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000558 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
559 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000560 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000561 TypeSpecSign = TSS_unspecified;
562 }
563 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000564
Chris Lattner839713c2006-08-04 06:15:52 +0000565 // Validate the width of the type.
566 switch (TypeSpecWidth) {
567 case TSW_unspecified: break;
568 case TSW_short: // short int
569 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000570 if (TypeSpecType == TST_unspecified)
571 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
572 else if (TypeSpecType != TST_int) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000573 Diag(D, TSWLoc, SrcMgr,
Chris Lattner36982e42007-05-16 17:49:37 +0000574 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000575 : diag::err_invalid_longlong_spec)
576 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000577 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000578 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000579 }
580 break;
581 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000582 if (TypeSpecType == TST_unspecified)
583 TypeSpecType = TST_int; // long -> long int.
584 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000585 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
586 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000587 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000588 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000589 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000590 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner0e894622006-08-13 19:58:17 +0000593 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000594 // disallow their use. Need information about the backend.
595 if (TypeSpecComplex != TSC_unspecified) {
596 if (TypeSpecType == TST_unspecified) {
Douglas Gregore3e01a22009-04-01 22:41:11 +0000597 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
Douglas Gregora771f462010-03-31 17:46:05 +0000598 << FixItHint::CreateInsertion(
Douglas Gregore3e01a22009-04-01 22:41:11 +0000599 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
600 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000601 TypeSpecType = TST_double; // _Complex -> _Complex double.
602 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000603 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000604 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000605 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000606 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
607 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000608 TypeSpecComplex = TSC_unspecified;
609 }
610 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000611
John McCall07e91c02009-08-06 02:15:43 +0000612 // C++ [class.friend]p6:
613 // No storage-class-specifier shall appear in the decl-specifier-seq
614 // of a friend declaration.
615 if (isFriendSpecified() && getStorageClassSpec()) {
616 DeclSpec::SCS SC = getStorageClassSpec();
617 const char *SpecName = getSpecifierName(SC);
618
619 SourceLocation SCLoc = getStorageClassSpecLoc();
620 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
621
622 Diag(D, SCLoc, SrcMgr, diag::err_friend_storage_spec)
623 << SpecName
Douglas Gregora771f462010-03-31 17:46:05 +0000624 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall07e91c02009-08-06 02:15:43 +0000625
626 ClearStorageClassSpecs();
627 }
628
John McCalla3707cc2010-08-26 17:22:34 +0000629 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
630
Chris Lattner8e90ef62006-08-05 03:30:45 +0000631 // Okay, now we can infer the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000632
Chris Lattner0e894622006-08-13 19:58:17 +0000633 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner1ae23292006-08-04 06:42:31 +0000635 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000636}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000637
Sebastian Redla2b5e312008-12-28 15:28:59 +0000638bool DeclSpec::isMissingDeclaratorOk() {
639 TST tst = getTypeSpecType();
John McCallba7bf592010-08-24 05:47:05 +0000640 return isDeclRep(tst) && getRepAsDecl() != 0 &&
641 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +0000642}
Douglas Gregor7861a802009-11-03 01:35:08 +0000643
644void UnqualifiedId::clear() {
645 if (Kind == IK_TemplateId)
646 TemplateId->Destroy();
647
648 Kind = IK_Identifier;
649 Identifier = 0;
650 StartLocation = SourceLocation();
651 EndLocation = SourceLocation();
652}
653
654void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
655 OverloadedOperatorKind Op,
656 SourceLocation SymbolLocations[3]) {
657 Kind = IK_OperatorFunctionId;
658 StartLocation = OperatorLoc;
659 EndLocation = OperatorLoc;
660 OperatorFunctionId.Operator = Op;
661 for (unsigned I = 0; I != 3; ++I) {
662 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
663
664 if (SymbolLocations[I].isValid())
665 EndLocation = SymbolLocations[I];
666 }
667}