blob: bfbf8ab9529abb0701692403edff78d9dedb6301 [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/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Douglas Gregor930d8b52009-01-30 22:09:00 +000021/// \brief Convert the specified declspec to the appropriate type
22/// object.
23/// \param DS the declaration specifiers
24/// \returns The type described by the declaration specifiers, or NULL
25/// if there was an error.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +000026QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000027 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
28 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000029 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000030
31 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000032 case DeclSpec::TST_void:
33 Result = Context.VoidTy;
34 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 case DeclSpec::TST_char:
36 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000037 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000038 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000039 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 else {
41 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
42 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000043 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 }
Chris Lattner958858e2008-02-20 21:40:32 +000045 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000046 case DeclSpec::TST_wchar:
47 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
48 Result = Context.WCharTy;
49 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000050 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
51 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000052 Result = Context.getSignedWCharType();
53 } else {
54 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
55 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000056 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
57 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000058 Result = Context.getUnsignedWCharType();
59 }
60 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000061 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000062 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000063 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000064 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000065 DS.getNumProtocolQualifiers());
66 break;
67 }
68
Chris Lattnerd658b562008-04-05 06:32:51 +000069 // Unspecified typespec defaults to int in C90. However, the C90 grammar
70 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
71 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
72 // Note that the one exception to this is function definitions, which are
73 // allowed to be completely missing a declspec. This is handled in the
74 // parser already though by it pretending to have seen an 'int' in this
75 // case.
76 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +000077 // In C89 mode, we only warn if there is a completely missing declspec
78 // when one is not allowed.
79 if (DS.isEmpty())
80 Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec);
Douglas Gregor4310f4e2009-02-16 22:38:20 +000081 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +000082 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
83 // "At least one type specifier shall be given in the declaration
84 // specifiers in each declaration, and in the specifier-qualifier list in
85 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +000086 // FIXME: Does Microsoft really have the implicit int extension in C++?
87 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
88 diag::err_missing_type_specifier
Chris Lattner35d276f2009-02-27 18:53:28 +000089 : diag::warn_missing_type_specifier;
Douglas Gregor4310f4e2009-02-16 22:38:20 +000090 Diag(DS.getSourceRange().getBegin(), DK);
Chris Lattnerd658b562008-04-05 06:32:51 +000091 }
92
93 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000094 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000095 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
96 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000097 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
98 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
99 case DeclSpec::TSW_long: Result = Context.LongTy; break;
100 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 }
102 } else {
103 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000104 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
105 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
106 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
107 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
109 }
Chris Lattner958858e2008-02-20 21:40:32 +0000110 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000111 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000112 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000113 case DeclSpec::TST_double:
114 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000115 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000116 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000117 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000118 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000119 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 case DeclSpec::TST_decimal32: // _Decimal32
121 case DeclSpec::TST_decimal64: // _Decimal64
122 case DeclSpec::TST_decimal128: // _Decimal128
123 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000124 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 case DeclSpec::TST_enum:
126 case DeclSpec::TST_union:
127 case DeclSpec::TST_struct: {
128 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000129 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
131 DS.getTypeSpecSign() == 0 &&
132 "Can't handle qualifiers on typedef names yet!");
133 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000134 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000135 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000137 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
139 DS.getTypeSpecSign() == 0 &&
140 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000141 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000142
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000143 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
144 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
145 // we have this "hack" for now...
146 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
147 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
148 (ObjCProtocolDecl**)PQ,
149 DS.getNumProtocolQualifiers());
150 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000151 // id<protocol-list>
152 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
153 DS.getNumProtocolQualifiers());
Steve Naroff4262a072009-02-23 18:53:24 +0000154 else if (Result == Context.getObjCClassType())
155 // Class<protocol-list>
Steve Naroff8dfb0c52009-02-21 19:50:43 +0000156 Diag(DS.getSourceRange().getBegin(),
Steve Naroff4262a072009-02-23 18:53:24 +0000157 diag::err_qualified_class_unsupported) << DS.getSourceRange();
158 else
159 Diag(DS.getSourceRange().getBegin(),
160 diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000161 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 // TypeQuals handled by caller.
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.
Douglas Gregor72564e72009-02-26 23:50:07 +0000175 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000176 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000177 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000178 case DeclSpec::TST_error:
179 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 }
Chris Lattner958858e2008-02-20 21:40:32 +0000181
182 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000183 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
184 if (getLangOptions().Freestanding)
185 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000186 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000187 }
Chris Lattner958858e2008-02-20 21:40:32 +0000188
189 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
190 "FIXME: imaginary types not supported yet!");
191
Chris Lattner38d8b982008-02-20 22:04:11 +0000192 // See if there are any attributes on the declspec that apply to the type (as
193 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000194 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000195 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000196
Chris Lattner96b77fc2008-04-02 06:50:17 +0000197 // Apply const/volatile/restrict qualifiers to T.
198 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
199
200 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
201 // or incomplete types shall not be restrict-qualified." C++ also allows
202 // restrict-qualified references.
203 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000204 if (Result->isPointerType() || Result->isReferenceType()) {
205 QualType EltTy = Result->isPointerType() ?
206 Result->getAsPointerType()->getPointeeType() :
207 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000208
209 // If we have a pointer or reference, the pointee must have an object or
210 // incomplete type.
211 if (!EltTy->isIncompleteOrObjectType()) {
212 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000213 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000214 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000215 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
216 }
217 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000218 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000219 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000220 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000221 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000222 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000223 }
224
225 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
226 // of a function type includes any type qualifiers, the behavior is
227 // undefined."
228 if (Result->isFunctionType() && TypeQuals) {
229 // Get some location to point at, either the C or V location.
230 SourceLocation Loc;
231 if (TypeQuals & QualType::Const)
232 Loc = DS.getConstSpecLoc();
233 else {
234 assert((TypeQuals & QualType::Volatile) &&
235 "Has CV quals but not C or V?");
236 Loc = DS.getVolatileSpecLoc();
237 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000238 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000239 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000240 }
241
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000242 // C++ [dcl.ref]p1:
243 // Cv-qualified references are ill-formed except when the
244 // cv-qualifiers are introduced through the use of a typedef
245 // (7.1.3) or of a template type argument (14.3), in which
246 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000247 // FIXME: Shouldn't we be checking SCS_typedef here?
248 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000249 TypeQuals && Result->isReferenceType()) {
250 TypeQuals &= ~QualType::Const;
251 TypeQuals &= ~QualType::Volatile;
252 }
253
Chris Lattner96b77fc2008-04-02 06:50:17 +0000254 Result = Result.getQualifiedType(TypeQuals);
255 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000256 return Result;
257}
258
Mike Stump98eb8a72009-02-04 22:31:32 +0000259/// GetTypeForDeclarator - Convert the type for the specified
260/// declarator to Type instances. Skip the outermost Skip type
261/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000262QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000263 bool OmittedReturnType = false;
264
265 if (D.getContext() == Declarator::BlockLiteralContext
266 && Skip == 0
267 && !D.getDeclSpec().hasTypeSpecifier()
268 && (D.getNumTypeObjects() == 0
269 || (D.getNumTypeObjects() == 1
270 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
271 OmittedReturnType = true;
272
Chris Lattnerb23deda2007-08-28 16:40:32 +0000273 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000274 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000275 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
276 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000277
278 // Determine the type of the declarator. Not all forms of declarator
279 // have a type.
280 QualType T;
281 switch (D.getKind()) {
282 case Declarator::DK_Abstract:
283 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000284 case Declarator::DK_Operator: {
285 const DeclSpec& DS = D.getDeclSpec();
286 if (OmittedReturnType)
287 // We default to a dependent type initially. Can be modified by
288 // the first return statement.
289 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000290 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000291 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000292 if (T.isNull())
293 return T;
294 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000295 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000296 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000297
298 case Declarator::DK_Constructor:
299 case Declarator::DK_Destructor:
300 case Declarator::DK_Conversion:
301 // Constructors and destructors don't have return types. Use
302 // "void" instead. Conversion operators will check their return
303 // types separately.
304 T = Context.VoidTy;
305 break;
306 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000307
Mike Stump98eb8a72009-02-04 22:31:32 +0000308 // Walk the DeclTypeInfo, building the recursive type as we go.
309 // DeclTypeInfos are ordered from the identifier out, which is
310 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000311 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
312 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 switch (DeclType.Kind) {
314 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000315 case DeclaratorChunk::BlockPointer:
316 if (DeclType.Cls.TypeQuals)
317 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
318 if (!T.getTypePtr()->isFunctionType())
319 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
320 else
321 T = Context.getBlockPointerType(T);
322 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000324 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000326 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
327 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000328 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000329 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 }
331
Chris Lattner96b77fc2008-04-02 06:50:17 +0000332 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
333 // object or incomplete types shall not be restrict-qualified."
334 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000335 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000336 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000337 << T;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000338 DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
339 }
340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // Apply the pointer typequals to the pointer object.
342 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
343 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000344 case DeclaratorChunk::Reference: {
345 // Whether we should suppress the creation of the reference.
346 bool SuppressReference = false;
347 if (T->isReferenceType()) {
348 // C++ [dcl.ref]p4: There shall be no references to references.
349 //
350 // According to C++ DR 106, references to references are only
351 // diagnosed when they are written directly (e.g., "int & &"),
352 // but not when they happen via a typedef:
353 //
354 // typedef int& intref;
355 // typedef intref& intref2;
356 //
357 // Parser::ParserDeclaratorInternal diagnoses the case where
358 // references are written directly; here, we handle the
359 // collapsing of references-to-references as described in C++
360 // DR 106 and amended by C++ DR 540.
361 SuppressReference = true;
362 }
363
364 // C++ [dcl.ref]p1:
365 // A declarator that specifies the type “reference to cv void”
366 // is ill-formed.
367 if (T->isVoidType()) {
368 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroffe1223f72007-08-28 03:03:08 +0000369 D.setInvalidType(true);
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000370 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 }
372
Chris Lattner96b77fc2008-04-02 06:50:17 +0000373 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
374 // object or incomplete types shall not be restrict-qualified."
375 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000376 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000377 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000378 << T;
Chris Lattner96b77fc2008-04-02 06:50:17 +0000379 DeclType.Ref.HasRestrict = false;
380 }
381
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000382 if (!SuppressReference)
383 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000384
385 // Handle restrict on references.
386 if (DeclType.Ref.HasRestrict)
387 T.addRestrict();
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000389 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000391 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000392 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 ArrayType::ArraySizeModifier ASM;
394 if (ATI.isStar)
395 ASM = ArrayType::Star;
396 else if (ATI.hasStatic)
397 ASM = ArrayType::Static;
398 else
399 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000400
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
402 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000403 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
404 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroffe1223f72007-08-28 03:03:08 +0000405 T = Context.IntTy;
406 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000407 } else if (T->isFunctionType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000408 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
409 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000410 T = Context.getPointerType(T);
411 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000412 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000414 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
415 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000416 T = RT->getPointeeType();
Steve Naroffe1223f72007-08-28 03:03:08 +0000417 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000418 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 // If the element type is a struct or union that contains a variadic
Douglas Gregor0bfe54f2009-02-10 21:49:46 +0000420 // array, accept it as a GNU extension: C99 6.7.2.1p2.
421 if (EltTy->getDecl()->hasFlexibleArrayMember())
422 Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
Chris Lattner43477ca2008-08-18 22:49:54 +0000423 } else if (T->isObjCInterfaceType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000424 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 }
Chris Lattner43477ca2008-08-18 22:49:54 +0000426
Steve Naroff42471f82007-08-30 22:35:45 +0000427 // C99 6.7.5.2p1: The size expression shall have integer type.
428 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000429 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattnerd1625842008-11-24 06:25:27 +0000430 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000431 D.setInvalidType(true);
Ted Kremenek169a2662009-02-07 01:51:40 +0000432 ArraySize->Destroy(Context);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000433 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000434 }
Eli Friedman1ca48132009-02-21 00:44:51 +0000435 llvm::APSInt ConstVal(32);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000436 if (!ArraySize) {
437 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000438 } else if (ArraySize->isValueDependent()) {
439 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman1ca48132009-02-21 00:44:51 +0000440 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000441 !T->isConstantSizeType()) {
Eli Friedman37148aa2008-05-14 00:40:18 +0000442 // Per C99, a variable array is an array with either a non-constant
443 // size or an element type that has a non-constant-size
Steve Naroffc9406122007-08-30 18:10:14 +0000444 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000445 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000446 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
447 // have a value greater than zero.
448 if (ConstVal.isSigned()) {
449 if (ConstVal.isNegative()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000450 Diag(ArraySize->getLocStart(),
451 diag::err_typecheck_negative_array_size)
452 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000453 D.setInvalidType(true);
454 } else if (ConstVal == 0) {
455 // GCC accepts zero sized static arrays.
Eli Friedman1ca48132009-02-21 00:44:51 +0000456 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000457 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000458 }
459 }
Steve Naroffc9406122007-08-30 18:10:14 +0000460 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000461 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000462 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattnera1fcbad2008-12-18 06:50:14 +0000463 if (!getLangOptions().C99) {
464 if (ArraySize && !ArraySize->isValueDependent() &&
465 !ArraySize->isIntegerConstantExpr(Context))
466 Diag(D.getIdentifierLoc(), diag::ext_vla);
467 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
468 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
469 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 break;
471 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000472 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 // If the function declarator has a prototype (i.e. it is not () and
474 // does not have a K&R-style identifier list), then the arguments are part
475 // of the type, otherwise the argument list is ().
476 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000477
Chris Lattnercd881292007-12-19 05:31:29 +0000478 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000479 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000480 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000481 T = Context.IntTy;
482 D.setInvalidType(true);
483 }
484
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000485 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000486 if (getLangOptions().CPlusPlus) {
487 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
488 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000489 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000490 } else if (FTI.isVariadic) {
491 // We allow a zero-parameter variadic function in C if the
492 // function is marked with the "overloadable"
493 // attribute. Scan for this attribute now.
494 bool Overloadable = false;
495 for (const AttributeList *Attrs = D.getAttributes();
496 Attrs; Attrs = Attrs->getNext()) {
497 if (Attrs->getKind() == AttributeList::AT_overloadable) {
498 Overloadable = true;
499 break;
500 }
501 }
502
503 if (!Overloadable)
504 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
505 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000506 } else {
507 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000508 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000509 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000510 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000512 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000513 } else {
514 // Otherwise, we have a function with an argument list that is
515 // potentially variadic.
516 llvm::SmallVector<QualType, 16> ArgTys;
517
518 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000519 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
520 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000521 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000522 //
523 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
524 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000525 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000526 // argument type in the function prototype *will not* match the
527 // type in ParmVarDecl (which makes the code generator unhappy).
528 //
529 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000530 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000531 // it should be driving off the type being created here.
532 //
533 // FIXME: If a source translation tool needs to see the original type,
534 // then we need to consider storing both types somewhere...
535 //
Chris Lattnere6327742008-04-02 05:18:44 +0000536 if (ArgTy->isArrayType()) {
537 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000538 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000539 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000540
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 // Look for 'void'. void is allowed only as a single argument to a
542 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000543 // int(void) as a FunctionProtoType with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000544 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 // If this is something like 'float(int, void)', reject it. 'void'
546 // is an incomplete type (C99 6.2.5p19) and function decls cannot
547 // have arguments of incomplete type.
548 if (FTI.NumArgs != 1 || FTI.isVariadic) {
549 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000550 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000551 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000552 } else if (FTI.ArgInfo[i].Ident) {
553 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000555 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000556 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000557 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000558 } else {
559 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000560 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000561 Diag(DeclType.Loc, diag::err_void_param_qualified);
562
563 // Do not add 'void' to the ArgTys list.
564 break;
565 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000566 } else if (!FTI.hasPrototype) {
567 if (ArgTy->isPromotableIntegerType()) {
568 ArgTy = Context.IntTy;
569 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
570 if (BTy->getKind() == BuiltinType::Float)
571 ArgTy = Context.DoubleTy;
572 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 }
574
575 ArgTys.push_back(ArgTy);
576 }
577 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000578 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 }
580 break;
581 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000582 case DeclaratorChunk::MemberPointer:
583 // The scope spec must refer to a class, or be dependent.
584 DeclContext *DC = static_cast<DeclContext*>(
585 DeclType.Mem.Scope().getScopeRep());
586 QualType ClsType;
587 // FIXME: Extend for dependent types when it's actually supported.
588 // See ActOnCXXNestedNameSpecifier.
589 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
590 ClsType = Context.getTagDeclType(RD);
591 } else {
592 if (DC) {
593 Diag(DeclType.Mem.Scope().getBeginLoc(),
594 diag::err_illegal_decl_mempointer_in_nonclass)
595 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
596 << DeclType.Mem.Scope().getRange();
597 }
598 D.setInvalidType(true);
599 ClsType = Context.IntTy;
600 }
601
602 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
603 // with reference type, or "cv void."
604 if (T->isReferenceType()) {
605 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
606 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
607 D.setInvalidType(true);
608 T = Context.IntTy;
609 }
610 if (T->isVoidType()) {
611 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
612 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
613 T = Context.IntTy;
614 }
615
616 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
617 // object or incomplete types shall not be restrict-qualified."
618 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
619 !T->isIncompleteOrObjectType()) {
620 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
621 << T;
622 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
623 }
624
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000625 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
626 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000627
628 break;
629 }
630
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000631 // See if there are any attributes on this declarator chunk.
632 if (const AttributeList *AL = DeclType.getAttrs())
633 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000635
636 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000637 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
638 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000639
640 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
641 // for a nonstatic member function, the function type to which a pointer
642 // to member refers, or the top-level function type of a function typedef
643 // declaration.
644 if (FnTy->getTypeQuals() != 0 &&
645 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000646 ((D.getContext() != Declarator::MemberContext &&
647 (!D.getCXXScopeSpec().isSet() ||
648 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000649 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000650 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000651 if (D.isFunctionDeclarator())
652 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
653 else
654 Diag(D.getIdentifierLoc(),
655 diag::err_invalid_qualified_typedef_function_type_use);
656
657 // Strip the cv-quals from the type.
658 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000659 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000660 }
661 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000662
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000663 // If there were any type attributes applied to the decl itself (not the
664 // type, apply the type attribute to the type!)
665 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000666 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 return T;
669}
670
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000671/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000672/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000673QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
Chris Lattner89951a82009-02-20 18:43:26 +0000674 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000675 QualType T = MDecl->getResultType();
676 llvm::SmallVector<QualType, 16> ArgTys;
677
Fariborz Jahanian35600022007-11-09 17:18:29 +0000678 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000679 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000680 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000681 selfTy = Context.getPointerType(selfTy);
682 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000683 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000684 ArgTys.push_back(Context.getObjCIdType());
685 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000686
Chris Lattner89951a82009-02-20 18:43:26 +0000687 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
688 E = MDecl->param_end(); PI != E; ++PI) {
689 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000690 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000691 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
692 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000693 // Sema::ActOnParamDeclarator().
694 if (ArgTy->isArrayType())
695 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000696 else if (ArgTy->isFunctionType())
697 ArgTy = Context.getPointerType(ArgTy);
698 ArgTys.push_back(ArgTy);
699 }
700 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000701 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000702 return T;
703}
704
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000705/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
706/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
707/// they point to and return true. If T1 and T2 aren't pointer types
708/// or pointer-to-member types, or if they are not similar at this
709/// level, returns false and leaves T1 and T2 unchanged. Top-level
710/// qualifiers on T1 and T2 are ignored. This function will typically
711/// be called in a loop that successively "unwraps" pointer and
712/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000713bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000714 const PointerType *T1PtrType = T1->getAsPointerType(),
715 *T2PtrType = T2->getAsPointerType();
716 if (T1PtrType && T2PtrType) {
717 T1 = T1PtrType->getPointeeType();
718 T2 = T2PtrType->getPointeeType();
719 return true;
720 }
721
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000722 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
723 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000724 if (T1MPType && T2MPType &&
725 Context.getCanonicalType(T1MPType->getClass()) ==
726 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000727 T1 = T1MPType->getPointeeType();
728 T2 = T2MPType->getPointeeType();
729 return true;
730 }
Douglas Gregor57373262008-10-22 14:17:15 +0000731 return false;
732}
733
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000734Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 // C99 6.7.6: Type names have no identifier. This is already validated by
736 // the parser.
737 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
738
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000739 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000740 if (T.isNull())
741 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000742
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000743 // Check that there are no default arguments (C++ only).
744 if (getLangOptions().CPlusPlus)
745 CheckExtraCXXDefaultArguments(D);
746
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 return T.getAsOpaquePtr();
748}
749
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000750
751
752//===----------------------------------------------------------------------===//
753// Type Attribute Processing
754//===----------------------------------------------------------------------===//
755
756/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
757/// specified type. The attribute contains 1 argument, the id of the address
758/// space for the type.
759static void HandleAddressSpaceTypeAttribute(QualType &Type,
760 const AttributeList &Attr, Sema &S){
761 // If this type is already address space qualified, reject it.
762 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
763 // for two or more different address spaces."
764 if (Type.getAddressSpace()) {
765 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
766 return;
767 }
768
769 // Check the attribute arguments.
770 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000771 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000772 return;
773 }
774 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
775 llvm::APSInt addrSpace(32);
776 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000777 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
778 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000779 return;
780 }
781
782 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000783 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000784}
785
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000786/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
787/// specified type. The attribute contains 1 argument, weak or strong.
788static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000789 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000791 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000792 return;
793 }
794
795 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000796 if (!Attr.getParameterName()) {
797 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
798 << "objc_gc" << 1;
799 return;
800 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000801 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000802 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
804 return;
805 }
806 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000807 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000808 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000809 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000810 else {
811 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
812 << "objc_gc" << Attr.getParameterName();
813 return;
814 }
815
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000816 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000817}
818
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000819void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000820 // Scan through and apply attributes to this type where it makes sense. Some
821 // attributes (such as __address_space__, __vector_size__, etc) apply to the
822 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000823 // apply to the decl. Here we apply type attributes and ignore the rest.
824 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000825 // If this is an attribute we can handle, do so now, otherwise, add it to
826 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000827 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000828 default: break;
829 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000830 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
831 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000832 case AttributeList::AT_objc_gc:
833 HandleObjCGCTypeAttribute(Result, *AL, *this);
834 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000835 }
Chris Lattner232e8822008-02-21 01:08:11 +0000836 }
Chris Lattner232e8822008-02-21 01:08:11 +0000837}
838
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000839/// @brief If the type T is incomplete and cannot be completed,
840/// produce a suitable diagnostic.
841///
842/// This routine checks whether the type @p T is complete in any
843/// context where a complete type is required. If @p T is a complete
844/// type, returns false. If @p T is incomplete, issues the diagnostic
845/// @p diag (giving it the type @p T) and returns true.
846///
847/// @param Loc The location in the source that the incomplete type
848/// diagnostic should refer to.
849///
850/// @param T The type that this routine is examining for completeness.
851///
852/// @param diag The diagnostic value (e.g.,
853/// @c diag::err_typecheck_decl_incomplete_type) that will be used
854/// for the error message if @p T is incomplete.
855///
856/// @param Range1 An optional range in the source code that will be a
857/// part of the "incomplete type" error message.
858///
859/// @param Range2 An optional range in the source code that will be a
860/// part of the "incomplete type" error message.
861///
862/// @param PrintType If non-NULL, the type that should be printed
863/// instead of @p T. This parameter should be used when the type that
864/// we're checking for incompleteness isn't the type that should be
865/// displayed to the user, e.g., when T is a type and PrintType is a
866/// pointer to T.
867///
868/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
869/// @c false otherwise.
870///
871/// @todo When Clang gets proper support for C++ templates, this
872/// routine will also be able perform template instantiation when @p T
873/// is a class template specialization.
874bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
875 SourceRange Range1, SourceRange Range2,
876 QualType PrintType) {
877 // If we have a complete type, we're done.
878 if (!T->isIncompleteType())
879 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +0000880
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000881 if (PrintType.isNull())
882 PrintType = T;
883
884 // We have an incomplete type. Produce a diagnostic.
885 Diag(Loc, diag) << PrintType << Range1 << Range2;
886
887 // If the type was a forward declaration of a class/struct/union
888 // type, produce
889 const TagType *Tag = 0;
890 if (const RecordType *Record = T->getAsRecordType())
891 Tag = Record;
892 else if (const EnumType *Enum = T->getAsEnumType())
893 Tag = Enum;
894
895 if (Tag && !Tag->getDecl()->isInvalidDecl())
896 Diag(Tag->getDecl()->getLocation(),
897 Tag->isBeingDefined() ? diag::note_type_being_defined
898 : diag::note_forward_declaration)
899 << QualType(Tag, 0);
900
901 return true;
902}