blob: 4cd8fe887bb1499b03870fb65bc075fb25b9018c [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
Chris Lattner288e86ff12006-11-11 23:03:42 +000014#include "clang/Parse/DeclSpec.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000016#include "clang/Parse/Template.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
Chris Lattner1ce41ed2009-01-20 19:11:22 +000039/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
40/// "TheDeclarator" is the declarator that this will be added to.
41DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +000042 SourceLocation EllipsisLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000043 ParamInfo *ArgInfo,
44 unsigned NumArgs,
45 unsigned TypeQuals,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000046 bool hasExceptionSpec,
Sebastian Redlfb3f1792009-05-31 11:47:27 +000047 SourceLocation ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000048 bool hasAnyExceptionSpec,
49 ActionBase::TypeTy **Exceptions,
Sebastian Redld6434562009-05-29 18:02:33 +000050 SourceRange *ExceptionRanges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000051 unsigned NumExceptions,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +000052 SourceLocation LPLoc,
53 SourceLocation RPLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000054 Declarator &TheDeclarator) {
55 DeclaratorChunk I;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000056 I.Kind = Function;
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +000057 I.Loc = LPLoc;
58 I.EndLoc = RPLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000059 I.Fun.hasPrototype = hasProto;
60 I.Fun.isVariadic = isVariadic;
61 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
62 I.Fun.DeleteArgInfo = false;
63 I.Fun.TypeQuals = TypeQuals;
64 I.Fun.NumArgs = NumArgs;
65 I.Fun.ArgInfo = 0;
66 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redlfb3f1792009-05-31 11:47:27 +000067 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000068 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
69 I.Fun.NumExceptions = NumExceptions;
70 I.Fun.Exceptions = 0;
71
Chris Lattner1ce41ed2009-01-20 19:11:22 +000072 // new[] an argument array if needed.
73 if (NumArgs) {
74 // If the 'InlineParams' in Declarator is unused and big enough, put our
75 // parameter list there (in an effort to avoid new/delete traffic). If it
76 // is already used (consider a function returning a function pointer) or too
77 // small (function taking too many arguments), go to the heap.
Mike Stump11289f42009-09-09 15:08:12 +000078 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner1ce41ed2009-01-20 19:11:22 +000079 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
80 I.Fun.ArgInfo = TheDeclarator.InlineParams;
81 I.Fun.DeleteArgInfo = false;
82 TheDeclarator.InlineParamsUsed = true;
83 } else {
84 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
85 I.Fun.DeleteArgInfo = true;
86 }
87 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
88 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000089 // new[] an exception array if needed
90 if (NumExceptions) {
Sebastian Redld6434562009-05-29 18:02:33 +000091 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
92 for (unsigned i = 0; i != NumExceptions; ++i) {
93 I.Fun.Exceptions[i].Ty = Exceptions[i];
94 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
95 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000096 }
Chris Lattner1ce41ed2009-01-20 19:11:22 +000097 return I;
98}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000099
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000100/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +0000101/// declaration specifier includes.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000102///
103unsigned DeclSpec::getParsedSpecifiers() const {
104 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000105 if (StorageClassSpec != SCS_unspecified ||
106 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000107 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +0000108
Chris Lattner4d8f8732006-11-28 05:05:08 +0000109 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000110 Res |= PQ_TypeQualifier;
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattnerf055d432006-11-28 04:28:12 +0000112 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000113 Res |= PQ_TypeSpecifier;
Mike Stump11289f42009-09-09 15:08:12 +0000114
Douglas Gregor61956c42008-10-31 09:07:45 +0000115 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000116 Res |= PQ_FunctionSpecifier;
117 return Res;
118}
119
John McCall49bfce42009-08-03 20:12:06 +0000120template <class T> static bool BadSpecifier(T TNew, T TPrev,
121 const char *&PrevSpec,
122 unsigned &DiagID) {
John McCall898cd0f2009-08-03 18:47:27 +0000123 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCall49bfce42009-08-03 20:12:06 +0000124 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
125 : diag::err_invalid_decl_spec_combination);
John McCall898cd0f2009-08-03 18:47:27 +0000126 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000127}
John McCall898cd0f2009-08-03 18:47:27 +0000128
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000129const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000130 switch (S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000131 case DeclSpec::SCS_unspecified: return "unspecified";
132 case DeclSpec::SCS_typedef: return "typedef";
133 case DeclSpec::SCS_extern: return "extern";
134 case DeclSpec::SCS_static: return "static";
135 case DeclSpec::SCS_auto: return "auto";
136 case DeclSpec::SCS_register: return "register";
Eli Friedmand5c0eed2009-04-19 20:27:55 +0000137 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000138 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000139 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000140 llvm_unreachable("Unknown typespec!");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000141}
142
John McCall898cd0f2009-08-03 18:47:27 +0000143const char *DeclSpec::getSpecifierName(TSW W) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000144 switch (W) {
John McCall898cd0f2009-08-03 18:47:27 +0000145 case TSW_unspecified: return "unspecified";
146 case TSW_short: return "short";
147 case TSW_long: return "long";
148 case TSW_longlong: return "long long";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000149 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000150 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000151}
152
John McCall898cd0f2009-08-03 18:47:27 +0000153const char *DeclSpec::getSpecifierName(TSC C) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000154 switch (C) {
John McCall898cd0f2009-08-03 18:47:27 +0000155 case TSC_unspecified: return "unspecified";
156 case TSC_imaginary: return "imaginary";
157 case TSC_complex: return "complex";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000158 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000159 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000160}
161
162
John McCall898cd0f2009-08-03 18:47:27 +0000163const char *DeclSpec::getSpecifierName(TSS S) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000164 switch (S) {
John McCall898cd0f2009-08-03 18:47:27 +0000165 case TSS_unspecified: return "unspecified";
166 case TSS_signed: return "signed";
167 case TSS_unsigned: return "unsigned";
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
Chris Lattner69680ea2007-01-23 04:34:43 +0000172const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000173 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000174 case DeclSpec::TST_unspecified: return "unspecified";
175 case DeclSpec::TST_void: return "void";
176 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000177 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000178 case DeclSpec::TST_char16: return "char16_t";
179 case DeclSpec::TST_char32: return "char32_t";
Chris Lattner839713c2006-08-04 06:15:52 +0000180 case DeclSpec::TST_int: return "int";
181 case DeclSpec::TST_float: return "float";
182 case DeclSpec::TST_double: return "double";
183 case DeclSpec::TST_bool: return "_Bool";
184 case DeclSpec::TST_decimal32: return "_Decimal32";
185 case DeclSpec::TST_decimal64: return "_Decimal64";
186 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +0000187 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000188 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000189 case DeclSpec::TST_union: return "union";
190 case DeclSpec::TST_struct: return "struct";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000191 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000192 case DeclSpec::TST_typeofType:
193 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall898cd0f2009-08-03 18:47:27 +0000194 case DeclSpec::TST_auto: return "auto";
195 case DeclSpec::TST_decltype: return "(decltype)";
196 case DeclSpec::TST_error: return "(error)";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000197 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000198 llvm_unreachable("Unknown typespec!");
Chris Lattner839713c2006-08-04 06:15:52 +0000199}
200
John McCall898cd0f2009-08-03 18:47:27 +0000201const char *DeclSpec::getSpecifierName(TQ T) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000202 switch (T) {
John McCall898cd0f2009-08-03 18:47:27 +0000203 case DeclSpec::TQ_unspecified: return "unspecified";
204 case DeclSpec::TQ_const: return "const";
205 case DeclSpec::TQ_restrict: return "restrict";
206 case DeclSpec::TQ_volatile: return "volatile";
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000207 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000208 llvm_unreachable("Unknown typespec!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000209}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000210
Chris Lattner4d8f8732006-11-28 05:05:08 +0000211bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000212 const char *&PrevSpec,
213 unsigned &DiagID) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000214 if (StorageClassSpec != SCS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000215 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000216 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000217 StorageClassSpecLoc = Loc;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000218 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000219 return false;
220}
221
Mike Stump11289f42009-09-09 15:08:12 +0000222bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000223 const char *&PrevSpec,
224 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000225 if (SCS_thread_specified) {
226 PrevSpec = "__thread";
John McCall49bfce42009-08-03 20:12:06 +0000227 DiagID = diag::ext_duplicate_declspec;
Chris Lattner353f5742006-11-28 04:50:12 +0000228 return true;
229 }
230 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000231 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000232 return false;
233}
234
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000235
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000236/// These methods set the specified attribute of the DeclSpec, but return true
237/// and ignore the request if invalid (e.g. "extern" then "auto" is
238/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000239bool DeclSpec::SetTypeSpecWidth(TSW W, 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 (TypeSpecWidth != TSW_unspecified &&
243 // Allow turning long -> long long.
244 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCall49bfce42009-08-03 20:12:06 +0000245 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000246 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000247 TSWLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000248 return false;
249}
250
Mike Stump11289f42009-09-09 15:08:12 +0000251bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000252 const char *&PrevSpec,
253 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000254 if (TypeSpecComplex != TSC_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000255 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000256 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000257 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000258 return false;
259}
260
Mike Stump11289f42009-09-09 15:08:12 +0000261bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000262 const char *&PrevSpec,
263 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000264 if (TypeSpecSign != TSS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000265 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000266 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000267 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000268 return false;
269}
270
Chris Lattnerb20e8942006-11-28 05:30:29 +0000271bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000272 const char *&PrevSpec,
273 unsigned &DiagID,
274 void *Rep, bool Owned) {
275 if (TypeSpecType != TST_unspecified) {
276 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
277 DiagID = diag::err_invalid_decl_spec_combination;
278 return true;
279 }
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000280 TypeSpecType = T;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000281 TypeRep = Rep;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000282 TSTLoc = Loc;
Douglas Gregord6ab8742009-05-28 23:31:59 +0000283 TypeSpecOwned = Owned;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000284 return false;
285}
286
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000287bool DeclSpec::SetTypeSpecError() {
288 TypeSpecType = TST_error;
289 TypeRep = 0;
290 TSTLoc = SourceLocation();
291 return false;
292}
293
Chris Lattner60809f52006-11-28 05:18:46 +0000294bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000295 unsigned &DiagID, const LangOptions &Lang) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000296 // Duplicates turn into warnings pre-C99.
297 if ((TypeQualifiers & T) && !Lang.C99)
John McCall49bfce42009-08-03 20:12:06 +0000298 return BadSpecifier(T, T, PrevSpec, DiagID);
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000299 TypeQualifiers |= T;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Chris Lattner60809f52006-11-28 05:18:46 +0000301 switch (T) {
302 default: assert(0 && "Unknown type qualifier!");
303 case TQ_const: TQ_constLoc = Loc; break;
304 case TQ_restrict: TQ_restrictLoc = Loc; break;
305 case TQ_volatile: TQ_volatileLoc = Loc; break;
306 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000307 return false;
308}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000309
John McCall49bfce42009-08-03 20:12:06 +0000310bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
311 unsigned &DiagID) {
Chris Lattnera925dc62006-11-28 04:33:46 +0000312 // 'inline inline' is ok.
313 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000314 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000315 return false;
316}
317
John McCall49bfce42009-08-03 20:12:06 +0000318bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
319 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000320 // 'virtual virtual' is ok.
321 FS_virtual_specified = true;
322 FS_virtualLoc = Loc;
323 return false;
324}
325
John McCall49bfce42009-08-03 20:12:06 +0000326bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
327 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000328 // 'explicit explicit' is ok.
329 FS_explicit_specified = true;
330 FS_explicitLoc = Loc;
331 return false;
332}
333
John McCall49bfce42009-08-03 20:12:06 +0000334bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
335 unsigned &DiagID) {
Anders Carlssoncd8db412009-05-06 04:46:28 +0000336 if (Friend_specified) {
337 PrevSpec = "friend";
John McCall49bfce42009-08-03 20:12:06 +0000338 DiagID = diag::ext_duplicate_declspec;
Anders Carlssoncd8db412009-05-06 04:46:28 +0000339 return true;
340 }
John McCall49bfce42009-08-03 20:12:06 +0000341
Anders Carlssoncd8db412009-05-06 04:46:28 +0000342 Friend_specified = true;
343 FriendLoc = Loc;
344 return false;
345}
Chris Lattnera925dc62006-11-28 04:33:46 +0000346
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000347bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
348 unsigned &DiagID) {
349 // 'constexpr constexpr' is ok.
350 Constexpr_specified = true;
351 ConstexprLoc = Loc;
352 return false;
353}
354
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000355void DeclSpec::setProtocolQualifiers(const ActionBase::DeclPtrTy *Protos,
356 unsigned NP,
357 SourceLocation *ProtoLocs,
358 SourceLocation LAngleLoc) {
359 if (NP == 0) return;
360 ProtocolQualifiers = new ActionBase::DeclPtrTy[NP];
361 ProtocolLocs = new SourceLocation[NP];
362 memcpy((void*)ProtocolQualifiers, Protos, sizeof(ActionBase::DeclPtrTy)*NP);
363 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
364 NumProtocolQualifiers = NP;
365 ProtocolLAngleLoc = LAngleLoc;
366}
367
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000368/// Finish - This does final analysis of the declspec, rejecting things like
369/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
370/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
371/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000372void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000373 // Check the type specifier components first.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000374 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000375
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000376 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000377 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000378 if (TypeSpecType == TST_unspecified)
379 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000380 else if (TypeSpecType != TST_int &&
381 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000382 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
383 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000384 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000385 TypeSpecSign = TSS_unspecified;
386 }
387 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000388
Chris Lattner839713c2006-08-04 06:15:52 +0000389 // Validate the width of the type.
390 switch (TypeSpecWidth) {
391 case TSW_unspecified: break;
392 case TSW_short: // short int
393 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000394 if (TypeSpecType == TST_unspecified)
395 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
396 else if (TypeSpecType != TST_int) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000397 Diag(D, TSWLoc, SrcMgr,
Chris Lattner36982e42007-05-16 17:49:37 +0000398 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000399 : diag::err_invalid_longlong_spec)
400 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000401 TypeSpecType = TST_int;
402 }
403 break;
404 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000405 if (TypeSpecType == TST_unspecified)
406 TypeSpecType = TST_int; // long -> long int.
407 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000408 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
409 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000410 TypeSpecType = TST_int;
411 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000412 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000413 }
Mike Stump11289f42009-09-09 15:08:12 +0000414
Chris Lattner0e894622006-08-13 19:58:17 +0000415 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000416 // disallow their use. Need information about the backend.
417 if (TypeSpecComplex != TSC_unspecified) {
418 if (TypeSpecType == TST_unspecified) {
Douglas Gregore3e01a22009-04-01 22:41:11 +0000419 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
420 << CodeModificationHint::CreateInsertion(
421 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
422 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000423 TypeSpecType = TST_double; // _Complex -> _Complex double.
424 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000425 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000426 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000427 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000428 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
429 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000430 TypeSpecComplex = TSC_unspecified;
431 }
432 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000433
John McCall07e91c02009-08-06 02:15:43 +0000434 // C++ [class.friend]p6:
435 // No storage-class-specifier shall appear in the decl-specifier-seq
436 // of a friend declaration.
437 if (isFriendSpecified() && getStorageClassSpec()) {
438 DeclSpec::SCS SC = getStorageClassSpec();
439 const char *SpecName = getSpecifierName(SC);
440
441 SourceLocation SCLoc = getStorageClassSpecLoc();
442 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
443
444 Diag(D, SCLoc, SrcMgr, diag::err_friend_storage_spec)
445 << SpecName
446 << CodeModificationHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
447
448 ClearStorageClassSpecs();
449 }
450
451
Chris Lattner8e90ef62006-08-05 03:30:45 +0000452 // Okay, now we can infer the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000453
Chris Lattner0e894622006-08-13 19:58:17 +0000454 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000455
Chris Lattner1ae23292006-08-04 06:42:31 +0000456 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000457}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000458
Sebastian Redla2b5e312008-12-28 15:28:59 +0000459bool DeclSpec::isMissingDeclaratorOk() {
460 TST tst = getTypeSpecType();
461 return (tst == TST_union
462 || tst == TST_struct
463 || tst == TST_class
464 || tst == TST_enum
Douglas Gregorc6f58fe2009-01-12 22:49:06 +0000465 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +0000466}
Douglas Gregor7861a802009-11-03 01:35:08 +0000467
468void UnqualifiedId::clear() {
469 if (Kind == IK_TemplateId)
470 TemplateId->Destroy();
471
472 Kind = IK_Identifier;
473 Identifier = 0;
474 StartLocation = SourceLocation();
475 EndLocation = SourceLocation();
476}
477
478void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
479 OverloadedOperatorKind Op,
480 SourceLocation SymbolLocations[3]) {
481 Kind = IK_OperatorFunctionId;
482 StartLocation = OperatorLoc;
483 EndLocation = OperatorLoc;
484 OperatorFunctionId.Operator = Op;
485 for (unsigned I = 0; I != 3; ++I) {
486 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
487
488 if (SymbolLocations[I].isValid())
489 EndLocation = SymbolLocations[I];
490 }
491}