blob: 117ff12e597f424421d7849df5aab5cc1a651f57 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
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//
10// This file implements semantic analysis for declaration specifiers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/DeclSpec.h"
15#include "clang/Basic/LangOptions.h"
16#include "clang/Basic/SourceLocation.h"
17using namespace clang;
18
19/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
20///
21unsigned DeclSpec::getParsedSpecifiers() const {
22 unsigned Res = 0;
23 if (StorageClassSpec != SCS_unspecified ||
24 SCS_thread_specified)
25 Res |= PQ_StorageClassSpecifier;
26
27 if (TypeQualifiers != TQ_unspecified)
28 Res |= PQ_TypeQualifier;
29
30 if (hasTypeSpecifier())
31 Res |= PQ_TypeSpecifier;
32
33 if (FS_inline_specified)
34 Res |= PQ_FunctionSpecifier;
35 return Res;
36}
37
38const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
39 switch (S) {
40 default: assert(0 && "Unknown typespec!");
41 case DeclSpec::SCS_unspecified: return "unspecified";
42 case DeclSpec::SCS_typedef: return "typedef";
43 case DeclSpec::SCS_extern: return "extern";
44 case DeclSpec::SCS_static: return "static";
45 case DeclSpec::SCS_auto: return "auto";
46 case DeclSpec::SCS_register: return "register";
47 }
48}
49
50static bool BadSpecifier(DeclSpec::SCS S, const char *&PrevSpec) {
51 PrevSpec = DeclSpec::getSpecifierName(S);
52 return true;
53}
54
55static bool BadSpecifier(DeclSpec::TSW W, const char *&PrevSpec) {
56 switch (W) {
57 case DeclSpec::TSW_unspecified: PrevSpec = "unspecified"; break;
58 case DeclSpec::TSW_short: PrevSpec = "short"; break;
59 case DeclSpec::TSW_long: PrevSpec = "long"; break;
60 case DeclSpec::TSW_longlong: PrevSpec = "long long"; break;
61 }
62 return true;
63}
64
65static bool BadSpecifier(DeclSpec::TSC C, const char *&PrevSpec) {
66 switch (C) {
67 case DeclSpec::TSC_unspecified: PrevSpec = "unspecified"; break;
68 case DeclSpec::TSC_imaginary: PrevSpec = "imaginary"; break;
69 case DeclSpec::TSC_complex: PrevSpec = "complex"; break;
70 }
71 return true;
72}
73
74
75static bool BadSpecifier(DeclSpec::TSS S, const char *&PrevSpec) {
76 switch (S) {
77 case DeclSpec::TSS_unspecified: PrevSpec = "unspecified"; break;
78 case DeclSpec::TSS_signed: PrevSpec = "signed"; break;
79 case DeclSpec::TSS_unsigned: PrevSpec = "unsigned"; break;
80 }
81 return true;
82}
83
84const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
85 switch (T) {
86 default: assert(0 && "Unknown typespec!");
87 case DeclSpec::TST_unspecified: return "unspecified";
88 case DeclSpec::TST_void: return "void";
89 case DeclSpec::TST_char: return "char";
90 case DeclSpec::TST_int: return "int";
91 case DeclSpec::TST_float: return "float";
92 case DeclSpec::TST_double: return "double";
93 case DeclSpec::TST_bool: return "_Bool";
94 case DeclSpec::TST_decimal32: return "_Decimal32";
95 case DeclSpec::TST_decimal64: return "_Decimal64";
96 case DeclSpec::TST_decimal128: return "_Decimal128";
97 case DeclSpec::TST_enum: return "enum";
98 case DeclSpec::TST_union: return "union";
99 case DeclSpec::TST_struct: return "struct";
100 case DeclSpec::TST_typedef: return "typedef";
101 }
102}
103
104static bool BadSpecifier(DeclSpec::TST T, const char *&PrevSpec) {
105 PrevSpec = DeclSpec::getSpecifierName(T);
106 return true;
107}
108
109static bool BadSpecifier(DeclSpec::TQ T, const char *&PrevSpec) {
110 switch (T) {
111 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
112 case DeclSpec::TQ_const: PrevSpec = "const"; break;
113 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
114 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
115 }
116 return true;
117}
118
119bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
120 const char *&PrevSpec) {
121 if (StorageClassSpec != SCS_unspecified)
122 return BadSpecifier(StorageClassSpec, PrevSpec);
123 StorageClassSpec = S;
124 StorageClassSpecLoc = Loc;
125 return false;
126}
127
128bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
129 const char *&PrevSpec) {
130 if (SCS_thread_specified) {
131 PrevSpec = "__thread";
132 return true;
133 }
134 SCS_thread_specified = true;
135 SCS_threadLoc = Loc;
136 return false;
137}
138
139
140/// These methods set the specified attribute of the DeclSpec, but return true
141/// and ignore the request if invalid (e.g. "extern" then "auto" is
142/// specified).
143bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
144 const char *&PrevSpec) {
145 if (TypeSpecWidth != TSW_unspecified &&
146 // Allow turning long -> long long.
147 (W != TSW_longlong || TypeSpecWidth != TSW_long))
148 return BadSpecifier(TypeSpecWidth, PrevSpec);
149 TypeSpecWidth = W;
150 TSWLoc = Loc;
151 return false;
152}
153
154bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
155 const char *&PrevSpec) {
156 if (TypeSpecComplex != TSC_unspecified)
157 return BadSpecifier(TypeSpecComplex, PrevSpec);
158 TypeSpecComplex = C;
159 TSCLoc = Loc;
160 return false;
161}
162
163bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
164 const char *&PrevSpec) {
165 if (TypeSpecSign != TSS_unspecified)
166 return BadSpecifier(TypeSpecSign, PrevSpec);
167 TypeSpecSign = S;
168 TSSLoc = Loc;
169 return false;
170}
171
172bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
173 const char *&PrevSpec, void *Rep) {
174 if (TypeSpecType != TST_unspecified)
175 return BadSpecifier(TypeSpecType, PrevSpec);
176 TypeSpecType = T;
177 TypeRep = Rep;
178 TSTLoc = Loc;
179 return false;
180}
181
182bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
183 const LangOptions &Lang) {
184 // Duplicates turn into warnings pre-C99.
185 if ((TypeQualifiers & T) && !Lang.C99)
186 return BadSpecifier(T, PrevSpec);
187 TypeQualifiers |= T;
188
189 switch (T) {
190 default: assert(0 && "Unknown type qualifier!");
191 case TQ_const: TQ_constLoc = Loc; break;
192 case TQ_restrict: TQ_restrictLoc = Loc; break;
193 case TQ_volatile: TQ_volatileLoc = Loc; break;
194 }
195 return false;
196}
197
198bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
199 // 'inline inline' is ok.
200 FS_inline_specified = true;
201 FS_inlineLoc = Loc;
202 return false;
203}
204
205
206/// Finish - This does final analysis of the declspec, rejecting things like
207/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
208/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
209/// DeclSpec is guaranteed self-consistent, even if an error occurred.
210void DeclSpec::Finish(Diagnostic &D, const LangOptions &Lang) {
211 // Check the type specifier components first.
212
213 // signed/unsigned are only valid with int/char.
214 if (TypeSpecSign != TSS_unspecified) {
215 if (TypeSpecType == TST_unspecified)
216 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
217 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
218 Diag(D, TSSLoc, diag::err_invalid_sign_spec,
219 getSpecifierName(TypeSpecType));
220 // signed double -> double.
221 TypeSpecSign = TSS_unspecified;
222 }
223 }
224
225 // Validate the width of the type.
226 switch (TypeSpecWidth) {
227 case TSW_unspecified: break;
228 case TSW_short: // short int
229 case TSW_longlong: // long long int
230 if (TypeSpecType == TST_unspecified)
231 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
232 else if (TypeSpecType != TST_int) {
233 Diag(D, TSWLoc,
234 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
235 : diag::err_invalid_longlong_spec,
236 getSpecifierName(TypeSpecType));
237 TypeSpecType = TST_int;
238 }
239 break;
240 case TSW_long: // long double, long int
241 if (TypeSpecType == TST_unspecified)
242 TypeSpecType = TST_int; // long -> long int.
243 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
244 Diag(D, TSWLoc, diag::err_invalid_long_spec,
245 getSpecifierName(TypeSpecType));
246 TypeSpecType = TST_int;
247 }
248 break;
249 }
250
251 // TODO: if the implementation does not implement _Complex or _Imaginary,
252 // disallow their use. Need information about the backend.
253 if (TypeSpecComplex != TSC_unspecified) {
254 if (TypeSpecType == TST_unspecified) {
255 Diag(D, TSCLoc, diag::ext_plain_complex);
256 TypeSpecType = TST_double; // _Complex -> _Complex double.
257 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
258 // Note that this intentionally doesn't include _Complex _Bool.
259 Diag(D, TSTLoc, diag::ext_integer_complex);
260 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
261 Diag(D, TSCLoc, diag::err_invalid_complex_spec,
262 getSpecifierName(TypeSpecType));
263 TypeSpecComplex = TSC_unspecified;
264 }
265 }
266
267 // Verify __thread.
268 if (SCS_thread_specified) {
269 if (StorageClassSpec == SCS_unspecified) {
270 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
271 } else if (StorageClassSpec != SCS_extern &&
272 StorageClassSpec != SCS_static) {
273 Diag(D, getStorageClassSpecLoc(), diag::err_invalid_thread_spec,
274 getSpecifierName(StorageClassSpec));
275 SCS_thread_specified = false;
276 }
277 }
278
279 // Okay, now we can infer the real type.
280 // TODO: infer real type.
281
282 // TODO: return "auto function" and other bad things based on the real type.
283
284 // 'data definition has no type or storage class'?
285}