blob: 2b853a99a33043c61db9108277f9b34c6737a16d [file] [log] [blame]
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001//===--- Declarations.cpp - Declaration Representation Implementation -----===//
2//
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//
10// This file implements the Declaration representation classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Declarations.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
31 if (TypeSpecWidth != TSW_unspecified ||
32 TypeSpecComplex != TSC_unspecified ||
33 TypeSpecSign != TSS_unspecified ||
34 TypeSpecType != TST_unspecified)
35 Res |= PQ_TypeSpecifier;
36
Chris Lattnerf63f89a2006-08-05 03:28:50 +000037 if (FS_inline_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +000038 Res |= PQ_FunctionSpecifier;
39 return Res;
40}
41
Chris Lattnerf63f89a2006-08-05 03:28:50 +000042static const char *getSpecifierName(DeclSpec::SCS S) {
43 switch (S) {
44 default: assert(0 && "Unknown typespec!");
45 case DeclSpec::SCS_unspecified: return "unspecified";
46 case DeclSpec::SCS_typedef: return "typedef";
47 case DeclSpec::SCS_extern: return "extern";
48 case DeclSpec::SCS_static: return "static";
49 case DeclSpec::SCS_auto: return "auto";
50 case DeclSpec::SCS_register: return "register";
51 }
52}
53
54static bool BadSpecifier(DeclSpec::SCS S, const char *&PrevSpec) {
55 PrevSpec = getSpecifierName(S);
56 return true;
57}
Chris Lattnerb9093cd2006-08-04 04:39:53 +000058
59static bool BadSpecifier(DeclSpec::TSW W, const char *&PrevSpec) {
60 switch (W) {
61 case DeclSpec::TSW_unspecified: PrevSpec = "unspecified"; break;
62 case DeclSpec::TSW_short: PrevSpec = "short"; break;
63 case DeclSpec::TSW_long: PrevSpec = "long"; break;
64 case DeclSpec::TSW_longlong: PrevSpec = "long long"; break;
65 }
66 return true;
67}
68
69static bool BadSpecifier(DeclSpec::TSC C, const char *&PrevSpec) {
70 switch (C) {
71 case DeclSpec::TSC_unspecified: PrevSpec = "unspecified"; break;
72 case DeclSpec::TSC_imaginary: PrevSpec = "imaginary"; break;
73 case DeclSpec::TSC_complex: PrevSpec = "complex"; break;
74 }
75 return true;
76}
77
78
79static bool BadSpecifier(DeclSpec::TSS S, const char *&PrevSpec) {
80 switch (S) {
81 case DeclSpec::TSS_unspecified: PrevSpec = "unspecified"; break;
82 case DeclSpec::TSS_signed: PrevSpec = "signed"; break;
83 case DeclSpec::TSS_unsigned: PrevSpec = "unsigned"; break;
84 }
85 return true;
86}
87
Chris Lattner839713c2006-08-04 06:15:52 +000088static const char *getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +000089 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +000090 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";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000101 }
Chris Lattner839713c2006-08-04 06:15:52 +0000102}
103
104static bool BadSpecifier(DeclSpec::TST T, const char *&PrevSpec) {
105 PrevSpec = getSpecifierName(T);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000106 return true;
107}
108
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000109static bool BadSpecifier(DeclSpec::TQ T, const char *&PrevSpec) {
110 switch (T) {
111 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
112 case DeclSpec::TQ_const: PrevSpec = "const"; break;
113 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
114 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
115 }
116 return true;
117}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000118
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000119bool DeclSpec::SetStorageClassSpec(SCS S, const char *&PrevSpec) {
120 if (StorageClassSpec != SCS_unspecified)
121 return BadSpecifier(StorageClassSpec, PrevSpec);
122 StorageClassSpec = S;
123 return false;
124}
125
126
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000127/// These methods set the specified attribute of the DeclSpec, but return true
128/// and ignore the request if invalid (e.g. "extern" then "auto" is
129/// specified).
130bool DeclSpec::SetTypeSpecWidth(TSW W, const char *&PrevSpec) {
131 if (TypeSpecWidth != TSW_unspecified)
132 return BadSpecifier(TypeSpecWidth, PrevSpec);
133 TypeSpecWidth = W;
134 return false;
135}
136
137bool DeclSpec::SetTypeSpecComplex(TSC C, const char *&PrevSpec) {
138 if (TypeSpecComplex != TSC_unspecified)
139 return BadSpecifier(TypeSpecComplex, PrevSpec);
140 TypeSpecComplex = C;
141 return false;
142}
143
144bool DeclSpec::SetTypeSpecSign(TSS S, const char *&PrevSpec) {
145 if (TypeSpecSign != TSS_unspecified)
146 return BadSpecifier(TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000147 TypeSpecSign = S;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000148 return false;
149}
150
151bool DeclSpec::SetTypeSpecType(TST T, const char *&PrevSpec) {
152 if (TypeSpecType != TST_unspecified)
153 return BadSpecifier(TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000154 TypeSpecType = T;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000155 return false;
156}
157
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000158bool DeclSpec::SetTypeQual(TQ T, const char *&PrevSpec,
159 const LangOptions &Lang) {
160 // Duplicates turn into warnings pre-C99.
161 if ((TypeQualifiers & T) && !Lang.C99)
162 return BadSpecifier(T, PrevSpec);
163 TypeQualifiers |= T;
164 return false;
165}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000166
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000167/// Finish - This does final analysis of the declspec, rejecting things like
168/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
169/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
170/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Chris Lattner839713c2006-08-04 06:15:52 +0000171void DeclSpec::Finish(SourceLocation Loc, Diagnostic &D,
172 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000173 // Check the type specifier components first.
174
175 // signed/unsigned are only valid with int/char.
Chris Lattner839713c2006-08-04 06:15:52 +0000176 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000177 if (TypeSpecType == TST_unspecified)
178 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
179 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Chris Lattner839713c2006-08-04 06:15:52 +0000180 D.Report(Loc, diag::err_invalid_sign_spec,getSpecifierName(TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000181 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000182 TypeSpecSign = TSS_unspecified;
183 }
184 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000185
Chris Lattner839713c2006-08-04 06:15:52 +0000186 // Validate the width of the type.
187 switch (TypeSpecWidth) {
188 case TSW_unspecified: break;
189 case TSW_short: // short int
190 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000191 if (TypeSpecType == TST_unspecified)
192 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
193 else if (TypeSpecType != TST_int) {
Chris Lattner839713c2006-08-04 06:15:52 +0000194 D.Report(Loc, TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec :
195 diag::err_invalid_longlong_spec,
196 getSpecifierName(TypeSpecType));
197 TypeSpecType = TST_int;
198 }
199 break;
200 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000201 if (TypeSpecType == TST_unspecified)
202 TypeSpecType = TST_int; // long -> long int.
203 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner839713c2006-08-04 06:15:52 +0000204 D.Report(Loc, diag::err_invalid_long_spec,
205 getSpecifierName(TypeSpecType));
206 TypeSpecType = TST_int;
207 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000208 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000209 }
210
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000211 // FIXME: if the implementation does not implement _Complex or _Imaginary,
212 // disallow their use. Need information about the backend.
213 if (TypeSpecComplex != TSC_unspecified) {
214 if (TypeSpecType == TST_unspecified) {
215 D.Report(Loc, diag::ext_plain_complex);
216 TypeSpecType = TST_double; // _Complex -> _Complex double.
217 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000218 // Note that this intentionally doesn't include _Complex _Bool.
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000219 D.Report(Loc, diag::ext_integer_complex);
220 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
221 D.Report(Loc, diag::err_invalid_complex_spec,
222 getSpecifierName(TypeSpecType));
223 TypeSpecComplex = TSC_unspecified;
224 }
225 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000226
227 // If this is C99, require that at least one specifier is present!
228 if (Lang.C99 && (getParsedSpecifiers() & PQ_TypeSpecifier) == 0) {
Chris Lattner158ede82006-08-04 06:42:38 +0000229 D.Report(Loc, diag::w_type_defaults_to_int); // C99 2.7.2p2.
Chris Lattner1ae23292006-08-04 06:42:31 +0000230 TypeSpecType = TST_int;
231 }
232
233 // Okay, now we can infer the real type.
234
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000235
236 // 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 }
247
Chris Lattner1ae23292006-08-04 06:42:31 +0000248 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000249}