blob: 0fc8d70fec5a1bb957160198931792645be5a8bb [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
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 Lattnerda72c822006-08-13 22:16:42 +0000101 case DeclSpec::TST_enum: return "enum";
102 case DeclSpec::TST_union: return "union";
103 case DeclSpec::TST_struct: return "struct";
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000104 case DeclSpec::TST_typedef: return "typedef";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000105 }
Chris Lattner839713c2006-08-04 06:15:52 +0000106}
107
108static bool BadSpecifier(DeclSpec::TST T, const char *&PrevSpec) {
109 PrevSpec = getSpecifierName(T);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000110 return true;
111}
112
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000113static bool BadSpecifier(DeclSpec::TQ T, const char *&PrevSpec) {
114 switch (T) {
115 case DeclSpec::TQ_unspecified: PrevSpec = "unspecified"; break;
116 case DeclSpec::TQ_const: PrevSpec = "const"; break;
117 case DeclSpec::TQ_restrict: PrevSpec = "restrict"; break;
118 case DeclSpec::TQ_volatile: PrevSpec = "volatile"; break;
119 }
120 return true;
121}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000122
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000123bool DeclSpec::SetStorageClassSpec(SCS S, const char *&PrevSpec) {
124 if (StorageClassSpec != SCS_unspecified)
125 return BadSpecifier(StorageClassSpec, PrevSpec);
126 StorageClassSpec = S;
127 return false;
128}
129
130
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000131/// These methods set the specified attribute of the DeclSpec, but return true
132/// and ignore the request if invalid (e.g. "extern" then "auto" is
133/// specified).
134bool DeclSpec::SetTypeSpecWidth(TSW W, const char *&PrevSpec) {
135 if (TypeSpecWidth != TSW_unspecified)
136 return BadSpecifier(TypeSpecWidth, PrevSpec);
137 TypeSpecWidth = W;
138 return false;
139}
140
141bool DeclSpec::SetTypeSpecComplex(TSC C, const char *&PrevSpec) {
142 if (TypeSpecComplex != TSC_unspecified)
143 return BadSpecifier(TypeSpecComplex, PrevSpec);
144 TypeSpecComplex = C;
145 return false;
146}
147
148bool DeclSpec::SetTypeSpecSign(TSS S, const char *&PrevSpec) {
149 if (TypeSpecSign != TSS_unspecified)
150 return BadSpecifier(TypeSpecSign, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000151 TypeSpecSign = S;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000152 return false;
153}
154
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000155bool DeclSpec::SetTypeSpecType(TST T, const char *&PrevSpec, void *TypeRep) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000156 if (TypeSpecType != TST_unspecified)
157 return BadSpecifier(TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000158 TypeSpecType = T;
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000159 TypenameRep = TypeRep;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000160 return false;
161}
162
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000163bool DeclSpec::SetTypeQual(TQ T, const char *&PrevSpec,
164 const LangOptions &Lang) {
165 // Duplicates turn into warnings pre-C99.
166 if ((TypeQualifiers & T) && !Lang.C99)
167 return BadSpecifier(T, PrevSpec);
168 TypeQualifiers |= T;
169 return false;
170}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000171
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000172/// Finish - This does final analysis of the declspec, rejecting things like
173/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
174/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
175/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Chris Lattner839713c2006-08-04 06:15:52 +0000176void DeclSpec::Finish(SourceLocation Loc, Diagnostic &D,
177 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000178 // Check the type specifier components first.
179
180 // signed/unsigned are only valid with int/char.
Chris Lattner839713c2006-08-04 06:15:52 +0000181 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000182 if (TypeSpecType == TST_unspecified)
183 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
184 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Chris Lattner839713c2006-08-04 06:15:52 +0000185 D.Report(Loc, diag::err_invalid_sign_spec,getSpecifierName(TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000186 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000187 TypeSpecSign = TSS_unspecified;
188 }
189 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000190
Chris Lattner839713c2006-08-04 06:15:52 +0000191 // Validate the width of the type.
192 switch (TypeSpecWidth) {
193 case TSW_unspecified: break;
194 case TSW_short: // short int
195 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000196 if (TypeSpecType == TST_unspecified)
197 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
198 else if (TypeSpecType != TST_int) {
Chris Lattner839713c2006-08-04 06:15:52 +0000199 D.Report(Loc, TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec :
200 diag::err_invalid_longlong_spec,
201 getSpecifierName(TypeSpecType));
202 TypeSpecType = TST_int;
203 }
204 break;
205 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000206 if (TypeSpecType == TST_unspecified)
207 TypeSpecType = TST_int; // long -> long int.
208 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner839713c2006-08-04 06:15:52 +0000209 D.Report(Loc, diag::err_invalid_long_spec,
210 getSpecifierName(TypeSpecType));
211 TypeSpecType = TST_int;
212 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000213 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000214 }
215
Chris Lattner0e894622006-08-13 19:58:17 +0000216 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000217 // disallow their use. Need information about the backend.
218 if (TypeSpecComplex != TSC_unspecified) {
219 if (TypeSpecType == TST_unspecified) {
220 D.Report(Loc, diag::ext_plain_complex);
221 TypeSpecType = TST_double; // _Complex -> _Complex double.
222 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000223 // Note that this intentionally doesn't include _Complex _Bool.
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000224 D.Report(Loc, diag::ext_integer_complex);
225 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
226 D.Report(Loc, diag::err_invalid_complex_spec,
227 getSpecifierName(TypeSpecType));
228 TypeSpecComplex = TSC_unspecified;
229 }
230 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000231
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000232 // Verify __thread.
233 if (SCS_thread_specified) {
234 if (StorageClassSpec == SCS_unspecified) {
235 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
236 } else if (StorageClassSpec != SCS_extern &&
237 StorageClassSpec != SCS_static) {
238 D.Report(Loc, diag::err_invalid_thread_spec,
239 getSpecifierName(StorageClassSpec));
240 SCS_thread_specified = false;
241 }
242 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000243
244 // Okay, now we can infer the real type.
Chris Lattner0e894622006-08-13 19:58:17 +0000245 // TODO: infer real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000246
Chris Lattner0e894622006-08-13 19:58:17 +0000247 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000248
Chris Lattner1ae23292006-08-04 06:42:31 +0000249 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000250}