blob: b50c1a7958242206d4049513014991092f585408 [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";
Steve Naroffd1861fd2007-07-31 12:34:36 +0000101 case DeclSpec::TST_typeofType:
102 case DeclSpec::TST_typeofExpr: return "typeof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 }
104}
105
106static bool BadSpecifier(DeclSpec::TST T, const char *&PrevSpec) {
107 PrevSpec = DeclSpec::getSpecifierName(T);
108 return true;
109}
110
111static bool BadSpecifier(DeclSpec::TQ T, const char *&PrevSpec) {
112 switch (T) {
113 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
114 case DeclSpec::TQ_const: PrevSpec = "const"; break;
115 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
116 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
117 }
118 return true;
119}
120
121bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
122 const char *&PrevSpec) {
123 if (StorageClassSpec != SCS_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000124 return BadSpecifier( (SCS)StorageClassSpec, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 StorageClassSpec = S;
126 StorageClassSpecLoc = Loc;
127 return false;
128}
129
130bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
131 const char *&PrevSpec) {
132 if (SCS_thread_specified) {
133 PrevSpec = "__thread";
134 return true;
135 }
136 SCS_thread_specified = true;
137 SCS_threadLoc = Loc;
138 return false;
139}
140
141
142/// These methods set the specified attribute of the DeclSpec, but return true
143/// and ignore the request if invalid (e.g. "extern" then "auto" is
144/// specified).
145bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
146 const char *&PrevSpec) {
147 if (TypeSpecWidth != TSW_unspecified &&
148 // Allow turning long -> long long.
149 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000150 return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 TypeSpecWidth = W;
152 TSWLoc = Loc;
153 return false;
154}
155
156bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
157 const char *&PrevSpec) {
158 if (TypeSpecComplex != TSC_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000159 return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 TypeSpecComplex = C;
161 TSCLoc = Loc;
162 return false;
163}
164
165bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
166 const char *&PrevSpec) {
167 if (TypeSpecSign != TSS_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000168 return BadSpecifier( (TSS)TypeSpecSign, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 TypeSpecSign = S;
170 TSSLoc = Loc;
171 return false;
172}
173
174bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
175 const char *&PrevSpec, void *Rep) {
176 if (TypeSpecType != TST_unspecified)
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000177 return BadSpecifier( (TST)TypeSpecType, PrevSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 TypeSpecType = T;
179 TypeRep = Rep;
180 TSTLoc = Loc;
181 return false;
182}
183
184bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
185 const LangOptions &Lang) {
186 // Duplicates turn into warnings pre-C99.
187 if ((TypeQualifiers & T) && !Lang.C99)
188 return BadSpecifier(T, PrevSpec);
189 TypeQualifiers |= T;
190
191 switch (T) {
192 default: assert(0 && "Unknown type qualifier!");
193 case TQ_const: TQ_constLoc = Loc; break;
194 case TQ_restrict: TQ_restrictLoc = Loc; break;
195 case TQ_volatile: TQ_volatileLoc = Loc; break;
196 }
197 return false;
198}
199
200bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
201 // 'inline inline' is ok.
202 FS_inline_specified = true;
203 FS_inlineLoc = Loc;
204 return false;
205}
206
207
208/// Finish - This does final analysis of the declspec, rejecting things like
209/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
210/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
211/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000212void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
213 const LangOptions &Lang) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 // Check the type specifier components first.
215
216 // signed/unsigned are only valid with int/char.
217 if (TypeSpecSign != TSS_unspecified) {
218 if (TypeSpecType == TST_unspecified)
219 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
220 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000221 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000222 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 // signed double -> double.
224 TypeSpecSign = TSS_unspecified;
225 }
226 }
227
228 // Validate the width of the type.
229 switch (TypeSpecWidth) {
230 case TSW_unspecified: break;
231 case TSW_short: // short int
232 case TSW_longlong: // long long int
233 if (TypeSpecType == TST_unspecified)
234 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
235 else if (TypeSpecType != TST_int) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000236 Diag(D, TSWLoc, SrcMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
238 : diag::err_invalid_longlong_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000239 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 TypeSpecType = TST_int;
241 }
242 break;
243 case TSW_long: // long double, long int
244 if (TypeSpecType == TST_unspecified)
245 TypeSpecType = TST_int; // long -> long int.
246 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000247 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000248 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 TypeSpecType = TST_int;
250 }
251 break;
252 }
253
254 // TODO: if the implementation does not implement _Complex or _Imaginary,
255 // disallow their use. Need information about the backend.
256 if (TypeSpecComplex != TSC_unspecified) {
257 if (TypeSpecType == TST_unspecified) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000258 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000259 TypeSpecType = TST_double; // _Complex -> _Complex double.
260 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
261 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000262 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000264 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000265 getSpecifierName( (TST)TypeSpecType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 TypeSpecComplex = TSC_unspecified;
267 }
268 }
269
270 // Verify __thread.
271 if (SCS_thread_specified) {
272 if (StorageClassSpec == SCS_unspecified) {
273 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
274 } else if (StorageClassSpec != SCS_extern &&
275 StorageClassSpec != SCS_static) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000276 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec,
Chris Lattner2efcd2f2007-12-02 00:47:03 +0000277 getSpecifierName( (SCS)StorageClassSpec));
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 SCS_thread_specified = false;
279 }
280 }
281
282 // Okay, now we can infer the real type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
284 // TODO: return "auto function" and other bad things based on the real type.
285
286 // 'data definition has no type or storage class'?
287}