blob: f17afd466db00bdc8dfa97a90d9c49e09e3b011c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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"
Steve Naroff3fafa102007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Parse/DeclSpec.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.
Chris Lattner726c5452008-02-20 23:53:49 +000024QualType Sema::ConvertDeclSpecToType(DeclSpec &DS) {
Chris Lattner4b009652007-07-25 00:24:17 +000025 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
26 // checking.
Chris Lattner06fb8672008-02-20 21:40:32 +000027 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000028
29 switch (DS.getTypeSpecType()) {
30 default: return QualType(); // FIXME: Handle unimp cases!
Chris Lattner726c5452008-02-20 23:53:49 +000031 case DeclSpec::TST_void: return Context.VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +000032 case DeclSpec::TST_char:
33 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner726c5452008-02-20 23:53:49 +000034 Result = Context.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000035 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner726c5452008-02-20 23:53:49 +000036 Result = Context.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000037 else {
38 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
39 "Unknown TSS value");
Chris Lattner726c5452008-02-20 23:53:49 +000040 Result = Context.UnsignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000041 }
Chris Lattner06fb8672008-02-20 21:40:32 +000042 break;
Chris Lattner4b009652007-07-25 00:24:17 +000043 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
Chris Lattner5328f312007-08-21 17:02:28 +000044 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000045 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
46 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000047 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
48 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
49 case DeclSpec::TSW_long: Result = Context.LongTy; break;
50 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000051 }
52 } else {
53 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000054 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
55 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
56 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
57 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000058 }
59 }
Chris Lattner06fb8672008-02-20 21:40:32 +000060 break;
Chris Lattner5328f312007-08-21 17:02:28 +000061 }
Chris Lattner726c5452008-02-20 23:53:49 +000062 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +000063 case DeclSpec::TST_double:
64 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +000065 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000066 else
Chris Lattner726c5452008-02-20 23:53:49 +000067 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000068 break;
Chris Lattner726c5452008-02-20 23:53:49 +000069 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +000070 case DeclSpec::TST_decimal32: // _Decimal32
71 case DeclSpec::TST_decimal64: // _Decimal64
72 case DeclSpec::TST_decimal128: // _Decimal128
73 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
74 case DeclSpec::TST_enum:
75 case DeclSpec::TST_union:
76 case DeclSpec::TST_struct: {
77 Decl *D = static_cast<Decl *>(DS.getTypeRep());
78 assert(D && "Didn't get a decl for a enum/union/struct?");
79 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
80 DS.getTypeSpecSign() == 0 &&
81 "Can't handle qualifiers on typedef names yet!");
82 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +000083 Result = Context.getTagDeclType(cast<TagDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +000084 break;
Chris Lattner4b009652007-07-25 00:24:17 +000085 }
86 case DeclSpec::TST_typedef: {
87 Decl *D = static_cast<Decl *>(DS.getTypeRep());
88 assert(D && "Didn't get a decl for a typedef?");
89 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
90 DS.getTypeSpecSign() == 0 &&
91 "Can't handle qualifiers on typedef names yet!");
Steve Naroff81f1bba2007-09-06 21:24:23 +000092 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
93 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +000094 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner06fb8672008-02-20 21:40:32 +000095 if (DS.getProtocolQualifiers() == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +000096 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +000097 break;
98 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +000099
100 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000101 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattner06fb8672008-02-20 21:40:32 +0000102 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner726c5452008-02-20 23:53:49 +0000103 DS.NumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000104 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000105 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000106 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattner726c5452008-02-20 23:53:49 +0000107 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000108 && DS.getProtocolQualifiers()) {
109 // id<protocol-list>
110 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000111 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
Chris Lattner06fb8672008-02-20 21:40:32 +0000112 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
113 DS.NumProtocolQualifiers());
114 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000115 }
116 }
Chris Lattner4b009652007-07-25 00:24:17 +0000117 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000118 Result = Context.getTypedefType(cast<TypedefDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000119 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000120 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000121 case DeclSpec::TST_typeofType:
122 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
123 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000124 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000125 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000126 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000127 case DeclSpec::TST_typeofExpr: {
128 Expr *E = static_cast<Expr *>(DS.getTypeRep());
129 assert(E && "Didn't get an expression for typeof?");
130 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000131 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000132 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000133 }
Chris Lattner4b009652007-07-25 00:24:17 +0000134 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000135
136 // Handle complex types.
137 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000138 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000139
140 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
141 "FIXME: imaginary types not supported yet!");
142
Chris Lattnerd496fb92008-02-20 22:04:11 +0000143 // See if there are any attributes on the declspec that apply to the type (as
144 // opposed to the decl).
145 if (!DS.getAttributes())
146 return Result;
147
148 // Scan through and apply attributes to this type where it makes sense. Some
149 // attributes (such as __address_space__, __vector_size__, etc) apply to the
150 // declspec, but others can be present in the decl spec even though they apply
151 // to the decl. Here we apply and delete attributes that apply to the
152 // declspec and leave the others alone.
153 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
154 AttributeList *AL = DS.getAttributes();
155 while (AL) {
156 AttributeList *ThisAttr = AL;
157 AL = AL->getNext();
158
159 LeftOverAttrs.push_back(ThisAttr);
160 }
161
162 // Rechain any attributes that haven't been deleted to the DeclSpec.
163 AttributeList *List = 0;
164 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
165 LeftOverAttrs[i]->setNext(List);
166 List = LeftOverAttrs[i];
167 }
168
169 DS.clearAttributes();
170 DS.AddAttributes(List);
171 //DS.setAttributes(List);
Chris Lattner06fb8672008-02-20 21:40:32 +0000172 return Result;
Chris Lattner4b009652007-07-25 00:24:17 +0000173}
174
175/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
176/// instances.
177QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000178 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000179 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000180 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
181 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
182
Chris Lattner726c5452008-02-20 23:53:49 +0000183 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroff91b03f72007-08-28 03:03:08 +0000184
Chris Lattner4b009652007-07-25 00:24:17 +0000185 // Apply const/volatile/restrict qualifiers to T.
186 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
187
188 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
189 // are ordered from the identifier out, which is opposite of what we want :).
190 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
191 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
192 switch (DeclType.Kind) {
193 default: assert(0 && "Unknown decltype!");
194 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000195 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000196 // C++ 8.3.2p4: There shall be no ... pointers to references ...
197 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000198 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000199 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000200 T = Context.IntTy;
201 }
202
203 // Apply the pointer typequals to the pointer object.
204 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
205 break;
206 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000207 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000208 // C++ 8.3.2p4: There shall be no references to references ...
209 Diag(D.getIdentifierLoc(),
210 diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000211 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000212 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000213 T = RT->getReferenceeType();
214 }
215
216 T = Context.getReferenceType(T);
217 break;
218 case DeclaratorChunk::Array: {
219 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000220 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000221 ArrayType::ArraySizeModifier ASM;
222 if (ATI.isStar)
223 ASM = ArrayType::Star;
224 else if (ATI.hasStatic)
225 ASM = ArrayType::Static;
226 else
227 ASM = ArrayType::Normal;
228
229 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
230 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
231 if (T->isIncompleteType()) {
232 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
233 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000234 T = Context.IntTy;
235 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000236 } else if (T->isFunctionType()) {
237 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000238 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000239 T = Context.getPointerType(T);
240 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000241 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000242 // C++ 8.3.2p4: There shall be no ... arrays of references ...
243 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000244 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000245 T = RT->getReferenceeType();
246 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000247 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000248 // If the element type is a struct or union that contains a variadic
249 // array, reject it: C99 6.7.2.1p2.
250 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
251 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
252 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000253 T = Context.IntTy;
254 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000255 }
256 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000257 // C99 6.7.5.2p1: The size expression shall have integer type.
258 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
259 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
260 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
261 D.setInvalidType(true);
262 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000263 llvm::APSInt ConstVal(32);
264 // If no expression was provided, we consider it a VLA.
Eli Friedman8ff07782008-02-15 18:16:39 +0000265 if (!ArraySize) {
266 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
267 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000268 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000269 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000270 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
271 // have a value greater than zero.
272 if (ConstVal.isSigned()) {
273 if (ConstVal.isNegative()) {
274 Diag(ArraySize->getLocStart(),
275 diag::err_typecheck_negative_array_size,
276 ArraySize->getSourceRange());
277 D.setInvalidType(true);
278 } else if (ConstVal == 0) {
279 // GCC accepts zero sized static arrays.
280 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
281 ArraySize->getSourceRange());
282 }
283 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000284 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000285 }
Chris Lattner47958f62007-08-28 16:54:00 +0000286 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
287 if (!getLangOptions().C99 &&
288 (ASM != ArrayType::Normal ||
289 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
290 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000291 break;
292 }
293 case DeclaratorChunk::Function:
294 // If the function declarator has a prototype (i.e. it is not () and
295 // does not have a K&R-style identifier list), then the arguments are part
296 // of the type, otherwise the argument list is ().
297 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000298
Chris Lattner6ad7e882007-12-19 05:31:29 +0000299 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000300 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000301 Diag(DeclType.Loc, diag::err_func_returning_array_function,
302 T.getAsString());
303 T = Context.IntTy;
304 D.setInvalidType(true);
305 }
306
Chris Lattner4b009652007-07-25 00:24:17 +0000307 if (!FTI.hasPrototype) {
308 // Simple void foo(), where the incoming T is the result type.
309 T = Context.getFunctionTypeNoProto(T);
310
311 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
312 if (FTI.NumArgs != 0)
313 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
314
315 } else {
316 // Otherwise, we have a function with an argument list that is
317 // potentially variadic.
318 llvm::SmallVector<QualType, 16> ArgTys;
319
320 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
321 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
322 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000323 //
324 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
325 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000326 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000327 // argument type in the function prototype *will not* match the
328 // type in ParmVarDecl (which makes the code generator unhappy).
329 //
330 // FIXME: We still apparently need the conversion in
331 // Sema::ParseParamDeclarator(). This doesn't make any sense, since
332 // it should be driving off the type being created here.
333 //
334 // FIXME: If a source translation tool needs to see the original type,
335 // then we need to consider storing both types somewhere...
336 //
Chris Lattnerc08564a2008-01-02 22:50:48 +0000337 if (const ArrayType *AT = ArgTy->getAsArrayType()) {
338 // int x[restrict 4] -> int *restrict
Steve Naroff1830be72007-09-10 22:17:00 +0000339 ArgTy = Context.getPointerType(AT->getElementType());
Chris Lattnerc08564a2008-01-02 22:50:48 +0000340 ArgTy = ArgTy.getQualifiedType(AT->getIndexTypeQualifier());
341 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000342 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000343 // Look for 'void'. void is allowed only as a single argument to a
344 // function with no other parameters (C99 6.7.5.3p10). We record
345 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000346 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000347 // If this is something like 'float(int, void)', reject it. 'void'
348 // is an incomplete type (C99 6.2.5p19) and function decls cannot
349 // have arguments of incomplete type.
350 if (FTI.NumArgs != 1 || FTI.isVariadic) {
351 Diag(DeclType.Loc, diag::err_void_only_param);
352 ArgTy = Context.IntTy;
353 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
354 } else if (FTI.ArgInfo[i].Ident) {
355 // Reject, but continue to parse 'int(void abc)'.
356 Diag(FTI.ArgInfo[i].IdentLoc,
357 diag::err_param_with_void_type);
358 ArgTy = Context.IntTy;
359 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
360 } else {
361 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000362 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000363 Diag(DeclType.Loc, diag::err_void_param_qualified);
364
365 // Do not add 'void' to the ArgTys list.
366 break;
367 }
368 }
369
370 ArgTys.push_back(ArgTy);
371 }
372 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
373 FTI.isVariadic);
374 }
375 break;
376 }
377 }
378
379 return T;
380}
381
Ted Kremenek42730c52008-01-07 19:49:32 +0000382/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000383/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000384QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
385 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000386 QualType T = MDecl->getResultType();
387 llvm::SmallVector<QualType, 16> ArgTys;
388
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000389 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000390 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000391 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000392 selfTy = Context.getPointerType(selfTy);
393 ArgTys.push_back(selfTy);
394 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000395 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000396 ArgTys.push_back(Context.getObjCIdType());
397 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000398
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000399 for (int i = 0; i < MDecl->getNumParams(); i++) {
400 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
401 QualType ArgTy = PDecl->getType();
402 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000403 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
404 // This matches the conversion that is done in
Steve Naroff434fa8d2007-11-12 03:44:46 +0000405 // Sema::ParseParamDeclarator().
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000406 if (const ArrayType *AT = ArgTy->getAsArrayType())
407 ArgTy = Context.getPointerType(AT->getElementType());
408 else if (ArgTy->isFunctionType())
409 ArgTy = Context.getPointerType(ArgTy);
410 ArgTys.push_back(ArgTy);
411 }
412 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000413 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000414 return T;
415}
416
Steve Naroff0acc9c92007-09-15 18:49:24 +0000417Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000418 // C99 6.7.6: Type names have no identifier. This is already validated by
419 // the parser.
420 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
421
422 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000423
424 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000425
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000426 // In this context, we *do not* check D.getInvalidType(). If the declarator
427 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
428 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000429 return T.getAsOpaquePtr();
430}
431
Steve Naroff91b03f72007-08-28 03:03:08 +0000432// Called from Parser::ParseParenDeclarator().
Steve Naroff0acc9c92007-09-15 18:49:24 +0000433Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000434 // Note: parameters have identifiers, but we don't care about them here, we
435 // just want the type converted.
436 QualType T = GetTypeForDeclarator(D, S);
437
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000438 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
439
440 // In this context, we *do not* check D.getInvalidType(). If the declarator
441 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
442 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000443 return T.getAsOpaquePtr();
444}