blob: 85a457bd562f4cf39bc4b30cc4797a25d0283958 [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 Lattnerfca0ddd2008-06-26 06:27:57 +000024QualType Sema::ConvertDeclSpecToType(const 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:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000046 // "<proto1,proto2>" is an objc qualified ID with a missing id.
47 if (llvm::SmallVector<Action::DeclTy *, 8> *PQ=DS.getProtocolQualifiers()) {
48 Action::DeclTy **PPDecl = &(*PQ)[0];
49 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)(PPDecl),
50 DS.getNumProtocolQualifiers());
51 break;
52 }
53
Chris Lattnerd658b562008-04-05 06:32:51 +000054 // Unspecified typespec defaults to int in C90. However, the C90 grammar
55 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
56 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
57 // Note that the one exception to this is function definitions, which are
58 // allowed to be completely missing a declspec. This is handled in the
59 // parser already though by it pretending to have seen an 'int' in this
60 // case.
61 if (getLangOptions().ImplicitInt) {
62 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
63 DeclSpec::PQ_TypeSpecifier |
64 DeclSpec::PQ_TypeQualifier)) == 0)
65 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
66 } else {
67 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
68 // "At least one type specifier shall be given in the declaration
69 // specifiers in each declaration, and in the specifier-qualifier list in
70 // each struct declaration and type name."
71 if (!DS.hasTypeSpecifier())
72 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
73 }
74
75 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000076 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000077 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
78 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000079 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
80 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
81 case DeclSpec::TSW_long: Result = Context.LongTy; break;
82 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000083 }
84 } else {
85 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000086 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
87 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
88 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
89 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000090 }
91 }
Chris Lattner958858e2008-02-20 21:40:32 +000092 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +000093 }
Chris Lattnerfab5b452008-02-20 23:53:49 +000094 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +000095 case DeclSpec::TST_double:
96 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +000097 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +000098 else
Chris Lattnerfab5b452008-02-20 23:53:49 +000099 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000100 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000101 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 case DeclSpec::TST_decimal32: // _Decimal32
103 case DeclSpec::TST_decimal64: // _Decimal64
104 case DeclSpec::TST_decimal128: // _Decimal128
105 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000106 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 case DeclSpec::TST_enum:
108 case DeclSpec::TST_union:
109 case DeclSpec::TST_struct: {
110 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000111 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
113 DS.getTypeSpecSign() == 0 &&
114 "Can't handle qualifiers on typedef names yet!");
115 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000116 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000117 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 case DeclSpec::TST_typedef: {
120 Decl *D = static_cast<Decl *>(DS.getTypeRep());
121 assert(D && "Didn't get a decl for a typedef?");
122 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
123 DS.getTypeSpecSign() == 0 &&
124 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000125
Steve Naroff3536b442007-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 Kremeneka526c5c2008-01-07 19:49:32 +0000128 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner958858e2008-02-20 21:40:32 +0000129 if (DS.getProtocolQualifiers() == 0) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000130 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner958858e2008-02-20 21:40:32 +0000131 break;
132 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000133
134 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattnerfab5b452008-02-20 23:53:49 +0000135 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
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;
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000139 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000140 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000141 && DS.getProtocolQualifiers()) {
142 // id<protocol-list>
143 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000144 Result = Context.getObjCQualifiedIdType(
Chris Lattner958858e2008-02-20 21:40:32 +0000145 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner76549142008-02-21 01:32:26 +0000146 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000147 break;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000148 }
149 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000151 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000152 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 }
Chris Lattner958858e2008-02-20 21:40:32 +0000154 case DeclSpec::TST_typeofType:
155 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
156 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000157 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000158 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000159 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000160 case DeclSpec::TST_typeofExpr: {
161 Expr *E = static_cast<Expr *>(DS.getTypeRep());
162 assert(E && "Didn't get an expression for typeof?");
163 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000164 Result = Context.getTypeOfExpr(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000165 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 }
Chris Lattner958858e2008-02-20 21:40:32 +0000168
169 // Handle complex types.
170 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000171 Result = Context.getComplexType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000172
173 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
174 "FIXME: imaginary types not supported yet!");
175
Chris Lattner38d8b982008-02-20 22:04:11 +0000176 // See if there are any attributes on the declspec that apply to the type (as
177 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000178 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000179 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000180
Chris Lattner96b77fc2008-04-02 06:50:17 +0000181 // Apply const/volatile/restrict qualifiers to T.
182 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
183
184 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
185 // or incomplete types shall not be restrict-qualified." C++ also allows
186 // restrict-qualified references.
187 if (TypeQuals & QualType::Restrict) {
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000188 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
189 QualType EltTy = PT->getPointeeType();
190
191 // If we have a pointer or reference, the pointee must have an object or
192 // incomplete type.
193 if (!EltTy->isIncompleteOrObjectType()) {
194 Diag(DS.getRestrictSpecLoc(),
195 diag::err_typecheck_invalid_restrict_invalid_pointee,
196 EltTy.getAsString(), DS.getSourceRange());
197 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
198 }
199 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000200 Diag(DS.getRestrictSpecLoc(),
201 diag::err_typecheck_invalid_restrict_not_pointer,
202 Result.getAsString(), DS.getSourceRange());
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000203 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000204 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000205 }
206
207 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
208 // of a function type includes any type qualifiers, the behavior is
209 // undefined."
210 if (Result->isFunctionType() && TypeQuals) {
211 // Get some location to point at, either the C or V location.
212 SourceLocation Loc;
213 if (TypeQuals & QualType::Const)
214 Loc = DS.getConstSpecLoc();
215 else {
216 assert((TypeQuals & QualType::Volatile) &&
217 "Has CV quals but not C or V?");
218 Loc = DS.getVolatileSpecLoc();
219 }
220 Diag(Loc, diag::warn_typecheck_function_qualifiers,
221 Result.getAsString(), DS.getSourceRange());
222 }
223
224 Result = Result.getQualifiedType(TypeQuals);
225 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000226 return Result;
227}
228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
230/// instances.
231QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattnerb23deda2007-08-28 16:40:32 +0000232 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000233 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000234 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
235 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
236
Chris Lattnerfab5b452008-02-20 23:53:49 +0000237 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroffe1223f72007-08-28 03:03:08 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
240 // are ordered from the identifier out, which is opposite of what we want :).
241 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner76549142008-02-21 01:32:26 +0000242 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 switch (DeclType.Kind) {
244 default: assert(0 && "Unknown decltype!");
245 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000246 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner96b77fc2008-04-02 06:50:17 +0000248 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000249 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000250 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000251 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 }
253
Chris Lattner96b77fc2008-04-02 06:50:17 +0000254 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
255 // object or incomplete types shall not be restrict-qualified."
256 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000257 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000258 Diag(DeclType.Loc,
259 diag::err_typecheck_invalid_restrict_invalid_pointee,
260 T.getAsString());
261 DeclType.Ptr.TypeQuals &= QualType::Restrict;
262 }
263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // Apply the pointer typequals to the pointer object.
265 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
266 break;
267 case DeclaratorChunk::Reference:
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000268 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattnerbde71842008-02-21 01:32:57 +0000269 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000270 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000271 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000272 D.setInvalidType(true);
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000273 T = RT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 }
275
Chris Lattner96b77fc2008-04-02 06:50:17 +0000276 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
277 // object or incomplete types shall not be restrict-qualified."
278 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000279 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000280 Diag(DeclType.Loc,
281 diag::err_typecheck_invalid_restrict_invalid_pointee,
282 T.getAsString());
283 DeclType.Ref.HasRestrict = false;
284 }
285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000287
288 // Handle restrict on references.
289 if (DeclType.Ref.HasRestrict)
290 T.addRestrict();
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);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000340 if (!ArraySize) {
341 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Eli Friedman37148aa2008-05-14 00:40:18 +0000342 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
343 !T->isConstantSizeType()) {
344 // Per C99, a variable array is an array with either a non-constant
345 // size or an element type that has a non-constant-size
Steve Naroffc9406122007-08-30 18:10:14 +0000346 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000347 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000348 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
349 // have a value greater than zero.
350 if (ConstVal.isSigned()) {
351 if (ConstVal.isNegative()) {
352 Diag(ArraySize->getLocStart(),
353 diag::err_typecheck_negative_array_size,
354 ArraySize->getSourceRange());
355 D.setInvalidType(true);
356 } else if (ConstVal == 0) {
357 // GCC accepts zero sized static arrays.
358 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
359 ArraySize->getSourceRange());
360 }
361 }
Steve Naroffc9406122007-08-30 18:10:14 +0000362 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000363 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000364 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
365 if (!getLangOptions().C99 &&
366 (ASM != ArrayType::Normal ||
367 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
368 Diag(D.getIdentifierLoc(), diag::ext_vla);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 break;
370 }
371 case DeclaratorChunk::Function:
372 // If the function declarator has a prototype (i.e. it is not () and
373 // does not have a K&R-style identifier list), then the arguments are part
374 // of the type, otherwise the argument list is ().
375 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000376
Chris Lattnercd881292007-12-19 05:31:29 +0000377 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000378 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnercd881292007-12-19 05:31:29 +0000379 Diag(DeclType.Loc, diag::err_func_returning_array_function,
380 T.getAsString());
381 T = Context.IntTy;
382 D.setInvalidType(true);
383 }
384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 if (!FTI.hasPrototype) {
386 // Simple void foo(), where the incoming T is the result type.
387 T = Context.getFunctionTypeNoProto(T);
388
389 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
390 if (FTI.NumArgs != 0)
391 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
392
393 } else {
394 // Otherwise, we have a function with an argument list that is
395 // potentially variadic.
396 llvm::SmallVector<QualType, 16> ArgTys;
397
398 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000399 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
400 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000401 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000402 //
403 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
404 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000405 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000406 // argument type in the function prototype *will not* match the
407 // type in ParmVarDecl (which makes the code generator unhappy).
408 //
409 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000410 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000411 // it should be driving off the type being created here.
412 //
413 // FIXME: If a source translation tool needs to see the original type,
414 // then we need to consider storing both types somewhere...
415 //
Chris Lattnere6327742008-04-02 05:18:44 +0000416 if (ArgTy->isArrayType()) {
417 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000418 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000419 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000420
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 // Look for 'void'. void is allowed only as a single argument to a
422 // function with no other parameters (C99 6.7.5.3p10). We record
423 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000424 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 // If this is something like 'float(int, void)', reject it. 'void'
426 // is an incomplete type (C99 6.2.5p19) and function decls cannot
427 // have arguments of incomplete type.
428 if (FTI.NumArgs != 1 || FTI.isVariadic) {
429 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000430 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000431 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000432 } else if (FTI.ArgInfo[i].Ident) {
433 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000435 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000436 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000437 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000438 } else {
439 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000440 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000441 Diag(DeclType.Loc, diag::err_void_param_qualified);
442
443 // Do not add 'void' to the ArgTys list.
444 break;
445 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 }
447
448 ArgTys.push_back(ArgTy);
449 }
450 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
451 FTI.isVariadic);
452 }
453 break;
454 }
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000455
456 // See if there are any attributes on this declarator chunk.
457 if (const AttributeList *AL = DeclType.getAttrs())
458 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 }
460
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000461 // If there were any type attributes applied to the decl itself (not the
462 // type, apply the type attribute to the type!)
463 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000464 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000465
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 return T;
467}
468
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000469/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000470/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000471QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
472 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000473 QualType T = MDecl->getResultType();
474 llvm::SmallVector<QualType, 16> ArgTys;
475
Fariborz Jahanian35600022007-11-09 17:18:29 +0000476 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000477 if (MDecl->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000478 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000479 selfTy = Context.getPointerType(selfTy);
480 ArgTys.push_back(selfTy);
481 }
Fariborz Jahanian35600022007-11-09 17:18:29 +0000482 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000483 ArgTys.push_back(Context.getObjCIdType());
484 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000485
Chris Lattner58cce3b2008-03-16 01:07:14 +0000486 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000487 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
488 QualType ArgTy = PDecl->getType();
489 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000490 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
491 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000492 // Sema::ActOnParamDeclarator().
493 if (ArgTy->isArrayType())
494 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000495 else if (ArgTy->isFunctionType())
496 ArgTy = Context.getPointerType(ArgTy);
497 ArgTys.push_back(ArgTy);
498 }
499 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffb99a4a32007-12-18 03:41:15 +0000500 MDecl->isVariadic());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000501 return T;
502}
503
Steve Naroff08d92e42007-09-15 18:49:24 +0000504Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 // C99 6.7.6: Type names have no identifier. This is already validated by
506 // the parser.
507 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
508
509 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000510
511 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000512
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000513 // Check that there are no default arguments (C++ only).
514 if (getLangOptions().CPlusPlus)
515 CheckExtraCXXDefaultArguments(D);
516
Steve Naroff5912a352007-08-28 20:14:24 +0000517 // In this context, we *do not* check D.getInvalidType(). If the declarator
518 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
519 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 return T.getAsOpaquePtr();
521}
522
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000523
524
525//===----------------------------------------------------------------------===//
526// Type Attribute Processing
527//===----------------------------------------------------------------------===//
528
529/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
530/// specified type. The attribute contains 1 argument, the id of the address
531/// space for the type.
532static void HandleAddressSpaceTypeAttribute(QualType &Type,
533 const AttributeList &Attr, Sema &S){
534 // If this type is already address space qualified, reject it.
535 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
536 // for two or more different address spaces."
537 if (Type.getAddressSpace()) {
538 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
539 return;
540 }
541
542 // Check the attribute arguments.
543 if (Attr.getNumArgs() != 1) {
544 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
545 std::string("1"));
546 return;
547 }
548 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
549 llvm::APSInt addrSpace(32);
550 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
551 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int,
552 ASArgExpr->getSourceRange());
553 return;
554 }
555
556 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
557 Type = S.Context.getASQualType(Type, ASIdx);
558}
559
560void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000561 // Scan through and apply attributes to this type where it makes sense. Some
562 // attributes (such as __address_space__, __vector_size__, etc) apply to the
563 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000564 // apply to the decl. Here we apply type attributes and ignore the rest.
565 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000566 // If this is an attribute we can handle, do so now, otherwise, add it to
567 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000568 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000569 default: break;
570 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000571 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
572 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000573 }
Chris Lattner232e8822008-02-21 01:08:11 +0000574 }
Chris Lattner232e8822008-02-21 01:08:11 +0000575}
576
Eli Friedman3c0eb162008-05-27 03:33:27 +0000577