blob: 887839eb61635f2aa235f819efbba4670b55f28a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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;
Mike Stumpd4204332008-06-19 19:52:46 +000026
Reid Spencer5f016e22007-07-11 17:01:13 +000027 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
Steve Naroff95324142008-02-12 04:08:59 +000050bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +000051 PrevSpec = getSpecifierName(S);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 return true;
53}
54
Steve Naroff95324142008-02-12 04:08:59 +000055bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000056 switch (W) {
Steve Naroff95324142008-02-12 04:08:59 +000057 case TSW_unspecified: PrevSpec = "unspecified"; break;
58 case TSW_short: PrevSpec = "short"; break;
59 case TSW_long: PrevSpec = "long"; break;
60 case TSW_longlong: PrevSpec = "long long"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 }
62 return true;
63}
64
Steve Naroff95324142008-02-12 04:08:59 +000065bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000066 switch (C) {
Steve Naroff95324142008-02-12 04:08:59 +000067 case TSC_unspecified: PrevSpec = "unspecified"; break;
68 case TSC_imaginary: PrevSpec = "imaginary"; break;
69 case TSC_complex: PrevSpec = "complex"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
71 return true;
72}
73
74
Steve Naroff95324142008-02-12 04:08:59 +000075bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +000076 switch (S) {
Steve Naroff95324142008-02-12 04:08:59 +000077 case TSS_unspecified: PrevSpec = "unspecified"; break;
78 case TSS_signed: PrevSpec = "signed"; break;
79 case TSS_unsigned: PrevSpec = "unsigned"; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000080 }
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";
Chris Lattner99dc9142008-04-13 18:59:07 +000098 case DeclSpec::TST_class: return "class";
Reid Spencer5f016e22007-07-11 17:01:13 +000099 case DeclSpec::TST_union: return "union";
100 case DeclSpec::TST_struct: return "struct";
101 case DeclSpec::TST_typedef: return "typedef";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000102 case DeclSpec::TST_typeofType:
103 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 }
105}
106
Steve Naroff95324142008-02-12 04:08:59 +0000107bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff95324142008-02-12 04:08:59 +0000108 PrevSpec = getSpecifierName(T);
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 return true;
110}
111
Steve Naroff95324142008-02-12 04:08:59 +0000112bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 switch (T) {
114 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
115 case DeclSpec::TQ_const: PrevSpec = "const"; break;
116 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
117 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
118 }
119 return true;
120}
121
122bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
123 const char *&PrevSpec) {
124 if (StorageClassSpec != SCS_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000125 return BadSpecifier( (SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 StorageClassSpec = S;
127 StorageClassSpecLoc = Loc;
128 return false;
129}
130
131bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
132 const char *&PrevSpec) {
133 if (SCS_thread_specified) {
134 PrevSpec = "__thread";
135 return true;
136 }
137 SCS_thread_specified = true;
138 SCS_threadLoc = Loc;
139 return false;
140}
141
142
143/// These methods set the specified attribute of the DeclSpec, but return true
144/// and ignore the request if invalid (e.g. "extern" then "auto" is
145/// specified).
146bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
147 const char *&PrevSpec) {
148 if (TypeSpecWidth != TSW_unspecified &&
149 // Allow turning long -> long long.
150 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000151 return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 TypeSpecWidth = W;
153 TSWLoc = Loc;
154 return false;
155}
156
157bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
158 const char *&PrevSpec) {
159 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000160 return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 TypeSpecComplex = C;
162 TSCLoc = Loc;
163 return false;
164}
165
166bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
167 const char *&PrevSpec) {
168 if (TypeSpecSign != TSS_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000169 return BadSpecifier( (TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 TypeSpecSign = S;
171 TSSLoc = Loc;
172 return false;
173}
174
175bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
176 const char *&PrevSpec, void *Rep) {
177 if (TypeSpecType != TST_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000178 return BadSpecifier( (TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 TypeSpecType = T;
180 TypeRep = Rep;
181 TSTLoc = Loc;
182 return false;
183}
184
185bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
186 const LangOptions &Lang) {
187 // Duplicates turn into warnings pre-C99.
188 if ((TypeQualifiers & T) && !Lang.C99)
189 return BadSpecifier(T, PrevSpec);
190 TypeQualifiers |= T;
191
192 switch (T) {
193 default: assert(0 && "Unknown type qualifier!");
194 case TQ_const: TQ_constLoc = Loc; break;
195 case TQ_restrict: TQ_restrictLoc = Loc; break;
196 case TQ_volatile: TQ_volatileLoc = Loc; break;
197 }
198 return false;
199}
200
201bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
202 // 'inline inline' is ok.
203 FS_inline_specified = true;
204 FS_inlineLoc = Loc;
205 return false;
206}
207
208
209/// Finish - This does final analysis of the declspec, rejecting things like
210/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
211/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
212/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000213void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
214 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 // Check the type specifier components first.
216
217 // signed/unsigned are only valid with int/char.
218 if (TypeSpecSign != TSS_unspecified) {
219 if (TypeSpecType == TST_unspecified)
220 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
221 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000222 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000223 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // signed double -> double.
225 TypeSpecSign = TSS_unspecified;
226 }
227 }
228
229 // Validate the width of the type.
230 switch (TypeSpecWidth) {
231 case TSW_unspecified: break;
232 case TSW_short: // short int
233 case TSW_longlong: // long long int
234 if (TypeSpecType == TST_unspecified)
235 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
236 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000237 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
239 : diag::err_invalid_longlong_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000240 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 TypeSpecType = TST_int;
242 }
243 break;
244 case TSW_long: // long double, long int
245 if (TypeSpecType == TST_unspecified)
246 TypeSpecType = TST_int; // long -> long int.
247 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000248 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000249 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 TypeSpecType = TST_int;
251 }
252 break;
253 }
254
255 // TODO: if the implementation does not implement _Complex or _Imaginary,
256 // disallow their use. Need information about the backend.
257 if (TypeSpecComplex != TSC_unspecified) {
258 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000259 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 TypeSpecType = TST_double; // _Complex -> _Complex double.
261 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
262 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000263 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000265 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000266 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 TypeSpecComplex = TSC_unspecified;
268 }
269 }
270
271 // Verify __thread.
272 if (SCS_thread_specified) {
273 if (StorageClassSpec == SCS_unspecified) {
274 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
275 } else if (StorageClassSpec != SCS_extern &&
276 StorageClassSpec != SCS_static) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000277 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000278 getSpecifierName( (SCS)StorageClassSpec));
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 SCS_thread_specified = false;
280 }
281 }
282
283 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000284
285 // TODO: return "auto function" and other bad things based on the real type.
286
287 // 'data definition has no type or storage class'?
288}