blob: a1cabcff64eeec7126fa3378b1d63ce2961289f6 [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 Lattner96b77fc2008-04-02 06:50:17 +000031 case DeclSpec::TST_void:
32 Result = Context.VoidTy;
33 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000034 case DeclSpec::TST_char:
35 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000036 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000037 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000038 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 else {
40 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
41 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000042 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000043 }
Chris Lattner958858e2008-02-20 21:40:32 +000044 break;
Chris Lattner7a543ad2007-07-13 21:02:29 +000045 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000046 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000047 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
48 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000053 }
54 } else {
55 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +000060 }
61 }
Chris Lattner958858e2008-02-20 21:40:32 +000062 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +000063 }
Chris Lattnerfab5b452008-02-20 23:53:49 +000064 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +000065 case DeclSpec::TST_double:
66 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +000067 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +000068 else
Chris Lattnerfab5b452008-02-20 23:53:49 +000069 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +000070 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +000071 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerfab5b452008-02-20 23:53:49 +000085 Result = Context.getTagDeclType(cast<TagDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +000086 break;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff3536b442007-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 Kremeneka526c5c2008-01-07 19:49:32 +000096 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner958858e2008-02-20 21:40:32 +000097 if (DS.getProtocolQualifiers() == 0) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000098 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner958858e2008-02-20 21:40:32 +000099 break;
100 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000101
102 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattnerfab5b452008-02-20 23:53:49 +0000103 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattner958858e2008-02-20 21:40:32 +0000104 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner76549142008-02-21 01:32:26 +0000105 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000106 break;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000107 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000108 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000109 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000110 && DS.getProtocolQualifiers()) {
111 // id<protocol-list>
112 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattnerfab5b452008-02-20 23:53:49 +0000113 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
Chris Lattner958858e2008-02-20 21:40:32 +0000114 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner76549142008-02-21 01:32:26 +0000115 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000116 break;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000117 }
118 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000120 Result = Context.getTypedefType(cast<TypedefDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000121 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
Chris Lattner958858e2008-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 Naroffd1861fd2007-07-31 12:34:36 +0000126 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000127 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000128 break;
Steve Naroffd1861fd2007-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 Lattnerfab5b452008-02-20 23:53:49 +0000133 Result = Context.getTypeOfExpr(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000134 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000135 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 }
Chris Lattner958858e2008-02-20 21:40:32 +0000137
138 // Handle complex types.
139 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000140 Result = Context.getComplexType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000141
142 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
143 "FIXME: imaginary types not supported yet!");
144
Chris Lattner38d8b982008-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 Lattnerf1d705c2008-02-21 01:07:18 +0000147 if (AttributeList *AL = DS.getAttributes())
148 DS.SetAttributes(ProcessTypeAttributes(Result, AL));
149
Chris Lattner96b77fc2008-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) {
157 QualType EltTy;
158 if (const PointerType *PT = Result->getAsPointerType())
159 EltTy = PT->getPointeeType();
160 else if (const ReferenceType *RT = Result->getAsReferenceType())
161 EltTy = RT->getReferenceeType();
162 else {
163 Diag(DS.getRestrictSpecLoc(),
164 diag::err_typecheck_invalid_restrict_not_pointer,
165 Result.getAsString(), DS.getSourceRange());
166 }
167
168 // If we have a pointer or reference, the pointee must have an object or
169 // incomplete type.
170 if (!EltTy.isNull() && !EltTy->isObjectType() &&
171 !EltTy->isIncompleteType()) {
172 Diag(DS.getRestrictSpecLoc(),
173 diag::err_typecheck_invalid_restrict_invalid_pointee,
174 EltTy.getAsString(), DS.getSourceRange());
175 EltTy = QualType();
176 }
177
178 if (EltTy.isNull()) // Invalid restrict: remove the restrict qualifier.
179 TypeQuals &= ~QualType::Restrict;
180 }
181
182 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
183 // of a function type includes any type qualifiers, the behavior is
184 // undefined."
185 if (Result->isFunctionType() && TypeQuals) {
186 // Get some location to point at, either the C or V location.
187 SourceLocation Loc;
188 if (TypeQuals & QualType::Const)
189 Loc = DS.getConstSpecLoc();
190 else {
191 assert((TypeQuals & QualType::Volatile) &&
192 "Has CV quals but not C or V?");
193 Loc = DS.getVolatileSpecLoc();
194 }
195 Diag(Loc, diag::warn_typecheck_function_qualifiers,
196 Result.getAsString(), DS.getSourceRange());
197 }
198
199 Result = Result.getQualifiedType(TypeQuals);
200 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000201 return Result;
202}
203
Reid Spencer5f016e22007-07-11 17:01:13 +0000204/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
205/// instances.
206QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattnerb23deda2007-08-28 16:40:32 +0000207 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000208 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000209 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
210 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
211
Chris Lattnerfab5b452008-02-20 23:53:49 +0000212 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroffe1223f72007-08-28 03:03:08 +0000213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
215 // are ordered from the identifier out, which is opposite of what we want :).
216 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner76549142008-02-21 01:32:26 +0000217 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 switch (DeclType.Kind) {
219 default: assert(0 && "Unknown decltype!");
220 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000221 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner96b77fc2008-04-02 06:50:17 +0000223 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000224 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000225 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000226 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 }
228
Chris Lattner96b77fc2008-04-02 06:50:17 +0000229 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
230 // object or incomplete types shall not be restrict-qualified."
231 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
232 !T->isObjectType() && !T->isIncompleteType()) {
233 Diag(DeclType.Loc,
234 diag::err_typecheck_invalid_restrict_invalid_pointee,
235 T.getAsString());
236 DeclType.Ptr.TypeQuals &= QualType::Restrict;
237 }
238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // Apply the pointer typequals to the pointer object.
240 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
Chris Lattner76549142008-02-21 01:32:26 +0000241
242 // See if there are any attributes on the pointer that apply to it.
243 if (AttributeList *AL = DeclType.Ptr.AttrList)
244 DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL);
245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 break;
247 case DeclaratorChunk::Reference:
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000248 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattnerbde71842008-02-21 01:32:57 +0000249 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000250 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000251 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000252 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000253 T = RT->getReferenceeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 }
255
Chris Lattner96b77fc2008-04-02 06:50:17 +0000256 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
257 // object or incomplete types shall not be restrict-qualified."
258 if (DeclType.Ref.HasRestrict &&
259 !T->isObjectType() && !T->isIncompleteType()) {
260 Diag(DeclType.Loc,
261 diag::err_typecheck_invalid_restrict_invalid_pointee,
262 T.getAsString());
263 DeclType.Ref.HasRestrict = false;
264 }
265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000267
268 // Handle restrict on references.
269 if (DeclType.Ref.HasRestrict)
270 T.addRestrict();
Chris Lattnerbde71842008-02-21 01:32:57 +0000271
Chris Lattner76549142008-02-21 01:32:26 +0000272 // See if there are any attributes on the pointer that apply to it.
273 if (AttributeList *AL = DeclType.Ref.AttrList)
274 DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 break;
276 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000277 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000278 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 ArrayType::ArraySizeModifier ASM;
280 if (ATI.isStar)
281 ASM = ArrayType::Star;
282 else if (ATI.hasStatic)
283 ASM = ArrayType::Static;
284 else
285 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
288 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
289 if (T->isIncompleteType()) {
290 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
291 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000292 T = Context.IntTy;
293 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000294 } else if (T->isFunctionType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000296 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000297 T = Context.getPointerType(T);
298 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000299 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // C++ 8.3.2p4: There shall be no ... arrays of references ...
301 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000302 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000303 T = RT->getReferenceeType();
304 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000305 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // If the element type is a struct or union that contains a variadic
307 // array, reject it: C99 6.7.2.1p2.
308 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
309 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
310 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000311 T = Context.IntTy;
312 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 }
314 }
Steve Naroff42471f82007-08-30 22:35:45 +0000315 // C99 6.7.5.2p1: The size expression shall have integer type.
316 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
317 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
318 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
319 D.setInvalidType(true);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000320 delete ArraySize;
321 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000322 }
Steve Naroffc9406122007-08-30 18:10:14 +0000323 llvm::APSInt ConstVal(32);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000324 // If no expression was provided, we consider it an incomplete array.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000325 if (!ArraySize) {
326 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
327 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroffc9406122007-08-30 18:10:14 +0000328 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000329 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000330 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
331 // have a value greater than zero.
332 if (ConstVal.isSigned()) {
333 if (ConstVal.isNegative()) {
334 Diag(ArraySize->getLocStart(),
335 diag::err_typecheck_negative_array_size,
336 ArraySize->getSourceRange());
337 D.setInvalidType(true);
338 } else if (ConstVal == 0) {
339 // GCC accepts zero sized static arrays.
340 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
341 ArraySize->getSourceRange());
342 }
343 }
Steve Naroffc9406122007-08-30 18:10:14 +0000344 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000345 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000346 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
347 if (!getLangOptions().C99 &&
348 (ASM != ArrayType::Normal ||
349 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
350 Diag(D.getIdentifierLoc(), diag::ext_vla);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 break;
352 }
353 case DeclaratorChunk::Function:
354 // If the function declarator has a prototype (i.e. it is not () and
355 // does not have a K&R-style identifier list), then the arguments are part
356 // of the type, otherwise the argument list is ().
357 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000358
Chris Lattnercd881292007-12-19 05:31:29 +0000359 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000360 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnercd881292007-12-19 05:31:29 +0000361 Diag(DeclType.Loc, diag::err_func_returning_array_function,
362 T.getAsString());
363 T = Context.IntTy;
364 D.setInvalidType(true);
365 }
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 if (!FTI.hasPrototype) {
368 // Simple void foo(), where the incoming T is the result type.
369 T = Context.getFunctionTypeNoProto(T);
370
371 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
372 if (FTI.NumArgs != 0)
373 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
374
375 } else {
376 // Otherwise, we have a function with an argument list that is
377 // potentially variadic.
378 llvm::SmallVector<QualType, 16> ArgTys;
379
380 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
381 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
Chris Lattner78c75fb2007-07-21 05:30:18 +0000382 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000383 //
384 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
385 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000386 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000387 // argument type in the function prototype *will not* match the
388 // type in ParmVarDecl (which makes the code generator unhappy).
389 //
390 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000391 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000392 // it should be driving off the type being created here.
393 //
394 // FIXME: If a source translation tool needs to see the original type,
395 // then we need to consider storing both types somewhere...
396 //
Chris Lattnere6327742008-04-02 05:18:44 +0000397 if (ArgTy->isArrayType()) {
398 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000399 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000400 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000401
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 // Look for 'void'. void is allowed only as a single argument to a
403 // function with no other parameters (C99 6.7.5.3p10). We record
404 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000405 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 // If this is something like 'float(int, void)', reject it. 'void'
407 // is an incomplete type (C99 6.2.5p19) and function decls cannot
408 // have arguments of incomplete type.
409 if (FTI.NumArgs != 1 || FTI.isVariadic) {
410 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000411 ArgTy = Context.IntTy;
Chris Lattner78c75fb2007-07-21 05:30:18 +0000412 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
Chris Lattner2ff54262007-07-21 05:18:12 +0000413 } else if (FTI.ArgInfo[i].Ident) {
414 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000416 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000417 ArgTy = Context.IntTy;
Chris Lattner78c75fb2007-07-21 05:30:18 +0000418 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
Chris Lattner2ff54262007-07-21 05:18:12 +0000419 } else {
420 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000421 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000422 Diag(DeclType.Loc, diag::err_void_param_qualified);
423
424 // Do not add 'void' to the ArgTys list.
425 break;
426 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 }
428
429 ArgTys.push_back(ArgTy);
430 }
431 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
432 FTI.isVariadic);
433 }
434 break;
435 }
436 }
437
438 return T;
439}
440
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000441/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000442/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000443QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
444 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000445 QualType T = MDecl->getResultType();
446 llvm::SmallVector<QualType, 16> ArgTys;
447
Fariborz Jahanian35600022007-11-09 17:18:29 +0000448 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000449 if (MDecl->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000450 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000451 selfTy = Context.getPointerType(selfTy);
452 ArgTys.push_back(selfTy);
453 }
Fariborz Jahanian35600022007-11-09 17:18:29 +0000454 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455 ArgTys.push_back(Context.getObjCIdType());
456 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000457
Chris Lattner58cce3b2008-03-16 01:07:14 +0000458 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000459 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
460 QualType ArgTy = PDecl->getType();
461 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000462 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
463 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000464 // Sema::ActOnParamDeclarator().
465 if (ArgTy->isArrayType())
466 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000467 else if (ArgTy->isFunctionType())
468 ArgTy = Context.getPointerType(ArgTy);
469 ArgTys.push_back(ArgTy);
470 }
471 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffb99a4a32007-12-18 03:41:15 +0000472 MDecl->isVariadic());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000473 return T;
474}
475
Steve Naroff08d92e42007-09-15 18:49:24 +0000476Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 // C99 6.7.6: Type names have no identifier. This is already validated by
478 // the parser.
479 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
480
481 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000482
483 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000484
Steve Naroff5912a352007-08-28 20:14:24 +0000485 // In this context, we *do not* check D.getInvalidType(). If the declarator
486 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
487 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 return T.getAsOpaquePtr();
489}
490
Steve Naroffe1223f72007-08-28 03:03:08 +0000491// Called from Parser::ParseParenDeclarator().
Steve Naroff08d92e42007-09-15 18:49:24 +0000492Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 // Note: parameters have identifiers, but we don't care about them here, we
494 // just want the type converted.
495 QualType T = GetTypeForDeclarator(D, S);
496
Steve Naroff5912a352007-08-28 20:14:24 +0000497 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
498
499 // In this context, we *do not* check D.getInvalidType(). If the declarator
500 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
501 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 return T.getAsOpaquePtr();
503}
Chris Lattner232e8822008-02-21 01:08:11 +0000504
505AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){
506 // Scan through and apply attributes to this type where it makes sense. Some
507 // attributes (such as __address_space__, __vector_size__, etc) apply to the
508 // type, but others can be present in the type specifiers even though they
509 // apply to the decl. Here we apply and delete attributes that apply to the
510 // type and leave the others alone.
511 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
512 while (AL) {
513 // Unlink this attribute from the chain, so we can process it independently.
514 AttributeList *ThisAttr = AL;
515 AL = AL->getNext();
516 ThisAttr->setNext(0);
517
518 // If this is an attribute we can handle, do so now, otherwise, add it to
519 // the LeftOverAttrs list for rechaining.
520 switch (ThisAttr->getKind()) {
521 default: break;
522 case AttributeList::AT_address_space:
523 Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr);
524 delete ThisAttr; // Consume the attribute.
525 continue;
526 }
527
528 LeftOverAttrs.push_back(ThisAttr);
529 }
530
531 // Rechain any attributes that haven't been deleted to the DeclSpec.
532 AttributeList *List = 0;
533 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
534 LeftOverAttrs[i]->setNext(List);
535 List = LeftOverAttrs[i];
536 }
537
538 return List;
539}
540
541/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
542/// specified type.
543QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type,
544 AttributeList *Attr) {
545 // If this type is already address space qualified, reject it.
546 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
547 // for two or more different address spaces."
548 if (Type.getAddressSpace()) {
549 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers);
550 return Type;
551 }
552
553 // Check the attribute arguments.
554 if (Attr->getNumArgs() != 1) {
555 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
556 std::string("1"));
557 return Type;
558 }
559 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0));
560 llvm::APSInt addrSpace(32);
561 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) {
562 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int,
563 ASArgExpr->getSourceRange());
564 return Type;
565 }
566
567 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
568 return Context.getASQualType(Type, ASIdx);
569}
570