blob: 8f9a5fd79195cae3bc07e7c9530512c81ede996b [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +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;
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
Steve Naroff3cd7fc32008-02-12 04:08:59 +000050bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
51 Invalid = true;
52 PrevSpec = getSpecifierName(S);
Chris Lattner4b009652007-07-25 00:24:17 +000053 return true;
54}
55
Steve Naroff3cd7fc32008-02-12 04:08:59 +000056bool DeclSpec::BadSpecifier(TSW W, const char *&PrevSpec) {
57 Invalid = true;
Chris Lattner4b009652007-07-25 00:24:17 +000058 switch (W) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +000059 case TSW_unspecified: PrevSpec = "unspecified"; break;
60 case TSW_short: PrevSpec = "short"; break;
61 case TSW_long: PrevSpec = "long"; break;
62 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattner4b009652007-07-25 00:24:17 +000063 }
64 return true;
65}
66
Steve Naroff3cd7fc32008-02-12 04:08:59 +000067bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
68 Invalid = true;
Chris Lattner4b009652007-07-25 00:24:17 +000069 switch (C) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +000070 case TSC_unspecified: PrevSpec = "unspecified"; break;
71 case TSC_imaginary: PrevSpec = "imaginary"; break;
72 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattner4b009652007-07-25 00:24:17 +000073 }
74 return true;
75}
76
77
Steve Naroff3cd7fc32008-02-12 04:08:59 +000078bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
79 Invalid = true;
Chris Lattner4b009652007-07-25 00:24:17 +000080 switch (S) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +000081 case TSS_unspecified: PrevSpec = "unspecified"; break;
82 case TSS_signed: PrevSpec = "signed"; break;
83 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattner4b009652007-07-25 00:24:17 +000084 }
85 return true;
86}
87
88const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
89 switch (T) {
90 default: assert(0 && "Unknown typespec!");
91 case DeclSpec::TST_unspecified: return "unspecified";
92 case DeclSpec::TST_void: return "void";
93 case DeclSpec::TST_char: return "char";
94 case DeclSpec::TST_int: return "int";
95 case DeclSpec::TST_float: return "float";
96 case DeclSpec::TST_double: return "double";
97 case DeclSpec::TST_bool: return "_Bool";
98 case DeclSpec::TST_decimal32: return "_Decimal32";
99 case DeclSpec::TST_decimal64: return "_Decimal64";
100 case DeclSpec::TST_decimal128: return "_Decimal128";
101 case DeclSpec::TST_enum: return "enum";
102 case DeclSpec::TST_union: return "union";
103 case DeclSpec::TST_struct: return "struct";
104 case DeclSpec::TST_typedef: return "typedef";
Steve Naroff7cbb1462007-07-31 12:34:36 +0000105 case DeclSpec::TST_typeofType:
106 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattner4b009652007-07-25 00:24:17 +0000107 }
108}
109
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000110bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
111 Invalid = true;
112 PrevSpec = getSpecifierName(T);
Chris Lattner4b009652007-07-25 00:24:17 +0000113 return true;
114}
115
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000116bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
117 Invalid = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000118 switch (T) {
119 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
120 case DeclSpec::TQ_const: PrevSpec = "const"; break;
121 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
122 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
123 }
124 return true;
125}
126
127bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
128 const char *&PrevSpec) {
129 if (StorageClassSpec != SCS_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000130 return BadSpecifier( (SCS)StorageClassSpec, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000131 StorageClassSpec = S;
132 StorageClassSpecLoc = Loc;
133 return false;
134}
135
136bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
137 const char *&PrevSpec) {
138 if (SCS_thread_specified) {
139 PrevSpec = "__thread";
140 return true;
141 }
142 SCS_thread_specified = true;
143 SCS_threadLoc = Loc;
144 return false;
145}
146
147
148/// These methods set the specified attribute of the DeclSpec, but return true
149/// and ignore the request if invalid (e.g. "extern" then "auto" is
150/// specified).
151bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
152 const char *&PrevSpec) {
153 if (TypeSpecWidth != TSW_unspecified &&
154 // Allow turning long -> long long.
155 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattnerbb95a382007-12-02 00:47:03 +0000156 return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000157 TypeSpecWidth = W;
158 TSWLoc = Loc;
159 return false;
160}
161
162bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
163 const char *&PrevSpec) {
164 if (TypeSpecComplex != TSC_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000165 return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000166 TypeSpecComplex = C;
167 TSCLoc = Loc;
168 return false;
169}
170
171bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
172 const char *&PrevSpec) {
173 if (TypeSpecSign != TSS_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000174 return BadSpecifier( (TSS)TypeSpecSign, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000175 TypeSpecSign = S;
176 TSSLoc = Loc;
177 return false;
178}
179
180bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
181 const char *&PrevSpec, void *Rep) {
182 if (TypeSpecType != TST_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000183 return BadSpecifier( (TST)TypeSpecType, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000184 TypeSpecType = T;
185 TypeRep = Rep;
186 TSTLoc = Loc;
187 return false;
188}
189
190bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
191 const LangOptions &Lang) {
192 // Duplicates turn into warnings pre-C99.
193 if ((TypeQualifiers & T) && !Lang.C99)
194 return BadSpecifier(T, PrevSpec);
195 TypeQualifiers |= T;
196
197 switch (T) {
198 default: assert(0 && "Unknown type qualifier!");
199 case TQ_const: TQ_constLoc = Loc; break;
200 case TQ_restrict: TQ_restrictLoc = Loc; break;
201 case TQ_volatile: TQ_volatileLoc = Loc; break;
202 }
203 return false;
204}
205
206bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
207 // 'inline inline' is ok.
208 FS_inline_specified = true;
209 FS_inlineLoc = Loc;
210 return false;
211}
212
213
214/// Finish - This does final analysis of the declspec, rejecting things like
215/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
216/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
217/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000218void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
219 const LangOptions &Lang) {
Chris Lattner4b009652007-07-25 00:24:17 +0000220 // Check the type specifier components first.
221
222 // signed/unsigned are only valid with int/char.
223 if (TypeSpecSign != TSS_unspecified) {
224 if (TypeSpecType == TST_unspecified)
225 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
226 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000227 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000228 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000229 // signed double -> double.
230 TypeSpecSign = TSS_unspecified;
231 }
232 }
233
234 // Validate the width of the type.
235 switch (TypeSpecWidth) {
236 case TSW_unspecified: break;
237 case TSW_short: // short int
238 case TSW_longlong: // long long int
239 if (TypeSpecType == TST_unspecified)
240 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
241 else if (TypeSpecType != TST_int) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000242 Diag(D, TSWLoc, SrcMgr,
Chris Lattner4b009652007-07-25 00:24:17 +0000243 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
244 : diag::err_invalid_longlong_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000245 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000246 TypeSpecType = TST_int;
247 }
248 break;
249 case TSW_long: // long double, long int
250 if (TypeSpecType == TST_unspecified)
251 TypeSpecType = TST_int; // long -> long int.
252 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000253 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000254 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000255 TypeSpecType = TST_int;
256 }
257 break;
258 }
259
260 // TODO: if the implementation does not implement _Complex or _Imaginary,
261 // disallow their use. Need information about the backend.
262 if (TypeSpecComplex != TSC_unspecified) {
263 if (TypeSpecType == TST_unspecified) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000264 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000265 TypeSpecType = TST_double; // _Complex -> _Complex double.
266 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
267 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000268 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000269 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000270 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000271 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000272 TypeSpecComplex = TSC_unspecified;
273 }
274 }
275
276 // Verify __thread.
277 if (SCS_thread_specified) {
278 if (StorageClassSpec == SCS_unspecified) {
279 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
280 } else if (StorageClassSpec != SCS_extern &&
281 StorageClassSpec != SCS_static) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000282 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000283 getSpecifierName( (SCS)StorageClassSpec));
Chris Lattner4b009652007-07-25 00:24:17 +0000284 SCS_thread_specified = false;
285 }
286 }
287
288 // Okay, now we can infer the real type.
289
290 // TODO: return "auto function" and other bad things based on the real type.
291
292 // 'data definition has no type or storage class'?
293}