blob: 223cb56636612d004963940e8f95dd99e21c9836 [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()) {
Chris Lattner6ab935b2008-04-05 06:32:51 +000030 default: assert(0 && "Unknown TypeSpecType!");
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 Lattner6ab935b2008-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 Lattner5328f312007-08-21 17:02:28 +000068 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000069 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
70 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-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;
Chris Lattner4b009652007-07-25 00:24:17 +000075 }
76 } else {
77 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-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;
Chris Lattner4b009652007-07-25 00:24:17 +000082 }
83 }
Chris Lattner06fb8672008-02-20 21:40:32 +000084 break;
Chris Lattner5328f312007-08-21 17:02:28 +000085 }
Chris Lattner726c5452008-02-20 23:53:49 +000086 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +000087 case DeclSpec::TST_double:
88 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +000089 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000090 else
Chris Lattner726c5452008-02-20 23:53:49 +000091 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000092 break;
Chris Lattner726c5452008-02-20 23:53:49 +000093 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +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!");
Chris Lattner2e78db32008-04-13 18:59:07 +000098 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +000099 case DeclSpec::TST_enum:
100 case DeclSpec::TST_union:
101 case DeclSpec::TST_struct: {
102 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000103 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +0000104 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
105 DS.getTypeSpecSign() == 0 &&
106 "Can't handle qualifiers on typedef names yet!");
107 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000108 Result = Context.getTagDeclType(cast<TagDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000109 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000110 }
111 case DeclSpec::TST_typedef: {
112 Decl *D = static_cast<Decl *>(DS.getTypeRep());
113 assert(D && "Didn't get a decl for a typedef?");
114 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
115 DS.getTypeSpecSign() == 0 &&
116 "Can't handle qualifiers on typedef names yet!");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000117 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
118 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +0000119 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner06fb8672008-02-20 21:40:32 +0000120 if (DS.getProtocolQualifiers() == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +0000121 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +0000122 break;
123 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000124
125 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000126 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattner06fb8672008-02-20 21:40:32 +0000127 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner69f01932008-02-21 01:32:26 +0000128 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000129 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000130 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000131 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattner726c5452008-02-20 23:53:49 +0000132 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000133 && DS.getProtocolQualifiers()) {
134 // id<protocol-list>
135 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000136 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
Chris Lattner06fb8672008-02-20 21:40:32 +0000137 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner69f01932008-02-21 01:32:26 +0000138 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000139 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000140 }
141 }
Chris Lattner4b009652007-07-25 00:24:17 +0000142 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000143 Result = Context.getTypedefType(cast<TypedefDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000144 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000146 case DeclSpec::TST_typeofType:
147 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
148 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000149 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000150 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000151 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000152 case DeclSpec::TST_typeofExpr: {
153 Expr *E = static_cast<Expr *>(DS.getTypeRep());
154 assert(E && "Didn't get an expression for typeof?");
155 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000156 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000157 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000158 }
Chris Lattner4b009652007-07-25 00:24:17 +0000159 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000160
161 // Handle complex types.
162 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000163 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000164
165 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
166 "FIXME: imaginary types not supported yet!");
167
Chris Lattnerd496fb92008-02-20 22:04:11 +0000168 // See if there are any attributes on the declspec that apply to the type (as
169 // opposed to the decl).
Chris Lattner9e982502008-02-21 01:07:18 +0000170 if (AttributeList *AL = DS.getAttributes())
171 DS.SetAttributes(ProcessTypeAttributes(Result, AL));
172
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000173 // Apply const/volatile/restrict qualifiers to T.
174 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
175
176 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
177 // or incomplete types shall not be restrict-qualified." C++ also allows
178 // restrict-qualified references.
179 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000180 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
181 QualType EltTy = PT->getPointeeType();
182
183 // If we have a pointer or reference, the pointee must have an object or
184 // incomplete type.
185 if (!EltTy->isIncompleteOrObjectType()) {
186 Diag(DS.getRestrictSpecLoc(),
187 diag::err_typecheck_invalid_restrict_invalid_pointee,
188 EltTy.getAsString(), DS.getSourceRange());
189 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
190 }
191 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000192 Diag(DS.getRestrictSpecLoc(),
193 diag::err_typecheck_invalid_restrict_not_pointer,
194 Result.getAsString(), DS.getSourceRange());
Chris Lattnercfac88d2008-04-02 17:35:06 +0000195 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000196 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000197 }
198
199 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
200 // of a function type includes any type qualifiers, the behavior is
201 // undefined."
202 if (Result->isFunctionType() && TypeQuals) {
203 // Get some location to point at, either the C or V location.
204 SourceLocation Loc;
205 if (TypeQuals & QualType::Const)
206 Loc = DS.getConstSpecLoc();
207 else {
208 assert((TypeQuals & QualType::Volatile) &&
209 "Has CV quals but not C or V?");
210 Loc = DS.getVolatileSpecLoc();
211 }
212 Diag(Loc, diag::warn_typecheck_function_qualifiers,
213 Result.getAsString(), DS.getSourceRange());
214 }
215
216 Result = Result.getQualifiedType(TypeQuals);
217 }
Chris Lattner9e982502008-02-21 01:07:18 +0000218 return Result;
219}
220
Chris Lattner4b009652007-07-25 00:24:17 +0000221/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
222/// instances.
223QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000224 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000225 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000226 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
227 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
228
Chris Lattner726c5452008-02-20 23:53:49 +0000229 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroff91b03f72007-08-28 03:03:08 +0000230
Chris Lattner4b009652007-07-25 00:24:17 +0000231 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
232 // are ordered from the identifier out, which is opposite of what we want :).
233 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner69f01932008-02-21 01:32:26 +0000234 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000235 switch (DeclType.Kind) {
236 default: assert(0 && "Unknown decltype!");
237 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000238 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000239 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000240 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000241 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000242 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000243 T = Context.IntTy;
244 }
245
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000246 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
247 // object or incomplete types shall not be restrict-qualified."
248 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000249 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000250 Diag(DeclType.Loc,
251 diag::err_typecheck_invalid_restrict_invalid_pointee,
252 T.getAsString());
253 DeclType.Ptr.TypeQuals &= QualType::Restrict;
254 }
255
Chris Lattner4b009652007-07-25 00:24:17 +0000256 // Apply the pointer typequals to the pointer object.
257 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
Chris Lattner69f01932008-02-21 01:32:26 +0000258
259 // See if there are any attributes on the pointer that apply to it.
260 if (AttributeList *AL = DeclType.Ptr.AttrList)
261 DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL);
262
Chris Lattner4b009652007-07-25 00:24:17 +0000263 break;
264 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000265 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000266 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000267 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000268 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000269 D.setInvalidType(true);
Chris Lattnercfac88d2008-04-02 17:35:06 +0000270 T = RT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000271 }
272
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000273 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
274 // object or incomplete types shall not be restrict-qualified."
275 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000276 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000277 Diag(DeclType.Loc,
278 diag::err_typecheck_invalid_restrict_invalid_pointee,
279 T.getAsString());
280 DeclType.Ref.HasRestrict = false;
281 }
282
Chris Lattner4b009652007-07-25 00:24:17 +0000283 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000284
285 // Handle restrict on references.
286 if (DeclType.Ref.HasRestrict)
287 T.addRestrict();
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000288
Chris Lattner69f01932008-02-21 01:32:26 +0000289 // See if there are any attributes on the pointer that apply to it.
290 if (AttributeList *AL = DeclType.Ref.AttrList)
291 DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000292 break;
293 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000294 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000295 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000296 ArrayType::ArraySizeModifier ASM;
297 if (ATI.isStar)
298 ASM = ArrayType::Star;
299 else if (ATI.hasStatic)
300 ASM = ArrayType::Static;
301 else
302 ASM = ArrayType::Normal;
303
304 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
305 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
306 if (T->isIncompleteType()) {
307 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
308 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000309 T = Context.IntTy;
310 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000311 } else if (T->isFunctionType()) {
312 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000313 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000314 T = Context.getPointerType(T);
315 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000316 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000317 // C++ 8.3.2p4: There shall be no ... arrays of references ...
318 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000319 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000320 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000321 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000322 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000323 // If the element type is a struct or union that contains a variadic
324 // array, reject it: C99 6.7.2.1p2.
325 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
326 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
327 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000328 T = Context.IntTy;
329 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000330 }
331 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000332 // C99 6.7.5.2p1: The size expression shall have integer type.
333 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
334 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
335 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
336 D.setInvalidType(true);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000337 delete ArraySize;
338 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000339 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000340 llvm::APSInt ConstVal(32);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000341 // If no expression was provided, we consider it an incomplete array.
Eli Friedman8ff07782008-02-15 18:16:39 +0000342 if (!ArraySize) {
343 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
344 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000345 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000346 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000347 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
348 // have a value greater than zero.
349 if (ConstVal.isSigned()) {
350 if (ConstVal.isNegative()) {
351 Diag(ArraySize->getLocStart(),
352 diag::err_typecheck_negative_array_size,
353 ArraySize->getSourceRange());
354 D.setInvalidType(true);
355 } else if (ConstVal == 0) {
356 // GCC accepts zero sized static arrays.
357 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
358 ArraySize->getSourceRange());
359 }
360 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000361 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000362 }
Chris Lattner47958f62007-08-28 16:54:00 +0000363 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
364 if (!getLangOptions().C99 &&
365 (ASM != ArrayType::Normal ||
366 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
367 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000368 break;
369 }
370 case DeclaratorChunk::Function:
371 // If the function declarator has a prototype (i.e. it is not () and
372 // does not have a K&R-style identifier list), then the arguments are part
373 // of the type, otherwise the argument list is ().
374 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000375
Chris Lattner6ad7e882007-12-19 05:31:29 +0000376 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000377 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000378 Diag(DeclType.Loc, diag::err_func_returning_array_function,
379 T.getAsString());
380 T = Context.IntTy;
381 D.setInvalidType(true);
382 }
383
Chris Lattner4b009652007-07-25 00:24:17 +0000384 if (!FTI.hasPrototype) {
385 // Simple void foo(), where the incoming T is the result type.
386 T = Context.getFunctionTypeNoProto(T);
387
388 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
389 if (FTI.NumArgs != 0)
390 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
391
392 } else {
393 // Otherwise, we have a function with an argument list that is
394 // potentially variadic.
395 llvm::SmallVector<QualType, 16> ArgTys;
396
397 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000398 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
399 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000400 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000401 //
402 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
403 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000404 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000405 // argument type in the function prototype *will not* match the
406 // type in ParmVarDecl (which makes the code generator unhappy).
407 //
408 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000409 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000410 // it should be driving off the type being created here.
411 //
412 // FIXME: If a source translation tool needs to see the original type,
413 // then we need to consider storing both types somewhere...
414 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000415 if (ArgTy->isArrayType()) {
416 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000417 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000418 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000419
Chris Lattner4b009652007-07-25 00:24:17 +0000420 // Look for 'void'. void is allowed only as a single argument to a
421 // function with no other parameters (C99 6.7.5.3p10). We record
422 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000423 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000424 // If this is something like 'float(int, void)', reject it. 'void'
425 // is an incomplete type (C99 6.2.5p19) and function decls cannot
426 // have arguments of incomplete type.
427 if (FTI.NumArgs != 1 || FTI.isVariadic) {
428 Diag(DeclType.Loc, diag::err_void_only_param);
429 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000430 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000431 } else if (FTI.ArgInfo[i].Ident) {
432 // Reject, but continue to parse 'int(void abc)'.
433 Diag(FTI.ArgInfo[i].IdentLoc,
434 diag::err_param_with_void_type);
435 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000436 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000437 } else {
438 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000439 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000440 Diag(DeclType.Loc, diag::err_void_param_qualified);
441
442 // Do not add 'void' to the ArgTys list.
443 break;
444 }
445 }
446
447 ArgTys.push_back(ArgTy);
448 }
449 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
450 FTI.isVariadic);
451 }
452 break;
453 }
454 }
455
456 return T;
457}
458
Ted Kremenek42730c52008-01-07 19:49:32 +0000459/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000460/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000461QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
462 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000463 QualType T = MDecl->getResultType();
464 llvm::SmallVector<QualType, 16> ArgTys;
465
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000466 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000467 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000468 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000469 selfTy = Context.getPointerType(selfTy);
470 ArgTys.push_back(selfTy);
471 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000472 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000473 ArgTys.push_back(Context.getObjCIdType());
474 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000475
Chris Lattner685d7922008-03-16 01:07:14 +0000476 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000477 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
478 QualType ArgTy = PDecl->getType();
479 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000480 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
481 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000482 // Sema::ActOnParamDeclarator().
483 if (ArgTy->isArrayType())
484 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000485 else if (ArgTy->isFunctionType())
486 ArgTy = Context.getPointerType(ArgTy);
487 ArgTys.push_back(ArgTy);
488 }
489 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000490 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000491 return T;
492}
493
Steve Naroff0acc9c92007-09-15 18:49:24 +0000494Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000495 // C99 6.7.6: Type names have no identifier. This is already validated by
496 // the parser.
497 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
498
499 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000500
501 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000502
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000503 // In this context, we *do not* check D.getInvalidType(). If the declarator
504 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
505 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000506 return T.getAsOpaquePtr();
507}
508
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000509AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){
510 // Scan through and apply attributes to this type where it makes sense. Some
511 // attributes (such as __address_space__, __vector_size__, etc) apply to the
512 // type, but others can be present in the type specifiers even though they
513 // apply to the decl. Here we apply and delete attributes that apply to the
514 // type and leave the others alone.
515 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
516 while (AL) {
517 // Unlink this attribute from the chain, so we can process it independently.
518 AttributeList *ThisAttr = AL;
519 AL = AL->getNext();
520 ThisAttr->setNext(0);
521
522 // If this is an attribute we can handle, do so now, otherwise, add it to
523 // the LeftOverAttrs list for rechaining.
524 switch (ThisAttr->getKind()) {
525 default: break;
526 case AttributeList::AT_address_space:
527 Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr);
528 delete ThisAttr; // Consume the attribute.
529 continue;
530 }
531
532 LeftOverAttrs.push_back(ThisAttr);
533 }
534
535 // Rechain any attributes that haven't been deleted to the DeclSpec.
536 AttributeList *List = 0;
537 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
538 LeftOverAttrs[i]->setNext(List);
539 List = LeftOverAttrs[i];
540 }
541
542 return List;
543}
544
545/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
546/// specified type.
547QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type,
548 AttributeList *Attr) {
549 // If this type is already address space qualified, reject it.
550 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
551 // for two or more different address spaces."
552 if (Type.getAddressSpace()) {
553 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers);
554 return Type;
555 }
556
557 // Check the attribute arguments.
558 if (Attr->getNumArgs() != 1) {
559 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
560 std::string("1"));
561 return Type;
562 }
563 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0));
564 llvm::APSInt addrSpace(32);
565 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) {
566 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int,
567 ASArgExpr->getSourceRange());
568 return Type;
569 }
570
571 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
572 return Context.getASQualType(Type, ASIdx);
573}
574