blob: f8b277ae78172958de76fc7eda285ca396a0f52f [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) {
77 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
78 DeclSpec::PQ_TypeSpecifier |
79 DeclSpec::PQ_TypeQualifier)) == 0)
80 Diag(DS.getSourceRange().getBegin(), diag::ext_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
89 : diag::ext_missing_type_specifier;
90 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.
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 }
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) {
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000204 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
205 QualType EltTy = PT->getPointeeType();
206
207 // If we have a pointer or reference, the pointee must have an object or
208 // incomplete type.
209 if (!EltTy->isIncompleteOrObjectType()) {
210 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000211 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000212 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000213 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
214 }
215 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000216 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000217 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000218 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000219 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000220 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000221 }
222
223 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
224 // of a function type includes any type qualifiers, the behavior is
225 // undefined."
226 if (Result->isFunctionType() && TypeQuals) {
227 // Get some location to point at, either the C or V location.
228 SourceLocation Loc;
229 if (TypeQuals & QualType::Const)
230 Loc = DS.getConstSpecLoc();
231 else {
232 assert((TypeQuals & QualType::Volatile) &&
233 "Has CV quals but not C or V?");
234 Loc = DS.getVolatileSpecLoc();
235 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000236 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000237 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000238 }
239
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000240 // C++ [dcl.ref]p1:
241 // Cv-qualified references are ill-formed except when the
242 // cv-qualifiers are introduced through the use of a typedef
243 // (7.1.3) or of a template type argument (14.3), in which
244 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000245 // FIXME: Shouldn't we be checking SCS_typedef here?
246 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000247 TypeQuals && Result->isReferenceType()) {
248 TypeQuals &= ~QualType::Const;
249 TypeQuals &= ~QualType::Volatile;
250 }
251
Chris Lattner96b77fc2008-04-02 06:50:17 +0000252 Result = Result.getQualifiedType(TypeQuals);
253 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000254 return Result;
255}
256
Mike Stump98eb8a72009-02-04 22:31:32 +0000257/// GetTypeForDeclarator - Convert the type for the specified
258/// declarator to Type instances. Skip the outermost Skip type
259/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000260QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000261 bool OmittedReturnType = false;
262
263 if (D.getContext() == Declarator::BlockLiteralContext
264 && Skip == 0
265 && !D.getDeclSpec().hasTypeSpecifier()
266 && (D.getNumTypeObjects() == 0
267 || (D.getNumTypeObjects() == 1
268 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
269 OmittedReturnType = true;
270
Chris Lattnerb23deda2007-08-28 16:40:32 +0000271 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000272 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000273 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
274 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000275
276 // Determine the type of the declarator. Not all forms of declarator
277 // have a type.
278 QualType T;
279 switch (D.getKind()) {
280 case Declarator::DK_Abstract:
281 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000282 case Declarator::DK_Operator: {
283 const DeclSpec& DS = D.getDeclSpec();
284 if (OmittedReturnType)
285 // We default to a dependent type initially. Can be modified by
286 // the first return statement.
287 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000288 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000289 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000290 if (T.isNull())
291 return T;
292 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000293 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000294 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000295
296 case Declarator::DK_Constructor:
297 case Declarator::DK_Destructor:
298 case Declarator::DK_Conversion:
299 // Constructors and destructors don't have return types. Use
300 // "void" instead. Conversion operators will check their return
301 // types separately.
302 T = Context.VoidTy;
303 break;
304 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000305
Mike Stump98eb8a72009-02-04 22:31:32 +0000306 // Walk the DeclTypeInfo, building the recursive type as we go.
307 // DeclTypeInfos are ordered from the identifier out, which is
308 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000309 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
310 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 switch (DeclType.Kind) {
312 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000313 case DeclaratorChunk::BlockPointer:
314 if (DeclType.Cls.TypeQuals)
315 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
316 if (!T.getTypePtr()->isFunctionType())
317 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
318 else
319 T = Context.getBlockPointerType(T);
320 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000322 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000324 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
325 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000326 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000327 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 }
329
Chris Lattner96b77fc2008-04-02 06:50:17 +0000330 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
331 // object or incomplete types shall not be restrict-qualified."
332 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000333 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000334 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000335 << T;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000336 DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
337 }
338
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 // Apply the pointer typequals to the pointer object.
340 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
341 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000342 case DeclaratorChunk::Reference: {
343 // Whether we should suppress the creation of the reference.
344 bool SuppressReference = false;
345 if (T->isReferenceType()) {
346 // C++ [dcl.ref]p4: There shall be no references to references.
347 //
348 // According to C++ DR 106, references to references are only
349 // diagnosed when they are written directly (e.g., "int & &"),
350 // but not when they happen via a typedef:
351 //
352 // typedef int& intref;
353 // typedef intref& intref2;
354 //
355 // Parser::ParserDeclaratorInternal diagnoses the case where
356 // references are written directly; here, we handle the
357 // collapsing of references-to-references as described in C++
358 // DR 106 and amended by C++ DR 540.
359 SuppressReference = true;
360 }
361
362 // C++ [dcl.ref]p1:
363 // A declarator that specifies the type “reference to cv void”
364 // is ill-formed.
365 if (T->isVoidType()) {
366 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroffe1223f72007-08-28 03:03:08 +0000367 D.setInvalidType(true);
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000368 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 }
370
Chris Lattner96b77fc2008-04-02 06:50:17 +0000371 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
372 // object or incomplete types shall not be restrict-qualified."
373 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000374 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000375 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000376 << T;
Chris Lattner96b77fc2008-04-02 06:50:17 +0000377 DeclType.Ref.HasRestrict = false;
378 }
379
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000380 if (!SuppressReference)
381 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000382
383 // Handle restrict on references.
384 if (DeclType.Ref.HasRestrict)
385 T.addRestrict();
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000387 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000389 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000390 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 ArrayType::ArraySizeModifier ASM;
392 if (ATI.isStar)
393 ASM = ArrayType::Star;
394 else if (ATI.hasStatic)
395 ASM = ArrayType::Static;
396 else
397 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000398
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
400 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000401 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
402 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroffe1223f72007-08-28 03:03:08 +0000403 T = Context.IntTy;
404 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000405 } else if (T->isFunctionType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000406 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
407 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000408 T = Context.getPointerType(T);
409 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000410 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000412 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
413 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000414 T = RT->getPointeeType();
Steve Naroffe1223f72007-08-28 03:03:08 +0000415 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000416 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 // If the element type is a struct or union that contains a variadic
Douglas Gregor0bfe54f2009-02-10 21:49:46 +0000418 // array, accept it as a GNU extension: C99 6.7.2.1p2.
419 if (EltTy->getDecl()->hasFlexibleArrayMember())
420 Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
Chris Lattner43477ca2008-08-18 22:49:54 +0000421 } else if (T->isObjCInterfaceType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000422 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 }
Chris Lattner43477ca2008-08-18 22:49:54 +0000424
Steve Naroff42471f82007-08-30 22:35:45 +0000425 // C99 6.7.5.2p1: The size expression shall have integer type.
426 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000427 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattnerd1625842008-11-24 06:25:27 +0000428 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000429 D.setInvalidType(true);
Ted Kremenek169a2662009-02-07 01:51:40 +0000430 ArraySize->Destroy(Context);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000431 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000432 }
Eli Friedman1ca48132009-02-21 00:44:51 +0000433 llvm::APSInt ConstVal(32);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000434 if (!ArraySize) {
435 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000436 } else if (ArraySize->isValueDependent()) {
437 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman1ca48132009-02-21 00:44:51 +0000438 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000439 !T->isConstantSizeType()) {
Eli Friedman37148aa2008-05-14 00:40:18 +0000440 // Per C99, a variable array is an array with either a non-constant
441 // size or an element type that has a non-constant-size
Steve Naroffc9406122007-08-30 18:10:14 +0000442 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000443 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000444 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
445 // have a value greater than zero.
446 if (ConstVal.isSigned()) {
447 if (ConstVal.isNegative()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000448 Diag(ArraySize->getLocStart(),
449 diag::err_typecheck_negative_array_size)
450 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000451 D.setInvalidType(true);
452 } else if (ConstVal == 0) {
453 // GCC accepts zero sized static arrays.
Eli Friedman1ca48132009-02-21 00:44:51 +0000454 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000455 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000456 }
457 }
Steve Naroffc9406122007-08-30 18:10:14 +0000458 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000459 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000460 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattnera1fcbad2008-12-18 06:50:14 +0000461 if (!getLangOptions().C99) {
462 if (ArraySize && !ArraySize->isValueDependent() &&
463 !ArraySize->isIntegerConstantExpr(Context))
464 Diag(D.getIdentifierLoc(), diag::ext_vla);
465 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
466 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
467 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 break;
469 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000470 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 // If the function declarator has a prototype (i.e. it is not () and
472 // does not have a K&R-style identifier list), then the arguments are part
473 // of the type, otherwise the argument list is ().
474 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000475
Chris Lattnercd881292007-12-19 05:31:29 +0000476 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000477 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000478 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000479 T = Context.IntTy;
480 D.setInvalidType(true);
481 }
482
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000483 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000484 if (getLangOptions().CPlusPlus) {
485 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
486 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000487 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000488 } else if (FTI.isVariadic) {
489 // We allow a zero-parameter variadic function in C if the
490 // function is marked with the "overloadable"
491 // attribute. Scan for this attribute now.
492 bool Overloadable = false;
493 for (const AttributeList *Attrs = D.getAttributes();
494 Attrs; Attrs = Attrs->getNext()) {
495 if (Attrs->getKind() == AttributeList::AT_overloadable) {
496 Overloadable = true;
497 break;
498 }
499 }
500
501 if (!Overloadable)
502 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
503 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000504 } else {
505 // Simple void foo(), where the incoming T is the result type.
506 T = Context.getFunctionTypeNoProto(T);
507 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000508 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000510 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 } else {
512 // Otherwise, we have a function with an argument list that is
513 // potentially variadic.
514 llvm::SmallVector<QualType, 16> ArgTys;
515
516 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000517 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
518 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000519 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000520 //
521 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
522 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000523 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000524 // argument type in the function prototype *will not* match the
525 // type in ParmVarDecl (which makes the code generator unhappy).
526 //
527 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000528 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000529 // it should be driving off the type being created here.
530 //
531 // FIXME: If a source translation tool needs to see the original type,
532 // then we need to consider storing both types somewhere...
533 //
Chris Lattnere6327742008-04-02 05:18:44 +0000534 if (ArgTy->isArrayType()) {
535 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000536 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000537 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000538
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 // Look for 'void'. void is allowed only as a single argument to a
540 // function with no other parameters (C99 6.7.5.3p10). We record
541 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000542 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 // If this is something like 'float(int, void)', reject it. 'void'
544 // is an incomplete type (C99 6.2.5p19) and function decls cannot
545 // have arguments of incomplete type.
546 if (FTI.NumArgs != 1 || FTI.isVariadic) {
547 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000548 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000549 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000550 } else if (FTI.ArgInfo[i].Ident) {
551 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000553 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000554 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000555 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000556 } else {
557 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000558 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000559 Diag(DeclType.Loc, diag::err_void_param_qualified);
560
561 // Do not add 'void' to the ArgTys list.
562 break;
563 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000564 } else if (!FTI.hasPrototype) {
565 if (ArgTy->isPromotableIntegerType()) {
566 ArgTy = Context.IntTy;
567 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
568 if (BTy->getKind() == BuiltinType::Float)
569 ArgTy = Context.DoubleTy;
570 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
572
573 ArgTys.push_back(ArgTy);
574 }
575 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000576 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 }
578 break;
579 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000580 case DeclaratorChunk::MemberPointer:
581 // The scope spec must refer to a class, or be dependent.
582 DeclContext *DC = static_cast<DeclContext*>(
583 DeclType.Mem.Scope().getScopeRep());
584 QualType ClsType;
585 // FIXME: Extend for dependent types when it's actually supported.
586 // See ActOnCXXNestedNameSpecifier.
587 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
588 ClsType = Context.getTagDeclType(RD);
589 } else {
590 if (DC) {
591 Diag(DeclType.Mem.Scope().getBeginLoc(),
592 diag::err_illegal_decl_mempointer_in_nonclass)
593 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
594 << DeclType.Mem.Scope().getRange();
595 }
596 D.setInvalidType(true);
597 ClsType = Context.IntTy;
598 }
599
600 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
601 // with reference type, or "cv void."
602 if (T->isReferenceType()) {
603 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
604 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
605 D.setInvalidType(true);
606 T = Context.IntTy;
607 }
608 if (T->isVoidType()) {
609 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
610 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
611 T = Context.IntTy;
612 }
613
614 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
615 // object or incomplete types shall not be restrict-qualified."
616 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
617 !T->isIncompleteOrObjectType()) {
618 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
619 << T;
620 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
621 }
622
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000623 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
624 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000625
626 break;
627 }
628
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000629 // See if there are any attributes on this declarator chunk.
630 if (const AttributeList *AL = DeclType.getAttrs())
631 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000633
634 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
635 const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
636 assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
637
638 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
639 // for a nonstatic member function, the function type to which a pointer
640 // to member refers, or the top-level function type of a function typedef
641 // declaration.
642 if (FnTy->getTypeQuals() != 0 &&
643 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000644 ((D.getContext() != Declarator::MemberContext &&
645 (!D.getCXXScopeSpec().isSet() ||
646 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000647 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000648 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000649 if (D.isFunctionDeclarator())
650 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
651 else
652 Diag(D.getIdentifierLoc(),
653 diag::err_invalid_qualified_typedef_function_type_use);
654
655 // Strip the cv-quals from the type.
656 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000657 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000658 }
659 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000660
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000661 // If there were any type attributes applied to the decl itself (not the
662 // type, apply the type attribute to the type!)
663 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000664 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000665
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 return T;
667}
668
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000670/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000671QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
Chris Lattner89951a82009-02-20 18:43:26 +0000672 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000673 QualType T = MDecl->getResultType();
674 llvm::SmallVector<QualType, 16> ArgTys;
675
Fariborz Jahanian35600022007-11-09 17:18:29 +0000676 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000677 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000678 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000679 selfTy = Context.getPointerType(selfTy);
680 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000681 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682 ArgTys.push_back(Context.getObjCIdType());
683 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000684
Chris Lattner89951a82009-02-20 18:43:26 +0000685 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
686 E = MDecl->param_end(); PI != E; ++PI) {
687 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000688 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000689 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
690 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000691 // Sema::ActOnParamDeclarator().
692 if (ArgTy->isArrayType())
693 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000694 else if (ArgTy->isFunctionType())
695 ArgTy = Context.getPointerType(ArgTy);
696 ArgTys.push_back(ArgTy);
697 }
698 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000699 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000700 return T;
701}
702
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000703/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
704/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
705/// they point to and return true. If T1 and T2 aren't pointer types
706/// or pointer-to-member types, or if they are not similar at this
707/// level, returns false and leaves T1 and T2 unchanged. Top-level
708/// qualifiers on T1 and T2 are ignored. This function will typically
709/// be called in a loop that successively "unwraps" pointer and
710/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000711bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000712 const PointerType *T1PtrType = T1->getAsPointerType(),
713 *T2PtrType = T2->getAsPointerType();
714 if (T1PtrType && T2PtrType) {
715 T1 = T1PtrType->getPointeeType();
716 T2 = T2PtrType->getPointeeType();
717 return true;
718 }
719
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000720 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
721 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000722 if (T1MPType && T2MPType &&
723 Context.getCanonicalType(T1MPType->getClass()) ==
724 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000725 T1 = T1MPType->getPointeeType();
726 T2 = T2MPType->getPointeeType();
727 return true;
728 }
Douglas Gregor57373262008-10-22 14:17:15 +0000729 return false;
730}
731
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000732Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // C99 6.7.6: Type names have no identifier. This is already validated by
734 // the parser.
735 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
736
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000737 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000738 if (T.isNull())
739 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000740
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000741 // Check that there are no default arguments (C++ only).
742 if (getLangOptions().CPlusPlus)
743 CheckExtraCXXDefaultArguments(D);
744
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 return T.getAsOpaquePtr();
746}
747
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000748
749
750//===----------------------------------------------------------------------===//
751// Type Attribute Processing
752//===----------------------------------------------------------------------===//
753
754/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
755/// specified type. The attribute contains 1 argument, the id of the address
756/// space for the type.
757static void HandleAddressSpaceTypeAttribute(QualType &Type,
758 const AttributeList &Attr, Sema &S){
759 // If this type is already address space qualified, reject it.
760 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
761 // for two or more different address spaces."
762 if (Type.getAddressSpace()) {
763 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
764 return;
765 }
766
767 // Check the attribute arguments.
768 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000769 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000770 return;
771 }
772 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
773 llvm::APSInt addrSpace(32);
774 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000775 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
776 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000777 return;
778 }
779
780 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000781 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000782}
783
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000784/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
785/// specified type. The attribute contains 1 argument, weak or strong.
786static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000787 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000788 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000789 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790 return;
791 }
792
793 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000794 if (!Attr.getParameterName()) {
795 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
796 << "objc_gc" << 1;
797 return;
798 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000799 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000800 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000801 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
802 return;
803 }
804 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000805 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000806 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000807 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000808 else {
809 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
810 << "objc_gc" << Attr.getParameterName();
811 return;
812 }
813
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000814 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000815}
816
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000817void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000818 // Scan through and apply attributes to this type where it makes sense. Some
819 // attributes (such as __address_space__, __vector_size__, etc) apply to the
820 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000821 // apply to the decl. Here we apply type attributes and ignore the rest.
822 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000823 // If this is an attribute we can handle, do so now, otherwise, add it to
824 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000825 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000826 default: break;
827 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000828 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
829 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000830 case AttributeList::AT_objc_gc:
831 HandleObjCGCTypeAttribute(Result, *AL, *this);
832 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000833 }
Chris Lattner232e8822008-02-21 01:08:11 +0000834 }
Chris Lattner232e8822008-02-21 01:08:11 +0000835}
836
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000837/// @brief If the type T is incomplete and cannot be completed,
838/// produce a suitable diagnostic.
839///
840/// This routine checks whether the type @p T is complete in any
841/// context where a complete type is required. If @p T is a complete
842/// type, returns false. If @p T is incomplete, issues the diagnostic
843/// @p diag (giving it the type @p T) and returns true.
844///
845/// @param Loc The location in the source that the incomplete type
846/// diagnostic should refer to.
847///
848/// @param T The type that this routine is examining for completeness.
849///
850/// @param diag The diagnostic value (e.g.,
851/// @c diag::err_typecheck_decl_incomplete_type) that will be used
852/// for the error message if @p T is incomplete.
853///
854/// @param Range1 An optional range in the source code that will be a
855/// part of the "incomplete type" error message.
856///
857/// @param Range2 An optional range in the source code that will be a
858/// part of the "incomplete type" error message.
859///
860/// @param PrintType If non-NULL, the type that should be printed
861/// instead of @p T. This parameter should be used when the type that
862/// we're checking for incompleteness isn't the type that should be
863/// displayed to the user, e.g., when T is a type and PrintType is a
864/// pointer to T.
865///
866/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
867/// @c false otherwise.
868///
869/// @todo When Clang gets proper support for C++ templates, this
870/// routine will also be able perform template instantiation when @p T
871/// is a class template specialization.
872bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
873 SourceRange Range1, SourceRange Range2,
874 QualType PrintType) {
875 // If we have a complete type, we're done.
876 if (!T->isIncompleteType())
877 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +0000878
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000879 if (PrintType.isNull())
880 PrintType = T;
881
882 // We have an incomplete type. Produce a diagnostic.
883 Diag(Loc, diag) << PrintType << Range1 << Range2;
884
885 // If the type was a forward declaration of a class/struct/union
886 // type, produce
887 const TagType *Tag = 0;
888 if (const RecordType *Record = T->getAsRecordType())
889 Tag = Record;
890 else if (const EnumType *Enum = T->getAsEnumType())
891 Tag = Enum;
892
893 if (Tag && !Tag->getDecl()->isInvalidDecl())
894 Diag(Tag->getDecl()->getLocation(),
895 Tag->isBeingDefined() ? diag::note_type_being_defined
896 : diag::note_forward_declaration)
897 << QualType(Tag, 0);
898
899 return true;
900}