blob: 5c26ec6857c409f2947e972213cb41f746cb5b4e [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"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/LangOptions.h"
17#include "clang/Basic/SourceLocation.h"
18using namespace clang;
19
20/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
21///
22unsigned DeclSpec::getParsedSpecifiers() const {
23 unsigned Res = 0;
24 if (StorageClassSpec != SCS_unspecified ||
25 SCS_thread_specified)
26 Res |= PQ_StorageClassSpecifier;
Mike Stumpea5a8b32008-06-19 19:52:46 +000027
Chris Lattner4b009652007-07-25 00:24:17 +000028 if (TypeQualifiers != TQ_unspecified)
29 Res |= PQ_TypeQualifier;
30
31 if (hasTypeSpecifier())
32 Res |= PQ_TypeSpecifier;
33
34 if (FS_inline_specified)
35 Res |= PQ_FunctionSpecifier;
36 return Res;
37}
38
39const char *DeclSpec::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
Steve Naroff3cd7fc32008-02-12 04:08:59 +000051bool DeclSpec::BadSpecifier(SCS S, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +000052 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) {
Chris Lattner4b009652007-07-25 00:24:17 +000057 switch (W) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +000058 case TSW_unspecified: PrevSpec = "unspecified"; break;
59 case TSW_short: PrevSpec = "short"; break;
60 case TSW_long: PrevSpec = "long"; break;
61 case TSW_longlong: PrevSpec = "long long"; break;
Chris Lattner4b009652007-07-25 00:24:17 +000062 }
63 return true;
64}
65
Steve Naroff3cd7fc32008-02-12 04:08:59 +000066bool DeclSpec::BadSpecifier(TSC C, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +000067 switch (C) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +000068 case TSC_unspecified: PrevSpec = "unspecified"; break;
69 case TSC_imaginary: PrevSpec = "imaginary"; break;
70 case TSC_complex: PrevSpec = "complex"; break;
Chris Lattner4b009652007-07-25 00:24:17 +000071 }
72 return true;
73}
74
75
Steve Naroff3cd7fc32008-02-12 04:08:59 +000076bool DeclSpec::BadSpecifier(TSS S, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +000077 switch (S) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +000078 case TSS_unspecified: PrevSpec = "unspecified"; break;
79 case TSS_signed: PrevSpec = "signed"; break;
80 case TSS_unsigned: PrevSpec = "unsigned"; break;
Chris Lattner4b009652007-07-25 00:24:17 +000081 }
82 return true;
83}
84
85const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
86 switch (T) {
87 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";
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000091 case DeclSpec::TST_wchar: return "wchar_t";
Chris Lattner4b009652007-07-25 00:24:17 +000092 case DeclSpec::TST_int: return "int";
93 case DeclSpec::TST_float: return "float";
94 case DeclSpec::TST_double: return "double";
95 case DeclSpec::TST_bool: return "_Bool";
96 case DeclSpec::TST_decimal32: return "_Decimal32";
97 case DeclSpec::TST_decimal64: return "_Decimal64";
98 case DeclSpec::TST_decimal128: return "_Decimal128";
99 case DeclSpec::TST_enum: return "enum";
Chris Lattner2e78db32008-04-13 18:59:07 +0000100 case DeclSpec::TST_class: return "class";
Chris Lattner4b009652007-07-25 00:24:17 +0000101 case DeclSpec::TST_union: return "union";
102 case DeclSpec::TST_struct: return "struct";
103 case DeclSpec::TST_typedef: return "typedef";
Steve Naroff7cbb1462007-07-31 12:34:36 +0000104 case DeclSpec::TST_typeofType:
105 case DeclSpec::TST_typeofExpr: return "typeof";
Chris Lattner4b009652007-07-25 00:24:17 +0000106 }
107}
108
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000109bool DeclSpec::BadSpecifier(TST T, const char *&PrevSpec) {
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000110 PrevSpec = getSpecifierName(T);
Chris Lattner4b009652007-07-25 00:24:17 +0000111 return true;
112}
113
Steve Naroff3cd7fc32008-02-12 04:08:59 +0000114bool DeclSpec::BadSpecifier(TQ T, const char *&PrevSpec) {
Chris Lattner4b009652007-07-25 00:24:17 +0000115 switch (T) {
116 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
117 case DeclSpec::TQ_const: PrevSpec = "const"; break;
118 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
119 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
120 }
121 return true;
122}
123
124bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
125 const char *&PrevSpec) {
126 if (StorageClassSpec != SCS_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000127 return BadSpecifier( (SCS)StorageClassSpec, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000128 StorageClassSpec = S;
129 StorageClassSpecLoc = Loc;
130 return false;
131}
132
133bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
134 const char *&PrevSpec) {
135 if (SCS_thread_specified) {
136 PrevSpec = "__thread";
137 return true;
138 }
139 SCS_thread_specified = true;
140 SCS_threadLoc = Loc;
141 return false;
142}
143
144
145/// These methods set the specified attribute of the DeclSpec, but return true
146/// and ignore the request if invalid (e.g. "extern" then "auto" is
147/// specified).
148bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
149 const char *&PrevSpec) {
150 if (TypeSpecWidth != TSW_unspecified &&
151 // Allow turning long -> long long.
152 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattnerbb95a382007-12-02 00:47:03 +0000153 return BadSpecifier( (TSW)TypeSpecWidth, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000154 TypeSpecWidth = W;
155 TSWLoc = Loc;
156 return false;
157}
158
159bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
160 const char *&PrevSpec) {
161 if (TypeSpecComplex != TSC_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000162 return BadSpecifier( (TSC)TypeSpecComplex, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000163 TypeSpecComplex = C;
164 TSCLoc = Loc;
165 return false;
166}
167
168bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
169 const char *&PrevSpec) {
170 if (TypeSpecSign != TSS_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000171 return BadSpecifier( (TSS)TypeSpecSign, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000172 TypeSpecSign = S;
173 TSSLoc = Loc;
174 return false;
175}
176
177bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000178 const char *&PrevSpec, Action::TypeTy *Rep) {
Chris Lattner4b009652007-07-25 00:24:17 +0000179 if (TypeSpecType != TST_unspecified)
Chris Lattnerbb95a382007-12-02 00:47:03 +0000180 return BadSpecifier( (TST)TypeSpecType, PrevSpec);
Chris Lattner4b009652007-07-25 00:24:17 +0000181 TypeSpecType = T;
182 TypeRep = Rep;
183 TSTLoc = Loc;
184 return false;
185}
186
187bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
188 const LangOptions &Lang) {
189 // Duplicates turn into warnings pre-C99.
190 if ((TypeQualifiers & T) && !Lang.C99)
191 return BadSpecifier(T, PrevSpec);
192 TypeQualifiers |= T;
193
194 switch (T) {
195 default: assert(0 && "Unknown type qualifier!");
196 case TQ_const: TQ_constLoc = Loc; break;
197 case TQ_restrict: TQ_restrictLoc = Loc; break;
198 case TQ_volatile: TQ_volatileLoc = Loc; break;
199 }
200 return false;
201}
202
203bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
204 // 'inline inline' is ok.
205 FS_inline_specified = true;
206 FS_inlineLoc = Loc;
207 return false;
208}
209
210
211/// Finish - This does final analysis of the declspec, rejecting things like
212/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
213/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
214/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000215void DeclSpec::Finish(Diagnostic &D, SourceManager& SrcMgr,
216 const LangOptions &Lang) {
Chris Lattner4b009652007-07-25 00:24:17 +0000217 // Check the type specifier components first.
218
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000219 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner4b009652007-07-25 00:24:17 +0000220 if (TypeSpecSign != TSS_unspecified) {
221 if (TypeSpecType == TST_unspecified)
222 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000223 else if (TypeSpecType != TST_int &&
224 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000225 Diag(D, TSSLoc, SrcMgr, diag::err_invalid_sign_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000226 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000227 // signed double -> double.
228 TypeSpecSign = TSS_unspecified;
229 }
230 }
231
232 // Validate the width of the type.
233 switch (TypeSpecWidth) {
234 case TSW_unspecified: break;
235 case TSW_short: // short int
236 case TSW_longlong: // long long int
237 if (TypeSpecType == TST_unspecified)
238 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
239 else if (TypeSpecType != TST_int) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000240 Diag(D, TSWLoc, SrcMgr,
Chris Lattner4b009652007-07-25 00:24:17 +0000241 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
242 : diag::err_invalid_longlong_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000243 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000244 TypeSpecType = TST_int;
245 }
246 break;
247 case TSW_long: // long double, long int
248 if (TypeSpecType == TST_unspecified)
249 TypeSpecType = TST_int; // long -> long int.
250 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000251 Diag(D, TSWLoc, SrcMgr, diag::err_invalid_long_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000252 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000253 TypeSpecType = TST_int;
254 }
255 break;
256 }
257
258 // TODO: if the implementation does not implement _Complex or _Imaginary,
259 // disallow their use. Need information about the backend.
260 if (TypeSpecComplex != TSC_unspecified) {
261 if (TypeSpecType == TST_unspecified) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000262 Diag(D, TSCLoc, SrcMgr, diag::ext_plain_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000263 TypeSpecType = TST_double; // _Complex -> _Complex double.
264 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
265 // Note that this intentionally doesn't include _Complex _Bool.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000266 Diag(D, TSTLoc, SrcMgr, diag::ext_integer_complex);
Chris Lattner4b009652007-07-25 00:24:17 +0000267 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000268 Diag(D, TSCLoc, SrcMgr, diag::err_invalid_complex_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000269 getSpecifierName( (TST)TypeSpecType));
Chris Lattner4b009652007-07-25 00:24:17 +0000270 TypeSpecComplex = TSC_unspecified;
271 }
272 }
273
274 // Verify __thread.
275 if (SCS_thread_specified) {
276 if (StorageClassSpec == SCS_unspecified) {
277 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
278 } else if (StorageClassSpec != SCS_extern &&
279 StorageClassSpec != SCS_static) {
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000280 Diag(D, getStorageClassSpecLoc(), SrcMgr, diag::err_invalid_thread_spec,
Chris Lattnerbb95a382007-12-02 00:47:03 +0000281 getSpecifierName( (SCS)StorageClassSpec));
Chris Lattner4b009652007-07-25 00:24:17 +0000282 SCS_thread_specified = false;
283 }
284 }
285
286 // Okay, now we can infer the real type.
287
288 // TODO: return "auto function" and other bad things based on the real type.
289
290 // 'data definition has no type or storage class'?
291}
Daniel Dunbarcc7b1602008-08-11 03:45:03 +0000292
293void DeclSpec::Diag(Diagnostic &D, SourceLocation Loc, SourceManager& SrcMgr,
294 unsigned DiagID) {
295 D.Report(FullSourceLoc(Loc,SrcMgr), DiagID);
296}
297
298void DeclSpec::Diag(Diagnostic &D, SourceLocation Loc, SourceManager& SrcMgr,
299 unsigned DiagID, const std::string &info) {
300 D.Report(FullSourceLoc(Loc,SrcMgr), DiagID, &info, 1);
301}