blob: b8422aad5a842cdd9a4b1b02d68fc5309a7bc8e3 [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 Gregore3e01a22009-04-01 22:41:11 +000016#include "clang/Lex/Preprocessor.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000017#include "clang/Basic/LangOptions.h"
Chris Lattner1ce41ed2009-01-20 19:11:22 +000018#include "llvm/ADT/STLExtras.h"
John McCall898cd0f2009-08-03 18:47:27 +000019#include "llvm/Support/ErrorHandling.h"
Douglas Gregor52537682009-03-19 00:18:19 +000020#include <cstring>
Chris Lattnerb9093cd2006-08-04 04:39:53 +000021using namespace clang;
22
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000023
24static DiagnosticBuilder Diag(Diagnostic &D, SourceLocation Loc,
25 SourceManager &SrcMgr, unsigned DiagID) {
26 return D.Report(FullSourceLoc(Loc, SrcMgr), DiagID);
27}
28
Chris Lattner1ce41ed2009-01-20 19:11:22 +000029/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
30/// "TheDeclarator" is the declarator that this will be added to.
31DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +000032 SourceLocation EllipsisLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000033 ParamInfo *ArgInfo,
34 unsigned NumArgs,
35 unsigned TypeQuals,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000036 bool hasExceptionSpec,
Sebastian Redlfb3f1792009-05-31 11:47:27 +000037 SourceLocation ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000038 bool hasAnyExceptionSpec,
39 ActionBase::TypeTy **Exceptions,
Sebastian Redld6434562009-05-29 18:02:33 +000040 SourceRange *ExceptionRanges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000041 unsigned NumExceptions,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +000042 SourceLocation LPLoc,
43 SourceLocation RPLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +000044 Declarator &TheDeclarator) {
45 DeclaratorChunk I;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000046 I.Kind = Function;
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +000047 I.Loc = LPLoc;
48 I.EndLoc = RPLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000049 I.Fun.hasPrototype = hasProto;
50 I.Fun.isVariadic = isVariadic;
51 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
52 I.Fun.DeleteArgInfo = false;
53 I.Fun.TypeQuals = TypeQuals;
54 I.Fun.NumArgs = NumArgs;
55 I.Fun.ArgInfo = 0;
56 I.Fun.hasExceptionSpec = hasExceptionSpec;
Sebastian Redlfb3f1792009-05-31 11:47:27 +000057 I.Fun.ThrowLoc = ThrowLoc.getRawEncoding();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000058 I.Fun.hasAnyExceptionSpec = hasAnyExceptionSpec;
59 I.Fun.NumExceptions = NumExceptions;
60 I.Fun.Exceptions = 0;
61
Chris Lattner1ce41ed2009-01-20 19:11:22 +000062 // new[] an argument array if needed.
63 if (NumArgs) {
64 // If the 'InlineParams' in Declarator is unused and big enough, put our
65 // parameter list there (in an effort to avoid new/delete traffic). If it
66 // is already used (consider a function returning a function pointer) or too
67 // small (function taking too many arguments), go to the heap.
Mike Stump11289f42009-09-09 15:08:12 +000068 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner1ce41ed2009-01-20 19:11:22 +000069 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
70 I.Fun.ArgInfo = TheDeclarator.InlineParams;
71 I.Fun.DeleteArgInfo = false;
72 TheDeclarator.InlineParamsUsed = true;
73 } else {
74 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
75 I.Fun.DeleteArgInfo = true;
76 }
77 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
78 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000079 // new[] an exception array if needed
80 if (NumExceptions) {
Sebastian Redld6434562009-05-29 18:02:33 +000081 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
82 for (unsigned i = 0; i != NumExceptions; ++i) {
83 I.Fun.Exceptions[i].Ty = Exceptions[i];
84 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
85 }
Sebastian Redl2b9cacb2009-04-29 17:30:04 +000086 }
Chris Lattner1ce41ed2009-01-20 19:11:22 +000087 return I;
88}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000089
Chris Lattnerb9093cd2006-08-04 04:39:53 +000090/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +000091/// declaration specifier includes.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000092///
93unsigned DeclSpec::getParsedSpecifiers() const {
94 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +000095 if (StorageClassSpec != SCS_unspecified ||
96 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000097 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +000098
Chris Lattner4d8f8732006-11-28 05:05:08 +000099 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000100 Res |= PQ_TypeQualifier;
Mike Stump11289f42009-09-09 15:08:12 +0000101
Chris Lattnerf055d432006-11-28 04:28:12 +0000102 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000103 Res |= PQ_TypeSpecifier;
Mike Stump11289f42009-09-09 15:08:12 +0000104
Douglas Gregor61956c42008-10-31 09:07:45 +0000105 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000106 Res |= PQ_FunctionSpecifier;
107 return Res;
108}
109
John McCall49bfce42009-08-03 20:12:06 +0000110template <class T> static bool BadSpecifier(T TNew, T TPrev,
111 const char *&PrevSpec,
112 unsigned &DiagID) {
John McCall898cd0f2009-08-03 18:47:27 +0000113 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCall49bfce42009-08-03 20:12:06 +0000114 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
115 : diag::err_invalid_decl_spec_combination);
John McCall898cd0f2009-08-03 18:47:27 +0000116 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000117}
John McCall898cd0f2009-08-03 18:47:27 +0000118
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000119const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000120 switch (S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000121 case DeclSpec::SCS_unspecified: return "unspecified";
122 case DeclSpec::SCS_typedef: return "typedef";
123 case DeclSpec::SCS_extern: return "extern";
124 case DeclSpec::SCS_static: return "static";
125 case DeclSpec::SCS_auto: return "auto";
126 case DeclSpec::SCS_register: return "register";
Eli Friedmand5c0eed2009-04-19 20:27:55 +0000127 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000128 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000129 }
John McCall898cd0f2009-08-03 18:47:27 +0000130 llvm::llvm_unreachable("Unknown typespec!");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000131}
132
John McCall898cd0f2009-08-03 18:47:27 +0000133const char *DeclSpec::getSpecifierName(TSW W) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000134 switch (W) {
John McCall898cd0f2009-08-03 18:47:27 +0000135 case TSW_unspecified: return "unspecified";
136 case TSW_short: return "short";
137 case TSW_long: return "long";
138 case TSW_longlong: return "long long";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000139 }
John McCall898cd0f2009-08-03 18:47:27 +0000140 llvm::llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000141}
142
John McCall898cd0f2009-08-03 18:47:27 +0000143const char *DeclSpec::getSpecifierName(TSC C) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000144 switch (C) {
John McCall898cd0f2009-08-03 18:47:27 +0000145 case TSC_unspecified: return "unspecified";
146 case TSC_imaginary: return "imaginary";
147 case TSC_complex: return "complex";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000148 }
John McCall898cd0f2009-08-03 18:47:27 +0000149 llvm::llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000150}
151
152
John McCall898cd0f2009-08-03 18:47:27 +0000153const char *DeclSpec::getSpecifierName(TSS S) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000154 switch (S) {
John McCall898cd0f2009-08-03 18:47:27 +0000155 case TSS_unspecified: return "unspecified";
156 case TSS_signed: return "signed";
157 case TSS_unsigned: return "unsigned";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000158 }
John McCall898cd0f2009-08-03 18:47:27 +0000159 llvm::llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000160}
161
Chris Lattner69680ea2007-01-23 04:34:43 +0000162const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000163 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000164 case DeclSpec::TST_unspecified: return "unspecified";
165 case DeclSpec::TST_void: return "void";
166 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000167 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000168 case DeclSpec::TST_char16: return "char16_t";
169 case DeclSpec::TST_char32: return "char32_t";
Chris Lattner839713c2006-08-04 06:15:52 +0000170 case DeclSpec::TST_int: return "int";
171 case DeclSpec::TST_float: return "float";
172 case DeclSpec::TST_double: return "double";
173 case DeclSpec::TST_bool: return "_Bool";
174 case DeclSpec::TST_decimal32: return "_Decimal32";
175 case DeclSpec::TST_decimal64: return "_Decimal64";
176 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +0000177 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000178 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000179 case DeclSpec::TST_union: return "union";
180 case DeclSpec::TST_struct: return "struct";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000181 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000182 case DeclSpec::TST_typeofType:
183 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall898cd0f2009-08-03 18:47:27 +0000184 case DeclSpec::TST_auto: return "auto";
185 case DeclSpec::TST_decltype: return "(decltype)";
186 case DeclSpec::TST_error: return "(error)";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000187 }
John McCall898cd0f2009-08-03 18:47:27 +0000188 llvm::llvm_unreachable("Unknown typespec!");
Chris Lattner839713c2006-08-04 06:15:52 +0000189}
190
John McCall898cd0f2009-08-03 18:47:27 +0000191const char *DeclSpec::getSpecifierName(TQ T) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000192 switch (T) {
John McCall898cd0f2009-08-03 18:47:27 +0000193 case DeclSpec::TQ_unspecified: return "unspecified";
194 case DeclSpec::TQ_const: return "const";
195 case DeclSpec::TQ_restrict: return "restrict";
196 case DeclSpec::TQ_volatile: return "volatile";
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000197 }
John McCall898cd0f2009-08-03 18:47:27 +0000198 llvm::llvm_unreachable("Unknown typespec!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000199}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000200
Chris Lattner4d8f8732006-11-28 05:05:08 +0000201bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000202 const char *&PrevSpec,
203 unsigned &DiagID) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000204 if (StorageClassSpec != SCS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000205 return BadSpecifier(S, (SCS)StorageClassSpec, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000206 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000207 StorageClassSpecLoc = Loc;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000208 assert((unsigned)S == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000209 return false;
210}
211
Mike Stump11289f42009-09-09 15:08:12 +0000212bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000213 const char *&PrevSpec,
214 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000215 if (SCS_thread_specified) {
216 PrevSpec = "__thread";
John McCall49bfce42009-08-03 20:12:06 +0000217 DiagID = diag::ext_duplicate_declspec;
Chris Lattner353f5742006-11-28 04:50:12 +0000218 return true;
219 }
220 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000221 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000222 return false;
223}
224
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000225
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000226/// These methods set the specified attribute of the DeclSpec, but return true
227/// and ignore the request if invalid (e.g. "extern" then "auto" is
228/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000229bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000230 const char *&PrevSpec,
231 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000232 if (TypeSpecWidth != TSW_unspecified &&
233 // Allow turning long -> long long.
234 (W != TSW_longlong || TypeSpecWidth != TSW_long))
John McCall49bfce42009-08-03 20:12:06 +0000235 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000236 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000237 TSWLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000238 return false;
239}
240
Mike Stump11289f42009-09-09 15:08:12 +0000241bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000242 const char *&PrevSpec,
243 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000244 if (TypeSpecComplex != TSC_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000245 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000246 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000247 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000248 return false;
249}
250
Mike Stump11289f42009-09-09 15:08:12 +0000251bool DeclSpec::SetTypeSpecSign(TSS S, 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 (TypeSpecSign != TSS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000255 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000256 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000257 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000258 return false;
259}
260
Chris Lattnerb20e8942006-11-28 05:30:29 +0000261bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000262 const char *&PrevSpec,
263 unsigned &DiagID,
264 void *Rep, bool Owned) {
265 if (TypeSpecType != TST_unspecified) {
266 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
267 DiagID = diag::err_invalid_decl_spec_combination;
268 return true;
269 }
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000270 TypeSpecType = T;
Chris Lattnerb9d572a2007-01-23 04:58:34 +0000271 TypeRep = Rep;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000272 TSTLoc = Loc;
Douglas Gregord6ab8742009-05-28 23:31:59 +0000273 TypeSpecOwned = Owned;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000274 return false;
275}
276
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000277bool DeclSpec::SetTypeSpecError() {
278 TypeSpecType = TST_error;
279 TypeRep = 0;
280 TSTLoc = SourceLocation();
281 return false;
282}
283
Chris Lattner60809f52006-11-28 05:18:46 +0000284bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000285 unsigned &DiagID, const LangOptions &Lang) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000286 // Duplicates turn into warnings pre-C99.
287 if ((TypeQualifiers & T) && !Lang.C99)
John McCall49bfce42009-08-03 20:12:06 +0000288 return BadSpecifier(T, T, PrevSpec, DiagID);
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000289 TypeQualifiers |= T;
Mike Stump11289f42009-09-09 15:08:12 +0000290
Chris Lattner60809f52006-11-28 05:18:46 +0000291 switch (T) {
292 default: assert(0 && "Unknown type qualifier!");
293 case TQ_const: TQ_constLoc = Loc; break;
294 case TQ_restrict: TQ_restrictLoc = Loc; break;
295 case TQ_volatile: TQ_volatileLoc = Loc; break;
296 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000297 return false;
298}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000299
John McCall49bfce42009-08-03 20:12:06 +0000300bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
301 unsigned &DiagID) {
Chris Lattnera925dc62006-11-28 04:33:46 +0000302 // 'inline inline' is ok.
303 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000304 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000305 return false;
306}
307
John McCall49bfce42009-08-03 20:12:06 +0000308bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
309 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000310 // 'virtual virtual' is ok.
311 FS_virtual_specified = true;
312 FS_virtualLoc = Loc;
313 return false;
314}
315
John McCall49bfce42009-08-03 20:12:06 +0000316bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
317 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000318 // 'explicit explicit' is ok.
319 FS_explicit_specified = true;
320 FS_explicitLoc = Loc;
321 return false;
322}
323
John McCall49bfce42009-08-03 20:12:06 +0000324bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
325 unsigned &DiagID) {
Anders Carlssoncd8db412009-05-06 04:46:28 +0000326 if (Friend_specified) {
327 PrevSpec = "friend";
John McCall49bfce42009-08-03 20:12:06 +0000328 DiagID = diag::ext_duplicate_declspec;
Anders Carlssoncd8db412009-05-06 04:46:28 +0000329 return true;
330 }
John McCall49bfce42009-08-03 20:12:06 +0000331
Anders Carlssoncd8db412009-05-06 04:46:28 +0000332 Friend_specified = true;
333 FriendLoc = Loc;
334 return false;
335}
Chris Lattnera925dc62006-11-28 04:33:46 +0000336
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000337void DeclSpec::setProtocolQualifiers(const ActionBase::DeclPtrTy *Protos,
338 unsigned NP,
339 SourceLocation *ProtoLocs,
340 SourceLocation LAngleLoc) {
341 if (NP == 0) return;
342 ProtocolQualifiers = new ActionBase::DeclPtrTy[NP];
343 ProtocolLocs = new SourceLocation[NP];
344 memcpy((void*)ProtocolQualifiers, Protos, sizeof(ActionBase::DeclPtrTy)*NP);
345 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
346 NumProtocolQualifiers = NP;
347 ProtocolLAngleLoc = LAngleLoc;
348}
349
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000350/// Finish - This does final analysis of the declspec, rejecting things like
351/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
352/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
353/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000354void DeclSpec::Finish(Diagnostic &D, Preprocessor &PP) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000355 // Check the type specifier components first.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000356 SourceManager &SrcMgr = PP.getSourceManager();
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000357
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000358 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000359 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000360 if (TypeSpecType == TST_unspecified)
361 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000362 else if (TypeSpecType != TST_int &&
363 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000364 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec)
365 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000366 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000367 TypeSpecSign = TSS_unspecified;
368 }
369 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000370
Chris Lattner839713c2006-08-04 06:15:52 +0000371 // Validate the width of the type.
372 switch (TypeSpecWidth) {
373 case TSW_unspecified: break;
374 case TSW_short: // short int
375 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000376 if (TypeSpecType == TST_unspecified)
377 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
378 else if (TypeSpecType != TST_int) {
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000379 Diag(D, TSWLoc, SrcMgr,
Chris Lattner36982e42007-05-16 17:49:37 +0000380 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000381 : diag::err_invalid_longlong_spec)
382 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000383 TypeSpecType = TST_int;
384 }
385 break;
386 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000387 if (TypeSpecType == TST_unspecified)
388 TypeSpecType = TST_int; // long -> long int.
389 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000390 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec)
391 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000392 TypeSpecType = TST_int;
393 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000394 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattner0e894622006-08-13 19:58:17 +0000397 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000398 // disallow their use. Need information about the backend.
399 if (TypeSpecComplex != TSC_unspecified) {
400 if (TypeSpecType == TST_unspecified) {
Douglas Gregore3e01a22009-04-01 22:41:11 +0000401 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex)
402 << CodeModificationHint::CreateInsertion(
403 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
404 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000405 TypeSpecType = TST_double; // _Complex -> _Complex double.
406 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000407 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000408 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000409 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000410 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec)
411 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000412 TypeSpecComplex = TSC_unspecified;
413 }
414 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000415
John McCall07e91c02009-08-06 02:15:43 +0000416 // C++ [class.friend]p6:
417 // No storage-class-specifier shall appear in the decl-specifier-seq
418 // of a friend declaration.
419 if (isFriendSpecified() && getStorageClassSpec()) {
420 DeclSpec::SCS SC = getStorageClassSpec();
421 const char *SpecName = getSpecifierName(SC);
422
423 SourceLocation SCLoc = getStorageClassSpecLoc();
424 SourceLocation SCEndLoc = SCLoc.getFileLocWithOffset(strlen(SpecName));
425
426 Diag(D, SCLoc, SrcMgr, diag::err_friend_storage_spec)
427 << SpecName
428 << CodeModificationHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
429
430 ClearStorageClassSpecs();
431 }
432
433
Chris Lattner8e90ef62006-08-05 03:30:45 +0000434 // Okay, now we can infer the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattner0e894622006-08-13 19:58:17 +0000436 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner1ae23292006-08-04 06:42:31 +0000438 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000439}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000440
Sebastian Redla2b5e312008-12-28 15:28:59 +0000441bool DeclSpec::isMissingDeclaratorOk() {
442 TST tst = getTypeSpecType();
443 return (tst == TST_union
444 || tst == TST_struct
445 || tst == TST_class
446 || tst == TST_enum
Douglas Gregorc6f58fe2009-01-12 22:49:06 +0000447 ) && getTypeRep() != 0 && StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +0000448}