blob: 7acb4587a0ba2a98bcadfd6571356b41be8d292c [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 Lattner99dbc962008-06-26 06:27:57 +000024QualType Sema::ConvertDeclSpecToType(const 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:
Chris Lattner4a68fe02008-07-26 00:46:50 +000046 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattnerada63792008-07-26 01:53:50 +000047 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
48 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner4a68fe02008-07-26 00:46:50 +000049 DS.getNumProtocolQualifiers());
50 break;
51 }
52
Chris Lattner6ab935b2008-04-05 06:32:51 +000053 // Unspecified typespec defaults to int in C90. However, the C90 grammar
54 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
55 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
56 // Note that the one exception to this is function definitions, which are
57 // allowed to be completely missing a declspec. This is handled in the
58 // parser already though by it pretending to have seen an 'int' in this
59 // case.
60 if (getLangOptions().ImplicitInt) {
61 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
62 DeclSpec::PQ_TypeSpecifier |
63 DeclSpec::PQ_TypeQualifier)) == 0)
64 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
65 } else {
66 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
67 // "At least one type specifier shall be given in the declaration
68 // specifiers in each declaration, and in the specifier-qualifier list in
69 // each struct declaration and type name."
70 if (!DS.hasTypeSpecifier())
71 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
72 }
73
74 // FALL THROUGH.
Chris Lattner5328f312007-08-21 17:02:28 +000075 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000076 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
77 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000078 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
79 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
80 case DeclSpec::TSW_long: Result = Context.LongTy; break;
81 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000082 }
83 } else {
84 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000085 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
86 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
87 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
88 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000089 }
90 }
Chris Lattner06fb8672008-02-20 21:40:32 +000091 break;
Chris Lattner5328f312007-08-21 17:02:28 +000092 }
Chris Lattner726c5452008-02-20 23:53:49 +000093 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +000094 case DeclSpec::TST_double:
95 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +000096 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000097 else
Chris Lattner726c5452008-02-20 23:53:49 +000098 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000099 break;
Chris Lattner726c5452008-02-20 23:53:49 +0000100 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +0000101 case DeclSpec::TST_decimal32: // _Decimal32
102 case DeclSpec::TST_decimal64: // _Decimal64
103 case DeclSpec::TST_decimal128: // _Decimal128
104 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner2e78db32008-04-13 18:59:07 +0000105 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000106 case DeclSpec::TST_enum:
107 case DeclSpec::TST_union:
108 case DeclSpec::TST_struct: {
109 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000110 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +0000111 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
112 DS.getTypeSpecSign() == 0 &&
113 "Can't handle qualifiers on typedef names yet!");
114 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000115 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000116 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000117 }
118 case DeclSpec::TST_typedef: {
119 Decl *D = static_cast<Decl *>(DS.getTypeRep());
120 assert(D && "Didn't get a decl for a typedef?");
121 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
122 DS.getTypeSpecSign() == 0 &&
123 "Can't handle qualifiers on typedef names yet!");
Chris Lattnerada63792008-07-26 01:53:50 +0000124 DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers();
Douglas Gregor1d661552008-04-13 21:07:44 +0000125
Steve Naroff81f1bba2007-09-06 21:24:23 +0000126 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
127 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +0000128 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000129 if (PQ == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +0000130 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +0000131 break;
132 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000133
Chris Lattner726c5452008-02-20 23:53:49 +0000134 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattnerada63792008-07-26 01:53:50 +0000135 (ObjCProtocolDecl**)PQ,
Chris Lattner69f01932008-02-21 01:32:26 +0000136 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000137 break;
Chris Lattner4a68fe02008-07-26 00:46:50 +0000138 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerada63792008-07-26 01:53:50 +0000139 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) {
140 // id<protocol-list>
141 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
142 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000143 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000144 }
145 }
Chris Lattner4b009652007-07-25 00:24:17 +0000146 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000147 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000148 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000150 case DeclSpec::TST_typeofType:
151 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
152 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000153 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000154 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000155 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000156 case DeclSpec::TST_typeofExpr: {
157 Expr *E = static_cast<Expr *>(DS.getTypeRep());
158 assert(E && "Didn't get an expression for typeof?");
159 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000160 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000161 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000162 }
Chris Lattner4b009652007-07-25 00:24:17 +0000163 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000164
165 // Handle complex types.
166 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000167 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000168
169 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
170 "FIXME: imaginary types not supported yet!");
171
Chris Lattnerd496fb92008-02-20 22:04:11 +0000172 // See if there are any attributes on the declspec that apply to the type (as
173 // opposed to the decl).
Chris Lattner99dbc962008-06-26 06:27:57 +0000174 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000175 ProcessTypeAttributeList(Result, AL);
Chris Lattner9e982502008-02-21 01:07:18 +0000176
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000177 // Apply const/volatile/restrict qualifiers to T.
178 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
179
180 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
181 // or incomplete types shall not be restrict-qualified." C++ also allows
182 // restrict-qualified references.
183 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000184 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
185 QualType EltTy = PT->getPointeeType();
186
187 // If we have a pointer or reference, the pointee must have an object or
188 // incomplete type.
189 if (!EltTy->isIncompleteOrObjectType()) {
190 Diag(DS.getRestrictSpecLoc(),
191 diag::err_typecheck_invalid_restrict_invalid_pointee,
192 EltTy.getAsString(), DS.getSourceRange());
193 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
194 }
195 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000196 Diag(DS.getRestrictSpecLoc(),
197 diag::err_typecheck_invalid_restrict_not_pointer,
198 Result.getAsString(), DS.getSourceRange());
Chris Lattnercfac88d2008-04-02 17:35:06 +0000199 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000200 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000201 }
202
203 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
204 // of a function type includes any type qualifiers, the behavior is
205 // undefined."
206 if (Result->isFunctionType() && TypeQuals) {
207 // Get some location to point at, either the C or V location.
208 SourceLocation Loc;
209 if (TypeQuals & QualType::Const)
210 Loc = DS.getConstSpecLoc();
211 else {
212 assert((TypeQuals & QualType::Volatile) &&
213 "Has CV quals but not C or V?");
214 Loc = DS.getVolatileSpecLoc();
215 }
216 Diag(Loc, diag::warn_typecheck_function_qualifiers,
217 Result.getAsString(), DS.getSourceRange());
218 }
219
220 Result = Result.getQualifiedType(TypeQuals);
221 }
Chris Lattner9e982502008-02-21 01:07:18 +0000222 return Result;
223}
224
Chris Lattner4b009652007-07-25 00:24:17 +0000225/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
226/// instances.
227QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000228 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000229 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000230 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
231 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
232
Chris Lattner726c5452008-02-20 23:53:49 +0000233 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroff91b03f72007-08-28 03:03:08 +0000234
Chris Lattner4b009652007-07-25 00:24:17 +0000235 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
236 // are ordered from the identifier out, which is opposite of what we want :).
237 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner69f01932008-02-21 01:32:26 +0000238 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000239 switch (DeclType.Kind) {
240 default: assert(0 && "Unknown decltype!");
241 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000242 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000243 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000244 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000245 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000246 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000247 T = Context.IntTy;
248 }
249
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000250 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
251 // object or incomplete types shall not be restrict-qualified."
252 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000253 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000254 Diag(DeclType.Loc,
255 diag::err_typecheck_invalid_restrict_invalid_pointee,
256 T.getAsString());
257 DeclType.Ptr.TypeQuals &= QualType::Restrict;
258 }
259
Chris Lattner4b009652007-07-25 00:24:17 +0000260 // Apply the pointer typequals to the pointer object.
261 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
262 break;
263 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000264 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000265 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000266 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000267 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000268 D.setInvalidType(true);
Chris Lattnercfac88d2008-04-02 17:35:06 +0000269 T = RT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000270 }
271
Chris Lattner5fcb38b2008-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 Lattner9db553e2008-04-02 06:59:01 +0000275 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-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
Chris Lattner4b009652007-07-25 00:24:17 +0000282 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000283
284 // Handle restrict on references.
285 if (DeclType.Ref.HasRestrict)
286 T.addRestrict();
Chris Lattner4b009652007-07-25 00:24:17 +0000287 break;
288 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000289 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000290 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000291 ArrayType::ArraySizeModifier ASM;
292 if (ATI.isStar)
293 ASM = ArrayType::Star;
294 else if (ATI.hasStatic)
295 ASM = ArrayType::Static;
296 else
297 ASM = ArrayType::Normal;
298
299 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
300 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
301 if (T->isIncompleteType()) {
302 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
303 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000304 T = Context.IntTy;
305 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000306 } else if (T->isFunctionType()) {
307 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000308 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000309 T = Context.getPointerType(T);
310 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000311 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000312 // C++ 8.3.2p4: There shall be no ... arrays of references ...
313 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000314 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000315 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000316 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000317 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000318 // If the element type is a struct or union that contains a variadic
319 // array, reject it: C99 6.7.2.1p2.
320 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
321 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
322 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000323 T = Context.IntTy;
324 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000325 }
326 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000327 // C99 6.7.5.2p1: The size expression shall have integer type.
328 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
329 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
330 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
331 D.setInvalidType(true);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000332 delete ArraySize;
333 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000334 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000335 llvm::APSInt ConstVal(32);
Eli Friedman8ff07782008-02-15 18:16:39 +0000336 if (!ArraySize) {
337 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Eli Friedman2ff28d12008-05-14 00:40:18 +0000338 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
339 !T->isConstantSizeType()) {
340 // Per C99, a variable array is an array with either a non-constant
341 // size or an element type that has a non-constant-size
Steve Naroff24c9b982007-08-30 18:10:14 +0000342 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000343 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000344 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
345 // have a value greater than zero.
346 if (ConstVal.isSigned()) {
347 if (ConstVal.isNegative()) {
348 Diag(ArraySize->getLocStart(),
349 diag::err_typecheck_negative_array_size,
350 ArraySize->getSourceRange());
351 D.setInvalidType(true);
352 } else if (ConstVal == 0) {
353 // GCC accepts zero sized static arrays.
354 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
355 ArraySize->getSourceRange());
356 }
357 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000358 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000359 }
Chris Lattner47958f62007-08-28 16:54:00 +0000360 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
361 if (!getLangOptions().C99 &&
362 (ASM != ArrayType::Normal ||
363 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
364 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000365 break;
366 }
367 case DeclaratorChunk::Function:
368 // If the function declarator has a prototype (i.e. it is not () and
369 // does not have a K&R-style identifier list), then the arguments are part
370 // of the type, otherwise the argument list is ().
371 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000372
Chris Lattner6ad7e882007-12-19 05:31:29 +0000373 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000374 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000375 Diag(DeclType.Loc, diag::err_func_returning_array_function,
376 T.getAsString());
377 T = Context.IntTy;
378 D.setInvalidType(true);
379 }
380
Chris Lattner4b009652007-07-25 00:24:17 +0000381 if (!FTI.hasPrototype) {
382 // Simple void foo(), where the incoming T is the result type.
383 T = Context.getFunctionTypeNoProto(T);
384
385 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
386 if (FTI.NumArgs != 0)
387 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
388
389 } else {
390 // Otherwise, we have a function with an argument list that is
391 // potentially variadic.
392 llvm::SmallVector<QualType, 16> ArgTys;
393
394 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000395 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
396 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000397 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000398 //
399 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
400 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000401 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000402 // argument type in the function prototype *will not* match the
403 // type in ParmVarDecl (which makes the code generator unhappy).
404 //
405 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000406 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000407 // it should be driving off the type being created here.
408 //
409 // FIXME: If a source translation tool needs to see the original type,
410 // then we need to consider storing both types somewhere...
411 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000412 if (ArgTy->isArrayType()) {
413 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000414 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000415 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000416
Chris Lattner4b009652007-07-25 00:24:17 +0000417 // Look for 'void'. void is allowed only as a single argument to a
418 // function with no other parameters (C99 6.7.5.3p10). We record
419 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000420 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // If this is something like 'float(int, void)', reject it. 'void'
422 // is an incomplete type (C99 6.2.5p19) and function decls cannot
423 // have arguments of incomplete type.
424 if (FTI.NumArgs != 1 || FTI.isVariadic) {
425 Diag(DeclType.Loc, diag::err_void_only_param);
426 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000427 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000428 } else if (FTI.ArgInfo[i].Ident) {
429 // Reject, but continue to parse 'int(void abc)'.
430 Diag(FTI.ArgInfo[i].IdentLoc,
431 diag::err_param_with_void_type);
432 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000433 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000434 } else {
435 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000436 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000437 Diag(DeclType.Loc, diag::err_void_param_qualified);
438
439 // Do not add 'void' to the ArgTys list.
440 break;
441 }
442 }
443
444 ArgTys.push_back(ArgTy);
445 }
446 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
447 FTI.isVariadic);
448 }
449 break;
450 }
Chris Lattner65a57042008-06-29 00:50:08 +0000451
452 // See if there are any attributes on this declarator chunk.
453 if (const AttributeList *AL = DeclType.getAttrs())
454 ProcessTypeAttributeList(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000455 }
456
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000457 // If there were any type attributes applied to the decl itself (not the
458 // type, apply the type attribute to the type!)
459 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000460 ProcessTypeAttributeList(T, Attrs);
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000461
Chris Lattner4b009652007-07-25 00:24:17 +0000462 return T;
463}
464
Ted Kremenek42730c52008-01-07 19:49:32 +0000465/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000466/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000467QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
468 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000469 QualType T = MDecl->getResultType();
470 llvm::SmallVector<QualType, 16> ArgTys;
471
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000472 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000473 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000474 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000475 selfTy = Context.getPointerType(selfTy);
476 ArgTys.push_back(selfTy);
477 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000478 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000479 ArgTys.push_back(Context.getObjCIdType());
480 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000481
Chris Lattner685d7922008-03-16 01:07:14 +0000482 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000483 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
484 QualType ArgTy = PDecl->getType();
485 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000486 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
487 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000488 // Sema::ActOnParamDeclarator().
489 if (ArgTy->isArrayType())
490 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000491 else if (ArgTy->isFunctionType())
492 ArgTy = Context.getPointerType(ArgTy);
493 ArgTys.push_back(ArgTy);
494 }
495 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000496 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000497 return T;
498}
499
Steve Naroff0acc9c92007-09-15 18:49:24 +0000500Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000501 // C99 6.7.6: Type names have no identifier. This is already validated by
502 // the parser.
503 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
504
505 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000506
507 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000508
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000509 // Check that there are no default arguments (C++ only).
510 if (getLangOptions().CPlusPlus)
511 CheckExtraCXXDefaultArguments(D);
512
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000513 // In this context, we *do not* check D.getInvalidType(). If the declarator
514 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
515 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000516 return T.getAsOpaquePtr();
517}
518
Chris Lattner65a57042008-06-29 00:50:08 +0000519
520
521//===----------------------------------------------------------------------===//
522// Type Attribute Processing
523//===----------------------------------------------------------------------===//
524
525/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
526/// specified type. The attribute contains 1 argument, the id of the address
527/// space for the type.
528static void HandleAddressSpaceTypeAttribute(QualType &Type,
529 const AttributeList &Attr, Sema &S){
530 // If this type is already address space qualified, reject it.
531 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
532 // for two or more different address spaces."
533 if (Type.getAddressSpace()) {
534 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
535 return;
536 }
537
538 // Check the attribute arguments.
539 if (Attr.getNumArgs() != 1) {
540 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
541 std::string("1"));
542 return;
543 }
544 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
545 llvm::APSInt addrSpace(32);
546 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
547 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int,
548 ASArgExpr->getSourceRange());
549 return;
550 }
551
552 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
553 Type = S.Context.getASQualType(Type, ASIdx);
554}
555
556void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000557 // Scan through and apply attributes to this type where it makes sense. Some
558 // attributes (such as __address_space__, __vector_size__, etc) apply to the
559 // type, but others can be present in the type specifiers even though they
Chris Lattner99dbc962008-06-26 06:27:57 +0000560 // apply to the decl. Here we apply type attributes and ignore the rest.
561 for (; AL; AL = AL->getNext()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000562 // If this is an attribute we can handle, do so now, otherwise, add it to
563 // the LeftOverAttrs list for rechaining.
Chris Lattner99dbc962008-06-26 06:27:57 +0000564 switch (AL->getKind()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000565 default: break;
566 case AttributeList::AT_address_space:
Chris Lattner65a57042008-06-29 00:50:08 +0000567 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
568 break;
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000569 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000570 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000571}
572
Eli Friedman86ad5222008-05-27 03:33:27 +0000573