blob: 45bae6d2f0bc0c9d4dcdf3e172f8d88a70fca89f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattnerb23deda2007-08-28 16:40:32 +000019#include "clang/Basic/LangOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
22/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
23/// type object. This returns null on error.
Chris Lattnerfab5b452008-02-20 23:53:49 +000024QualType Sema::ConvertDeclSpecToType(DeclSpec &DS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000025 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
26 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000027 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000028
29 switch (DS.getTypeSpecType()) {
30 default: return QualType(); // FIXME: Handle unimp cases!
Chris Lattnerfab5b452008-02-20 23:53:49 +000031 case DeclSpec::TST_void: return Context.VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000032 case DeclSpec::TST_char:
33 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000034 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000036 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 else {
38 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
39 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000040 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 }
Chris Lattner958858e2008-02-20 21:40:32 +000042 break;
Chris Lattner7a543ad2007-07-13 21:02:29 +000043 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000044 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000045 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
46 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000051 }
52 } else {
53 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000058 }
59 }
Chris Lattner958858e2008-02-20 21:40:32 +000060 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +000061 }
Chris Lattnerfab5b452008-02-20 23:53:49 +000062 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +000063 case DeclSpec::TST_double:
64 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +000065 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +000066 else
Chris Lattnerfab5b452008-02-20 23:53:49 +000067 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +000068 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +000069 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerfab5b452008-02-20 23:53:49 +000083 Result = Context.getTagDeclType(cast<TagDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +000084 break;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff3536b442007-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 Kremeneka526c5c2008-01-07 19:49:32 +000094 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner958858e2008-02-20 21:40:32 +000095 if (DS.getProtocolQualifiers() == 0) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000096 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner958858e2008-02-20 21:40:32 +000097 break;
98 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +000099
100 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattnerfab5b452008-02-20 23:53:49 +0000101 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattner958858e2008-02-20 21:40:32 +0000102 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattnerfab5b452008-02-20 23:53:49 +0000103 DS.NumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000104 break;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000105 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000106 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000107 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000108 && DS.getProtocolQualifiers()) {
109 // id<protocol-list>
110 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattnerfab5b452008-02-20 23:53:49 +0000111 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
Chris Lattner958858e2008-02-20 21:40:32 +0000112 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
113 DS.NumProtocolQualifiers());
114 break;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000115 }
116 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000118 Result = Context.getTypedefType(cast<TypedefDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000119 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
Chris Lattner958858e2008-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 Naroffd1861fd2007-07-31 12:34:36 +0000124 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000125 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000126 break;
Steve Naroffd1861fd2007-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 Lattnerfab5b452008-02-20 23:53:49 +0000131 Result = Context.getTypeOfExpr(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000132 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000133 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 }
Chris Lattner958858e2008-02-20 21:40:32 +0000135
136 // Handle complex types.
137 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000138 Result = Context.getComplexType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000139
140 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
141 "FIXME: imaginary types not supported yet!");
142
Chris Lattner38d8b982008-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).
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000145 if (AttributeList *AL = DS.getAttributes())
146 DS.SetAttributes(ProcessTypeAttributes(Result, AL));
147
148 return Result;
149}
150
Reid Spencer5f016e22007-07-11 17:01:13 +0000151/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
152/// instances.
153QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattnerb23deda2007-08-28 16:40:32 +0000154 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000155 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000156 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
157 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
158
Chris Lattnerfab5b452008-02-20 23:53:49 +0000159 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroffe1223f72007-08-28 03:03:08 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // Apply const/volatile/restrict qualifiers to T.
162 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
163
164 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
165 // are ordered from the identifier out, which is opposite of what we want :).
166 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
167 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
168 switch (DeclType.Kind) {
169 default: assert(0 && "Unknown decltype!");
170 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000171 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 // C++ 8.3.2p4: There shall be no ... pointers to references ...
173 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000174 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000175 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000176 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 }
178
179 // Apply the pointer typequals to the pointer object.
180 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
181 break;
182 case DeclaratorChunk::Reference:
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000183 if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 // C++ 8.3.2p4: There shall be no references to references ...
185 Diag(D.getIdentifierLoc(),
186 diag::err_illegal_decl_reference_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000187 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000188 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000189 T = RT->getReferenceeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 }
191
192 T = Context.getReferenceType(T);
193 break;
194 case DeclaratorChunk::Array: {
195 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000196 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 ArrayType::ArraySizeModifier ASM;
198 if (ATI.isStar)
199 ASM = ArrayType::Star;
200 else if (ATI.hasStatic)
201 ASM = ArrayType::Static;
202 else
203 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
206 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
207 if (T->isIncompleteType()) {
208 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
209 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000210 T = Context.IntTy;
211 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000212 } else if (T->isFunctionType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000214 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000215 T = Context.getPointerType(T);
216 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000217 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 // C++ 8.3.2p4: There shall be no ... arrays of references ...
219 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000220 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000221 T = RT->getReferenceeType();
222 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000223 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // If the element type is a struct or union that contains a variadic
225 // array, reject it: C99 6.7.2.1p2.
226 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
227 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
228 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000229 T = Context.IntTy;
230 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 }
Steve Naroff42471f82007-08-30 22:35:45 +0000233 // C99 6.7.5.2p1: The size expression shall have integer type.
234 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
235 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
236 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
237 D.setInvalidType(true);
238 }
Steve Naroffc9406122007-08-30 18:10:14 +0000239 llvm::APSInt ConstVal(32);
240 // If no expression was provided, we consider it a VLA.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000241 if (!ArraySize) {
242 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
243 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroffc9406122007-08-30 18:10:14 +0000244 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000245 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000246 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
247 // have a value greater than zero.
248 if (ConstVal.isSigned()) {
249 if (ConstVal.isNegative()) {
250 Diag(ArraySize->getLocStart(),
251 diag::err_typecheck_negative_array_size,
252 ArraySize->getSourceRange());
253 D.setInvalidType(true);
254 } else if (ConstVal == 0) {
255 // GCC accepts zero sized static arrays.
256 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
257 ArraySize->getSourceRange());
258 }
259 }
Steve Naroffc9406122007-08-30 18:10:14 +0000260 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000261 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000262 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
263 if (!getLangOptions().C99 &&
264 (ASM != ArrayType::Normal ||
265 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
266 Diag(D.getIdentifierLoc(), diag::ext_vla);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 break;
268 }
269 case DeclaratorChunk::Function:
270 // If the function declarator has a prototype (i.e. it is not () and
271 // does not have a K&R-style identifier list), then the arguments are part
272 // of the type, otherwise the argument list is ().
273 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000274
Chris Lattnercd881292007-12-19 05:31:29 +0000275 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000276 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnercd881292007-12-19 05:31:29 +0000277 Diag(DeclType.Loc, diag::err_func_returning_array_function,
278 T.getAsString());
279 T = Context.IntTy;
280 D.setInvalidType(true);
281 }
282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 if (!FTI.hasPrototype) {
284 // Simple void foo(), where the incoming T is the result type.
285 T = Context.getFunctionTypeNoProto(T);
286
287 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
288 if (FTI.NumArgs != 0)
289 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
290
291 } else {
292 // Otherwise, we have a function with an argument list that is
293 // potentially variadic.
294 llvm::SmallVector<QualType, 16> ArgTys;
295
296 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
297 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
Chris Lattner78c75fb2007-07-21 05:30:18 +0000298 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000299 //
300 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
301 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000302 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000303 // argument type in the function prototype *will not* match the
304 // type in ParmVarDecl (which makes the code generator unhappy).
305 //
306 // FIXME: We still apparently need the conversion in
307 // Sema::ParseParamDeclarator(). This doesn't make any sense, since
308 // it should be driving off the type being created here.
309 //
310 // FIXME: If a source translation tool needs to see the original type,
311 // then we need to consider storing both types somewhere...
312 //
Chris Lattner529bd022008-01-02 22:50:48 +0000313 if (const ArrayType *AT = ArgTy->getAsArrayType()) {
314 // int x[restrict 4] -> int *restrict
Steve Naroff08d51392007-09-10 22:17:00 +0000315 ArgTy = Context.getPointerType(AT->getElementType());
Chris Lattner529bd022008-01-02 22:50:48 +0000316 ArgTy = ArgTy.getQualifiedType(AT->getIndexTypeQualifier());
317 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000318 ArgTy = Context.getPointerType(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Look for 'void'. void is allowed only as a single argument to a
320 // function with no other parameters (C99 6.7.5.3p10). We record
321 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000322 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // If this is something like 'float(int, void)', reject it. 'void'
324 // is an incomplete type (C99 6.2.5p19) and function decls cannot
325 // have arguments of incomplete type.
326 if (FTI.NumArgs != 1 || FTI.isVariadic) {
327 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000328 ArgTy = Context.IntTy;
Chris Lattner78c75fb2007-07-21 05:30:18 +0000329 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
Chris Lattner2ff54262007-07-21 05:18:12 +0000330 } else if (FTI.ArgInfo[i].Ident) {
331 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000333 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000334 ArgTy = Context.IntTy;
Chris Lattner78c75fb2007-07-21 05:30:18 +0000335 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
Chris Lattner2ff54262007-07-21 05:18:12 +0000336 } else {
337 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000338 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000339 Diag(DeclType.Loc, diag::err_void_param_qualified);
340
341 // Do not add 'void' to the ArgTys list.
342 break;
343 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 }
345
346 ArgTys.push_back(ArgTy);
347 }
348 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
349 FTI.isVariadic);
350 }
351 break;
352 }
353 }
354
355 return T;
356}
357
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000358/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000359/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000360QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
361 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000362 QualType T = MDecl->getResultType();
363 llvm::SmallVector<QualType, 16> ArgTys;
364
Fariborz Jahanian35600022007-11-09 17:18:29 +0000365 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000366 if (MDecl->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000367 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000368 selfTy = Context.getPointerType(selfTy);
369 ArgTys.push_back(selfTy);
370 }
Fariborz Jahanian35600022007-11-09 17:18:29 +0000371 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000372 ArgTys.push_back(Context.getObjCIdType());
373 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000374
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000375 for (int i = 0; i < MDecl->getNumParams(); i++) {
376 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
377 QualType ArgTy = PDecl->getType();
378 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000379 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
380 // This matches the conversion that is done in
Steve Naroff66499922007-11-12 03:44:46 +0000381 // Sema::ParseParamDeclarator().
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000382 if (const ArrayType *AT = ArgTy->getAsArrayType())
383 ArgTy = Context.getPointerType(AT->getElementType());
384 else if (ArgTy->isFunctionType())
385 ArgTy = Context.getPointerType(ArgTy);
386 ArgTys.push_back(ArgTy);
387 }
388 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffb99a4a32007-12-18 03:41:15 +0000389 MDecl->isVariadic());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000390 return T;
391}
392
Steve Naroff08d92e42007-09-15 18:49:24 +0000393Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 // C99 6.7.6: Type names have no identifier. This is already validated by
395 // the parser.
396 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
397
398 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000399
400 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000401
Steve Naroff5912a352007-08-28 20:14:24 +0000402 // In this context, we *do not* check D.getInvalidType(). If the declarator
403 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
404 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 return T.getAsOpaquePtr();
406}
407
Steve Naroffe1223f72007-08-28 03:03:08 +0000408// Called from Parser::ParseParenDeclarator().
Steve Naroff08d92e42007-09-15 18:49:24 +0000409Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 // Note: parameters have identifiers, but we don't care about them here, we
411 // just want the type converted.
412 QualType T = GetTypeForDeclarator(D, S);
413
Steve Naroff5912a352007-08-28 20:14:24 +0000414 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
415
416 // In this context, we *do not* check D.getInvalidType(). If the declarator
417 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
418 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 return T.getAsOpaquePtr();
420}
Chris Lattner232e8822008-02-21 01:08:11 +0000421
422AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){
423 // Scan through and apply attributes to this type where it makes sense. Some
424 // attributes (such as __address_space__, __vector_size__, etc) apply to the
425 // type, but others can be present in the type specifiers even though they
426 // apply to the decl. Here we apply and delete attributes that apply to the
427 // type and leave the others alone.
428 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
429 while (AL) {
430 // Unlink this attribute from the chain, so we can process it independently.
431 AttributeList *ThisAttr = AL;
432 AL = AL->getNext();
433 ThisAttr->setNext(0);
434
435 // If this is an attribute we can handle, do so now, otherwise, add it to
436 // the LeftOverAttrs list for rechaining.
437 switch (ThisAttr->getKind()) {
438 default: break;
439 case AttributeList::AT_address_space:
440 Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr);
441 delete ThisAttr; // Consume the attribute.
442 continue;
443 }
444
445 LeftOverAttrs.push_back(ThisAttr);
446 }
447
448 // Rechain any attributes that haven't been deleted to the DeclSpec.
449 AttributeList *List = 0;
450 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
451 LeftOverAttrs[i]->setNext(List);
452 List = LeftOverAttrs[i];
453 }
454
455 return List;
456}
457
458/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
459/// specified type.
460QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type,
461 AttributeList *Attr) {
462 // If this type is already address space qualified, reject it.
463 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
464 // for two or more different address spaces."
465 if (Type.getAddressSpace()) {
466 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers);
467 return Type;
468 }
469
470 // Check the attribute arguments.
471 if (Attr->getNumArgs() != 1) {
472 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
473 std::string("1"));
474 return Type;
475 }
476 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0));
477 llvm::APSInt addrSpace(32);
478 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) {
479 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int,
480 ASArgExpr->getSourceRange());
481 return Type;
482 }
483
484 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
485 return Context.getASQualType(Type, ASIdx);
486}
487