blob: c4a9c0feaf56a1e52058beb9041bf79fa8c25da2 [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.
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...
Ted Kremenek42730c52008-01-07 19:49:32 +0000107 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000108 if (DS.getProtocolQualifiers() == 0)
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 return Ctx.getObjCInterfaceType(ObjCIntDecl);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000110
111 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Ted Kremenek42730c52008-01-07 19:49:32 +0000112 return Ctx.getObjCQualifiedInterfaceType(ObjCIntDecl,
113 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000114 DS.NumProtocolQualifiers());
115 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000116 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000117 if (Ctx.getObjCIdType() == Ctx.getTypedefType(typeDecl)
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000118 && DS.getProtocolQualifiers()) {
119 // id<protocol-list>
120 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Ted Kremenek42730c52008-01-07 19:49:32 +0000121 return Ctx.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
122 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000123 DS.NumProtocolQualifiers());
124 }
125 }
Chris Lattner4b009652007-07-25 00:24:17 +0000126 // TypeQuals handled by caller.
127 return Ctx.getTypedefType(cast<TypedefDecl>(D));
128 }
Steve Naroff7cbb1462007-07-31 12:34:36 +0000129 case DeclSpec::TST_typeofType: {
130 QualType T = QualType::getFromOpaquePtr(DS.getTypeRep());
131 assert(!T.isNull() && "Didn't get a type for typeof?");
132 // TypeQuals handled by caller.
133 return Ctx.getTypeOfType(T);
134 }
135 case DeclSpec::TST_typeofExpr: {
136 Expr *E = static_cast<Expr *>(DS.getTypeRep());
137 assert(E && "Didn't get an expression for typeof?");
138 // TypeQuals handled by caller.
Steve Naroff11b649c2007-08-01 17:20:42 +0000139 return Ctx.getTypeOfExpr(E);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000140 }
Chris Lattner4b009652007-07-25 00:24:17 +0000141 }
142}
143
144/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
145/// instances.
146QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000147 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000148 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000149 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
150 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
151
Chris Lattner4b009652007-07-25 00:24:17 +0000152 QualType T = ConvertDeclSpecToType(D.getDeclSpec(), Context);
Steve Naroff91b03f72007-08-28 03:03:08 +0000153
Chris Lattner4b009652007-07-25 00:24:17 +0000154 // Apply const/volatile/restrict qualifiers to T.
155 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
156
157 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
158 // are ordered from the identifier out, which is opposite of what we want :).
159 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
160 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
161 switch (DeclType.Kind) {
162 default: assert(0 && "Unknown decltype!");
163 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000164 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000165 // C++ 8.3.2p4: There shall be no ... pointers to references ...
166 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
167 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000168 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000169 T = Context.IntTy;
170 }
171
172 // Apply the pointer typequals to the pointer object.
173 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
174 break;
175 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000176 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000177 // C++ 8.3.2p4: There shall be no references to references ...
178 Diag(D.getIdentifierLoc(),
179 diag::err_illegal_decl_reference_to_reference,
180 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000181 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000182 T = RT->getReferenceeType();
183 }
184
185 T = Context.getReferenceType(T);
186 break;
187 case DeclaratorChunk::Array: {
188 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000189 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000190 ArrayType::ArraySizeModifier ASM;
191 if (ATI.isStar)
192 ASM = ArrayType::Star;
193 else if (ATI.hasStatic)
194 ASM = ArrayType::Static;
195 else
196 ASM = ArrayType::Normal;
197
198 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
199 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
200 if (T->isIncompleteType()) {
201 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
202 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000203 T = Context.IntTy;
204 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000205 } else if (T->isFunctionType()) {
206 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
207 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000208 T = Context.getPointerType(T);
209 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000210 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000211 // C++ 8.3.2p4: There shall be no ... arrays of references ...
212 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
213 D.getIdentifier()->getName());
Steve Naroff91b03f72007-08-28 03:03:08 +0000214 T = RT->getReferenceeType();
215 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000216 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000217 // If the element type is a struct or union that contains a variadic
218 // array, reject it: C99 6.7.2.1p2.
219 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
220 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
221 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000222 T = Context.IntTy;
223 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000224 }
225 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000226 // C99 6.7.5.2p1: The size expression shall have integer type.
227 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
228 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
229 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
230 D.setInvalidType(true);
231 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000232 llvm::APSInt ConstVal(32);
233 // If no expression was provided, we consider it a VLA.
234 if (!ArraySize || !ArraySize->isIntegerConstantExpr(ConstVal, Context))
235 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000236 else {
237 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
238 // have a value greater than zero.
239 if (ConstVal.isSigned()) {
240 if (ConstVal.isNegative()) {
241 Diag(ArraySize->getLocStart(),
242 diag::err_typecheck_negative_array_size,
243 ArraySize->getSourceRange());
244 D.setInvalidType(true);
245 } else if (ConstVal == 0) {
246 // GCC accepts zero sized static arrays.
247 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
248 ArraySize->getSourceRange());
249 }
250 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000251 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000252 }
Chris Lattner47958f62007-08-28 16:54:00 +0000253 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
254 if (!getLangOptions().C99 &&
255 (ASM != ArrayType::Normal ||
256 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
257 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000258 break;
259 }
260 case DeclaratorChunk::Function:
261 // If the function declarator has a prototype (i.e. it is not () and
262 // does not have a K&R-style identifier list), then the arguments are part
263 // of the type, otherwise the argument list is ().
264 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000265
Chris Lattner6ad7e882007-12-19 05:31:29 +0000266 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000267 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000268 Diag(DeclType.Loc, diag::err_func_returning_array_function,
269 T.getAsString());
270 T = Context.IntTy;
271 D.setInvalidType(true);
272 }
273
Chris Lattner4b009652007-07-25 00:24:17 +0000274 if (!FTI.hasPrototype) {
275 // Simple void foo(), where the incoming T is the result type.
276 T = Context.getFunctionTypeNoProto(T);
277
278 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
279 if (FTI.NumArgs != 0)
280 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
281
282 } else {
283 // Otherwise, we have a function with an argument list that is
284 // potentially variadic.
285 llvm::SmallVector<QualType, 16> ArgTys;
286
287 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
288 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
289 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000290 //
291 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
292 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000293 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000294 // argument type in the function prototype *will not* match the
295 // type in ParmVarDecl (which makes the code generator unhappy).
296 //
297 // FIXME: We still apparently need the conversion in
298 // Sema::ParseParamDeclarator(). This doesn't make any sense, since
299 // it should be driving off the type being created here.
300 //
301 // FIXME: If a source translation tool needs to see the original type,
302 // then we need to consider storing both types somewhere...
303 //
Chris Lattnerc08564a2008-01-02 22:50:48 +0000304 if (const ArrayType *AT = ArgTy->getAsArrayType()) {
305 // int x[restrict 4] -> int *restrict
Steve Naroff1830be72007-09-10 22:17:00 +0000306 ArgTy = Context.getPointerType(AT->getElementType());
Chris Lattnerc08564a2008-01-02 22:50:48 +0000307 ArgTy = ArgTy.getQualifiedType(AT->getIndexTypeQualifier());
308 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000309 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000310 // Look for 'void'. void is allowed only as a single argument to a
311 // function with no other parameters (C99 6.7.5.3p10). We record
312 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000313 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000314 // If this is something like 'float(int, void)', reject it. 'void'
315 // is an incomplete type (C99 6.2.5p19) and function decls cannot
316 // have arguments of incomplete type.
317 if (FTI.NumArgs != 1 || FTI.isVariadic) {
318 Diag(DeclType.Loc, diag::err_void_only_param);
319 ArgTy = Context.IntTy;
320 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
321 } else if (FTI.ArgInfo[i].Ident) {
322 // Reject, but continue to parse 'int(void abc)'.
323 Diag(FTI.ArgInfo[i].IdentLoc,
324 diag::err_param_with_void_type);
325 ArgTy = Context.IntTy;
326 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
327 } else {
328 // Reject, but continue to parse 'float(const void)'.
329 if (ArgTy.getQualifiers())
330 Diag(DeclType.Loc, diag::err_void_param_qualified);
331
332 // Do not add 'void' to the ArgTys list.
333 break;
334 }
335 }
336
337 ArgTys.push_back(ArgTy);
338 }
339 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
340 FTI.isVariadic);
341 }
342 break;
343 }
344 }
345
346 return T;
347}
348
Ted Kremenek42730c52008-01-07 19:49:32 +0000349/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000350/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000351QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
352 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000353 QualType T = MDecl->getResultType();
354 llvm::SmallVector<QualType, 16> ArgTys;
355
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000356 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000357 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000358 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000359 selfTy = Context.getPointerType(selfTy);
360 ArgTys.push_back(selfTy);
361 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000362 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000363 ArgTys.push_back(Context.getObjCIdType());
364 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000365
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000366 for (int i = 0; i < MDecl->getNumParams(); i++) {
367 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
368 QualType ArgTy = PDecl->getType();
369 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000370 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
371 // This matches the conversion that is done in
Steve Naroff434fa8d2007-11-12 03:44:46 +0000372 // Sema::ParseParamDeclarator().
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000373 if (const ArrayType *AT = ArgTy->getAsArrayType())
374 ArgTy = Context.getPointerType(AT->getElementType());
375 else if (ArgTy->isFunctionType())
376 ArgTy = Context.getPointerType(ArgTy);
377 ArgTys.push_back(ArgTy);
378 }
379 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000380 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000381 return T;
382}
383
Steve Naroff0acc9c92007-09-15 18:49:24 +0000384Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000385 // C99 6.7.6: Type names have no identifier. This is already validated by
386 // the parser.
387 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
388
389 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000390
391 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000392
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000393 // In this context, we *do not* check D.getInvalidType(). If the declarator
394 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
395 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000396 return T.getAsOpaquePtr();
397}
398
Steve Naroff91b03f72007-08-28 03:03:08 +0000399// Called from Parser::ParseParenDeclarator().
Steve Naroff0acc9c92007-09-15 18:49:24 +0000400Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000401 // Note: parameters have identifiers, but we don't care about them here, we
402 // just want the type converted.
403 QualType T = GetTypeForDeclarator(D, S);
404
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000405 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
406
407 // In this context, we *do not* check D.getInvalidType(). If the declarator
408 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
409 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000410 return T.getAsOpaquePtr();
411}