blob: 7e0a0f025bfa1e93f052f4c1e138669981cb2d7d [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.
24static QualType ConvertDeclSpecToType(const DeclSpec &DS, ASTContext &Ctx) {
25 // 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!
31 case DeclSpec::TST_void: return Ctx.VoidTy;
32 case DeclSpec::TST_char:
33 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner06fb8672008-02-20 21:40:32 +000034 Result = Ctx.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000035 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner06fb8672008-02-20 21:40:32 +000036 Result = Ctx.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000037 else {
38 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
39 "Unknown TSS value");
Chris Lattner06fb8672008-02-20 21:40:32 +000040 Result = Ctx.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 Lattner5328f312007-08-21 17:02:28 +000047 case DeclSpec::TSW_unspecified: Result = Ctx.IntTy; break;
48 case DeclSpec::TSW_short: Result = Ctx.ShortTy; break;
49 case DeclSpec::TSW_long: Result = Ctx.LongTy; break;
50 case DeclSpec::TSW_longlong: Result = Ctx.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000051 }
52 } else {
53 switch (DS.getTypeSpecWidth()) {
Chris Lattner5328f312007-08-21 17:02:28 +000054 case DeclSpec::TSW_unspecified: Result = Ctx.UnsignedIntTy; break;
55 case DeclSpec::TSW_short: Result = Ctx.UnsignedShortTy; break;
56 case DeclSpec::TSW_long: Result = Ctx.UnsignedLongTy; break;
57 case DeclSpec::TSW_longlong: Result = Ctx.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 Lattner06fb8672008-02-20 21:40:32 +000062 case DeclSpec::TST_float: Result = Ctx.FloatTy; break;
63 case DeclSpec::TST_double:
64 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
65 Result = Ctx.LongDoubleTy;
66 else
67 Result = Ctx.DoubleTy;
68 break;
69 case DeclSpec::TST_bool: Result = Ctx.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 Lattner06fb8672008-02-20 21:40:32 +000083 Result = Ctx.getTagDeclType(cast<TagDecl>(D));
84 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) {
96 Result = Ctx.getObjCInterfaceType(ObjCIntDecl);
97 break;
98 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +000099
100 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner06fb8672008-02-20 21:40:32 +0000101 Result = Ctx.getObjCQualifiedInterfaceType(ObjCIntDecl,
102 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
103 DS.NumProtocolQualifiers());
104 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)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000107 if (Ctx.getObjCIdType() == Ctx.getTypedefType(typeDecl)
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000108 && DS.getProtocolQualifiers()) {
109 // id<protocol-list>
110 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner06fb8672008-02-20 21:40:32 +0000111 Result = Ctx.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
112 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 Lattner06fb8672008-02-20 21:40:32 +0000118 Result = Ctx.getTypedefType(cast<TypedefDecl>(D));
119 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 Lattner06fb8672008-02-20 21:40:32 +0000125 Result = Ctx.getTypeOfType(Result);
126 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 Lattner06fb8672008-02-20 21:40:32 +0000131 Result = Ctx.getTypeOfExpr(E);
132 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)
138 Result = Ctx.getComplexType(Result);
139
140 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
141 "FIXME: imaginary types not supported yet!");
142
143 return Result;
Chris Lattner4b009652007-07-25 00:24:17 +0000144}
145
146/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
147/// instances.
148QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000149 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000150 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000151 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
152 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
153
Chris Lattner4b009652007-07-25 00:24:17 +0000154 QualType T = ConvertDeclSpecToType(D.getDeclSpec(), Context);
Steve Naroff91b03f72007-08-28 03:03:08 +0000155
Chris Lattner4b009652007-07-25 00:24:17 +0000156 // Apply const/volatile/restrict qualifiers to T.
157 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
158
159 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
160 // are ordered from the identifier out, which is opposite of what we want :).
161 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
162 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
163 switch (DeclType.Kind) {
164 default: assert(0 && "Unknown decltype!");
165 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000166 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000167 // C++ 8.3.2p4: There shall be no ... pointers to references ...
168 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000169 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000170 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000171 T = Context.IntTy;
172 }
173
174 // Apply the pointer typequals to the pointer object.
175 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
176 break;
177 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000178 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000179 // C++ 8.3.2p4: There shall be no references to references ...
180 Diag(D.getIdentifierLoc(),
181 diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000182 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000183 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000184 T = RT->getReferenceeType();
185 }
186
187 T = Context.getReferenceType(T);
188 break;
189 case DeclaratorChunk::Array: {
190 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000191 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000192 ArrayType::ArraySizeModifier ASM;
193 if (ATI.isStar)
194 ASM = ArrayType::Star;
195 else if (ATI.hasStatic)
196 ASM = ArrayType::Static;
197 else
198 ASM = ArrayType::Normal;
199
200 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
201 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
202 if (T->isIncompleteType()) {
203 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
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 } else if (T->isFunctionType()) {
208 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000209 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000210 T = Context.getPointerType(T);
211 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000212 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000213 // C++ 8.3.2p4: There shall be no ... arrays of references ...
214 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000215 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000216 T = RT->getReferenceeType();
217 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000218 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000219 // If the element type is a struct or union that contains a variadic
220 // array, reject it: C99 6.7.2.1p2.
221 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
222 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
223 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000224 T = Context.IntTy;
225 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000226 }
227 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000228 // C99 6.7.5.2p1: The size expression shall have integer type.
229 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
230 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
231 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
232 D.setInvalidType(true);
233 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000234 llvm::APSInt ConstVal(32);
235 // If no expression was provided, we consider it a VLA.
Eli Friedman8ff07782008-02-15 18:16:39 +0000236 if (!ArraySize) {
237 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
238 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000239 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000240 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000241 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
242 // have a value greater than zero.
243 if (ConstVal.isSigned()) {
244 if (ConstVal.isNegative()) {
245 Diag(ArraySize->getLocStart(),
246 diag::err_typecheck_negative_array_size,
247 ArraySize->getSourceRange());
248 D.setInvalidType(true);
249 } else if (ConstVal == 0) {
250 // GCC accepts zero sized static arrays.
251 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
252 ArraySize->getSourceRange());
253 }
254 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000255 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000256 }
Chris Lattner47958f62007-08-28 16:54:00 +0000257 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
258 if (!getLangOptions().C99 &&
259 (ASM != ArrayType::Normal ||
260 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
261 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000262 break;
263 }
264 case DeclaratorChunk::Function:
265 // If the function declarator has a prototype (i.e. it is not () and
266 // does not have a K&R-style identifier list), then the arguments are part
267 // of the type, otherwise the argument list is ().
268 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000269
Chris Lattner6ad7e882007-12-19 05:31:29 +0000270 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000271 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000272 Diag(DeclType.Loc, diag::err_func_returning_array_function,
273 T.getAsString());
274 T = Context.IntTy;
275 D.setInvalidType(true);
276 }
277
Chris Lattner4b009652007-07-25 00:24:17 +0000278 if (!FTI.hasPrototype) {
279 // Simple void foo(), where the incoming T is the result type.
280 T = Context.getFunctionTypeNoProto(T);
281
282 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
283 if (FTI.NumArgs != 0)
284 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
285
286 } else {
287 // Otherwise, we have a function with an argument list that is
288 // potentially variadic.
289 llvm::SmallVector<QualType, 16> ArgTys;
290
291 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
292 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
293 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000294 //
295 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
296 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000297 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000298 // argument type in the function prototype *will not* match the
299 // type in ParmVarDecl (which makes the code generator unhappy).
300 //
301 // FIXME: We still apparently need the conversion in
302 // Sema::ParseParamDeclarator(). This doesn't make any sense, since
303 // it should be driving off the type being created here.
304 //
305 // FIXME: If a source translation tool needs to see the original type,
306 // then we need to consider storing both types somewhere...
307 //
Chris Lattnerc08564a2008-01-02 22:50:48 +0000308 if (const ArrayType *AT = ArgTy->getAsArrayType()) {
309 // int x[restrict 4] -> int *restrict
Steve Naroff1830be72007-09-10 22:17:00 +0000310 ArgTy = Context.getPointerType(AT->getElementType());
Chris Lattnerc08564a2008-01-02 22:50:48 +0000311 ArgTy = ArgTy.getQualifiedType(AT->getIndexTypeQualifier());
312 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000313 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000314 // Look for 'void'. void is allowed only as a single argument to a
315 // function with no other parameters (C99 6.7.5.3p10). We record
316 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000317 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000318 // If this is something like 'float(int, void)', reject it. 'void'
319 // is an incomplete type (C99 6.2.5p19) and function decls cannot
320 // have arguments of incomplete type.
321 if (FTI.NumArgs != 1 || FTI.isVariadic) {
322 Diag(DeclType.Loc, diag::err_void_only_param);
323 ArgTy = Context.IntTy;
324 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
325 } else if (FTI.ArgInfo[i].Ident) {
326 // Reject, but continue to parse 'int(void abc)'.
327 Diag(FTI.ArgInfo[i].IdentLoc,
328 diag::err_param_with_void_type);
329 ArgTy = Context.IntTy;
330 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
331 } else {
332 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000333 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000334 Diag(DeclType.Loc, diag::err_void_param_qualified);
335
336 // Do not add 'void' to the ArgTys list.
337 break;
338 }
339 }
340
341 ArgTys.push_back(ArgTy);
342 }
343 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
344 FTI.isVariadic);
345 }
346 break;
347 }
348 }
349
350 return T;
351}
352
Ted Kremenek42730c52008-01-07 19:49:32 +0000353/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000354/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000355QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
356 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000357 QualType T = MDecl->getResultType();
358 llvm::SmallVector<QualType, 16> ArgTys;
359
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000360 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000361 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000362 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000363 selfTy = Context.getPointerType(selfTy);
364 ArgTys.push_back(selfTy);
365 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000366 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000367 ArgTys.push_back(Context.getObjCIdType());
368 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000369
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000370 for (int i = 0; i < MDecl->getNumParams(); i++) {
371 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
372 QualType ArgTy = PDecl->getType();
373 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000374 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
375 // This matches the conversion that is done in
Steve Naroff434fa8d2007-11-12 03:44:46 +0000376 // Sema::ParseParamDeclarator().
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000377 if (const ArrayType *AT = ArgTy->getAsArrayType())
378 ArgTy = Context.getPointerType(AT->getElementType());
379 else if (ArgTy->isFunctionType())
380 ArgTy = Context.getPointerType(ArgTy);
381 ArgTys.push_back(ArgTy);
382 }
383 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000384 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000385 return T;
386}
387
Steve Naroff0acc9c92007-09-15 18:49:24 +0000388Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000389 // C99 6.7.6: Type names have no identifier. This is already validated by
390 // the parser.
391 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
392
393 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000394
395 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000396
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000397 // In this context, we *do not* check D.getInvalidType(). If the declarator
398 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
399 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000400 return T.getAsOpaquePtr();
401}
402
Steve Naroff91b03f72007-08-28 03:03:08 +0000403// Called from Parser::ParseParenDeclarator().
Steve Naroff0acc9c92007-09-15 18:49:24 +0000404Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000405 // Note: parameters have identifiers, but we don't care about them here, we
406 // just want the type converted.
407 QualType T = GetTypeForDeclarator(D, S);
408
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000409 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
410
411 // In this context, we *do not* check D.getInvalidType(). If the declarator
412 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
413 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000414 return T.getAsOpaquePtr();
415}