blob: b8969a2202205c013757021499aa8f1b50b1ae23 [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"
Douglas Gregor2943aed2009-03-03 04:44:36 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000019#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Douglas Gregor2dc0e642009-03-23 23:06:20 +000022/// \brief Perform adjustment on the parameter type of a function.
23///
24/// This routine adjusts the given parameter type @p T to the actual
25/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
26/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
27QualType Sema::adjustParameterType(QualType T) {
28 // C99 6.7.5.3p7:
29 if (T->isArrayType()) {
30 // C99 6.7.5.3p7:
31 // A declaration of a parameter as "array of type" shall be
32 // adjusted to "qualified pointer to type", where the type
33 // qualifiers (if any) are those specified within the [ and ] of
34 // the array type derivation.
35 return Context.getArrayDecayedType(T);
36 } else if (T->isFunctionType())
37 // C99 6.7.5.3p8:
38 // A declaration of a parameter as "function returning type"
39 // shall be adjusted to "pointer to function returning type", as
40 // in 6.3.2.1.
41 return Context.getPointerType(T);
42
43 return T;
44}
45
Douglas Gregor930d8b52009-01-30 22:09:00 +000046/// \brief Convert the specified declspec to the appropriate type
47/// object.
48/// \param DS the declaration specifiers
49/// \returns The type described by the declaration specifiers, or NULL
50/// if there was an error.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +000051QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000052 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
53 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000054 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000055
56 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000057 case DeclSpec::TST_void:
58 Result = Context.VoidTy;
59 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000060 case DeclSpec::TST_char:
61 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000062 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000064 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 else {
66 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
67 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000068 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 }
Chris Lattner958858e2008-02-20 21:40:32 +000070 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000071 case DeclSpec::TST_wchar:
72 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
73 Result = Context.WCharTy;
74 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000075 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
76 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000077 Result = Context.getSignedWCharType();
78 } else {
79 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
80 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000081 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
82 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000083 Result = Context.getUnsignedWCharType();
84 }
85 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000086 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000087 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000088 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000089 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000090 DS.getNumProtocolQualifiers());
91 break;
92 }
93
Chris Lattnerd658b562008-04-05 06:32:51 +000094 // Unspecified typespec defaults to int in C90. However, the C90 grammar
95 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
96 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
97 // Note that the one exception to this is function definitions, which are
98 // allowed to be completely missing a declspec. This is handled in the
99 // parser already though by it pretending to have seen an 'int' in this
100 // case.
101 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000102 // In C89 mode, we only warn if there is a completely missing declspec
103 // when one is not allowed.
104 if (DS.isEmpty())
Chris Lattner173144a2009-02-27 22:31:56 +0000105 Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec)
106 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
107 "int");
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000108 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000109 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
110 // "At least one type specifier shall be given in the declaration
111 // specifiers in each declaration, and in the specifier-qualifier list in
112 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000113 // FIXME: Does Microsoft really have the implicit int extension in C++?
114 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
115 diag::err_missing_type_specifier
Chris Lattner35d276f2009-02-27 18:53:28 +0000116 : diag::warn_missing_type_specifier;
Douglas Gregord17a9e52009-03-27 05:10:56 +0000117 Diag(DS.getSourceRange().getBegin(), DK);
118
119 // FIXME: If we could guarantee that the result would be
120 // well-formed, it would be useful to have a code insertion hint
121 // here. However, after emitting this warning/error, we often
122 // emit other errors.
Chris Lattnerd658b562008-04-05 06:32:51 +0000123 }
124
125 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000126 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
128 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000129 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
130 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
131 case DeclSpec::TSW_long: Result = Context.LongTy; break;
132 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 }
134 } else {
135 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000136 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
137 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
138 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
139 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
141 }
Chris Lattner958858e2008-02-20 21:40:32 +0000142 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000143 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000144 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000145 case DeclSpec::TST_double:
146 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000147 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000148 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000149 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000150 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000151 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 case DeclSpec::TST_decimal32: // _Decimal32
153 case DeclSpec::TST_decimal64: // _Decimal64
154 case DeclSpec::TST_decimal128: // _Decimal128
155 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000156 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 case DeclSpec::TST_enum:
158 case DeclSpec::TST_union:
159 case DeclSpec::TST_struct: {
160 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000161 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
163 DS.getTypeSpecSign() == 0 &&
164 "Can't handle qualifiers on typedef names yet!");
165 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000166 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000167 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000169 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
171 DS.getTypeSpecSign() == 0 &&
172 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000173 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000174
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000175 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
176 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
177 // we have this "hack" for now...
178 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
179 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
180 (ObjCProtocolDecl**)PQ,
181 DS.getNumProtocolQualifiers());
182 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000183 // id<protocol-list>
184 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
185 DS.getNumProtocolQualifiers());
Steve Naroff4262a072009-02-23 18:53:24 +0000186 else if (Result == Context.getObjCClassType())
187 // Class<protocol-list>
Steve Naroff8dfb0c52009-02-21 19:50:43 +0000188 Diag(DS.getSourceRange().getBegin(),
Steve Naroff4262a072009-02-23 18:53:24 +0000189 diag::err_qualified_class_unsupported) << DS.getSourceRange();
190 else
191 Diag(DS.getSourceRange().getBegin(),
192 diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000193 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000195 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 }
Chris Lattner958858e2008-02-20 21:40:32 +0000197 case DeclSpec::TST_typeofType:
198 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
199 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000200 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000201 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000202 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000203 case DeclSpec::TST_typeofExpr: {
204 Expr *E = static_cast<Expr *>(DS.getTypeRep());
205 assert(E && "Didn't get an expression for typeof?");
206 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000207 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000208 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000209 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000210 case DeclSpec::TST_error:
211 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 }
Chris Lattner958858e2008-02-20 21:40:32 +0000213
214 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000215 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
216 if (getLangOptions().Freestanding)
217 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000218 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000219 }
Chris Lattner958858e2008-02-20 21:40:32 +0000220
221 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
222 "FIXME: imaginary types not supported yet!");
223
Chris Lattner38d8b982008-02-20 22:04:11 +0000224 // See if there are any attributes on the declspec that apply to the type (as
225 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000226 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000227 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000228
Chris Lattner96b77fc2008-04-02 06:50:17 +0000229 // Apply const/volatile/restrict qualifiers to T.
230 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
231
232 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
233 // or incomplete types shall not be restrict-qualified." C++ also allows
234 // restrict-qualified references.
235 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000236 if (Result->isPointerType() || Result->isReferenceType()) {
237 QualType EltTy = Result->isPointerType() ?
238 Result->getAsPointerType()->getPointeeType() :
239 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000240
Douglas Gregorbad0e652009-03-24 20:32:41 +0000241 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000242 // incomplete type.
243 if (!EltTy->isIncompleteOrObjectType()) {
244 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000245 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000246 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000247 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
248 }
249 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000250 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000251 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000252 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000253 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000254 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000255 }
256
257 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
258 // of a function type includes any type qualifiers, the behavior is
259 // undefined."
260 if (Result->isFunctionType() && TypeQuals) {
261 // Get some location to point at, either the C or V location.
262 SourceLocation Loc;
263 if (TypeQuals & QualType::Const)
264 Loc = DS.getConstSpecLoc();
265 else {
266 assert((TypeQuals & QualType::Volatile) &&
267 "Has CV quals but not C or V?");
268 Loc = DS.getVolatileSpecLoc();
269 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000270 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000271 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000272 }
273
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000274 // C++ [dcl.ref]p1:
275 // Cv-qualified references are ill-formed except when the
276 // cv-qualifiers are introduced through the use of a typedef
277 // (7.1.3) or of a template type argument (14.3), in which
278 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000279 // FIXME: Shouldn't we be checking SCS_typedef here?
280 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000281 TypeQuals && Result->isReferenceType()) {
282 TypeQuals &= ~QualType::Const;
283 TypeQuals &= ~QualType::Volatile;
284 }
285
Chris Lattner96b77fc2008-04-02 06:50:17 +0000286 Result = Result.getQualifiedType(TypeQuals);
287 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000288 return Result;
289}
290
Douglas Gregorcd281c32009-02-28 00:25:32 +0000291static std::string getPrintableNameForEntity(DeclarationName Entity) {
292 if (Entity)
293 return Entity.getAsString();
294
295 return "type name";
296}
297
298/// \brief Build a pointer type.
299///
300/// \param T The type to which we'll be building a pointer.
301///
302/// \param Quals The cvr-qualifiers to be applied to the pointer type.
303///
304/// \param Loc The location of the entity whose type involves this
305/// pointer type or, if there is no such entity, the location of the
306/// type that will have pointer type.
307///
308/// \param Entity The name of the entity that involves the pointer
309/// type, if known.
310///
311/// \returns A suitable pointer type, if there are no
312/// errors. Otherwise, returns a NULL type.
313QualType Sema::BuildPointerType(QualType T, unsigned Quals,
314 SourceLocation Loc, DeclarationName Entity) {
315 if (T->isReferenceType()) {
316 // C++ 8.3.2p4: There shall be no ... pointers to references ...
317 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
318 << getPrintableNameForEntity(Entity);
319 return QualType();
320 }
321
322 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
323 // object or incomplete types shall not be restrict-qualified."
324 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
325 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
326 << T;
327 Quals &= ~QualType::Restrict;
328 }
329
330 // Build the pointer type.
331 return Context.getPointerType(T).getQualifiedType(Quals);
332}
333
334/// \brief Build a reference type.
335///
336/// \param T The type to which we'll be building a reference.
337///
338/// \param Quals The cvr-qualifiers to be applied to the reference type.
339///
340/// \param Loc The location of the entity whose type involves this
341/// reference type or, if there is no such entity, the location of the
342/// type that will have reference type.
343///
344/// \param Entity The name of the entity that involves the reference
345/// type, if known.
346///
347/// \returns A suitable reference type, if there are no
348/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000349QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000350 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000351 if (LValueRef) {
352 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000353 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
354 // reference to a type T, and attempt to create the type "lvalue
355 // reference to cv TD" creates the type "lvalue reference to T".
356 // We use the qualifiers (restrict or none) of the original reference,
357 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000358 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000359 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000360 }
361 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000362 if (T->isReferenceType()) {
363 // C++ [dcl.ref]p4: There shall be no references to references.
364 //
365 // According to C++ DR 106, references to references are only
366 // diagnosed when they are written directly (e.g., "int & &"),
367 // but not when they happen via a typedef:
368 //
369 // typedef int& intref;
370 // typedef intref& intref2;
371 //
372 // Parser::ParserDeclaratorInternal diagnoses the case where
373 // references are written directly; here, we handle the
374 // collapsing of references-to-references as described in C++
375 // DR 106 and amended by C++ DR 540.
376 return T;
377 }
378
379 // C++ [dcl.ref]p1:
380 // A declarator that specifies the type “reference to cv void”
381 // is ill-formed.
382 if (T->isVoidType()) {
383 Diag(Loc, diag::err_reference_to_void);
384 return QualType();
385 }
386
387 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
388 // object or incomplete types shall not be restrict-qualified."
389 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
390 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
391 << T;
392 Quals &= ~QualType::Restrict;
393 }
394
395 // C++ [dcl.ref]p1:
396 // [...] Cv-qualified references are ill-formed except when the
397 // cv-qualifiers are introduced through the use of a typedef
398 // (7.1.3) or of a template type argument (14.3), in which case
399 // the cv-qualifiers are ignored.
400 //
401 // We diagnose extraneous cv-qualifiers for the non-typedef,
402 // non-template type argument case within the parser. Here, we just
403 // ignore any extraneous cv-qualifiers.
404 Quals &= ~QualType::Const;
405 Quals &= ~QualType::Volatile;
406
407 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000408 if (LValueRef)
409 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
410 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000411}
412
413/// \brief Build an array type.
414///
415/// \param T The type of each element in the array.
416///
417/// \param ASM C99 array size modifier (e.g., '*', 'static').
418///
419/// \param ArraySize Expression describing the size of the array.
420///
421/// \param Quals The cvr-qualifiers to be applied to the array's
422/// element type.
423///
424/// \param Loc The location of the entity whose type involves this
425/// array type or, if there is no such entity, the location of the
426/// type that will have array type.
427///
428/// \param Entity The name of the entity that involves the array
429/// type, if known.
430///
431/// \returns A suitable array type, if there are no errors. Otherwise,
432/// returns a NULL type.
433QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
434 Expr *ArraySize, unsigned Quals,
435 SourceLocation Loc, DeclarationName Entity) {
436 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
437 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000438 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000439 diag::err_illegal_decl_array_incomplete_type))
440 return QualType();
441
442 if (T->isFunctionType()) {
443 Diag(Loc, diag::err_illegal_decl_array_of_functions)
444 << getPrintableNameForEntity(Entity);
445 return QualType();
446 }
447
448 // C++ 8.3.2p4: There shall be no ... arrays of references ...
449 if (T->isReferenceType()) {
450 Diag(Loc, diag::err_illegal_decl_array_of_references)
451 << getPrintableNameForEntity(Entity);
452 return QualType();
453 }
454
455 if (const RecordType *EltTy = T->getAsRecordType()) {
456 // If the element type is a struct or union that contains a variadic
457 // array, accept it as a GNU extension: C99 6.7.2.1p2.
458 if (EltTy->getDecl()->hasFlexibleArrayMember())
459 Diag(Loc, diag::ext_flexible_array_in_array) << T;
460 } else if (T->isObjCInterfaceType()) {
461 Diag(Loc, diag::warn_objc_array_of_interfaces) << T;
462 }
463
464 // C99 6.7.5.2p1: The size expression shall have integer type.
465 if (ArraySize && !ArraySize->isTypeDependent() &&
466 !ArraySize->getType()->isIntegerType()) {
467 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
468 << ArraySize->getType() << ArraySize->getSourceRange();
469 ArraySize->Destroy(Context);
470 return QualType();
471 }
472 llvm::APSInt ConstVal(32);
473 if (!ArraySize) {
474 T = Context.getIncompleteArrayType(T, ASM, Quals);
475 } else if (ArraySize->isValueDependent()) {
476 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
477 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
478 (!T->isDependentType() && !T->isConstantSizeType())) {
479 // Per C99, a variable array is an array with either a non-constant
480 // size or an element type that has a non-constant-size
481 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
482 } else {
483 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
484 // have a value greater than zero.
485 if (ConstVal.isSigned()) {
486 if (ConstVal.isNegative()) {
487 Diag(ArraySize->getLocStart(),
488 diag::err_typecheck_negative_array_size)
489 << ArraySize->getSourceRange();
490 return QualType();
491 } else if (ConstVal == 0) {
492 // GCC accepts zero sized static arrays.
493 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
494 << ArraySize->getSourceRange();
495 }
496 }
497 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
498 }
499 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
500 if (!getLangOptions().C99) {
501 if (ArraySize && !ArraySize->isTypeDependent() &&
502 !ArraySize->isValueDependent() &&
503 !ArraySize->isIntegerConstantExpr(Context))
504 Diag(Loc, diag::ext_vla);
505 else if (ASM != ArrayType::Normal || Quals != 0)
506 Diag(Loc, diag::ext_c99_array_usage);
507 }
508
509 return T;
510}
511
Douglas Gregor724651c2009-02-28 01:04:19 +0000512/// \brief Build a function type.
513///
514/// This routine checks the function type according to C++ rules and
515/// under the assumption that the result type and parameter types have
516/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000517/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000518/// simpler form that is only suitable for this narrow use case.
519///
520/// \param T The return type of the function.
521///
522/// \param ParamTypes The parameter types of the function. This array
523/// will be modified to account for adjustments to the types of the
524/// function parameters.
525///
526/// \param NumParamTypes The number of parameter types in ParamTypes.
527///
528/// \param Variadic Whether this is a variadic function type.
529///
530/// \param Quals The cvr-qualifiers to be applied to the function type.
531///
532/// \param Loc The location of the entity whose type involves this
533/// function type or, if there is no such entity, the location of the
534/// type that will have function type.
535///
536/// \param Entity The name of the entity that involves the function
537/// type, if known.
538///
539/// \returns A suitable function type, if there are no
540/// errors. Otherwise, returns a NULL type.
541QualType Sema::BuildFunctionType(QualType T,
542 QualType *ParamTypes,
543 unsigned NumParamTypes,
544 bool Variadic, unsigned Quals,
545 SourceLocation Loc, DeclarationName Entity) {
546 if (T->isArrayType() || T->isFunctionType()) {
547 Diag(Loc, diag::err_func_returning_array_function) << T;
548 return QualType();
549 }
550
551 bool Invalid = false;
552 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000553 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
554 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000555 Diag(Loc, diag::err_param_with_void_type);
556 Invalid = true;
557 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000558
Douglas Gregor724651c2009-02-28 01:04:19 +0000559 ParamTypes[Idx] = ParamType;
560 }
561
562 if (Invalid)
563 return QualType();
564
565 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
566 Quals);
567}
568
Mike Stump98eb8a72009-02-04 22:31:32 +0000569/// GetTypeForDeclarator - Convert the type for the specified
570/// declarator to Type instances. Skip the outermost Skip type
571/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000572QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000573 bool OmittedReturnType = false;
574
575 if (D.getContext() == Declarator::BlockLiteralContext
576 && Skip == 0
577 && !D.getDeclSpec().hasTypeSpecifier()
578 && (D.getNumTypeObjects() == 0
579 || (D.getNumTypeObjects() == 1
580 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
581 OmittedReturnType = true;
582
Chris Lattnerb23deda2007-08-28 16:40:32 +0000583 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000584 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000585 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
586 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000587
588 // Determine the type of the declarator. Not all forms of declarator
589 // have a type.
590 QualType T;
591 switch (D.getKind()) {
592 case Declarator::DK_Abstract:
593 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000594 case Declarator::DK_Operator: {
595 const DeclSpec& DS = D.getDeclSpec();
596 if (OmittedReturnType)
597 // We default to a dependent type initially. Can be modified by
598 // the first return statement.
599 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000600 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000601 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000602 if (T.isNull())
603 return T;
604 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000605 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000606 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000607
608 case Declarator::DK_Constructor:
609 case Declarator::DK_Destructor:
610 case Declarator::DK_Conversion:
611 // Constructors and destructors don't have return types. Use
612 // "void" instead. Conversion operators will check their return
613 // types separately.
614 T = Context.VoidTy;
615 break;
616 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000617
Douglas Gregorcd281c32009-02-28 00:25:32 +0000618 // The name we're declaring, if any.
619 DeclarationName Name;
620 if (D.getIdentifier())
621 Name = D.getIdentifier();
622
Mike Stump98eb8a72009-02-04 22:31:32 +0000623 // Walk the DeclTypeInfo, building the recursive type as we go.
624 // DeclTypeInfos are ordered from the identifier out, which is
625 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000626 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
627 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 switch (DeclType.Kind) {
629 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000630 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000631 // If blocks are disabled, emit an error.
632 if (!LangOpts.Blocks)
633 Diag(DeclType.Loc, diag::err_blocks_disable);
634
Steve Naroff5618bd42008-08-27 16:04:49 +0000635 if (DeclType.Cls.TypeQuals)
636 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
637 if (!T.getTypePtr()->isFunctionType())
638 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
639 else
640 T = Context.getBlockPointerType(T);
641 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 case DeclaratorChunk::Pointer:
Douglas Gregorcd281c32009-02-28 00:25:32 +0000643 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000645 case DeclaratorChunk::Reference:
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000646 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
647 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000648 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 break;
650 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000651 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000652 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 ArrayType::ArraySizeModifier ASM;
654 if (ATI.isStar)
655 ASM = ArrayType::Star;
656 else if (ATI.hasStatic)
657 ASM = ArrayType::Static;
658 else
659 ASM = ArrayType::Normal;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000660 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 break;
662 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000663 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 // If the function declarator has a prototype (i.e. it is not () and
665 // does not have a K&R-style identifier list), then the arguments are part
666 // of the type, otherwise the argument list is ().
667 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000668
Chris Lattnercd881292007-12-19 05:31:29 +0000669 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000670 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000671 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000672 T = Context.IntTy;
673 D.setInvalidType(true);
674 }
675
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000676 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000677 if (getLangOptions().CPlusPlus) {
678 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
679 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000680 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000681 } else if (FTI.isVariadic) {
682 // We allow a zero-parameter variadic function in C if the
683 // function is marked with the "overloadable"
684 // attribute. Scan for this attribute now.
685 bool Overloadable = false;
686 for (const AttributeList *Attrs = D.getAttributes();
687 Attrs; Attrs = Attrs->getNext()) {
688 if (Attrs->getKind() == AttributeList::AT_overloadable) {
689 Overloadable = true;
690 break;
691 }
692 }
693
694 if (!Overloadable)
695 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
696 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000697 } else {
698 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000699 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000700 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000701 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000703 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 } else {
705 // Otherwise, we have a function with an argument list that is
706 // potentially variadic.
707 llvm::SmallVector<QualType, 16> ArgTys;
708
709 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000710 ParmVarDecl *Param =
711 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +0000712 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000713 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000714
715 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000716 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 // Look for 'void'. void is allowed only as a single argument to a
719 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000720 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000721 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000722 // If this is something like 'float(int, void)', reject it. 'void'
723 // is an incomplete type (C99 6.2.5p19) and function decls cannot
724 // have arguments of incomplete type.
725 if (FTI.NumArgs != 1 || FTI.isVariadic) {
726 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000727 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000728 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000729 } else if (FTI.ArgInfo[i].Ident) {
730 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000732 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000733 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000734 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000735 } else {
736 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000737 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000738 Diag(DeclType.Loc, diag::err_void_param_qualified);
739
740 // Do not add 'void' to the ArgTys list.
741 break;
742 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000743 } else if (!FTI.hasPrototype) {
744 if (ArgTy->isPromotableIntegerType()) {
745 ArgTy = Context.IntTy;
746 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
747 if (BTy->getKind() == BuiltinType::Float)
748 ArgTy = Context.DoubleTy;
749 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 }
751
752 ArgTys.push_back(ArgTy);
753 }
754 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000755 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 }
757 break;
758 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000759 case DeclaratorChunk::MemberPointer:
760 // The scope spec must refer to a class, or be dependent.
Douglas Gregore4e5b052009-03-19 00:18:19 +0000761 DeclContext *DC = computeDeclContext(DeclType.Mem.Scope());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000762 QualType ClsType;
763 // FIXME: Extend for dependent types when it's actually supported.
764 // See ActOnCXXNestedNameSpecifier.
765 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
766 ClsType = Context.getTagDeclType(RD);
767 } else {
768 if (DC) {
769 Diag(DeclType.Mem.Scope().getBeginLoc(),
770 diag::err_illegal_decl_mempointer_in_nonclass)
771 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
772 << DeclType.Mem.Scope().getRange();
773 }
774 D.setInvalidType(true);
775 ClsType = Context.IntTy;
776 }
777
778 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
779 // with reference type, or "cv void."
780 if (T->isReferenceType()) {
781 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
782 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
783 D.setInvalidType(true);
784 T = Context.IntTy;
785 }
786 if (T->isVoidType()) {
787 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
788 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
789 T = Context.IntTy;
790 }
791
792 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
793 // object or incomplete types shall not be restrict-qualified."
794 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
795 !T->isIncompleteOrObjectType()) {
796 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
797 << T;
798 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
799 }
800
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000801 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
802 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000803
804 break;
805 }
806
Douglas Gregorcd281c32009-02-28 00:25:32 +0000807 if (T.isNull()) {
808 D.setInvalidType(true);
809 T = Context.IntTy;
810 }
811
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000812 // See if there are any attributes on this declarator chunk.
813 if (const AttributeList *AL = DeclType.getAttrs())
814 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000816
817 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000818 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
819 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000820
821 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
822 // for a nonstatic member function, the function type to which a pointer
823 // to member refers, or the top-level function type of a function typedef
824 // declaration.
825 if (FnTy->getTypeQuals() != 0 &&
826 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000827 ((D.getContext() != Declarator::MemberContext &&
828 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +0000829 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000830 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000831 if (D.isFunctionDeclarator())
832 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
833 else
834 Diag(D.getIdentifierLoc(),
835 diag::err_invalid_qualified_typedef_function_type_use);
836
837 // Strip the cv-quals from the type.
838 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000839 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000840 }
841 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000842
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000843 // If there were any type attributes applied to the decl itself (not the
844 // type, apply the type attribute to the type!)
845 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000846 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000847
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 return T;
849}
850
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000852/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +0000853QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
854 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000855 QualType T = MDecl->getResultType();
856 llvm::SmallVector<QualType, 16> ArgTys;
857
Fariborz Jahanian35600022007-11-09 17:18:29 +0000858 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000859 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000860 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000861 selfTy = Context.getPointerType(selfTy);
862 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000863 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000864 ArgTys.push_back(Context.getObjCIdType());
865 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000866
Chris Lattner89951a82009-02-20 18:43:26 +0000867 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
868 E = MDecl->param_end(); PI != E; ++PI) {
869 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000870 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000871 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000872 ArgTys.push_back(ArgTy);
873 }
874 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000875 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000876 return T;
877}
878
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000879/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
880/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
881/// they point to and return true. If T1 and T2 aren't pointer types
882/// or pointer-to-member types, or if they are not similar at this
883/// level, returns false and leaves T1 and T2 unchanged. Top-level
884/// qualifiers on T1 and T2 are ignored. This function will typically
885/// be called in a loop that successively "unwraps" pointer and
886/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000887bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000888 const PointerType *T1PtrType = T1->getAsPointerType(),
889 *T2PtrType = T2->getAsPointerType();
890 if (T1PtrType && T2PtrType) {
891 T1 = T1PtrType->getPointeeType();
892 T2 = T2PtrType->getPointeeType();
893 return true;
894 }
895
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000896 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
897 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000898 if (T1MPType && T2MPType &&
899 Context.getCanonicalType(T1MPType->getClass()) ==
900 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000901 T1 = T1MPType->getPointeeType();
902 T2 = T2MPType->getPointeeType();
903 return true;
904 }
Douglas Gregor57373262008-10-22 14:17:15 +0000905 return false;
906}
907
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000908Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 // C99 6.7.6: Type names have no identifier. This is already validated by
910 // the parser.
911 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
912
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000913 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000914 if (T.isNull())
915 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000916
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000917 // Check that there are no default arguments (C++ only).
918 if (getLangOptions().CPlusPlus)
919 CheckExtraCXXDefaultArguments(D);
920
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 return T.getAsOpaquePtr();
922}
923
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000924
925
926//===----------------------------------------------------------------------===//
927// Type Attribute Processing
928//===----------------------------------------------------------------------===//
929
930/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
931/// specified type. The attribute contains 1 argument, the id of the address
932/// space for the type.
933static void HandleAddressSpaceTypeAttribute(QualType &Type,
934 const AttributeList &Attr, Sema &S){
935 // If this type is already address space qualified, reject it.
936 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
937 // for two or more different address spaces."
938 if (Type.getAddressSpace()) {
939 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
940 return;
941 }
942
943 // Check the attribute arguments.
944 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000945 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000946 return;
947 }
948 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
949 llvm::APSInt addrSpace(32);
950 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000951 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
952 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000953 return;
954 }
955
956 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000957 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000958}
959
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000960/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
961/// specified type. The attribute contains 1 argument, weak or strong.
962static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000963 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000964 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000965 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000966 return;
967 }
968
969 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000970 if (!Attr.getParameterName()) {
971 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
972 << "objc_gc" << 1;
973 return;
974 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000975 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000976 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000977 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
978 return;
979 }
980 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000981 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000982 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000983 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000984 else {
985 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
986 << "objc_gc" << Attr.getParameterName();
987 return;
988 }
989
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000990 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000991}
992
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000993void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000994 // Scan through and apply attributes to this type where it makes sense. Some
995 // attributes (such as __address_space__, __vector_size__, etc) apply to the
996 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000997 // apply to the decl. Here we apply type attributes and ignore the rest.
998 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000999 // If this is an attribute we can handle, do so now, otherwise, add it to
1000 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001001 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001002 default: break;
1003 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001004 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1005 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001006 case AttributeList::AT_objc_gc:
1007 HandleObjCGCTypeAttribute(Result, *AL, *this);
1008 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001009 }
Chris Lattner232e8822008-02-21 01:08:11 +00001010 }
Chris Lattner232e8822008-02-21 01:08:11 +00001011}
1012
Douglas Gregor86447ec2009-03-09 16:13:40 +00001013/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001014///
1015/// This routine checks whether the type @p T is complete in any
1016/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001017/// type, returns false. If @p T is a class template specialization,
1018/// this routine then attempts to perform class template
1019/// instantiation. If instantiation fails, or if @p T is incomplete
1020/// and cannot be completed, issues the diagnostic @p diag (giving it
1021/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001022///
1023/// @param Loc The location in the source that the incomplete type
1024/// diagnostic should refer to.
1025///
1026/// @param T The type that this routine is examining for completeness.
1027///
1028/// @param diag The diagnostic value (e.g.,
1029/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1030/// for the error message if @p T is incomplete.
1031///
1032/// @param Range1 An optional range in the source code that will be a
1033/// part of the "incomplete type" error message.
1034///
1035/// @param Range2 An optional range in the source code that will be a
1036/// part of the "incomplete type" error message.
1037///
1038/// @param PrintType If non-NULL, the type that should be printed
1039/// instead of @p T. This parameter should be used when the type that
1040/// we're checking for incompleteness isn't the type that should be
1041/// displayed to the user, e.g., when T is a type and PrintType is a
1042/// pointer to T.
1043///
1044/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1045/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001046bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001047 SourceRange Range1, SourceRange Range2,
1048 QualType PrintType) {
1049 // If we have a complete type, we're done.
1050 if (!T->isIncompleteType())
1051 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001052
Douglas Gregord475b8d2009-03-25 21:17:03 +00001053 // If we have a class template specialization or a class member of a
1054 // class template specialization, try to instantiate it.
1055 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001056 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001057 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001058 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1059 // Update the class template specialization's location to
1060 // refer to the point of instantiation.
1061 if (Loc.isValid())
1062 ClassTemplateSpec->setLocation(Loc);
1063 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1064 /*ExplicitInstantiation=*/false);
1065 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001066 } else if (CXXRecordDecl *Rec
1067 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1068 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1069 // Find the class template specialization that surrounds this
1070 // member class.
1071 ClassTemplateSpecializationDecl *Spec = 0;
1072 for (DeclContext *Parent = Rec->getDeclContext();
1073 Parent && !Spec; Parent = Parent->getParent())
1074 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1075 assert(Spec && "Not a member of a class template specialization?");
1076 return InstantiateClass(Loc, Rec, Pattern,
1077 Spec->getTemplateArgs(),
1078 Spec->getNumTemplateArgs());
1079 }
1080 }
1081 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001082
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001083 if (PrintType.isNull())
1084 PrintType = T;
1085
1086 // We have an incomplete type. Produce a diagnostic.
1087 Diag(Loc, diag) << PrintType << Range1 << Range2;
1088
1089 // If the type was a forward declaration of a class/struct/union
1090 // type, produce
1091 const TagType *Tag = 0;
1092 if (const RecordType *Record = T->getAsRecordType())
1093 Tag = Record;
1094 else if (const EnumType *Enum = T->getAsEnumType())
1095 Tag = Enum;
1096
1097 if (Tag && !Tag->getDecl()->isInvalidDecl())
1098 Diag(Tag->getDecl()->getLocation(),
1099 Tag->isBeingDefined() ? diag::note_type_being_defined
1100 : diag::note_forward_declaration)
1101 << QualType(Tag, 0);
1102
1103 return true;
1104}
Douglas Gregore6258932009-03-19 00:39:20 +00001105
1106/// \brief Retrieve a version of the type 'T' that is qualified by the
1107/// nested-name-specifier contained in SS.
1108QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1109 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1110 return T;
1111
Douglas Gregorab452ba2009-03-26 23:50:42 +00001112 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001113 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001114 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001115}