blob: ca56c56dd9adbadf9f92b7ae753bd724ebf0fa6d [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 Lattner5fcb38b2008-04-02 06:50:17 +000031 case DeclSpec::TST_void:
32 Result = Context.VoidTy;
33 break;
Chris Lattner4b009652007-07-25 00:24:17 +000034 case DeclSpec::TST_char:
35 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner726c5452008-02-20 23:53:49 +000036 Result = Context.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000037 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner726c5452008-02-20 23:53:49 +000038 Result = Context.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000039 else {
40 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
41 "Unknown TSS value");
Chris Lattner726c5452008-02-20 23:53:49 +000042 Result = Context.UnsignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000043 }
Chris Lattner06fb8672008-02-20 21:40:32 +000044 break;
Chris Lattner4b009652007-07-25 00:24:17 +000045 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
Chris Lattner5328f312007-08-21 17:02:28 +000046 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000047 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
48 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000049 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
50 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
51 case DeclSpec::TSW_long: Result = Context.LongTy; break;
52 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54 } else {
55 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000056 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
57 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
58 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
59 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000060 }
61 }
Chris Lattner06fb8672008-02-20 21:40:32 +000062 break;
Chris Lattner5328f312007-08-21 17:02:28 +000063 }
Chris Lattner726c5452008-02-20 23:53:49 +000064 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +000065 case DeclSpec::TST_double:
66 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +000067 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000068 else
Chris Lattner726c5452008-02-20 23:53:49 +000069 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000070 break;
Chris Lattner726c5452008-02-20 23:53:49 +000071 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +000072 case DeclSpec::TST_decimal32: // _Decimal32
73 case DeclSpec::TST_decimal64: // _Decimal64
74 case DeclSpec::TST_decimal128: // _Decimal128
75 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
76 case DeclSpec::TST_enum:
77 case DeclSpec::TST_union:
78 case DeclSpec::TST_struct: {
79 Decl *D = static_cast<Decl *>(DS.getTypeRep());
80 assert(D && "Didn't get a decl for a enum/union/struct?");
81 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
82 DS.getTypeSpecSign() == 0 &&
83 "Can't handle qualifiers on typedef names yet!");
84 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +000085 Result = Context.getTagDeclType(cast<TagDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +000086 break;
Chris Lattner4b009652007-07-25 00:24:17 +000087 }
88 case DeclSpec::TST_typedef: {
89 Decl *D = static_cast<Decl *>(DS.getTypeRep());
90 assert(D && "Didn't get a decl for a typedef?");
91 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
92 DS.getTypeSpecSign() == 0 &&
93 "Can't handle qualifiers on typedef names yet!");
Steve Naroff81f1bba2007-09-06 21:24:23 +000094 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
95 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +000096 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner06fb8672008-02-20 21:40:32 +000097 if (DS.getProtocolQualifiers() == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +000098 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +000099 break;
100 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000101
102 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000103 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattner06fb8672008-02-20 21:40:32 +0000104 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner69f01932008-02-21 01:32:26 +0000105 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000106 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000107 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000108 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattner726c5452008-02-20 23:53:49 +0000109 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000110 && DS.getProtocolQualifiers()) {
111 // id<protocol-list>
112 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000113 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
Chris Lattner06fb8672008-02-20 21:40:32 +0000114 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner69f01932008-02-21 01:32:26 +0000115 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000116 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000117 }
118 }
Chris Lattner4b009652007-07-25 00:24:17 +0000119 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000120 Result = Context.getTypedefType(cast<TypedefDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000121 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000122 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000123 case DeclSpec::TST_typeofType:
124 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
125 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000126 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000127 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000128 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000129 case DeclSpec::TST_typeofExpr: {
130 Expr *E = static_cast<Expr *>(DS.getTypeRep());
131 assert(E && "Didn't get an expression for typeof?");
132 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000133 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000134 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000135 }
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000137
138 // Handle complex types.
139 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000140 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000141
142 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
143 "FIXME: imaginary types not supported yet!");
144
Chris Lattnerd496fb92008-02-20 22:04:11 +0000145 // See if there are any attributes on the declspec that apply to the type (as
146 // opposed to the decl).
Chris Lattner9e982502008-02-21 01:07:18 +0000147 if (AttributeList *AL = DS.getAttributes())
148 DS.SetAttributes(ProcessTypeAttributes(Result, AL));
149
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000150 // Apply const/volatile/restrict qualifiers to T.
151 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
152
153 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
154 // or incomplete types shall not be restrict-qualified." C++ also allows
155 // restrict-qualified references.
156 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000157 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
158 QualType EltTy = PT->getPointeeType();
159
160 // If we have a pointer or reference, the pointee must have an object or
161 // incomplete type.
162 if (!EltTy->isIncompleteOrObjectType()) {
163 Diag(DS.getRestrictSpecLoc(),
164 diag::err_typecheck_invalid_restrict_invalid_pointee,
165 EltTy.getAsString(), DS.getSourceRange());
166 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
167 }
168 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000169 Diag(DS.getRestrictSpecLoc(),
170 diag::err_typecheck_invalid_restrict_not_pointer,
171 Result.getAsString(), DS.getSourceRange());
Chris Lattnercfac88d2008-04-02 17:35:06 +0000172 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000173 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000174 }
175
176 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
177 // of a function type includes any type qualifiers, the behavior is
178 // undefined."
179 if (Result->isFunctionType() && TypeQuals) {
180 // Get some location to point at, either the C or V location.
181 SourceLocation Loc;
182 if (TypeQuals & QualType::Const)
183 Loc = DS.getConstSpecLoc();
184 else {
185 assert((TypeQuals & QualType::Volatile) &&
186 "Has CV quals but not C or V?");
187 Loc = DS.getVolatileSpecLoc();
188 }
189 Diag(Loc, diag::warn_typecheck_function_qualifiers,
190 Result.getAsString(), DS.getSourceRange());
191 }
192
193 Result = Result.getQualifiedType(TypeQuals);
194 }
Chris Lattner9e982502008-02-21 01:07:18 +0000195 return Result;
196}
197
Chris Lattner4b009652007-07-25 00:24:17 +0000198/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
199/// instances.
200QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000201 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000202 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000203 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
204 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
205
Chris Lattner726c5452008-02-20 23:53:49 +0000206 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroff91b03f72007-08-28 03:03:08 +0000207
Chris Lattner4b009652007-07-25 00:24:17 +0000208 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
209 // are ordered from the identifier out, which is opposite of what we want :).
210 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner69f01932008-02-21 01:32:26 +0000211 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000212 switch (DeclType.Kind) {
213 default: assert(0 && "Unknown decltype!");
214 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000215 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000216 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000217 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000218 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000219 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000220 T = Context.IntTy;
221 }
222
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000223 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
224 // object or incomplete types shall not be restrict-qualified."
225 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000226 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000227 Diag(DeclType.Loc,
228 diag::err_typecheck_invalid_restrict_invalid_pointee,
229 T.getAsString());
230 DeclType.Ptr.TypeQuals &= QualType::Restrict;
231 }
232
Chris Lattner4b009652007-07-25 00:24:17 +0000233 // Apply the pointer typequals to the pointer object.
234 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
Chris Lattner69f01932008-02-21 01:32:26 +0000235
236 // See if there are any attributes on the pointer that apply to it.
237 if (AttributeList *AL = DeclType.Ptr.AttrList)
238 DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL);
239
Chris Lattner4b009652007-07-25 00:24:17 +0000240 break;
241 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000242 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000243 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000244 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000245 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000246 D.setInvalidType(true);
Chris Lattnercfac88d2008-04-02 17:35:06 +0000247 T = RT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000248 }
249
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000250 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
251 // object or incomplete types shall not be restrict-qualified."
252 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000253 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000254 Diag(DeclType.Loc,
255 diag::err_typecheck_invalid_restrict_invalid_pointee,
256 T.getAsString());
257 DeclType.Ref.HasRestrict = false;
258 }
259
Chris Lattner4b009652007-07-25 00:24:17 +0000260 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000261
262 // Handle restrict on references.
263 if (DeclType.Ref.HasRestrict)
264 T.addRestrict();
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000265
Chris Lattner69f01932008-02-21 01:32:26 +0000266 // See if there are any attributes on the pointer that apply to it.
267 if (AttributeList *AL = DeclType.Ref.AttrList)
268 DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000269 break;
270 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000271 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000272 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000273 ArrayType::ArraySizeModifier ASM;
274 if (ATI.isStar)
275 ASM = ArrayType::Star;
276 else if (ATI.hasStatic)
277 ASM = ArrayType::Static;
278 else
279 ASM = ArrayType::Normal;
280
281 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
282 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
283 if (T->isIncompleteType()) {
284 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
285 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000286 T = Context.IntTy;
287 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000288 } else if (T->isFunctionType()) {
289 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000290 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000291 T = Context.getPointerType(T);
292 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000293 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000294 // C++ 8.3.2p4: There shall be no ... arrays of references ...
295 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000296 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000297 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000298 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000299 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000300 // If the element type is a struct or union that contains a variadic
301 // array, reject it: C99 6.7.2.1p2.
302 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
303 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
304 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000305 T = Context.IntTy;
306 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000307 }
308 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000309 // C99 6.7.5.2p1: The size expression shall have integer type.
310 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
311 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
312 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
313 D.setInvalidType(true);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000314 delete ArraySize;
315 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000316 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000317 llvm::APSInt ConstVal(32);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000318 // If no expression was provided, we consider it an incomplete array.
Eli Friedman8ff07782008-02-15 18:16:39 +0000319 if (!ArraySize) {
320 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
321 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000322 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000323 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000324 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
325 // have a value greater than zero.
326 if (ConstVal.isSigned()) {
327 if (ConstVal.isNegative()) {
328 Diag(ArraySize->getLocStart(),
329 diag::err_typecheck_negative_array_size,
330 ArraySize->getSourceRange());
331 D.setInvalidType(true);
332 } else if (ConstVal == 0) {
333 // GCC accepts zero sized static arrays.
334 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
335 ArraySize->getSourceRange());
336 }
337 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000338 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000339 }
Chris Lattner47958f62007-08-28 16:54:00 +0000340 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
341 if (!getLangOptions().C99 &&
342 (ASM != ArrayType::Normal ||
343 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
344 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000345 break;
346 }
347 case DeclaratorChunk::Function:
348 // If the function declarator has a prototype (i.e. it is not () and
349 // does not have a K&R-style identifier list), then the arguments are part
350 // of the type, otherwise the argument list is ().
351 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000352
Chris Lattner6ad7e882007-12-19 05:31:29 +0000353 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000354 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000355 Diag(DeclType.Loc, diag::err_func_returning_array_function,
356 T.getAsString());
357 T = Context.IntTy;
358 D.setInvalidType(true);
359 }
360
Chris Lattner4b009652007-07-25 00:24:17 +0000361 if (!FTI.hasPrototype) {
362 // Simple void foo(), where the incoming T is the result type.
363 T = Context.getFunctionTypeNoProto(T);
364
365 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
366 if (FTI.NumArgs != 0)
367 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
368
369 } else {
370 // Otherwise, we have a function with an argument list that is
371 // potentially variadic.
372 llvm::SmallVector<QualType, 16> ArgTys;
373
374 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
375 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
376 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000377 //
378 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
379 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000380 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000381 // argument type in the function prototype *will not* match the
382 // type in ParmVarDecl (which makes the code generator unhappy).
383 //
384 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000385 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000386 // it should be driving off the type being created here.
387 //
388 // FIXME: If a source translation tool needs to see the original type,
389 // then we need to consider storing both types somewhere...
390 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000391 if (ArgTy->isArrayType()) {
392 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000393 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000394 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000395
Chris Lattner4b009652007-07-25 00:24:17 +0000396 // Look for 'void'. void is allowed only as a single argument to a
397 // function with no other parameters (C99 6.7.5.3p10). We record
398 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000399 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000400 // If this is something like 'float(int, void)', reject it. 'void'
401 // is an incomplete type (C99 6.2.5p19) and function decls cannot
402 // have arguments of incomplete type.
403 if (FTI.NumArgs != 1 || FTI.isVariadic) {
404 Diag(DeclType.Loc, diag::err_void_only_param);
405 ArgTy = Context.IntTy;
406 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
407 } else if (FTI.ArgInfo[i].Ident) {
408 // Reject, but continue to parse 'int(void abc)'.
409 Diag(FTI.ArgInfo[i].IdentLoc,
410 diag::err_param_with_void_type);
411 ArgTy = Context.IntTy;
412 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
413 } else {
414 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000415 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000416 Diag(DeclType.Loc, diag::err_void_param_qualified);
417
418 // Do not add 'void' to the ArgTys list.
419 break;
420 }
421 }
422
423 ArgTys.push_back(ArgTy);
424 }
425 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
426 FTI.isVariadic);
427 }
428 break;
429 }
430 }
431
432 return T;
433}
434
Ted Kremenek42730c52008-01-07 19:49:32 +0000435/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000436/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000437QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
438 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000439 QualType T = MDecl->getResultType();
440 llvm::SmallVector<QualType, 16> ArgTys;
441
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000442 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000443 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000444 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000445 selfTy = Context.getPointerType(selfTy);
446 ArgTys.push_back(selfTy);
447 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000448 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000449 ArgTys.push_back(Context.getObjCIdType());
450 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000451
Chris Lattner685d7922008-03-16 01:07:14 +0000452 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000453 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
454 QualType ArgTy = PDecl->getType();
455 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000456 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
457 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000458 // Sema::ActOnParamDeclarator().
459 if (ArgTy->isArrayType())
460 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000461 else if (ArgTy->isFunctionType())
462 ArgTy = Context.getPointerType(ArgTy);
463 ArgTys.push_back(ArgTy);
464 }
465 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000466 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000467 return T;
468}
469
Steve Naroff0acc9c92007-09-15 18:49:24 +0000470Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000471 // C99 6.7.6: Type names have no identifier. This is already validated by
472 // the parser.
473 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
474
475 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000476
477 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000478
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000479 // In this context, we *do not* check D.getInvalidType(). If the declarator
480 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
481 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000482 return T.getAsOpaquePtr();
483}
484
Steve Naroff91b03f72007-08-28 03:03:08 +0000485// Called from Parser::ParseParenDeclarator().
Steve Naroff0acc9c92007-09-15 18:49:24 +0000486Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000487 // Note: parameters have identifiers, but we don't care about them here, we
488 // just want the type converted.
489 QualType T = GetTypeForDeclarator(D, S);
490
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000491 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
492
493 // In this context, we *do not* check D.getInvalidType(). If the declarator
494 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
495 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000496 return T.getAsOpaquePtr();
497}
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000498
499AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){
500 // Scan through and apply attributes to this type where it makes sense. Some
501 // attributes (such as __address_space__, __vector_size__, etc) apply to the
502 // type, but others can be present in the type specifiers even though they
503 // apply to the decl. Here we apply and delete attributes that apply to the
504 // type and leave the others alone.
505 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
506 while (AL) {
507 // Unlink this attribute from the chain, so we can process it independently.
508 AttributeList *ThisAttr = AL;
509 AL = AL->getNext();
510 ThisAttr->setNext(0);
511
512 // If this is an attribute we can handle, do so now, otherwise, add it to
513 // the LeftOverAttrs list for rechaining.
514 switch (ThisAttr->getKind()) {
515 default: break;
516 case AttributeList::AT_address_space:
517 Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr);
518 delete ThisAttr; // Consume the attribute.
519 continue;
520 }
521
522 LeftOverAttrs.push_back(ThisAttr);
523 }
524
525 // Rechain any attributes that haven't been deleted to the DeclSpec.
526 AttributeList *List = 0;
527 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
528 LeftOverAttrs[i]->setNext(List);
529 List = LeftOverAttrs[i];
530 }
531
532 return List;
533}
534
535/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
536/// specified type.
537QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type,
538 AttributeList *Attr) {
539 // If this type is already address space qualified, reject it.
540 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
541 // for two or more different address spaces."
542 if (Type.getAddressSpace()) {
543 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers);
544 return Type;
545 }
546
547 // Check the attribute arguments.
548 if (Attr->getNumArgs() != 1) {
549 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
550 std::string("1"));
551 return Type;
552 }
553 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0));
554 llvm::APSInt addrSpace(32);
555 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) {
556 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int,
557 ASArgExpr->getSourceRange());
558 return Type;
559 }
560
561 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
562 return Context.getASQualType(Type, ASIdx);
563}
564