blob: bc2bce3883aee17a0ae7be328185bbd97e32c085 [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())
Chris Lattner173144a2009-02-27 22:31:56 +000080 Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec)
81 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
82 "int");
Douglas Gregor4310f4e2009-02-16 22:38:20 +000083 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +000084 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
85 // "At least one type specifier shall be given in the declaration
86 // specifiers in each declaration, and in the specifier-qualifier list in
87 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +000088 // FIXME: Does Microsoft really have the implicit int extension in C++?
89 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
90 diag::err_missing_type_specifier
Chris Lattner35d276f2009-02-27 18:53:28 +000091 : diag::warn_missing_type_specifier;
Chris Lattner173144a2009-02-27 22:31:56 +000092 Diag(DS.getSourceRange().getBegin(), DK)
93 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
94 "int");
Chris Lattnerd658b562008-04-05 06:32:51 +000095 }
96
97 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000098 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
100 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000101 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
102 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
103 case DeclSpec::TSW_long: Result = Context.LongTy; break;
104 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 }
106 } else {
107 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000108 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
109 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
110 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
111 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113 }
Chris Lattner958858e2008-02-20 21:40:32 +0000114 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000115 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000116 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000117 case DeclSpec::TST_double:
118 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000119 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000120 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000121 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000122 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000123 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 case DeclSpec::TST_decimal32: // _Decimal32
125 case DeclSpec::TST_decimal64: // _Decimal64
126 case DeclSpec::TST_decimal128: // _Decimal128
127 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000128 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 case DeclSpec::TST_enum:
130 case DeclSpec::TST_union:
131 case DeclSpec::TST_struct: {
132 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000133 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
135 DS.getTypeSpecSign() == 0 &&
136 "Can't handle qualifiers on typedef names yet!");
137 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000138 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000139 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000141 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
143 DS.getTypeSpecSign() == 0 &&
144 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000145 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000146
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000147 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
148 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
149 // we have this "hack" for now...
150 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
151 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
152 (ObjCProtocolDecl**)PQ,
153 DS.getNumProtocolQualifiers());
154 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000155 // id<protocol-list>
156 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
157 DS.getNumProtocolQualifiers());
Steve Naroff4262a072009-02-23 18:53:24 +0000158 else if (Result == Context.getObjCClassType())
159 // Class<protocol-list>
Steve Naroff8dfb0c52009-02-21 19:50:43 +0000160 Diag(DS.getSourceRange().getBegin(),
Steve Naroff4262a072009-02-23 18:53:24 +0000161 diag::err_qualified_class_unsupported) << DS.getSourceRange();
162 else
163 Diag(DS.getSourceRange().getBegin(),
164 diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000165 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000167 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Chris Lattner958858e2008-02-20 21:40:32 +0000169 case DeclSpec::TST_typeofType:
170 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
171 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000172 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000173 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000174 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000175 case DeclSpec::TST_typeofExpr: {
176 Expr *E = static_cast<Expr *>(DS.getTypeRep());
177 assert(E && "Didn't get an expression for typeof?");
178 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000179 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000180 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000181 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000182 case DeclSpec::TST_error:
183 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 }
Chris Lattner958858e2008-02-20 21:40:32 +0000185
186 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000187 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
188 if (getLangOptions().Freestanding)
189 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000190 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000191 }
Chris Lattner958858e2008-02-20 21:40:32 +0000192
193 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
194 "FIXME: imaginary types not supported yet!");
195
Chris Lattner38d8b982008-02-20 22:04:11 +0000196 // See if there are any attributes on the declspec that apply to the type (as
197 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000198 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000199 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000200
Chris Lattner96b77fc2008-04-02 06:50:17 +0000201 // Apply const/volatile/restrict qualifiers to T.
202 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
203
204 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
205 // or incomplete types shall not be restrict-qualified." C++ also allows
206 // restrict-qualified references.
207 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000208 if (Result->isPointerType() || Result->isReferenceType()) {
209 QualType EltTy = Result->isPointerType() ?
210 Result->getAsPointerType()->getPointeeType() :
211 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000212
213 // If we have a pointer or reference, the pointee must have an object or
214 // incomplete type.
215 if (!EltTy->isIncompleteOrObjectType()) {
216 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000217 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000218 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000219 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
220 }
221 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000222 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000223 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000224 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000225 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000226 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000227 }
228
229 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
230 // of a function type includes any type qualifiers, the behavior is
231 // undefined."
232 if (Result->isFunctionType() && TypeQuals) {
233 // Get some location to point at, either the C or V location.
234 SourceLocation Loc;
235 if (TypeQuals & QualType::Const)
236 Loc = DS.getConstSpecLoc();
237 else {
238 assert((TypeQuals & QualType::Volatile) &&
239 "Has CV quals but not C or V?");
240 Loc = DS.getVolatileSpecLoc();
241 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000242 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000243 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000244 }
245
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000246 // C++ [dcl.ref]p1:
247 // Cv-qualified references are ill-formed except when the
248 // cv-qualifiers are introduced through the use of a typedef
249 // (7.1.3) or of a template type argument (14.3), in which
250 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000251 // FIXME: Shouldn't we be checking SCS_typedef here?
252 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000253 TypeQuals && Result->isReferenceType()) {
254 TypeQuals &= ~QualType::Const;
255 TypeQuals &= ~QualType::Volatile;
256 }
257
Chris Lattner96b77fc2008-04-02 06:50:17 +0000258 Result = Result.getQualifiedType(TypeQuals);
259 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000260 return Result;
261}
262
Douglas Gregorcd281c32009-02-28 00:25:32 +0000263static std::string getPrintableNameForEntity(DeclarationName Entity) {
264 if (Entity)
265 return Entity.getAsString();
266
267 return "type name";
268}
269
270/// \brief Build a pointer type.
271///
272/// \param T The type to which we'll be building a pointer.
273///
274/// \param Quals The cvr-qualifiers to be applied to the pointer type.
275///
276/// \param Loc The location of the entity whose type involves this
277/// pointer type or, if there is no such entity, the location of the
278/// type that will have pointer type.
279///
280/// \param Entity The name of the entity that involves the pointer
281/// type, if known.
282///
283/// \returns A suitable pointer type, if there are no
284/// errors. Otherwise, returns a NULL type.
285QualType Sema::BuildPointerType(QualType T, unsigned Quals,
286 SourceLocation Loc, DeclarationName Entity) {
287 if (T->isReferenceType()) {
288 // C++ 8.3.2p4: There shall be no ... pointers to references ...
289 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
290 << getPrintableNameForEntity(Entity);
291 return QualType();
292 }
293
294 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
295 // object or incomplete types shall not be restrict-qualified."
296 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
297 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
298 << T;
299 Quals &= ~QualType::Restrict;
300 }
301
302 // Build the pointer type.
303 return Context.getPointerType(T).getQualifiedType(Quals);
304}
305
306/// \brief Build a reference type.
307///
308/// \param T The type to which we'll be building a reference.
309///
310/// \param Quals The cvr-qualifiers to be applied to the reference type.
311///
312/// \param Loc The location of the entity whose type involves this
313/// reference type or, if there is no such entity, the location of the
314/// type that will have reference type.
315///
316/// \param Entity The name of the entity that involves the reference
317/// type, if known.
318///
319/// \returns A suitable reference type, if there are no
320/// errors. Otherwise, returns a NULL type.
321QualType Sema::BuildReferenceType(QualType T, unsigned Quals,
322 SourceLocation Loc, DeclarationName Entity) {
323 if (T->isReferenceType()) {
324 // C++ [dcl.ref]p4: There shall be no references to references.
325 //
326 // According to C++ DR 106, references to references are only
327 // diagnosed when they are written directly (e.g., "int & &"),
328 // but not when they happen via a typedef:
329 //
330 // typedef int& intref;
331 // typedef intref& intref2;
332 //
333 // Parser::ParserDeclaratorInternal diagnoses the case where
334 // references are written directly; here, we handle the
335 // collapsing of references-to-references as described in C++
336 // DR 106 and amended by C++ DR 540.
337 return T;
338 }
339
340 // C++ [dcl.ref]p1:
341 // A declarator that specifies the type “reference to cv void”
342 // is ill-formed.
343 if (T->isVoidType()) {
344 Diag(Loc, diag::err_reference_to_void);
345 return QualType();
346 }
347
348 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
349 // object or incomplete types shall not be restrict-qualified."
350 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
351 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
352 << T;
353 Quals &= ~QualType::Restrict;
354 }
355
356 // C++ [dcl.ref]p1:
357 // [...] Cv-qualified references are ill-formed except when the
358 // cv-qualifiers are introduced through the use of a typedef
359 // (7.1.3) or of a template type argument (14.3), in which case
360 // the cv-qualifiers are ignored.
361 //
362 // We diagnose extraneous cv-qualifiers for the non-typedef,
363 // non-template type argument case within the parser. Here, we just
364 // ignore any extraneous cv-qualifiers.
365 Quals &= ~QualType::Const;
366 Quals &= ~QualType::Volatile;
367
368 // Handle restrict on references.
369 return Context.getReferenceType(T).getQualifiedType(Quals);
370}
371
372/// \brief Build an array type.
373///
374/// \param T The type of each element in the array.
375///
376/// \param ASM C99 array size modifier (e.g., '*', 'static').
377///
378/// \param ArraySize Expression describing the size of the array.
379///
380/// \param Quals The cvr-qualifiers to be applied to the array's
381/// element type.
382///
383/// \param Loc The location of the entity whose type involves this
384/// array type or, if there is no such entity, the location of the
385/// type that will have array type.
386///
387/// \param Entity The name of the entity that involves the array
388/// type, if known.
389///
390/// \returns A suitable array type, if there are no errors. Otherwise,
391/// returns a NULL type.
392QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
393 Expr *ArraySize, unsigned Quals,
394 SourceLocation Loc, DeclarationName Entity) {
395 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
396 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
397 if (DiagnoseIncompleteType(Loc, T,
398 diag::err_illegal_decl_array_incomplete_type))
399 return QualType();
400
401 if (T->isFunctionType()) {
402 Diag(Loc, diag::err_illegal_decl_array_of_functions)
403 << getPrintableNameForEntity(Entity);
404 return QualType();
405 }
406
407 // C++ 8.3.2p4: There shall be no ... arrays of references ...
408 if (T->isReferenceType()) {
409 Diag(Loc, diag::err_illegal_decl_array_of_references)
410 << getPrintableNameForEntity(Entity);
411 return QualType();
412 }
413
414 if (const RecordType *EltTy = T->getAsRecordType()) {
415 // If the element type is a struct or union that contains a variadic
416 // array, accept it as a GNU extension: C99 6.7.2.1p2.
417 if (EltTy->getDecl()->hasFlexibleArrayMember())
418 Diag(Loc, diag::ext_flexible_array_in_array) << T;
419 } else if (T->isObjCInterfaceType()) {
420 Diag(Loc, diag::warn_objc_array_of_interfaces) << T;
421 }
422
423 // C99 6.7.5.2p1: The size expression shall have integer type.
424 if (ArraySize && !ArraySize->isTypeDependent() &&
425 !ArraySize->getType()->isIntegerType()) {
426 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
427 << ArraySize->getType() << ArraySize->getSourceRange();
428 ArraySize->Destroy(Context);
429 return QualType();
430 }
431 llvm::APSInt ConstVal(32);
432 if (!ArraySize) {
433 T = Context.getIncompleteArrayType(T, ASM, Quals);
434 } else if (ArraySize->isValueDependent()) {
435 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
436 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
437 (!T->isDependentType() && !T->isConstantSizeType())) {
438 // Per C99, a variable array is an array with either a non-constant
439 // size or an element type that has a non-constant-size
440 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
441 } else {
442 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
443 // have a value greater than zero.
444 if (ConstVal.isSigned()) {
445 if (ConstVal.isNegative()) {
446 Diag(ArraySize->getLocStart(),
447 diag::err_typecheck_negative_array_size)
448 << ArraySize->getSourceRange();
449 return QualType();
450 } else if (ConstVal == 0) {
451 // GCC accepts zero sized static arrays.
452 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
453 << ArraySize->getSourceRange();
454 }
455 }
456 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
457 }
458 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
459 if (!getLangOptions().C99) {
460 if (ArraySize && !ArraySize->isTypeDependent() &&
461 !ArraySize->isValueDependent() &&
462 !ArraySize->isIntegerConstantExpr(Context))
463 Diag(Loc, diag::ext_vla);
464 else if (ASM != ArrayType::Normal || Quals != 0)
465 Diag(Loc, diag::ext_c99_array_usage);
466 }
467
468 return T;
469}
470
471
Mike Stump98eb8a72009-02-04 22:31:32 +0000472/// GetTypeForDeclarator - Convert the type for the specified
473/// declarator to Type instances. Skip the outermost Skip type
474/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000475QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000476 bool OmittedReturnType = false;
477
478 if (D.getContext() == Declarator::BlockLiteralContext
479 && Skip == 0
480 && !D.getDeclSpec().hasTypeSpecifier()
481 && (D.getNumTypeObjects() == 0
482 || (D.getNumTypeObjects() == 1
483 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
484 OmittedReturnType = true;
485
Chris Lattnerb23deda2007-08-28 16:40:32 +0000486 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000487 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000488 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
489 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000490
491 // Determine the type of the declarator. Not all forms of declarator
492 // have a type.
493 QualType T;
494 switch (D.getKind()) {
495 case Declarator::DK_Abstract:
496 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000497 case Declarator::DK_Operator: {
498 const DeclSpec& DS = D.getDeclSpec();
499 if (OmittedReturnType)
500 // We default to a dependent type initially. Can be modified by
501 // the first return statement.
502 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000503 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000504 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000505 if (T.isNull())
506 return T;
507 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000508 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000509 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000510
511 case Declarator::DK_Constructor:
512 case Declarator::DK_Destructor:
513 case Declarator::DK_Conversion:
514 // Constructors and destructors don't have return types. Use
515 // "void" instead. Conversion operators will check their return
516 // types separately.
517 T = Context.VoidTy;
518 break;
519 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000520
Douglas Gregorcd281c32009-02-28 00:25:32 +0000521 // The name we're declaring, if any.
522 DeclarationName Name;
523 if (D.getIdentifier())
524 Name = D.getIdentifier();
525
Mike Stump98eb8a72009-02-04 22:31:32 +0000526 // Walk the DeclTypeInfo, building the recursive type as we go.
527 // DeclTypeInfos are ordered from the identifier out, which is
528 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000529 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
530 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 switch (DeclType.Kind) {
532 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000533 case DeclaratorChunk::BlockPointer:
534 if (DeclType.Cls.TypeQuals)
535 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
536 if (!T.getTypePtr()->isFunctionType())
537 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
538 else
539 T = Context.getBlockPointerType(T);
540 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 case DeclaratorChunk::Pointer:
Douglas Gregorcd281c32009-02-28 00:25:32 +0000542 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000544 case DeclaratorChunk::Reference:
545 T = BuildReferenceType(T,
546 DeclType.Ref.HasRestrict? QualType::Restrict : 0,
547 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 break;
549 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000550 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000551 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 ArrayType::ArraySizeModifier ASM;
553 if (ATI.isStar)
554 ASM = ArrayType::Star;
555 else if (ATI.hasStatic)
556 ASM = ArrayType::Static;
557 else
558 ASM = ArrayType::Normal;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000559 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 break;
561 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000562 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 // If the function declarator has a prototype (i.e. it is not () and
564 // does not have a K&R-style identifier list), then the arguments are part
565 // of the type, otherwise the argument list is ().
566 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000567
Chris Lattnercd881292007-12-19 05:31:29 +0000568 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000569 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000570 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000571 T = Context.IntTy;
572 D.setInvalidType(true);
573 }
574
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000575 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000576 if (getLangOptions().CPlusPlus) {
577 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
578 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000579 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000580 } else if (FTI.isVariadic) {
581 // We allow a zero-parameter variadic function in C if the
582 // function is marked with the "overloadable"
583 // attribute. Scan for this attribute now.
584 bool Overloadable = false;
585 for (const AttributeList *Attrs = D.getAttributes();
586 Attrs; Attrs = Attrs->getNext()) {
587 if (Attrs->getKind() == AttributeList::AT_overloadable) {
588 Overloadable = true;
589 break;
590 }
591 }
592
593 if (!Overloadable)
594 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
595 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000596 } else {
597 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000598 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000599 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000600 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000602 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 } else {
604 // Otherwise, we have a function with an argument list that is
605 // potentially variadic.
606 llvm::SmallVector<QualType, 16> ArgTys;
607
608 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000609 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
610 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000611 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000612 //
613 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
614 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000615 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000616 // argument type in the function prototype *will not* match the
617 // type in ParmVarDecl (which makes the code generator unhappy).
618 //
619 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000620 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000621 // it should be driving off the type being created here.
622 //
623 // FIXME: If a source translation tool needs to see the original type,
624 // then we need to consider storing both types somewhere...
625 //
Chris Lattnere6327742008-04-02 05:18:44 +0000626 if (ArgTy->isArrayType()) {
627 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000628 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000629 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000630
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 // Look for 'void'. void is allowed only as a single argument to a
632 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000633 // int(void) as a FunctionProtoType with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000634 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 // If this is something like 'float(int, void)', reject it. 'void'
636 // is an incomplete type (C99 6.2.5p19) and function decls cannot
637 // have arguments of incomplete type.
638 if (FTI.NumArgs != 1 || FTI.isVariadic) {
639 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000640 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000641 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000642 } else if (FTI.ArgInfo[i].Ident) {
643 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000645 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000646 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000647 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000648 } else {
649 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000650 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000651 Diag(DeclType.Loc, diag::err_void_param_qualified);
652
653 // Do not add 'void' to the ArgTys list.
654 break;
655 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000656 } else if (!FTI.hasPrototype) {
657 if (ArgTy->isPromotableIntegerType()) {
658 ArgTy = Context.IntTy;
659 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
660 if (BTy->getKind() == BuiltinType::Float)
661 ArgTy = Context.DoubleTy;
662 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 }
664
665 ArgTys.push_back(ArgTy);
666 }
667 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000668 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 }
670 break;
671 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000672 case DeclaratorChunk::MemberPointer:
673 // The scope spec must refer to a class, or be dependent.
674 DeclContext *DC = static_cast<DeclContext*>(
675 DeclType.Mem.Scope().getScopeRep());
676 QualType ClsType;
677 // FIXME: Extend for dependent types when it's actually supported.
678 // See ActOnCXXNestedNameSpecifier.
679 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
680 ClsType = Context.getTagDeclType(RD);
681 } else {
682 if (DC) {
683 Diag(DeclType.Mem.Scope().getBeginLoc(),
684 diag::err_illegal_decl_mempointer_in_nonclass)
685 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
686 << DeclType.Mem.Scope().getRange();
687 }
688 D.setInvalidType(true);
689 ClsType = Context.IntTy;
690 }
691
692 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
693 // with reference type, or "cv void."
694 if (T->isReferenceType()) {
695 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
696 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
697 D.setInvalidType(true);
698 T = Context.IntTy;
699 }
700 if (T->isVoidType()) {
701 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
702 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
703 T = Context.IntTy;
704 }
705
706 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
707 // object or incomplete types shall not be restrict-qualified."
708 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
709 !T->isIncompleteOrObjectType()) {
710 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
711 << T;
712 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
713 }
714
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000715 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
716 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000717
718 break;
719 }
720
Douglas Gregorcd281c32009-02-28 00:25:32 +0000721 if (T.isNull()) {
722 D.setInvalidType(true);
723 T = Context.IntTy;
724 }
725
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000726 // See if there are any attributes on this declarator chunk.
727 if (const AttributeList *AL = DeclType.getAttrs())
728 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000730
731 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000732 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
733 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000734
735 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
736 // for a nonstatic member function, the function type to which a pointer
737 // to member refers, or the top-level function type of a function typedef
738 // declaration.
739 if (FnTy->getTypeQuals() != 0 &&
740 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000741 ((D.getContext() != Declarator::MemberContext &&
742 (!D.getCXXScopeSpec().isSet() ||
743 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000744 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000745 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000746 if (D.isFunctionDeclarator())
747 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
748 else
749 Diag(D.getIdentifierLoc(),
750 diag::err_invalid_qualified_typedef_function_type_use);
751
752 // Strip the cv-quals from the type.
753 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000754 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000755 }
756 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000757
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000758 // If there were any type attributes applied to the decl itself (not the
759 // type, apply the type attribute to the type!)
760 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000761 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000762
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 return T;
764}
765
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000766/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000767/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000768QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
Chris Lattner89951a82009-02-20 18:43:26 +0000769 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000770 QualType T = MDecl->getResultType();
771 llvm::SmallVector<QualType, 16> ArgTys;
772
Fariborz Jahanian35600022007-11-09 17:18:29 +0000773 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000774 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000775 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000776 selfTy = Context.getPointerType(selfTy);
777 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000778 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000779 ArgTys.push_back(Context.getObjCIdType());
780 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000781
Chris Lattner89951a82009-02-20 18:43:26 +0000782 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
783 E = MDecl->param_end(); PI != E; ++PI) {
784 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000785 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000786 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
787 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000788 // Sema::ActOnParamDeclarator().
789 if (ArgTy->isArrayType())
790 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000791 else if (ArgTy->isFunctionType())
792 ArgTy = Context.getPointerType(ArgTy);
793 ArgTys.push_back(ArgTy);
794 }
795 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000796 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000797 return T;
798}
799
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000800/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
801/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
802/// they point to and return true. If T1 and T2 aren't pointer types
803/// or pointer-to-member types, or if they are not similar at this
804/// level, returns false and leaves T1 and T2 unchanged. Top-level
805/// qualifiers on T1 and T2 are ignored. This function will typically
806/// be called in a loop that successively "unwraps" pointer and
807/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000808bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000809 const PointerType *T1PtrType = T1->getAsPointerType(),
810 *T2PtrType = T2->getAsPointerType();
811 if (T1PtrType && T2PtrType) {
812 T1 = T1PtrType->getPointeeType();
813 T2 = T2PtrType->getPointeeType();
814 return true;
815 }
816
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000817 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
818 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000819 if (T1MPType && T2MPType &&
820 Context.getCanonicalType(T1MPType->getClass()) ==
821 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000822 T1 = T1MPType->getPointeeType();
823 T2 = T2MPType->getPointeeType();
824 return true;
825 }
Douglas Gregor57373262008-10-22 14:17:15 +0000826 return false;
827}
828
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000829Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 // C99 6.7.6: Type names have no identifier. This is already validated by
831 // the parser.
832 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
833
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000834 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000835 if (T.isNull())
836 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000837
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000838 // Check that there are no default arguments (C++ only).
839 if (getLangOptions().CPlusPlus)
840 CheckExtraCXXDefaultArguments(D);
841
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 return T.getAsOpaquePtr();
843}
844
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000845
846
847//===----------------------------------------------------------------------===//
848// Type Attribute Processing
849//===----------------------------------------------------------------------===//
850
851/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
852/// specified type. The attribute contains 1 argument, the id of the address
853/// space for the type.
854static void HandleAddressSpaceTypeAttribute(QualType &Type,
855 const AttributeList &Attr, Sema &S){
856 // If this type is already address space qualified, reject it.
857 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
858 // for two or more different address spaces."
859 if (Type.getAddressSpace()) {
860 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
861 return;
862 }
863
864 // Check the attribute arguments.
865 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000866 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000867 return;
868 }
869 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
870 llvm::APSInt addrSpace(32);
871 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000872 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
873 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000874 return;
875 }
876
877 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000878 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000879}
880
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000881/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
882/// specified type. The attribute contains 1 argument, weak or strong.
883static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000884 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000885 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000886 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000887 return;
888 }
889
890 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000891 if (!Attr.getParameterName()) {
892 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
893 << "objc_gc" << 1;
894 return;
895 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000896 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000897 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000898 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
899 return;
900 }
901 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000902 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000903 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000904 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000905 else {
906 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
907 << "objc_gc" << Attr.getParameterName();
908 return;
909 }
910
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000911 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000912}
913
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000914void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000915 // Scan through and apply attributes to this type where it makes sense. Some
916 // attributes (such as __address_space__, __vector_size__, etc) apply to the
917 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000918 // apply to the decl. Here we apply type attributes and ignore the rest.
919 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000920 // If this is an attribute we can handle, do so now, otherwise, add it to
921 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000922 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000923 default: break;
924 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000925 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
926 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000927 case AttributeList::AT_objc_gc:
928 HandleObjCGCTypeAttribute(Result, *AL, *this);
929 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000930 }
Chris Lattner232e8822008-02-21 01:08:11 +0000931 }
Chris Lattner232e8822008-02-21 01:08:11 +0000932}
933
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000934/// @brief If the type T is incomplete and cannot be completed,
935/// produce a suitable diagnostic.
936///
937/// This routine checks whether the type @p T is complete in any
938/// context where a complete type is required. If @p T is a complete
939/// type, returns false. If @p T is incomplete, issues the diagnostic
940/// @p diag (giving it the type @p T) and returns true.
941///
942/// @param Loc The location in the source that the incomplete type
943/// diagnostic should refer to.
944///
945/// @param T The type that this routine is examining for completeness.
946///
947/// @param diag The diagnostic value (e.g.,
948/// @c diag::err_typecheck_decl_incomplete_type) that will be used
949/// for the error message if @p T is incomplete.
950///
951/// @param Range1 An optional range in the source code that will be a
952/// part of the "incomplete type" error message.
953///
954/// @param Range2 An optional range in the source code that will be a
955/// part of the "incomplete type" error message.
956///
957/// @param PrintType If non-NULL, the type that should be printed
958/// instead of @p T. This parameter should be used when the type that
959/// we're checking for incompleteness isn't the type that should be
960/// displayed to the user, e.g., when T is a type and PrintType is a
961/// pointer to T.
962///
963/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
964/// @c false otherwise.
965///
966/// @todo When Clang gets proper support for C++ templates, this
967/// routine will also be able perform template instantiation when @p T
968/// is a class template specialization.
969bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
970 SourceRange Range1, SourceRange Range2,
971 QualType PrintType) {
972 // If we have a complete type, we're done.
973 if (!T->isIncompleteType())
974 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +0000975
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000976 if (PrintType.isNull())
977 PrintType = T;
978
979 // We have an incomplete type. Produce a diagnostic.
980 Diag(Loc, diag) << PrintType << Range1 << Range2;
981
982 // If the type was a forward declaration of a class/struct/union
983 // type, produce
984 const TagType *Tag = 0;
985 if (const RecordType *Record = T->getAsRecordType())
986 Tag = Record;
987 else if (const EnumType *Enum = T->getAsEnumType())
988 Tag = Enum;
989
990 if (Tag && !Tag->getDecl()->isInvalidDecl())
991 Diag(Tag->getDecl()->getLocation(),
992 Tag->isBeingDefined() ? diag::note_type_being_defined
993 : diag::note_forward_declaration)
994 << QualType(Tag, 0);
995
996 return true;
997}