blob: ef353926b59fb96292529d75e3a5e62fcf69aa56 [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
28 if (TypeQualifiers != TQ_unspecified)
29 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 Lattnerf63f89a2006-08-05 03:28:50 +0000120bool DeclSpec::SetStorageClassSpec(SCS S, const char *&PrevSpec) {
121 if (StorageClassSpec != SCS_unspecified)
122 return BadSpecifier(StorageClassSpec, PrevSpec);
123 StorageClassSpec = S;
124 return false;
125}
126
127
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000128/// These methods set the specified attribute of the DeclSpec, but return true
129/// and ignore the request if invalid (e.g. "extern" then "auto" is
130/// specified).
131bool DeclSpec::SetTypeSpecWidth(TSW W, const char *&PrevSpec) {
132 if (TypeSpecWidth != TSW_unspecified)
133 return BadSpecifier(TypeSpecWidth, PrevSpec);
134 TypeSpecWidth = W;
135 return false;
136}
137
138bool DeclSpec::SetTypeSpecComplex(TSC C, const char *&PrevSpec) {
139 if (TypeSpecComplex != TSC_unspecified)
140 return BadSpecifier(TypeSpecComplex, PrevSpec);
141 TypeSpecComplex = C;
142 return false;
143}
144
145bool DeclSpec::SetTypeSpecSign(TSS S, const char *&PrevSpec) {
146 if (TypeSpecSign != TSS_unspecified)
147 return BadSpecifier(TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000148 TypeSpecSign = S;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000149 return false;
150}
151
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000152bool DeclSpec::SetTypeSpecType(TST T, const char *&PrevSpec, void *TypeRep) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000153 if (TypeSpecType != TST_unspecified)
154 return BadSpecifier(TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000155 TypeSpecType = T;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000156 TypenameRep = TypeRep;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000157 return false;
158}
159
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000160bool DeclSpec::SetTypeQual(TQ T, const char *&PrevSpec,
161 const LangOptions &Lang) {
162 // Duplicates turn into warnings pre-C99.
163 if ((TypeQualifiers & T) && !Lang.C99)
164 return BadSpecifier(T, PrevSpec);
165 TypeQualifiers |= T;
166 return false;
167}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000168
Chris Lattnera925dc62006-11-28 04:33:46 +0000169bool DeclSpec::SetFunctionSpecInline(const char *&PrevSpec) {
170 // 'inline inline' is ok.
171 FS_inline_specified = true;
172 return false;
173}
174
175
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000176/// Finish - This does final analysis of the declspec, rejecting things like
177/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
178/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
179/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Chris Lattner839713c2006-08-04 06:15:52 +0000180void DeclSpec::Finish(SourceLocation Loc, Diagnostic &D,
181 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000182 // Check the type specifier components first.
183
184 // signed/unsigned are only valid with int/char.
Chris Lattner839713c2006-08-04 06:15:52 +0000185 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000186 if (TypeSpecType == TST_unspecified)
187 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
188 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Chris Lattner839713c2006-08-04 06:15:52 +0000189 D.Report(Loc, diag::err_invalid_sign_spec,getSpecifierName(TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000190 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000191 TypeSpecSign = TSS_unspecified;
192 }
193 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000194
Chris Lattner839713c2006-08-04 06:15:52 +0000195 // Validate the width of the type.
196 switch (TypeSpecWidth) {
197 case TSW_unspecified: break;
198 case TSW_short: // short int
199 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000200 if (TypeSpecType == TST_unspecified)
201 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
202 else if (TypeSpecType != TST_int) {
Chris Lattner839713c2006-08-04 06:15:52 +0000203 D.Report(Loc, TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec :
204 diag::err_invalid_longlong_spec,
205 getSpecifierName(TypeSpecType));
206 TypeSpecType = TST_int;
207 }
208 break;
209 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000210 if (TypeSpecType == TST_unspecified)
211 TypeSpecType = TST_int; // long -> long int.
212 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner839713c2006-08-04 06:15:52 +0000213 D.Report(Loc, diag::err_invalid_long_spec,
214 getSpecifierName(TypeSpecType));
215 TypeSpecType = TST_int;
216 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000217 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000218 }
219
Chris Lattner0e894622006-08-13 19:58:17 +0000220 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000221 // disallow their use. Need information about the backend.
222 if (TypeSpecComplex != TSC_unspecified) {
223 if (TypeSpecType == TST_unspecified) {
224 D.Report(Loc, diag::ext_plain_complex);
225 TypeSpecType = TST_double; // _Complex -> _Complex double.
226 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000227 // Note that this intentionally doesn't include _Complex _Bool.
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000228 D.Report(Loc, diag::ext_integer_complex);
229 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
230 D.Report(Loc, diag::err_invalid_complex_spec,
231 getSpecifierName(TypeSpecType));
232 TypeSpecComplex = TSC_unspecified;
233 }
234 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000235
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000236 // Verify __thread.
237 if (SCS_thread_specified) {
238 if (StorageClassSpec == SCS_unspecified) {
239 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
240 } else if (StorageClassSpec != SCS_extern &&
241 StorageClassSpec != SCS_static) {
242 D.Report(Loc, diag::err_invalid_thread_spec,
243 getSpecifierName(StorageClassSpec));
244 SCS_thread_specified = false;
245 }
246 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000247
248 // Okay, now we can infer the real type.
Chris Lattner0e894622006-08-13 19:58:17 +0000249 // TODO: infer real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000250
Chris Lattner0e894622006-08-13 19:58:17 +0000251 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000252
Chris Lattner1ae23292006-08-04 06:42:31 +0000253 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000254}