blob: 6fcfe0c9e9a36c5fd8aeca24c9125b5aed27109b [file] [log] [blame]
Chris Lattner289ab7b2006-11-08 06:54:53 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002//
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//
Chris Lattner289ab7b2006-11-08 06:54:53 +000010// This file implements semantic analysis for declaration specifiers.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner288e86ff12006-11-11 23:03:42 +000014#include "clang/Parse/DeclSpec.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000015#include "clang/Basic/LangOptions.h"
Chris Lattner839713c2006-08-04 06:15:52 +000016#include "clang/Basic/SourceLocation.h"
Chris Lattnerb9093cd2006-08-04 04:39:53 +000017using namespace llvm;
18using namespace clang;
19
20/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
21///
22unsigned DeclSpec::getParsedSpecifiers() const {
23 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +000024 if (StorageClassSpec != SCS_unspecified ||
25 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000026 Res |= PQ_StorageClassSpecifier;
27
Chris Lattner4d8f8732006-11-28 05:05:08 +000028 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000029 Res |= PQ_TypeQualifier;
30
Chris Lattnerf055d432006-11-28 04:28:12 +000031 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +000032 Res |= PQ_TypeSpecifier;
33
Chris Lattnerf63f89a2006-08-05 03:28:50 +000034 if (FS_inline_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000035 Res |= PQ_FunctionSpecifier;
36 return Res;
37}
38
Chris Lattnerf63f89a2006-08-05 03:28:50 +000039static const char *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
51static bool BadSpecifier(DeclSpec::SCS S, const char *&PrevSpec) {
52 PrevSpec = getSpecifierName(S);
53 return true;
54}
Chris Lattnerb9093cd2006-08-04 04:39:53 +000055
56static bool BadSpecifier(DeclSpec::TSW W, const char *&PrevSpec) {
57 switch (W) {
58 case DeclSpec::TSW_unspecified: PrevSpec = "unspecified"; break;
59 case DeclSpec::TSW_short: PrevSpec = "short"; break;
60 case DeclSpec::TSW_long: PrevSpec = "long"; break;
61 case DeclSpec::TSW_longlong: PrevSpec = "long long"; break;
62 }
63 return true;
64}
65
66static bool BadSpecifier(DeclSpec::TSC C, const char *&PrevSpec) {
67 switch (C) {
68 case DeclSpec::TSC_unspecified: PrevSpec = "unspecified"; break;
69 case DeclSpec::TSC_imaginary: PrevSpec = "imaginary"; break;
70 case DeclSpec::TSC_complex: PrevSpec = "complex"; break;
71 }
72 return true;
73}
74
75
76static bool BadSpecifier(DeclSpec::TSS S, const char *&PrevSpec) {
77 switch (S) {
78 case DeclSpec::TSS_unspecified: PrevSpec = "unspecified"; break;
79 case DeclSpec::TSS_signed: PrevSpec = "signed"; break;
80 case DeclSpec::TSS_unsigned: PrevSpec = "unsigned"; break;
81 }
82 return true;
83}
84
Chris Lattner839713c2006-08-04 06:15:52 +000085static const char *getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000086 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +000087 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";
91 case DeclSpec::TST_int: return "int";
92 case DeclSpec::TST_float: return "float";
93 case DeclSpec::TST_double: return "double";
94 case DeclSpec::TST_bool: return "_Bool";
95 case DeclSpec::TST_decimal32: return "_Decimal32";
96 case DeclSpec::TST_decimal64: return "_Decimal64";
97 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +000098 case DeclSpec::TST_enum: return "enum";
99 case DeclSpec::TST_union: return "union";
100 case DeclSpec::TST_struct: return "struct";
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000101 case DeclSpec::TST_typedef: return "typedef";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000102 }
Chris Lattner839713c2006-08-04 06:15:52 +0000103}
104
105static bool BadSpecifier(DeclSpec::TST T, const char *&PrevSpec) {
106 PrevSpec = getSpecifierName(T);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000107 return true;
108}
109
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000110static bool BadSpecifier(DeclSpec::TQ T, const char *&PrevSpec) {
111 switch (T) {
112 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
113 case DeclSpec::TQ_const: PrevSpec = "const"; break;
114 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
115 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
116 }
117 return true;
118}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000119
Chris Lattner4d8f8732006-11-28 05:05:08 +0000120bool DeclSpec::SetStorageClassSpec(SCS S, SourceLocation Loc,
121 const char *&PrevSpec) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000122 if (StorageClassSpec != SCS_unspecified)
123 return BadSpecifier(StorageClassSpec, PrevSpec);
124 StorageClassSpec = S;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000125 StorageClassSpecLoc = Loc;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000126 return false;
127}
128
Chris Lattner4d8f8732006-11-28 05:05:08 +0000129bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
130 const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000131 if (SCS_thread_specified) {
132 PrevSpec = "__thread";
133 return true;
134 }
135 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000136 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000137 return false;
138}
139
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000140
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000141/// These methods set the specified attribute of the DeclSpec, but return true
142/// and ignore the request if invalid (e.g. "extern" then "auto" is
143/// specified).
144bool DeclSpec::SetTypeSpecWidth(TSW W, const char *&PrevSpec) {
Chris Lattner353f5742006-11-28 04:50:12 +0000145 if (TypeSpecWidth != TSW_unspecified &&
146 // Allow turning long -> long long.
147 (W != TSW_longlong || TypeSpecWidth != TSW_long))
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000148 return BadSpecifier(TypeSpecWidth, PrevSpec);
149 TypeSpecWidth = W;
150 return false;
151}
152
153bool DeclSpec::SetTypeSpecComplex(TSC C, const char *&PrevSpec) {
154 if (TypeSpecComplex != TSC_unspecified)
155 return BadSpecifier(TypeSpecComplex, PrevSpec);
156 TypeSpecComplex = C;
157 return false;
158}
159
160bool DeclSpec::SetTypeSpecSign(TSS S, const char *&PrevSpec) {
161 if (TypeSpecSign != TSS_unspecified)
162 return BadSpecifier(TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000163 TypeSpecSign = S;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000164 return false;
165}
166
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000167bool DeclSpec::SetTypeSpecType(TST T, const char *&PrevSpec, void *TypeRep) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000168 if (TypeSpecType != TST_unspecified)
169 return BadSpecifier(TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000170 TypeSpecType = T;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000171 TypenameRep = TypeRep;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000172 return false;
173}
174
Chris Lattner60809f52006-11-28 05:18:46 +0000175bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000176 const LangOptions &Lang) {
177 // Duplicates turn into warnings pre-C99.
178 if ((TypeQualifiers & T) && !Lang.C99)
179 return BadSpecifier(T, PrevSpec);
180 TypeQualifiers |= T;
Chris Lattner60809f52006-11-28 05:18:46 +0000181
182 switch (T) {
183 default: assert(0 && "Unknown type qualifier!");
184 case TQ_const: TQ_constLoc = Loc; break;
185 case TQ_restrict: TQ_restrictLoc = Loc; break;
186 case TQ_volatile: TQ_volatileLoc = Loc; break;
187 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000188 return false;
189}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000190
Chris Lattner1b22eed2006-11-28 05:12:07 +0000191bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
Chris Lattnera925dc62006-11-28 04:33:46 +0000192 // 'inline inline' is ok.
193 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000194 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000195 return false;
196}
197
198
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000199/// Finish - This does final analysis of the declspec, rejecting things like
200/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
201/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
202/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Chris Lattner839713c2006-08-04 06:15:52 +0000203void DeclSpec::Finish(SourceLocation Loc, Diagnostic &D,
204 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000205 // Check the type specifier components first.
206
207 // signed/unsigned are only valid with int/char.
Chris Lattner839713c2006-08-04 06:15:52 +0000208 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000209 if (TypeSpecType == TST_unspecified)
210 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
211 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Chris Lattner839713c2006-08-04 06:15:52 +0000212 D.Report(Loc, diag::err_invalid_sign_spec,getSpecifierName(TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000213 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000214 TypeSpecSign = TSS_unspecified;
215 }
216 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000217
Chris Lattner839713c2006-08-04 06:15:52 +0000218 // Validate the width of the type.
219 switch (TypeSpecWidth) {
220 case TSW_unspecified: break;
221 case TSW_short: // short int
222 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000223 if (TypeSpecType == TST_unspecified)
224 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
225 else if (TypeSpecType != TST_int) {
Chris Lattner839713c2006-08-04 06:15:52 +0000226 D.Report(Loc, TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec :
227 diag::err_invalid_longlong_spec,
228 getSpecifierName(TypeSpecType));
229 TypeSpecType = TST_int;
230 }
231 break;
232 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000233 if (TypeSpecType == TST_unspecified)
234 TypeSpecType = TST_int; // long -> long int.
235 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner839713c2006-08-04 06:15:52 +0000236 D.Report(Loc, diag::err_invalid_long_spec,
237 getSpecifierName(TypeSpecType));
238 TypeSpecType = TST_int;
239 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000240 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000241 }
242
Chris Lattner0e894622006-08-13 19:58:17 +0000243 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000244 // disallow their use. Need information about the backend.
245 if (TypeSpecComplex != TSC_unspecified) {
246 if (TypeSpecType == TST_unspecified) {
247 D.Report(Loc, diag::ext_plain_complex);
248 TypeSpecType = TST_double; // _Complex -> _Complex double.
249 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000250 // Note that this intentionally doesn't include _Complex _Bool.
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000251 D.Report(Loc, diag::ext_integer_complex);
252 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
253 D.Report(Loc, diag::err_invalid_complex_spec,
254 getSpecifierName(TypeSpecType));
255 TypeSpecComplex = TSC_unspecified;
256 }
257 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000258
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000259 // Verify __thread.
260 if (SCS_thread_specified) {
261 if (StorageClassSpec == SCS_unspecified) {
262 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
263 } else if (StorageClassSpec != SCS_extern &&
264 StorageClassSpec != SCS_static) {
Chris Lattner4d8f8732006-11-28 05:05:08 +0000265 D.Report(getStorageClassSpecLoc(), diag::err_invalid_thread_spec,
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000266 getSpecifierName(StorageClassSpec));
267 SCS_thread_specified = false;
268 }
269 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000270
271 // Okay, now we can infer the real type.
Chris Lattner0e894622006-08-13 19:58:17 +0000272 // TODO: infer real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000273
Chris Lattner0e894622006-08-13 19:58:17 +0000274 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000275
Chris Lattner1ae23292006-08-04 06:42:31 +0000276 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000277}