blob: 48e9a11e334410703ca0c0c8dc917b96c746b109 [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
155bool DeclSpec::SetTypeSpecType(TST T, const char *&PrevSpec) {
156 if (TypeSpecType != TST_unspecified)
157 return BadSpecifier(TypeSpecType, PrevSpec);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000158 TypeSpecType = T;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000159 return false;
160}
161
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000162bool DeclSpec::SetTypeQual(TQ T, const char *&PrevSpec,
163 const LangOptions &Lang) {
164 // Duplicates turn into warnings pre-C99.
165 if ((TypeQualifiers & T) && !Lang.C99)
166 return BadSpecifier(T, PrevSpec);
167 TypeQualifiers |= T;
168 return false;
169}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000170
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000171/// Finish - This does final analysis of the declspec, rejecting things like
172/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
173/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
174/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Chris Lattner839713c2006-08-04 06:15:52 +0000175void DeclSpec::Finish(SourceLocation Loc, Diagnostic &D,
176 const LangOptions &Lang) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000177 // Check the type specifier components first.
178
179 // signed/unsigned are only valid with int/char.
Chris Lattner839713c2006-08-04 06:15:52 +0000180 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000181 if (TypeSpecType == TST_unspecified)
182 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
183 else if (TypeSpecType != TST_int && TypeSpecType != TST_char) {
Chris Lattner839713c2006-08-04 06:15:52 +0000184 D.Report(Loc, diag::err_invalid_sign_spec,getSpecifierName(TypeSpecType));
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000185 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000186 TypeSpecSign = TSS_unspecified;
187 }
188 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000189
Chris Lattner839713c2006-08-04 06:15:52 +0000190 // Validate the width of the type.
191 switch (TypeSpecWidth) {
192 case TSW_unspecified: break;
193 case TSW_short: // short int
194 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000195 if (TypeSpecType == TST_unspecified)
196 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
197 else if (TypeSpecType != TST_int) {
Chris Lattner839713c2006-08-04 06:15:52 +0000198 D.Report(Loc, TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec :
199 diag::err_invalid_longlong_spec,
200 getSpecifierName(TypeSpecType));
201 TypeSpecType = TST_int;
202 }
203 break;
204 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000205 if (TypeSpecType == TST_unspecified)
206 TypeSpecType = TST_int; // long -> long int.
207 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Chris Lattner839713c2006-08-04 06:15:52 +0000208 D.Report(Loc, diag::err_invalid_long_spec,
209 getSpecifierName(TypeSpecType));
210 TypeSpecType = TST_int;
211 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000212 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000213 }
214
Chris Lattner0e894622006-08-13 19:58:17 +0000215 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000216 // disallow their use. Need information about the backend.
217 if (TypeSpecComplex != TSC_unspecified) {
218 if (TypeSpecType == TST_unspecified) {
219 D.Report(Loc, diag::ext_plain_complex);
220 TypeSpecType = TST_double; // _Complex -> _Complex double.
221 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000222 // Note that this intentionally doesn't include _Complex _Bool.
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000223 D.Report(Loc, diag::ext_integer_complex);
224 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
225 D.Report(Loc, diag::err_invalid_complex_spec,
226 getSpecifierName(TypeSpecType));
227 TypeSpecComplex = TSC_unspecified;
228 }
229 }
Chris Lattner1ae23292006-08-04 06:42:31 +0000230
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000231 // Verify __thread.
232 if (SCS_thread_specified) {
233 if (StorageClassSpec == SCS_unspecified) {
234 StorageClassSpec = SCS_extern; // '__thread int' -> 'extern __thread int'
235 } else if (StorageClassSpec != SCS_extern &&
236 StorageClassSpec != SCS_static) {
237 D.Report(Loc, diag::err_invalid_thread_spec,
238 getSpecifierName(StorageClassSpec));
239 SCS_thread_specified = false;
240 }
241 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000242
243 // Okay, now we can infer the real type.
Chris Lattner0e894622006-08-13 19:58:17 +0000244 // TODO: infer real type.
Chris Lattner8e90ef62006-08-05 03:30:45 +0000245
Chris Lattner0e894622006-08-13 19:58:17 +0000246 // TODO: return "auto function" and other bad things based on the real type.
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000247
Chris Lattner1ae23292006-08-04 06:42:31 +0000248 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000249}