blob: bf8d48b553d7203ecfd0bff769a247cefa5a6d1f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
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 type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Lex/IdentifierTable.h"
Chris Lattner11f20f92007-08-28 16:40:32 +000019#include "clang/Basic/LangOptions.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
22/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
23/// type object. This returns null on error.
24static QualType ConvertDeclSpecToType(const DeclSpec &DS, ASTContext &Ctx) {
25 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
26 // checking.
27
28 switch (DS.getTypeSpecType()) {
29 default: return QualType(); // FIXME: Handle unimp cases!
30 case DeclSpec::TST_void: return Ctx.VoidTy;
31 case DeclSpec::TST_char:
32 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
33 return Ctx.CharTy;
34 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
35 return Ctx.SignedCharTy;
36 else {
37 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
38 "Unknown TSS value");
39 return Ctx.UnsignedCharTy;
40 }
41 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
Chris Lattner5328f312007-08-21 17:02:28 +000042 case DeclSpec::TST_int: {
43 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000044 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
45 switch (DS.getTypeSpecWidth()) {
Chris Lattner5328f312007-08-21 17:02:28 +000046 case DeclSpec::TSW_unspecified: Result = Ctx.IntTy; break;
47 case DeclSpec::TSW_short: Result = Ctx.ShortTy; break;
48 case DeclSpec::TSW_long: Result = Ctx.LongTy; break;
49 case DeclSpec::TSW_longlong: Result = Ctx.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000050 }
51 } else {
52 switch (DS.getTypeSpecWidth()) {
Chris Lattner5328f312007-08-21 17:02:28 +000053 case DeclSpec::TSW_unspecified: Result = Ctx.UnsignedIntTy; break;
54 case DeclSpec::TSW_short: Result = Ctx.UnsignedShortTy; break;
55 case DeclSpec::TSW_long: Result = Ctx.UnsignedLongTy; break;
56 case DeclSpec::TSW_longlong: Result = Ctx.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000057 }
58 }
Chris Lattner5328f312007-08-21 17:02:28 +000059 // Handle complex integer types.
60 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
61 return Result;
62 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
63 "FIXME: imaginary types not supported yet!");
64 return Ctx.getComplexType(Result);
65 }
Chris Lattner4b009652007-07-25 00:24:17 +000066 case DeclSpec::TST_float:
67 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
68 return Ctx.FloatTy;
69 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
70 "FIXME: imaginary types not supported yet!");
Chris Lattner5328f312007-08-21 17:02:28 +000071 return Ctx.getComplexType(Ctx.FloatTy);
Chris Lattner4b009652007-07-25 00:24:17 +000072
73 case DeclSpec::TST_double: {
74 bool isLong = DS.getTypeSpecWidth() == DeclSpec::TSW_long;
Chris Lattner5328f312007-08-21 17:02:28 +000075 QualType T = isLong ? Ctx.LongDoubleTy : Ctx.DoubleTy;
Chris Lattner4b009652007-07-25 00:24:17 +000076 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
Chris Lattner5328f312007-08-21 17:02:28 +000077 return T;
Chris Lattner4b009652007-07-25 00:24:17 +000078 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
79 "FIXME: imaginary types not supported yet!");
Chris Lattner5328f312007-08-21 17:02:28 +000080 return Ctx.getComplexType(T);
Chris Lattner4b009652007-07-25 00:24:17 +000081 }
82 case DeclSpec::TST_bool: // _Bool or bool
83 return Ctx.BoolTy;
84 case DeclSpec::TST_decimal32: // _Decimal32
85 case DeclSpec::TST_decimal64: // _Decimal64
86 case DeclSpec::TST_decimal128: // _Decimal128
87 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
88 case DeclSpec::TST_enum:
89 case DeclSpec::TST_union:
90 case DeclSpec::TST_struct: {
91 Decl *D = static_cast<Decl *>(DS.getTypeRep());
92 assert(D && "Didn't get a decl for a enum/union/struct?");
93 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
94 DS.getTypeSpecSign() == 0 &&
95 "Can't handle qualifiers on typedef names yet!");
96 // TypeQuals handled by caller.
97 return Ctx.getTagDeclType(cast<TagDecl>(D));
98 }
99 case DeclSpec::TST_typedef: {
100 Decl *D = static_cast<Decl *>(DS.getTypeRep());
101 assert(D && "Didn't get a decl for a typedef?");
102 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
103 DS.getTypeSpecSign() == 0 &&
104 "Can't handle qualifiers on typedef names yet!");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000105 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
106 // we have this "hack" for now...
107 if (isa<ObjcInterfaceDecl>(D))
108 return Ctx.getObjcInterfaceType(cast<ObjcInterfaceDecl>(D));
Chris Lattner4b009652007-07-25 00:24:17 +0000109 // TypeQuals handled by caller.
110 return Ctx.getTypedefType(cast<TypedefDecl>(D));
111 }
Steve Naroff7cbb1462007-07-31 12:34:36 +0000112 case DeclSpec::TST_typeofType: {
113 QualType T = QualType::getFromOpaquePtr(DS.getTypeRep());
114 assert(!T.isNull() && "Didn't get a type for typeof?");
115 // TypeQuals handled by caller.
116 return Ctx.getTypeOfType(T);
117 }
118 case DeclSpec::TST_typeofExpr: {
119 Expr *E = static_cast<Expr *>(DS.getTypeRep());
120 assert(E && "Didn't get an expression for typeof?");
121 // TypeQuals handled by caller.
Steve Naroff11b649c2007-08-01 17:20:42 +0000122 return Ctx.getTypeOfExpr(E);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000123 }
Chris Lattner4b009652007-07-25 00:24:17 +0000124 }
125}
126
127/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
128/// instances.
129QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000130 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000131 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000132 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
133 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
134
Chris Lattner4b009652007-07-25 00:24:17 +0000135 QualType T = ConvertDeclSpecToType(D.getDeclSpec(), Context);
Steve Naroff91b03f72007-08-28 03:03:08 +0000136
Chris Lattner4b009652007-07-25 00:24:17 +0000137 // Apply const/volatile/restrict qualifiers to T.
138 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
139
140 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
141 // are ordered from the identifier out, which is opposite of what we want :).
142 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
143 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
144 switch (DeclType.Kind) {
145 default: assert(0 && "Unknown decltype!");
146 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000147 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000148 // C++ 8.3.2p4: There shall be no ... pointers to references ...
149 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
150 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000151 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000152 T = Context.IntTy;
153 }
154
155 // Apply the pointer typequals to the pointer object.
156 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
157 break;
158 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000159 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000160 // C++ 8.3.2p4: There shall be no references to references ...
161 Diag(D.getIdentifierLoc(),
162 diag::err_illegal_decl_reference_to_reference,
163 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000164 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000165 T = RT->getReferenceeType();
166 }
167
168 T = Context.getReferenceType(T);
169 break;
170 case DeclaratorChunk::Array: {
171 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000172 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000173 ArrayType::ArraySizeModifier ASM;
174 if (ATI.isStar)
175 ASM = ArrayType::Star;
176 else if (ATI.hasStatic)
177 ASM = ArrayType::Static;
178 else
179 ASM = ArrayType::Normal;
180
181 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
182 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
183 if (T->isIncompleteType()) {
184 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
185 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000186 T = Context.IntTy;
187 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000188 } else if (T->isFunctionType()) {
189 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
190 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000191 T = Context.getPointerType(T);
192 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000193 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000194 // C++ 8.3.2p4: There shall be no ... arrays of references ...
195 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
196 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000197 T = RT->getReferenceeType();
198 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000199 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000200 // If the element type is a struct or union that contains a variadic
201 // array, reject it: C99 6.7.2.1p2.
202 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
203 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
204 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000205 T = Context.IntTy;
206 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000207 }
208 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000209 // C99 6.7.5.2p1: The size expression shall have integer type.
210 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
211 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
212 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
213 D.setInvalidType(true);
214 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000215 llvm::APSInt ConstVal(32);
216 // If no expression was provided, we consider it a VLA.
217 if (!ArraySize || !ArraySize->isIntegerConstantExpr(ConstVal, Context))
218 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000219 else {
220 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
221 // have a value greater than zero.
222 if (ConstVal.isSigned()) {
223 if (ConstVal.isNegative()) {
224 Diag(ArraySize->getLocStart(),
225 diag::err_typecheck_negative_array_size,
226 ArraySize->getSourceRange());
227 D.setInvalidType(true);
228 } else if (ConstVal == 0) {
229 // GCC accepts zero sized static arrays.
230 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
231 ArraySize->getSourceRange());
232 }
233 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000234 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000235 }
Chris Lattner47958f62007-08-28 16:54:00 +0000236 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
237 if (!getLangOptions().C99 &&
238 (ASM != ArrayType::Normal ||
239 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
240 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000241 break;
242 }
243 case DeclaratorChunk::Function:
244 // If the function declarator has a prototype (i.e. it is not () and
245 // does not have a K&R-style identifier list), then the arguments are part
246 // of the type, otherwise the argument list is ().
247 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
248 if (!FTI.hasPrototype) {
249 // Simple void foo(), where the incoming T is the result type.
250 T = Context.getFunctionTypeNoProto(T);
251
252 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
253 if (FTI.NumArgs != 0)
254 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
255
256 } else {
257 // Otherwise, we have a function with an argument list that is
258 // potentially variadic.
259 llvm::SmallVector<QualType, 16> ArgTys;
260
261 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
262 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
263 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000264 //
265 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
266 // This matches the conversion that is done in
267 // Sema::ParseParamDeclarator(). Without this conversion, the
268 // argument type in the function prototype *will not* match the
269 // type in ParmVarDecl (which makes the code generator unhappy).
270 //
271 // FIXME: We still apparently need the conversion in
272 // Sema::ParseParamDeclarator(). This doesn't make any sense, since
273 // it should be driving off the type being created here.
274 //
275 // FIXME: If a source translation tool needs to see the original type,
276 // then we need to consider storing both types somewhere...
277 //
278 if (const ArrayType *AT = ArgTy->getAsArrayType())
279 ArgTy = Context.getPointerType(AT->getElementType());
280 else if (ArgTy->isFunctionType())
281 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000282 // Look for 'void'. void is allowed only as a single argument to a
283 // function with no other parameters (C99 6.7.5.3p10). We record
284 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000285 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000286 // If this is something like 'float(int, void)', reject it. 'void'
287 // is an incomplete type (C99 6.2.5p19) and function decls cannot
288 // have arguments of incomplete type.
289 if (FTI.NumArgs != 1 || FTI.isVariadic) {
290 Diag(DeclType.Loc, diag::err_void_only_param);
291 ArgTy = Context.IntTy;
292 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
293 } else if (FTI.ArgInfo[i].Ident) {
294 // Reject, but continue to parse 'int(void abc)'.
295 Diag(FTI.ArgInfo[i].IdentLoc,
296 diag::err_param_with_void_type);
297 ArgTy = Context.IntTy;
298 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
299 } else {
300 // Reject, but continue to parse 'float(const void)'.
301 if (ArgTy.getQualifiers())
302 Diag(DeclType.Loc, diag::err_void_param_qualified);
303
304 // Do not add 'void' to the ArgTys list.
305 break;
306 }
307 }
308
309 ArgTys.push_back(ArgTy);
310 }
311 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
312 FTI.isVariadic);
313 }
314 break;
315 }
316 }
317
318 return T;
319}
320
321Sema::TypeResult Sema::ParseTypeName(Scope *S, Declarator &D) {
322 // C99 6.7.6: Type names have no identifier. This is already validated by
323 // the parser.
324 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
325
326 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000327
328 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000329
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000330 // In this context, we *do not* check D.getInvalidType(). If the declarator
331 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
332 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000333 return T.getAsOpaquePtr();
334}
335
Steve Naroff91b03f72007-08-28 03:03:08 +0000336// Called from Parser::ParseParenDeclarator().
Chris Lattner4b009652007-07-25 00:24:17 +0000337Sema::TypeResult Sema::ParseParamDeclaratorType(Scope *S, Declarator &D) {
338 // Note: parameters have identifiers, but we don't care about them here, we
339 // just want the type converted.
340 QualType T = GetTypeForDeclarator(D, S);
341
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000342 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
343
344 // In this context, we *do not* check D.getInvalidType(). If the declarator
345 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
346 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000347 return T.getAsOpaquePtr();
348}