blob: ad9d138f91b88aa6090ae983f353cc1ee42e18bb [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 Lattner69f01932008-02-21 01:32:26 +0000103 DS.getNumProtocolQualifiers());
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),
Chris Lattner69f01932008-02-21 01:32:26 +0000113 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000114 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).
Chris Lattner9e982502008-02-21 01:07:18 +0000145 if (AttributeList *AL = DS.getAttributes())
146 DS.SetAttributes(ProcessTypeAttributes(Result, AL));
147
148 return Result;
149}
150
Chris Lattner4b009652007-07-25 00:24:17 +0000151/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
152/// instances.
153QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000154 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000155 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000156 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
157 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
158
Chris Lattner726c5452008-02-20 23:53:49 +0000159 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroff91b03f72007-08-28 03:03:08 +0000160
Chris Lattner4b009652007-07-25 00:24:17 +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) {
Chris Lattner69f01932008-02-21 01:32:26 +0000167 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000168 switch (DeclType.Kind) {
169 default: assert(0 && "Unknown decltype!");
170 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000171 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000172 // C++ 8.3.2p4: There shall be no ... pointers to references ...
173 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000174 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000175 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000176 T = Context.IntTy;
177 }
178
179 // Apply the pointer typequals to the pointer object.
180 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
Chris Lattner69f01932008-02-21 01:32:26 +0000181
182 // See if there are any attributes on the pointer that apply to it.
183 if (AttributeList *AL = DeclType.Ptr.AttrList)
184 DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL);
185
Chris Lattner4b009652007-07-25 00:24:17 +0000186 break;
187 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000188 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000189 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner4b009652007-07-25 00:24:17 +0000190 Diag(D.getIdentifierLoc(),
191 diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000192 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000193 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000194 T = RT->getReferenceeType();
195 }
196
197 T = Context.getReferenceType(T);
Chris Lattner69f01932008-02-21 01:32:26 +0000198
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000199 // FIXME: Handle Ref.Restrict!
200
Chris Lattner69f01932008-02-21 01:32:26 +0000201 // See if there are any attributes on the pointer that apply to it.
202 if (AttributeList *AL = DeclType.Ref.AttrList)
203 DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000204 break;
205 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000206 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000207 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000208 ArrayType::ArraySizeModifier ASM;
209 if (ATI.isStar)
210 ASM = ArrayType::Star;
211 else if (ATI.hasStatic)
212 ASM = ArrayType::Static;
213 else
214 ASM = ArrayType::Normal;
215
216 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
217 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
218 if (T->isIncompleteType()) {
219 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
220 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000221 T = Context.IntTy;
222 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000223 } else if (T->isFunctionType()) {
224 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000225 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000226 T = Context.getPointerType(T);
227 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000228 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000229 // C++ 8.3.2p4: There shall be no ... arrays of references ...
230 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000231 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000232 T = RT->getReferenceeType();
233 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000234 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000235 // If the element type is a struct or union that contains a variadic
236 // array, reject it: C99 6.7.2.1p2.
237 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
238 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
239 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000240 T = Context.IntTy;
241 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000242 }
243 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000244 // C99 6.7.5.2p1: The size expression shall have integer type.
245 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
246 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
247 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
248 D.setInvalidType(true);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000249 delete ArraySize;
250 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000251 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000252 llvm::APSInt ConstVal(32);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000253 // If no expression was provided, we consider it an incomplete array.
Eli Friedman8ff07782008-02-15 18:16:39 +0000254 if (!ArraySize) {
255 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
256 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000257 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000258 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000259 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
260 // have a value greater than zero.
261 if (ConstVal.isSigned()) {
262 if (ConstVal.isNegative()) {
263 Diag(ArraySize->getLocStart(),
264 diag::err_typecheck_negative_array_size,
265 ArraySize->getSourceRange());
266 D.setInvalidType(true);
267 } else if (ConstVal == 0) {
268 // GCC accepts zero sized static arrays.
269 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
270 ArraySize->getSourceRange());
271 }
272 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000273 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000274 }
Chris Lattner47958f62007-08-28 16:54:00 +0000275 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
276 if (!getLangOptions().C99 &&
277 (ASM != ArrayType::Normal ||
278 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
279 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000280 break;
281 }
282 case DeclaratorChunk::Function:
283 // If the function declarator has a prototype (i.e. it is not () and
284 // does not have a K&R-style identifier list), then the arguments are part
285 // of the type, otherwise the argument list is ().
286 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000287
Chris Lattner6ad7e882007-12-19 05:31:29 +0000288 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000289 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000290 Diag(DeclType.Loc, diag::err_func_returning_array_function,
291 T.getAsString());
292 T = Context.IntTy;
293 D.setInvalidType(true);
294 }
295
Chris Lattner4b009652007-07-25 00:24:17 +0000296 if (!FTI.hasPrototype) {
297 // Simple void foo(), where the incoming T is the result type.
298 T = Context.getFunctionTypeNoProto(T);
299
300 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
301 if (FTI.NumArgs != 0)
302 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
303
304 } else {
305 // Otherwise, we have a function with an argument list that is
306 // potentially variadic.
307 llvm::SmallVector<QualType, 16> ArgTys;
308
309 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
310 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
311 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000312 //
313 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
314 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000315 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000316 // argument type in the function prototype *will not* match the
317 // type in ParmVarDecl (which makes the code generator unhappy).
318 //
319 // FIXME: We still apparently need the conversion in
320 // Sema::ParseParamDeclarator(). This doesn't make any sense, since
321 // it should be driving off the type being created here.
322 //
323 // FIXME: If a source translation tool needs to see the original type,
324 // then we need to consider storing both types somewhere...
325 //
Chris Lattnerc08564a2008-01-02 22:50:48 +0000326 if (const ArrayType *AT = ArgTy->getAsArrayType()) {
327 // int x[restrict 4] -> int *restrict
Steve Naroff1830be72007-09-10 22:17:00 +0000328 ArgTy = Context.getPointerType(AT->getElementType());
Chris Lattnerc08564a2008-01-02 22:50:48 +0000329 ArgTy = ArgTy.getQualifiedType(AT->getIndexTypeQualifier());
330 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000331 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000332 // Look for 'void'. void is allowed only as a single argument to a
333 // function with no other parameters (C99 6.7.5.3p10). We record
334 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000335 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000336 // If this is something like 'float(int, void)', reject it. 'void'
337 // is an incomplete type (C99 6.2.5p19) and function decls cannot
338 // have arguments of incomplete type.
339 if (FTI.NumArgs != 1 || FTI.isVariadic) {
340 Diag(DeclType.Loc, diag::err_void_only_param);
341 ArgTy = Context.IntTy;
342 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
343 } else if (FTI.ArgInfo[i].Ident) {
344 // Reject, but continue to parse 'int(void abc)'.
345 Diag(FTI.ArgInfo[i].IdentLoc,
346 diag::err_param_with_void_type);
347 ArgTy = Context.IntTy;
348 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
349 } else {
350 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000351 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000352 Diag(DeclType.Loc, diag::err_void_param_qualified);
353
354 // Do not add 'void' to the ArgTys list.
355 break;
356 }
357 }
358
359 ArgTys.push_back(ArgTy);
360 }
361 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
362 FTI.isVariadic);
363 }
364 break;
365 }
366 }
367
368 return T;
369}
370
Ted Kremenek42730c52008-01-07 19:49:32 +0000371/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000372/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000373QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
374 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000375 QualType T = MDecl->getResultType();
376 llvm::SmallVector<QualType, 16> ArgTys;
377
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000378 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000379 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000380 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000381 selfTy = Context.getPointerType(selfTy);
382 ArgTys.push_back(selfTy);
383 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000384 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000385 ArgTys.push_back(Context.getObjCIdType());
386 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000387
Chris Lattner685d7922008-03-16 01:07:14 +0000388 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000389 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
390 QualType ArgTy = PDecl->getType();
391 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000392 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
393 // This matches the conversion that is done in
Steve Naroff434fa8d2007-11-12 03:44:46 +0000394 // Sema::ParseParamDeclarator().
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000395 if (const ArrayType *AT = ArgTy->getAsArrayType())
396 ArgTy = Context.getPointerType(AT->getElementType());
397 else if (ArgTy->isFunctionType())
398 ArgTy = Context.getPointerType(ArgTy);
399 ArgTys.push_back(ArgTy);
400 }
401 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000402 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000403 return T;
404}
405
Steve Naroff0acc9c92007-09-15 18:49:24 +0000406Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000407 // C99 6.7.6: Type names have no identifier. This is already validated by
408 // the parser.
409 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
410
411 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000412
413 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000414
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000415 // In this context, we *do not* check D.getInvalidType(). If the declarator
416 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
417 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000418 return T.getAsOpaquePtr();
419}
420
Steve Naroff91b03f72007-08-28 03:03:08 +0000421// Called from Parser::ParseParenDeclarator().
Steve Naroff0acc9c92007-09-15 18:49:24 +0000422Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000423 // Note: parameters have identifiers, but we don't care about them here, we
424 // just want the type converted.
425 QualType T = GetTypeForDeclarator(D, S);
426
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000427 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
428
429 // In this context, we *do not* check D.getInvalidType(). If the declarator
430 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
431 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000432 return T.getAsOpaquePtr();
433}
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000434
435AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){
436 // Scan through and apply attributes to this type where it makes sense. Some
437 // attributes (such as __address_space__, __vector_size__, etc) apply to the
438 // type, but others can be present in the type specifiers even though they
439 // apply to the decl. Here we apply and delete attributes that apply to the
440 // type and leave the others alone.
441 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
442 while (AL) {
443 // Unlink this attribute from the chain, so we can process it independently.
444 AttributeList *ThisAttr = AL;
445 AL = AL->getNext();
446 ThisAttr->setNext(0);
447
448 // If this is an attribute we can handle, do so now, otherwise, add it to
449 // the LeftOverAttrs list for rechaining.
450 switch (ThisAttr->getKind()) {
451 default: break;
452 case AttributeList::AT_address_space:
453 Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr);
454 delete ThisAttr; // Consume the attribute.
455 continue;
456 }
457
458 LeftOverAttrs.push_back(ThisAttr);
459 }
460
461 // Rechain any attributes that haven't been deleted to the DeclSpec.
462 AttributeList *List = 0;
463 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
464 LeftOverAttrs[i]->setNext(List);
465 List = LeftOverAttrs[i];
466 }
467
468 return List;
469}
470
471/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
472/// specified type.
473QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type,
474 AttributeList *Attr) {
475 // If this type is already address space qualified, reject it.
476 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
477 // for two or more different address spaces."
478 if (Type.getAddressSpace()) {
479 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers);
480 return Type;
481 }
482
483 // Check the attribute arguments.
484 if (Attr->getNumArgs() != 1) {
485 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
486 std::string("1"));
487 return Type;
488 }
489 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0));
490 llvm::APSInt addrSpace(32);
491 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) {
492 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int,
493 ASArgExpr->getSourceRange());
494 return Type;
495 }
496
497 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
498 return Context.getASQualType(Type, ASIdx);
499}
500