blob: 942d7063a1d8ac6044ff300d57b5a431579ba0f4 [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"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000018#include "clang/Basic/Diagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000019#include "clang/Parse/DeclSpec.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;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000045 case DeclSpec::TST_wchar:
46 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
47 Result = Context.WCharTy;
48 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
49 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec,
50 DS.getSpecifierName(DS.getTypeSpecType()));
51 Result = Context.getSignedWCharType();
52 } else {
53 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
54 "Unknown TSS value");
55 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec,
56 DS.getSpecifierName(DS.getTypeSpecType()));
57 Result = Context.getUnsignedWCharType();
58 }
59 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000060 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000061 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000062 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000063 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000064 DS.getNumProtocolQualifiers());
65 break;
66 }
67
Chris Lattnerd658b562008-04-05 06:32:51 +000068 // Unspecified typespec defaults to int in C90. However, the C90 grammar
69 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
70 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
71 // Note that the one exception to this is function definitions, which are
72 // allowed to be completely missing a declspec. This is handled in the
73 // parser already though by it pretending to have seen an 'int' in this
74 // case.
75 if (getLangOptions().ImplicitInt) {
76 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
77 DeclSpec::PQ_TypeSpecifier |
78 DeclSpec::PQ_TypeQualifier)) == 0)
79 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
80 } else {
81 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
82 // "At least one type specifier shall be given in the declaration
83 // specifiers in each declaration, and in the specifier-qualifier list in
84 // each struct declaration and type name."
85 if (!DS.hasTypeSpecifier())
86 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
87 }
88
89 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000090 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
92 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000093 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
94 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
95 case DeclSpec::TSW_long: Result = Context.LongTy; break;
96 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000097 }
98 } else {
99 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000100 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
101 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
102 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
103 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 }
105 }
Chris Lattner958858e2008-02-20 21:40:32 +0000106 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000107 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000108 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000109 case DeclSpec::TST_double:
110 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000111 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000112 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000113 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000114 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000115 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 case DeclSpec::TST_decimal32: // _Decimal32
117 case DeclSpec::TST_decimal64: // _Decimal64
118 case DeclSpec::TST_decimal128: // _Decimal128
119 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000120 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 case DeclSpec::TST_enum:
122 case DeclSpec::TST_union:
123 case DeclSpec::TST_struct: {
124 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000125 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
127 DS.getTypeSpecSign() == 0 &&
128 "Can't handle qualifiers on typedef names yet!");
129 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000130 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000131 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 }
133 case DeclSpec::TST_typedef: {
134 Decl *D = static_cast<Decl *>(DS.getTypeRep());
135 assert(D && "Didn't get a decl for a typedef?");
136 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
137 DS.getTypeSpecSign() == 0 &&
138 "Can't handle qualifiers on typedef names yet!");
Chris Lattnerae4da612008-07-26 01:53:50 +0000139 DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers();
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000140
Steve Naroff3536b442007-09-06 21:24:23 +0000141 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
142 // we have this "hack" for now...
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000143 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000144 if (PQ == 0) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000145 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner958858e2008-02-20 21:40:32 +0000146 break;
147 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000148
Chris Lattnerfab5b452008-02-20 23:53:49 +0000149 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattnerae4da612008-07-26 01:53:50 +0000150 (ObjCProtocolDecl**)PQ,
Chris Lattner76549142008-02-21 01:32:26 +0000151 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000152 break;
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000153 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000154 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) {
155 // id<protocol-list>
156 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
157 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000158 break;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000159 }
160 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000162 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000163 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
Chris Lattner958858e2008-02-20 21:40:32 +0000165 case DeclSpec::TST_typeofType:
166 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
167 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000168 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000169 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000170 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000171 case DeclSpec::TST_typeofExpr: {
172 Expr *E = static_cast<Expr *>(DS.getTypeRep());
173 assert(E && "Didn't get an expression for typeof?");
174 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000175 Result = Context.getTypeOfExpr(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000176 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000177 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 }
Chris Lattner958858e2008-02-20 21:40:32 +0000179
180 // Handle complex types.
181 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000182 Result = Context.getComplexType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000183
184 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
185 "FIXME: imaginary types not supported yet!");
186
Chris Lattner38d8b982008-02-20 22:04:11 +0000187 // See if there are any attributes on the declspec that apply to the type (as
188 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000189 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000190 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000191
Chris Lattner96b77fc2008-04-02 06:50:17 +0000192 // Apply const/volatile/restrict qualifiers to T.
193 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
194
195 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
196 // or incomplete types shall not be restrict-qualified." C++ also allows
197 // restrict-qualified references.
198 if (TypeQuals & QualType::Restrict) {
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000199 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
200 QualType EltTy = PT->getPointeeType();
201
202 // If we have a pointer or reference, the pointee must have an object or
203 // incomplete type.
204 if (!EltTy->isIncompleteOrObjectType()) {
205 Diag(DS.getRestrictSpecLoc(),
206 diag::err_typecheck_invalid_restrict_invalid_pointee,
207 EltTy.getAsString(), DS.getSourceRange());
208 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
209 }
210 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000211 Diag(DS.getRestrictSpecLoc(),
212 diag::err_typecheck_invalid_restrict_not_pointer,
213 Result.getAsString(), DS.getSourceRange());
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000214 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000215 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000216 }
217
218 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
219 // of a function type includes any type qualifiers, the behavior is
220 // undefined."
221 if (Result->isFunctionType() && TypeQuals) {
222 // Get some location to point at, either the C or V location.
223 SourceLocation Loc;
224 if (TypeQuals & QualType::Const)
225 Loc = DS.getConstSpecLoc();
226 else {
227 assert((TypeQuals & QualType::Volatile) &&
228 "Has CV quals but not C or V?");
229 Loc = DS.getVolatileSpecLoc();
230 }
231 Diag(Loc, diag::warn_typecheck_function_qualifiers,
232 Result.getAsString(), DS.getSourceRange());
233 }
234
235 Result = Result.getQualifiedType(TypeQuals);
236 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000237 return Result;
238}
239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
241/// instances.
242QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattnerb23deda2007-08-28 16:40:32 +0000243 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000244 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000245 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
246 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
247
Chris Lattnerfab5b452008-02-20 23:53:49 +0000248 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroffe1223f72007-08-28 03:03:08 +0000249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
251 // are ordered from the identifier out, which is opposite of what we want :).
252 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner76549142008-02-21 01:32:26 +0000253 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 switch (DeclType.Kind) {
255 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000256 case DeclaratorChunk::BlockPointer:
257 if (DeclType.Cls.TypeQuals)
258 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
259 if (!T.getTypePtr()->isFunctionType())
260 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
261 else
262 T = Context.getBlockPointerType(T);
263 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000265 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner96b77fc2008-04-02 06:50:17 +0000267 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000268 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000269 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000270 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 }
272
Chris Lattner96b77fc2008-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.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000276 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000277 Diag(DeclType.Loc,
278 diag::err_typecheck_invalid_restrict_invalid_pointee,
279 T.getAsString());
280 DeclType.Ptr.TypeQuals &= QualType::Restrict;
281 }
282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // Apply the pointer typequals to the pointer object.
284 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
285 break;
286 case DeclaratorChunk::Reference:
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000287 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattnerbde71842008-02-21 01:32:57 +0000288 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000289 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000290 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000291 D.setInvalidType(true);
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000292 T = RT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 }
294
Chris Lattner96b77fc2008-04-02 06:50:17 +0000295 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
296 // object or incomplete types shall not be restrict-qualified."
297 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000298 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000299 Diag(DeclType.Loc,
300 diag::err_typecheck_invalid_restrict_invalid_pointee,
301 T.getAsString());
302 DeclType.Ref.HasRestrict = false;
303 }
304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000306
307 // Handle restrict on references.
308 if (DeclType.Ref.HasRestrict)
309 T.addRestrict();
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 break;
311 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000312 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000313 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 ArrayType::ArraySizeModifier ASM;
315 if (ATI.isStar)
316 ASM = ArrayType::Star;
317 else if (ATI.hasStatic)
318 ASM = ArrayType::Static;
319 else
320 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
323 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
324 if (T->isIncompleteType()) {
325 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
326 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000327 T = Context.IntTy;
328 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000329 } else if (T->isFunctionType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000331 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000332 T = Context.getPointerType(T);
333 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000334 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // C++ 8.3.2p4: There shall be no ... arrays of references ...
336 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000337 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000338 T = RT->getPointeeType();
Steve Naroffe1223f72007-08-28 03:03:08 +0000339 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000340 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // If the element type is a struct or union that contains a variadic
342 // array, reject it: C99 6.7.2.1p2.
343 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
344 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
345 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000346 T = Context.IntTy;
347 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 }
Chris Lattner43477ca2008-08-18 22:49:54 +0000349 } else if (T->isObjCInterfaceType()) {
350 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces,
351 T.getAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 }
Chris Lattner43477ca2008-08-18 22:49:54 +0000353
Steve Naroff42471f82007-08-30 22:35:45 +0000354 // C99 6.7.5.2p1: The size expression shall have integer type.
355 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
356 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
357 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
358 D.setInvalidType(true);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000359 delete ArraySize;
360 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000361 }
Steve Naroffc9406122007-08-30 18:10:14 +0000362 llvm::APSInt ConstVal(32);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000363 if (!ArraySize) {
364 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Eli Friedman37148aa2008-05-14 00:40:18 +0000365 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
366 !T->isConstantSizeType()) {
367 // Per C99, a variable array is an array with either a non-constant
368 // size or an element type that has a non-constant-size
Steve Naroffc9406122007-08-30 18:10:14 +0000369 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000370 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000371 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
372 // have a value greater than zero.
373 if (ConstVal.isSigned()) {
374 if (ConstVal.isNegative()) {
375 Diag(ArraySize->getLocStart(),
376 diag::err_typecheck_negative_array_size,
377 ArraySize->getSourceRange());
378 D.setInvalidType(true);
379 } else if (ConstVal == 0) {
380 // GCC accepts zero sized static arrays.
381 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
382 ArraySize->getSourceRange());
383 }
384 }
Steve Naroffc9406122007-08-30 18:10:14 +0000385 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000386 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000387 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
388 if (!getLangOptions().C99 &&
389 (ASM != ArrayType::Normal ||
390 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
391 Diag(D.getIdentifierLoc(), diag::ext_vla);
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 break;
393 }
394 case DeclaratorChunk::Function:
395 // If the function declarator has a prototype (i.e. it is not () and
396 // does not have a K&R-style identifier list), then the arguments are part
397 // of the type, otherwise the argument list is ().
398 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000399
Chris Lattnercd881292007-12-19 05:31:29 +0000400 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000401 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnercd881292007-12-19 05:31:29 +0000402 Diag(DeclType.Loc, diag::err_func_returning_array_function,
403 T.getAsString());
404 T = Context.IntTy;
405 D.setInvalidType(true);
406 }
407
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000408 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000409 if (getLangOptions().CPlusPlus) {
410 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
411 // function takes no arguments.
412 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic);
413 } else {
414 // Simple void foo(), where the incoming T is the result type.
415 T = Context.getFunctionTypeNoProto(T);
416 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000417 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000419 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 } else {
421 // Otherwise, we have a function with an argument list that is
422 // potentially variadic.
423 llvm::SmallVector<QualType, 16> ArgTys;
424
425 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000426 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
427 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000428 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000429 //
430 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
431 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000432 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000433 // argument type in the function prototype *will not* match the
434 // type in ParmVarDecl (which makes the code generator unhappy).
435 //
436 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000437 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000438 // it should be driving off the type being created here.
439 //
440 // FIXME: If a source translation tool needs to see the original type,
441 // then we need to consider storing both types somewhere...
442 //
Chris Lattnere6327742008-04-02 05:18:44 +0000443 if (ArgTy->isArrayType()) {
444 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000445 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000446 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000447
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 // Look for 'void'. void is allowed only as a single argument to a
449 // function with no other parameters (C99 6.7.5.3p10). We record
450 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000451 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 // If this is something like 'float(int, void)', reject it. 'void'
453 // is an incomplete type (C99 6.2.5p19) and function decls cannot
454 // have arguments of incomplete type.
455 if (FTI.NumArgs != 1 || FTI.isVariadic) {
456 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000457 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000458 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000459 } else if (FTI.ArgInfo[i].Ident) {
460 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000462 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000463 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000464 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000465 } else {
466 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000467 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000468 Diag(DeclType.Loc, diag::err_void_param_qualified);
469
470 // Do not add 'void' to the ArgTys list.
471 break;
472 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000473 } else if (!FTI.hasPrototype) {
474 if (ArgTy->isPromotableIntegerType()) {
475 ArgTy = Context.IntTy;
476 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
477 if (BTy->getKind() == BuiltinType::Float)
478 ArgTy = Context.DoubleTy;
479 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 }
481
482 ArgTys.push_back(ArgTy);
483 }
484 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
485 FTI.isVariadic);
486 }
487 break;
488 }
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000489
490 // See if there are any attributes on this declarator chunk.
491 if (const AttributeList *AL = DeclType.getAttrs())
492 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 }
494
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000495 // If there were any type attributes applied to the decl itself (not the
496 // type, apply the type attribute to the type!)
497 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000498 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000499
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 return T;
501}
502
Douglas Gregor98cd5992008-10-21 23:43:52 +0000503/// GetNonReferenceType - If Type is a reference type (e.g., const
504/// int&), returns the type that the reference refers to ("const
505/// int"). Otherwise, returns the type itself. This routine is used
506/// throughout to implement C++ 5p6:
507///
508/// If an expression initially has the type "reference to T" (8.3.2,
509/// 8.5.3), the type is adjusted to "T" prior to any further
510/// analysis, the expression designates the object or function
511/// denoted by the reference, and the expression is an lvalue.
512QualType Sema::GetNonReferenceType(QualType Type)
513{
514 if (const ReferenceType *RefType = Type->getAsReferenceType())
515 return RefType->getPointeeType();
516 else
517 return Type;
518}
519
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000521/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
523 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000524 QualType T = MDecl->getResultType();
525 llvm::SmallVector<QualType, 16> ArgTys;
526
Fariborz Jahanian35600022007-11-09 17:18:29 +0000527 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000528 if (MDecl->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000530 selfTy = Context.getPointerType(selfTy);
531 ArgTys.push_back(selfTy);
532 }
Fariborz Jahanian35600022007-11-09 17:18:29 +0000533 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000534 ArgTys.push_back(Context.getObjCIdType());
535 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000536
Chris Lattner58cce3b2008-03-16 01:07:14 +0000537 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000538 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
539 QualType ArgTy = PDecl->getType();
540 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000541 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
542 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000543 // Sema::ActOnParamDeclarator().
544 if (ArgTy->isArrayType())
545 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000546 else if (ArgTy->isFunctionType())
547 ArgTy = Context.getPointerType(ArgTy);
548 ArgTys.push_back(ArgTy);
549 }
550 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffb99a4a32007-12-18 03:41:15 +0000551 MDecl->isVariadic());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000552 return T;
553}
554
Steve Naroff08d92e42007-09-15 18:49:24 +0000555Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 // C99 6.7.6: Type names have no identifier. This is already validated by
557 // the parser.
558 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
559
560 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000561
562 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000564 // Check that there are no default arguments (C++ only).
565 if (getLangOptions().CPlusPlus)
566 CheckExtraCXXDefaultArguments(D);
567
Steve Naroff5912a352007-08-28 20:14:24 +0000568 // In this context, we *do not* check D.getInvalidType(). If the declarator
569 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
570 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 return T.getAsOpaquePtr();
572}
573
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000574
575
576//===----------------------------------------------------------------------===//
577// Type Attribute Processing
578//===----------------------------------------------------------------------===//
579
580/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
581/// specified type. The attribute contains 1 argument, the id of the address
582/// space for the type.
583static void HandleAddressSpaceTypeAttribute(QualType &Type,
584 const AttributeList &Attr, Sema &S){
585 // If this type is already address space qualified, reject it.
586 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
587 // for two or more different address spaces."
588 if (Type.getAddressSpace()) {
589 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
590 return;
591 }
592
593 // Check the attribute arguments.
594 if (Attr.getNumArgs() != 1) {
595 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
596 std::string("1"));
597 return;
598 }
599 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
600 llvm::APSInt addrSpace(32);
601 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
602 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int,
603 ASArgExpr->getSourceRange());
604 return;
605 }
606
607 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
608 Type = S.Context.getASQualType(Type, ASIdx);
609}
610
611void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000612 // Scan through and apply attributes to this type where it makes sense. Some
613 // attributes (such as __address_space__, __vector_size__, etc) apply to the
614 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000615 // apply to the decl. Here we apply type attributes and ignore the rest.
616 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000617 // If this is an attribute we can handle, do so now, otherwise, add it to
618 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000619 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000620 default: break;
621 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000622 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
623 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000624 }
Chris Lattner232e8822008-02-21 01:08:11 +0000625 }
Chris Lattner232e8822008-02-21 01:08:11 +0000626}
627
Eli Friedman3c0eb162008-05-27 03:33:27 +0000628