blob: 41dd10c70f130dab40718d710e77b69787e020f0 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 Lattnerda48a8e2006-08-04 05:25:55 +000015#include "clang/Basic/LangOptions.h"
Chris Lattner839713c2006-08-04 06:15:52 +000016#include "clang/Basic/SourceLocation.h"
Chris Lattnerb9093cd2006-08-04 04:39:53 +000017using namespace llvm;
18using namespace clang;
19
20/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
21///
22unsigned DeclSpec::getParsedSpecifiers() const {
23 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +000024 if (StorageClassSpec != SCS_unspecified ||
25 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000026 Res |= PQ_StorageClassSpecifier;
27
Chris Lattner4d8f8732006-11-28 05:05:08 +000028 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000029 Res |= PQ_TypeQualifier;
30
Chris Lattnerf055d432006-11-28 04:28:12 +000031 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +000032 Res |= PQ_TypeSpecifier;
33
Chris Lattnerf63f89a2006-08-05 03:28:50 +000034 if (FS_inline_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000035 Res |= PQ_FunctionSpecifier;
36 return Res;
37}
38
Chris Lattnerf63f89a2006-08-05 03:28:50 +000039static const char *getSpecifierName(DeclSpec::SCS S) {
40 switch (S) {
41 default: assert(0 && "Unknown typespec!");
42 case DeclSpec::SCS_unspecified: return "unspecified";
43 case DeclSpec::SCS_typedef: return "typedef";
44 case DeclSpec::SCS_extern: return "extern";
45 case DeclSpec::SCS_static: return "static";
46 case DeclSpec::SCS_auto: return "auto";
47 case DeclSpec::SCS_register: return "register";
48 }
49}
50
51static bool BadSpecifier(DeclSpec::SCS S, const char *&PrevSpec) {
52 PrevSpec = getSpecifierName(S);
53 return true;
54}
Chris Lattnerb9093cd2006-08-04 04:39:53 +000055
56static bool BadSpecifier(DeclSpec::TSW W, const char *&PrevSpec) {
57 switch (W) {
58 case DeclSpec::TSW_unspecified: PrevSpec = "unspecified"; break;
59 case DeclSpec::TSW_short: PrevSpec = "short"; break;
60 case DeclSpec::TSW_long: PrevSpec = "long"; break;
61 case DeclSpec::TSW_longlong: PrevSpec = "long long"; break;
62 }
63 return true;
64}
65
66static bool BadSpecifier(DeclSpec::TSC C, const char *&PrevSpec) {
67 switch (C) {
68 case DeclSpec::TSC_unspecified: PrevSpec = "unspecified"; break;
69 case DeclSpec::TSC_imaginary: PrevSpec = "imaginary"; break;
70 case DeclSpec::TSC_complex: PrevSpec = "complex"; break;
71 }
72 return true;
73}
74
75
76static bool BadSpecifier(DeclSpec::TSS S, const char *&PrevSpec) {
77 switch (S) {
78 case DeclSpec::TSS_unspecified: PrevSpec = "unspecified"; break;
79 case DeclSpec::TSS_signed: PrevSpec = "signed"; break;
80 case DeclSpec::TSS_unsigned: PrevSpec = "unsigned"; break;
81 }
82 return true;
83}
84
Chris Lattner69680ea2007-01-23 04:34:43 +000085const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000086 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +000087 default: assert(0 && "Unknown typespec!");
88 case DeclSpec::TST_unspecified: return "unspecified";
89 case DeclSpec::TST_void: return "void";
90 case DeclSpec::TST_char: return "char";
91 case DeclSpec::TST_int: return "int";
92 case DeclSpec::TST_float: return "float";
93 case DeclSpec::TST_double: return "double";
94 case DeclSpec::TST_bool: return "_Bool";
95 case DeclSpec::TST_decimal32: return "_Decimal32";
96 case DeclSpec::TST_decimal64: return "_Decimal64";
97 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +000098 case DeclSpec::TST_enum: return "enum";
99 case DeclSpec::TST_union: return "union";
100 case DeclSpec::TST_struct: return "struct";
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000101 case DeclSpec::TST_typedef: return "typedef";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000102 }
Chris Lattner839713c2006-08-04 06:15:52 +0000103}
104
105static bool BadSpecifier(DeclSpec::TST T, const char *&PrevSpec) {
Chris Lattner69680ea2007-01-23 04:34:43 +0000106 PrevSpec = DeclSpec::getSpecifierName(T);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000107 return true;
108}
109
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000110static bool BadSpecifier(DeclSpec::TQ T, const char *&PrevSpec) {
111 switch (T) {
112 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
113 case DeclSpec::TQ_const: PrevSpec = "const"; break;
114 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
115 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
116 }
117 return true;
118}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000119
Chris Lattner4d8f8732006-11-28 05:05:08 +0000120bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
121 const char *&PrevSpec) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000122 if (StorageClassSpec != SCS_unspecified)
123 return BadSpecifier(StorageClassSpec, PrevSpec);
124 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000125 StorageClassSpecLoc = Loc;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000126 return false;
127}
128
Chris Lattner4d8f8732006-11-28 05:05:08 +0000129bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
130 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000131 if (SCS_thread_specified) {
132 PrevSpec = "__thread";
133 return true;
134 }
135 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000136 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000137 return false;
138}
139
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000140
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000141/// These methods set the specified attribute of the DeclSpec, but return true
142/// and ignore the request if invalid (e.g. "extern" then "auto" is
143/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000144bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
145 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000146 if (TypeSpecWidth != TSW_unspecified &&
147 // Allow turning long -> long long.
148 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000149 return BadSpecifier(TypeSpecWidth, PrevSpec);
150 TypeSpecWidth = W;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000151 TSWLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000152 return false;
153}
154
Chris Lattnerb20e8942006-11-28 05:30:29 +0000155bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
156 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000157 if (TypeSpecComplex != TSC_unspecified)
158 return BadSpecifier(TypeSpecComplex, PrevSpec);
159 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000160 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000161 return false;
162}
163
Chris Lattnerb20e8942006-11-28 05:30:29 +0000164bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
165 const char *&PrevSpec) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000166 if (TypeSpecSign != TSS_unspecified)
167 return BadSpecifier(TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000168 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000169 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000170 return false;
171}
172
Chris Lattnerb20e8942006-11-28 05:30:29 +0000173bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
174 const char *&PrevSpec, void *TypeRep) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000175 if (TypeSpecType != TST_unspecified)
176 return BadSpecifier(TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000177 TypeSpecType = T;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000178 TypenameRep = TypeRep;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000179 TSTLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000180 return false;
181}
182
Chris Lattner60809f52006-11-28 05:18:46 +0000183bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000184 const LangOptions &Lang) {
185 // Duplicates turn into warnings pre-C99.
186 if ((TypeQualifiers & T) && !Lang.C99)
187 return BadSpecifier(T, PrevSpec);
188 TypeQualifiers |= T;
Chris Lattner60809f52006-11-28 05:18:46 +0000189
190 switch (T) {
191 default: assert(0 && "Unknown type qualifier!");
192 case TQ_const: TQ_constLoc = Loc; break;
193 case TQ_restrict: TQ_restrictLoc = Loc; break;
194 case TQ_volatile: TQ_volatileLoc = Loc; break;
195 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000196 return false;
197}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000198
Chris Lattner1b22eed2006-11-28 05:12:07 +0000199bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
Chris Lattnera925dc62006-11-28 04:33:46 +0000200 // 'inline inline' is ok.
201 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000202 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000203 return false;
204}
205
206
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000207/// Finish - This does final analysis of the declspec, rejecting things like
208/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
209/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
210/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000211void DeclSpec::Finish(Diagnostic &D, const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000212 // Check the type specifier components first.
213
214 // signed/unsigned are only valid with int/char.
Chris Lattner839713c2006-08-04 06:15:52 +0000215 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000216 if (TypeSpecType == TST_unspecified)
217 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
218 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000219 D.Report(TSSLoc, diag::err_invalid_sign_spec,
220 getSpecifierName(TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000221 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000222 TypeSpecSign = TSS_unspecified;
223 }
224 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000225
Chris Lattner839713c2006-08-04 06:15:52 +0000226 // Validate the width of the type.
227 switch (TypeSpecWidth) {
228 case TSW_unspecified: break;
229 case TSW_short: // short int
230 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000231 if (TypeSpecType == TST_unspecified)
232 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
233 else if (TypeSpecType != TST_int) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000234 D.Report(TSWLoc,
235 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
236 : diag::err_invalid_longlong_spec,
Chris Lattner839713c2006-08-04 06:15:52 +0000237 getSpecifierName(TypeSpecType));
238 TypeSpecType = TST_int;
239 }
240 break;
241 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000242 if (TypeSpecType == TST_unspecified)
243 TypeSpecType = TST_int; // long -> long int.
244 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000245 D.Report(TSWLoc, diag::err_invalid_long_spec,
Chris Lattner839713c2006-08-04 06:15:52 +0000246 getSpecifierName(TypeSpecType));
247 TypeSpecType = TST_int;
248 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000249 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000250 }
251
Chris Lattner0e894622006-08-13 19:58:17 +0000252 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000253 // disallow their use. Need information about the backend.
254 if (TypeSpecComplex != TSC_unspecified) {
255 if (TypeSpecType == TST_unspecified) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000256 D.Report(TSCLoc, diag::ext_plain_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000257 TypeSpecType = TST_double; // _Complex -> _Complex double.
258 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000259 // Note that this intentionally doesn't include _Complex _Bool.
Chris Lattnerb20e8942006-11-28 05:30:29 +0000260 D.Report(TSTLoc, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000261 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Chris Lattnerb20e8942006-11-28 05:30:29 +0000262 D.Report(TSCLoc, diag::err_invalid_complex_spec,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000263 getSpecifierName(TypeSpecType));
264 TypeSpecComplex = TSC_unspecified;
265 }
266 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000267
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000268 // Verify __thread.
269 if (SCS_thread_specified) {
270 if (StorageClassSpec == SCS_unspecified) {
271 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
272 } else if (StorageClassSpec != SCS_extern &&
273 StorageClassSpec != SCS_static) {
Chris Lattner4d8f8732006-11-28 05:05:08 +0000274 D.Report(getStorageClassSpecLoc(), diag::err_invalid_thread_spec,
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000275 getSpecifierName(StorageClassSpec));
276 SCS_thread_specified = false;
277 }
278 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000279
280 // Okay, now we can infer the real type.
Chris Lattner0e894622006-08-13 19:58:17 +0000281 // TODO: infer real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000282
Chris Lattner0e894622006-08-13 19:58:17 +0000283 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000284
Chris Lattner1ae23292006-08-04 06:42:31 +0000285 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000286}