blob: be028063771130f5e5eedfbb7355d3c61b3c5c31 [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 Lattnerda48a8e2006-08-04 05:25:55 +0000175bool DeclSpec::SetTypeQual(TQ T, const char *&PrevSpec,
176 const LangOptions &Lang) {
177 // Duplicates turn into warnings pre-C99.
178 if ((TypeQualifiers & T) && !Lang.C99)
179 return BadSpecifier(T, PrevSpec);
180 TypeQualifiers |= T;
181 return false;
182}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000183
Chris Lattner1b22eed2006-11-28 05:12:07 +0000184bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec){
Chris Lattnera925dc62006-11-28 04:33:46 +0000185 // 'inline inline' is ok.
186 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000187 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000188 return false;
189}
190
191
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000192/// Finish - This does final analysis of the declspec, rejecting things like
193/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
194/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
195/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Chris Lattner839713c2006-08-04 06:15:52 +0000196void DeclSpec::Finish(SourceLocation Loc, Diagnostic &D,
197 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000198 // Check the type specifier components first.
199
200 // signed/unsigned are only valid with int/char.
Chris Lattner839713c2006-08-04 06:15:52 +0000201 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000202 if (TypeSpecType == TST_unspecified)
203 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
204 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Chris Lattner839713c2006-08-04 06:15:52 +0000205 D.Report(Loc, diag::err_invalid_sign_spec,getSpecifierName(TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000206 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000207 TypeSpecSign = TSS_unspecified;
208 }
209 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000210
Chris Lattner839713c2006-08-04 06:15:52 +0000211 // Validate the width of the type.
212 switch (TypeSpecWidth) {
213 case TSW_unspecified: break;
214 case TSW_short: // short int
215 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000216 if (TypeSpecType == TST_unspecified)
217 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
218 else if (TypeSpecType != TST_int) {
Chris Lattner839713c2006-08-04 06:15:52 +0000219 D.Report(Loc, TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec :
220 diag::err_invalid_longlong_spec,
221 getSpecifierName(TypeSpecType));
222 TypeSpecType = TST_int;
223 }
224 break;
225 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000226 if (TypeSpecType == TST_unspecified)
227 TypeSpecType = TST_int; // long -> long int.
228 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner839713c2006-08-04 06:15:52 +0000229 D.Report(Loc, diag::err_invalid_long_spec,
230 getSpecifierName(TypeSpecType));
231 TypeSpecType = TST_int;
232 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000233 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000234 }
235
Chris Lattner0e894622006-08-13 19:58:17 +0000236 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000237 // disallow their use. Need information about the backend.
238 if (TypeSpecComplex != TSC_unspecified) {
239 if (TypeSpecType == TST_unspecified) {
240 D.Report(Loc, diag::ext_plain_complex);
241 TypeSpecType = TST_double; // _Complex -> _Complex double.
242 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000243 // Note that this intentionally doesn't include _Complex _Bool.
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000244 D.Report(Loc, diag::ext_integer_complex);
245 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
246 D.Report(Loc, diag::err_invalid_complex_spec,
247 getSpecifierName(TypeSpecType));
248 TypeSpecComplex = TSC_unspecified;
249 }
250 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000251
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000252 // Verify __thread.
253 if (SCS_thread_specified) {
254 if (StorageClassSpec == SCS_unspecified) {
255 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
256 } else if (StorageClassSpec != SCS_extern &&
257 StorageClassSpec != SCS_static) {
Chris Lattner4d8f8732006-11-28 05:05:08 +0000258 D.Report(getStorageClassSpecLoc(), diag::err_invalid_thread_spec,
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000259 getSpecifierName(StorageClassSpec));
260 SCS_thread_specified = false;
261 }
262 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000263
264 // Okay, now we can infer the real type.
Chris Lattner0e894622006-08-13 19:58:17 +0000265 // TODO: infer real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000266
Chris Lattner0e894622006-08-13 19:58:17 +0000267 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000268
Chris Lattner1ae23292006-08-04 06:42:31 +0000269 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000270}