blob: 5e155f88170a184e658e0922ed4b0a33761aaa7c [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()) {
Chris Lattnerd658b562008-04-05 06:32:51 +000030 default: assert(0 && "Unknown TypeSpecType!");
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 Lattnerd658b562008-04-05 06:32:51 +000045 case DeclSpec::TST_unspecified:
46 // Unspecified typespec defaults to int in C90. However, the C90 grammar
47 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
48 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
49 // Note that the one exception to this is function definitions, which are
50 // allowed to be completely missing a declspec. This is handled in the
51 // parser already though by it pretending to have seen an 'int' in this
52 // case.
53 if (getLangOptions().ImplicitInt) {
54 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
55 DeclSpec::PQ_TypeSpecifier |
56 DeclSpec::PQ_TypeQualifier)) == 0)
57 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
58 } else {
59 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
60 // "At least one type specifier shall be given in the declaration
61 // specifiers in each declaration, and in the specifier-qualifier list in
62 // each struct declaration and type name."
63 if (!DS.hasTypeSpecifier())
64 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
65 }
66
67 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000068 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
70 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000071 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
72 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
73 case DeclSpec::TSW_long: Result = Context.LongTy; break;
74 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000075 }
76 } else {
77 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000078 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
79 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
80 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
81 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000082 }
83 }
Chris Lattner958858e2008-02-20 21:40:32 +000084 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +000085 }
Chris Lattnerfab5b452008-02-20 23:53:49 +000086 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +000087 case DeclSpec::TST_double:
88 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +000089 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +000090 else
Chris Lattnerfab5b452008-02-20 23:53:49 +000091 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +000092 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +000093 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +000094 case DeclSpec::TST_decimal32: // _Decimal32
95 case DeclSpec::TST_decimal64: // _Decimal64
96 case DeclSpec::TST_decimal128: // _Decimal128
97 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
98 case DeclSpec::TST_enum:
99 case DeclSpec::TST_union:
100 case DeclSpec::TST_struct: {
101 Decl *D = static_cast<Decl *>(DS.getTypeRep());
102 assert(D && "Didn't get a decl for a enum/union/struct?");
103 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
104 DS.getTypeSpecSign() == 0 &&
105 "Can't handle qualifiers on typedef names yet!");
106 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000107 Result = Context.getTagDeclType(cast<TagDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000108 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 }
110 case DeclSpec::TST_typedef: {
111 Decl *D = static_cast<Decl *>(DS.getTypeRep());
112 assert(D && "Didn't get a decl for a typedef?");
113 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
114 DS.getTypeSpecSign() == 0 &&
115 "Can't handle qualifiers on typedef names yet!");
Steve Naroff3536b442007-09-06 21:24:23 +0000116 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
117 // we have this "hack" for now...
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000118 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner958858e2008-02-20 21:40:32 +0000119 if (DS.getProtocolQualifiers() == 0) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000120 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner958858e2008-02-20 21:40:32 +0000121 break;
122 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000123
124 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattnerfab5b452008-02-20 23:53:49 +0000125 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattner958858e2008-02-20 21:40:32 +0000126 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner76549142008-02-21 01:32:26 +0000127 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000128 break;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000129 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000130 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000131 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000132 && DS.getProtocolQualifiers()) {
133 // id<protocol-list>
134 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattnerfab5b452008-02-20 23:53:49 +0000135 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
Chris Lattner958858e2008-02-20 21:40:32 +0000136 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner76549142008-02-21 01:32:26 +0000137 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000138 break;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000139 }
140 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000142 Result = Context.getTypedefType(cast<TypedefDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000143 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 }
Chris Lattner958858e2008-02-20 21:40:32 +0000145 case DeclSpec::TST_typeofType:
146 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
147 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000148 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000149 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000150 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000151 case DeclSpec::TST_typeofExpr: {
152 Expr *E = static_cast<Expr *>(DS.getTypeRep());
153 assert(E && "Didn't get an expression for typeof?");
154 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000155 Result = Context.getTypeOfExpr(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000156 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000157 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 }
Chris Lattner958858e2008-02-20 21:40:32 +0000159
160 // Handle complex types.
161 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000162 Result = Context.getComplexType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000163
164 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
165 "FIXME: imaginary types not supported yet!");
166
Chris Lattner38d8b982008-02-20 22:04:11 +0000167 // See if there are any attributes on the declspec that apply to the type (as
168 // opposed to the decl).
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000169 if (AttributeList *AL = DS.getAttributes())
170 DS.SetAttributes(ProcessTypeAttributes(Result, AL));
171
Chris Lattner96b77fc2008-04-02 06:50:17 +0000172 // Apply const/volatile/restrict qualifiers to T.
173 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
174
175 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
176 // or incomplete types shall not be restrict-qualified." C++ also allows
177 // restrict-qualified references.
178 if (TypeQuals & QualType::Restrict) {
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000179 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
180 QualType EltTy = PT->getPointeeType();
181
182 // If we have a pointer or reference, the pointee must have an object or
183 // incomplete type.
184 if (!EltTy->isIncompleteOrObjectType()) {
185 Diag(DS.getRestrictSpecLoc(),
186 diag::err_typecheck_invalid_restrict_invalid_pointee,
187 EltTy.getAsString(), DS.getSourceRange());
188 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
189 }
190 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000191 Diag(DS.getRestrictSpecLoc(),
192 diag::err_typecheck_invalid_restrict_not_pointer,
193 Result.getAsString(), DS.getSourceRange());
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000194 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000195 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000196 }
197
198 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
199 // of a function type includes any type qualifiers, the behavior is
200 // undefined."
201 if (Result->isFunctionType() && TypeQuals) {
202 // Get some location to point at, either the C or V location.
203 SourceLocation Loc;
204 if (TypeQuals & QualType::Const)
205 Loc = DS.getConstSpecLoc();
206 else {
207 assert((TypeQuals & QualType::Volatile) &&
208 "Has CV quals but not C or V?");
209 Loc = DS.getVolatileSpecLoc();
210 }
211 Diag(Loc, diag::warn_typecheck_function_qualifiers,
212 Result.getAsString(), DS.getSourceRange());
213 }
214
215 Result = Result.getQualifiedType(TypeQuals);
216 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000217 return Result;
218}
219
Reid Spencer5f016e22007-07-11 17:01:13 +0000220/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
221/// instances.
222QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattnerb23deda2007-08-28 16:40:32 +0000223 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000224 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000225 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
226 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
227
Chris Lattnerfab5b452008-02-20 23:53:49 +0000228 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroffe1223f72007-08-28 03:03:08 +0000229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
231 // are ordered from the identifier out, which is opposite of what we want :).
232 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner76549142008-02-21 01:32:26 +0000233 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 switch (DeclType.Kind) {
235 default: assert(0 && "Unknown decltype!");
236 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000237 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner96b77fc2008-04-02 06:50:17 +0000239 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000240 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000241 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000242 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
244
Chris Lattner96b77fc2008-04-02 06:50:17 +0000245 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
246 // object or incomplete types shall not be restrict-qualified."
247 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000248 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000249 Diag(DeclType.Loc,
250 diag::err_typecheck_invalid_restrict_invalid_pointee,
251 T.getAsString());
252 DeclType.Ptr.TypeQuals &= QualType::Restrict;
253 }
254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // Apply the pointer typequals to the pointer object.
256 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
Chris Lattner76549142008-02-21 01:32:26 +0000257
258 // See if there are any attributes on the pointer that apply to it.
259 if (AttributeList *AL = DeclType.Ptr.AttrList)
260 DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL);
261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 break;
263 case DeclaratorChunk::Reference:
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000264 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattnerbde71842008-02-21 01:32:57 +0000265 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000266 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000267 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000268 D.setInvalidType(true);
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000269 T = RT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 }
271
Chris Lattner96b77fc2008-04-02 06:50:17 +0000272 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
273 // object or incomplete types shall not be restrict-qualified."
274 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000275 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000276 Diag(DeclType.Loc,
277 diag::err_typecheck_invalid_restrict_invalid_pointee,
278 T.getAsString());
279 DeclType.Ref.HasRestrict = false;
280 }
281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000283
284 // Handle restrict on references.
285 if (DeclType.Ref.HasRestrict)
286 T.addRestrict();
Chris Lattnerbde71842008-02-21 01:32:57 +0000287
Chris Lattner76549142008-02-21 01:32:26 +0000288 // See if there are any attributes on the pointer that apply to it.
289 if (AttributeList *AL = DeclType.Ref.AttrList)
290 DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 break;
292 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000293 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000294 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 ArrayType::ArraySizeModifier ASM;
296 if (ATI.isStar)
297 ASM = ArrayType::Star;
298 else if (ATI.hasStatic)
299 ASM = ArrayType::Static;
300 else
301 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
304 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
305 if (T->isIncompleteType()) {
306 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
307 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000308 T = Context.IntTy;
309 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000310 } else if (T->isFunctionType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000312 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000313 T = Context.getPointerType(T);
314 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000315 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 // C++ 8.3.2p4: There shall be no ... arrays of references ...
317 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000318 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000319 T = RT->getPointeeType();
Steve Naroffe1223f72007-08-28 03:03:08 +0000320 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000321 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 // If the element type is a struct or union that contains a variadic
323 // array, reject it: C99 6.7.2.1p2.
324 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
325 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
326 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000327 T = Context.IntTy;
328 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 }
330 }
Steve Naroff42471f82007-08-30 22:35:45 +0000331 // C99 6.7.5.2p1: The size expression shall have integer type.
332 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
333 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
334 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
335 D.setInvalidType(true);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000336 delete ArraySize;
337 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000338 }
Steve Naroffc9406122007-08-30 18:10:14 +0000339 llvm::APSInt ConstVal(32);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000340 // If no expression was provided, we consider it an incomplete array.
Eli Friedmanc5773c42008-02-15 18:16:39 +0000341 if (!ArraySize) {
342 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
343 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroffc9406122007-08-30 18:10:14 +0000344 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000345 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000346 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
347 // have a value greater than zero.
348 if (ConstVal.isSigned()) {
349 if (ConstVal.isNegative()) {
350 Diag(ArraySize->getLocStart(),
351 diag::err_typecheck_negative_array_size,
352 ArraySize->getSourceRange());
353 D.setInvalidType(true);
354 } else if (ConstVal == 0) {
355 // GCC accepts zero sized static arrays.
356 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
357 ArraySize->getSourceRange());
358 }
359 }
Steve Naroffc9406122007-08-30 18:10:14 +0000360 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000361 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000362 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
363 if (!getLangOptions().C99 &&
364 (ASM != ArrayType::Normal ||
365 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
366 Diag(D.getIdentifierLoc(), diag::ext_vla);
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 break;
368 }
369 case DeclaratorChunk::Function:
370 // If the function declarator has a prototype (i.e. it is not () and
371 // does not have a K&R-style identifier list), then the arguments are part
372 // of the type, otherwise the argument list is ().
373 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000374
Chris Lattnercd881292007-12-19 05:31:29 +0000375 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000376 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnercd881292007-12-19 05:31:29 +0000377 Diag(DeclType.Loc, diag::err_func_returning_array_function,
378 T.getAsString());
379 T = Context.IntTy;
380 D.setInvalidType(true);
381 }
382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 if (!FTI.hasPrototype) {
384 // Simple void foo(), where the incoming T is the result type.
385 T = Context.getFunctionTypeNoProto(T);
386
387 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
388 if (FTI.NumArgs != 0)
389 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
390
391 } else {
392 // Otherwise, we have a function with an argument list that is
393 // potentially variadic.
394 llvm::SmallVector<QualType, 16> ArgTys;
395
396 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000397 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
398 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000399 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000400 //
401 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
402 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000403 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000404 // argument type in the function prototype *will not* match the
405 // type in ParmVarDecl (which makes the code generator unhappy).
406 //
407 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000408 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000409 // it should be driving off the type being created here.
410 //
411 // FIXME: If a source translation tool needs to see the original type,
412 // then we need to consider storing both types somewhere...
413 //
Chris Lattnere6327742008-04-02 05:18:44 +0000414 if (ArgTy->isArrayType()) {
415 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000416 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000417 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 // Look for 'void'. void is allowed only as a single argument to a
420 // function with no other parameters (C99 6.7.5.3p10). We record
421 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000422 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 // If this is something like 'float(int, void)', reject it. 'void'
424 // is an incomplete type (C99 6.2.5p19) and function decls cannot
425 // have arguments of incomplete type.
426 if (FTI.NumArgs != 1 || FTI.isVariadic) {
427 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000428 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000429 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000430 } else if (FTI.ArgInfo[i].Ident) {
431 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000433 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000434 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000435 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000436 } else {
437 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000438 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000439 Diag(DeclType.Loc, diag::err_void_param_qualified);
440
441 // Do not add 'void' to the ArgTys list.
442 break;
443 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 }
445
446 ArgTys.push_back(ArgTy);
447 }
448 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
449 FTI.isVariadic);
450 }
451 break;
452 }
453 }
454
455 return T;
456}
457
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000458/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000459/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000460QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
461 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000462 QualType T = MDecl->getResultType();
463 llvm::SmallVector<QualType, 16> ArgTys;
464
Fariborz Jahanian35600022007-11-09 17:18:29 +0000465 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000466 if (MDecl->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000467 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000468 selfTy = Context.getPointerType(selfTy);
469 ArgTys.push_back(selfTy);
470 }
Fariborz Jahanian35600022007-11-09 17:18:29 +0000471 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000472 ArgTys.push_back(Context.getObjCIdType());
473 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000474
Chris Lattner58cce3b2008-03-16 01:07:14 +0000475 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000476 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
477 QualType ArgTy = PDecl->getType();
478 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000479 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
480 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000481 // Sema::ActOnParamDeclarator().
482 if (ArgTy->isArrayType())
483 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000484 else if (ArgTy->isFunctionType())
485 ArgTy = Context.getPointerType(ArgTy);
486 ArgTys.push_back(ArgTy);
487 }
488 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffb99a4a32007-12-18 03:41:15 +0000489 MDecl->isVariadic());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000490 return T;
491}
492
Steve Naroff08d92e42007-09-15 18:49:24 +0000493Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 // C99 6.7.6: Type names have no identifier. This is already validated by
495 // the parser.
496 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
497
498 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000499
500 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000501
Steve Naroff5912a352007-08-28 20:14:24 +0000502 // In this context, we *do not* check D.getInvalidType(). If the declarator
503 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
504 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 return T.getAsOpaquePtr();
506}
507
Chris Lattner232e8822008-02-21 01:08:11 +0000508AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){
509 // Scan through and apply attributes to this type where it makes sense. Some
510 // attributes (such as __address_space__, __vector_size__, etc) apply to the
511 // type, but others can be present in the type specifiers even though they
512 // apply to the decl. Here we apply and delete attributes that apply to the
513 // type and leave the others alone.
514 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
515 while (AL) {
516 // Unlink this attribute from the chain, so we can process it independently.
517 AttributeList *ThisAttr = AL;
518 AL = AL->getNext();
519 ThisAttr->setNext(0);
520
521 // If this is an attribute we can handle, do so now, otherwise, add it to
522 // the LeftOverAttrs list for rechaining.
523 switch (ThisAttr->getKind()) {
524 default: break;
525 case AttributeList::AT_address_space:
526 Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr);
527 delete ThisAttr; // Consume the attribute.
528 continue;
529 }
530
531 LeftOverAttrs.push_back(ThisAttr);
532 }
533
534 // Rechain any attributes that haven't been deleted to the DeclSpec.
535 AttributeList *List = 0;
536 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
537 LeftOverAttrs[i]->setNext(List);
538 List = LeftOverAttrs[i];
539 }
540
541 return List;
542}
543
544/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
545/// specified type.
546QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type,
547 AttributeList *Attr) {
548 // If this type is already address space qualified, reject it.
549 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
550 // for two or more different address spaces."
551 if (Type.getAddressSpace()) {
552 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers);
553 return Type;
554 }
555
556 // Check the attribute arguments.
557 if (Attr->getNumArgs() != 1) {
558 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
559 std::string("1"));
560 return Type;
561 }
562 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0));
563 llvm::APSInt addrSpace(32);
564 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) {
565 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int,
566 ASArgExpr->getSourceRange());
567 return Type;
568 }
569
570 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
571 return Context.getASQualType(Type, ASIdx);
572}
573