blob: 01b8aa4fd41c0002502d566a769803e31ca2b216 [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
Chris Lattner3f84ad22009-04-22 05:27:59 +000049/// \param DeclLoc The location of the declarator identifier or invalid if none.
Chris Lattner5153ee62009-04-25 08:47:54 +000050/// \returns The type described by the declaration specifiers. This function
51/// never returns null.
Chris Lattner3f84ad22009-04-22 05:27:59 +000052QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
Chris Lattnereaaebc72009-04-25 08:06:05 +000053 SourceLocation DeclLoc,
54 bool &isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
56 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000057 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000058
59 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000060 case DeclSpec::TST_void:
61 Result = Context.VoidTy;
62 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 case DeclSpec::TST_char:
64 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000065 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000066 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000067 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 else {
69 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
70 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000071 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Chris Lattner958858e2008-02-20 21:40:32 +000073 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000074 case DeclSpec::TST_wchar:
75 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
76 Result = Context.WCharTy;
77 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000078 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
79 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000080 Result = Context.getSignedWCharType();
81 } else {
82 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
83 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000084 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
85 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000086 Result = Context.getUnsignedWCharType();
87 }
88 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000089 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000090 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000091 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000092 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000093 DS.getNumProtocolQualifiers());
94 break;
95 }
96
Chris Lattnerd658b562008-04-05 06:32:51 +000097 // Unspecified typespec defaults to int in C90. However, the C90 grammar
98 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
99 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
100 // Note that the one exception to this is function definitions, which are
101 // allowed to be completely missing a declspec. This is handled in the
102 // parser already though by it pretending to have seen an 'int' in this
103 // case.
104 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000105 // In C89 mode, we only warn if there is a completely missing declspec
106 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000107 if (DS.isEmpty()) {
108 if (DeclLoc.isInvalid())
109 DeclLoc = DS.getSourceRange().getBegin();
110 Diag(DeclLoc, diag::warn_missing_declspec)
111 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000112 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
113 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000114 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000115 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000116 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
117 // "At least one type specifier shall be given in the declaration
118 // specifiers in each declaration, and in the specifier-qualifier list in
119 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000120 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000121 if (DeclLoc.isInvalid())
122 DeclLoc = DS.getSourceRange().getBegin();
123
124 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft)
125 Diag(DeclLoc, diag::err_missing_type_specifier)
126 << DS.getSourceRange();
127 else
128 Diag(DeclLoc, diag::warn_missing_type_specifier)
129 << DS.getSourceRange();
Douglas Gregord17a9e52009-03-27 05:10:56 +0000130
Mike Stump390b4cc2009-05-16 07:39:55 +0000131 // FIXME: If we could guarantee that the result would be well-formed, it
132 // would be useful to have a code insertion hint here. However, after
133 // emitting this warning/error, we often emit other errors.
Chris Lattnerd658b562008-04-05 06:32:51 +0000134 }
135
136 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000137 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
139 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000140 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
141 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
142 case DeclSpec::TSW_long: Result = Context.LongTy; break;
143 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 }
145 } else {
146 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000147 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
148 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
149 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
150 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 }
152 }
Chris Lattner958858e2008-02-20 21:40:32 +0000153 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000154 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000155 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000156 case DeclSpec::TST_double:
157 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000158 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000159 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000160 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000161 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000162 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 case DeclSpec::TST_decimal32: // _Decimal32
164 case DeclSpec::TST_decimal64: // _Decimal64
165 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000166 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
167 Result = Context.IntTy;
168 isInvalid = true;
169 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000170 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 case DeclSpec::TST_enum:
172 case DeclSpec::TST_union:
173 case DeclSpec::TST_struct: {
174 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000175 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
177 DS.getTypeSpecSign() == 0 &&
178 "Can't handle qualifiers on typedef names yet!");
179 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000180 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner5153ee62009-04-25 08:47:54 +0000181
182 if (D->isInvalidDecl())
183 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000184 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000186 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
188 DS.getTypeSpecSign() == 0 &&
189 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000190 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000191
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000192 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump390b4cc2009-05-16 07:39:55 +0000193 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have
194 // this "hack" for now...
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000195 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
196 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
197 (ObjCProtocolDecl**)PQ,
198 DS.getNumProtocolQualifiers());
199 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000200 // id<protocol-list>
201 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
202 DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000203 else if (Result == Context.getObjCClassType()) {
204 if (DeclLoc.isInvalid())
205 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000206 // Class<protocol-list>
Chris Lattner3f84ad22009-04-22 05:27:59 +0000207 Diag(DeclLoc, diag::err_qualified_class_unsupported)
208 << DS.getSourceRange();
209 } else {
210 if (DeclLoc.isInvalid())
211 DeclLoc = DS.getSourceRange().getBegin();
212 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
213 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000214 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000215 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000216 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000217
218 // If this is a reference to an invalid typedef, propagate the invalidity.
219 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
220 if (TDT->getDecl()->isInvalidDecl())
221 isInvalid = true;
222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000224 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 }
Chris Lattner958858e2008-02-20 21:40:32 +0000226 case DeclSpec::TST_typeofType:
227 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
228 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000229 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000230 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000231 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000232 case DeclSpec::TST_typeofExpr: {
233 Expr *E = static_cast<Expr *>(DS.getTypeRep());
234 assert(E && "Didn't get an expression for typeof?");
235 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000236 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000237 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000238 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000239 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000240 Result = Context.IntTy;
241 isInvalid = true;
242 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
Chris Lattner958858e2008-02-20 21:40:32 +0000244
245 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000246 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
247 if (getLangOptions().Freestanding)
248 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000249 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000250 }
Chris Lattner958858e2008-02-20 21:40:32 +0000251
252 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
253 "FIXME: imaginary types not supported yet!");
254
Chris Lattner38d8b982008-02-20 22:04:11 +0000255 // See if there are any attributes on the declspec that apply to the type (as
256 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000257 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000258 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000259
Chris Lattner96b77fc2008-04-02 06:50:17 +0000260 // Apply const/volatile/restrict qualifiers to T.
261 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
262
263 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
264 // or incomplete types shall not be restrict-qualified." C++ also allows
265 // restrict-qualified references.
266 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000267 if (Result->isPointerType() || Result->isReferenceType()) {
268 QualType EltTy = Result->isPointerType() ?
269 Result->getAsPointerType()->getPointeeType() :
270 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000271
Douglas Gregorbad0e652009-03-24 20:32:41 +0000272 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000273 // incomplete type.
274 if (!EltTy->isIncompleteOrObjectType()) {
275 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000276 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000277 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000278 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
279 }
280 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000281 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000282 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000283 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000284 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000285 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000286 }
287
288 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
289 // of a function type includes any type qualifiers, the behavior is
290 // undefined."
291 if (Result->isFunctionType() && TypeQuals) {
292 // Get some location to point at, either the C or V location.
293 SourceLocation Loc;
294 if (TypeQuals & QualType::Const)
295 Loc = DS.getConstSpecLoc();
296 else {
297 assert((TypeQuals & QualType::Volatile) &&
298 "Has CV quals but not C or V?");
299 Loc = DS.getVolatileSpecLoc();
300 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000301 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000302 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000303 }
304
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000305 // C++ [dcl.ref]p1:
306 // Cv-qualified references are ill-formed except when the
307 // cv-qualifiers are introduced through the use of a typedef
308 // (7.1.3) or of a template type argument (14.3), in which
309 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000310 // FIXME: Shouldn't we be checking SCS_typedef here?
311 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000312 TypeQuals && Result->isReferenceType()) {
313 TypeQuals &= ~QualType::Const;
314 TypeQuals &= ~QualType::Volatile;
315 }
316
Chris Lattner96b77fc2008-04-02 06:50:17 +0000317 Result = Result.getQualifiedType(TypeQuals);
318 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000319 return Result;
320}
321
Douglas Gregorcd281c32009-02-28 00:25:32 +0000322static std::string getPrintableNameForEntity(DeclarationName Entity) {
323 if (Entity)
324 return Entity.getAsString();
325
326 return "type name";
327}
328
329/// \brief Build a pointer type.
330///
331/// \param T The type to which we'll be building a pointer.
332///
333/// \param Quals The cvr-qualifiers to be applied to the pointer type.
334///
335/// \param Loc The location of the entity whose type involves this
336/// pointer type or, if there is no such entity, the location of the
337/// type that will have pointer type.
338///
339/// \param Entity The name of the entity that involves the pointer
340/// type, if known.
341///
342/// \returns A suitable pointer type, if there are no
343/// errors. Otherwise, returns a NULL type.
344QualType Sema::BuildPointerType(QualType T, unsigned Quals,
345 SourceLocation Loc, DeclarationName Entity) {
346 if (T->isReferenceType()) {
347 // C++ 8.3.2p4: There shall be no ... pointers to references ...
348 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
349 << getPrintableNameForEntity(Entity);
350 return QualType();
351 }
352
353 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
354 // object or incomplete types shall not be restrict-qualified."
355 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
356 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
357 << T;
358 Quals &= ~QualType::Restrict;
359 }
360
361 // Build the pointer type.
362 return Context.getPointerType(T).getQualifiedType(Quals);
363}
364
365/// \brief Build a reference type.
366///
367/// \param T The type to which we'll be building a reference.
368///
369/// \param Quals The cvr-qualifiers to be applied to the reference type.
370///
371/// \param Loc The location of the entity whose type involves this
372/// reference type or, if there is no such entity, the location of the
373/// type that will have reference type.
374///
375/// \param Entity The name of the entity that involves the reference
376/// type, if known.
377///
378/// \returns A suitable reference type, if there are no
379/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000380QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000381 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000382 if (LValueRef) {
383 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000384 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
385 // reference to a type T, and attempt to create the type "lvalue
386 // reference to cv TD" creates the type "lvalue reference to T".
387 // We use the qualifiers (restrict or none) of the original reference,
388 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000389 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000390 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000391 }
392 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000393 if (T->isReferenceType()) {
394 // C++ [dcl.ref]p4: There shall be no references to references.
395 //
396 // According to C++ DR 106, references to references are only
397 // diagnosed when they are written directly (e.g., "int & &"),
398 // but not when they happen via a typedef:
399 //
400 // typedef int& intref;
401 // typedef intref& intref2;
402 //
403 // Parser::ParserDeclaratorInternal diagnoses the case where
404 // references are written directly; here, we handle the
405 // collapsing of references-to-references as described in C++
406 // DR 106 and amended by C++ DR 540.
407 return T;
408 }
409
410 // C++ [dcl.ref]p1:
411 // A declarator that specifies the type “reference to cv void”
412 // is ill-formed.
413 if (T->isVoidType()) {
414 Diag(Loc, diag::err_reference_to_void);
415 return QualType();
416 }
417
418 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
419 // object or incomplete types shall not be restrict-qualified."
420 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
421 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
422 << T;
423 Quals &= ~QualType::Restrict;
424 }
425
426 // C++ [dcl.ref]p1:
427 // [...] Cv-qualified references are ill-formed except when the
428 // cv-qualifiers are introduced through the use of a typedef
429 // (7.1.3) or of a template type argument (14.3), in which case
430 // the cv-qualifiers are ignored.
431 //
432 // We diagnose extraneous cv-qualifiers for the non-typedef,
433 // non-template type argument case within the parser. Here, we just
434 // ignore any extraneous cv-qualifiers.
435 Quals &= ~QualType::Const;
436 Quals &= ~QualType::Volatile;
437
438 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000439 if (LValueRef)
440 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
441 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000442}
443
444/// \brief Build an array type.
445///
446/// \param T The type of each element in the array.
447///
448/// \param ASM C99 array size modifier (e.g., '*', 'static').
449///
450/// \param ArraySize Expression describing the size of the array.
451///
452/// \param Quals The cvr-qualifiers to be applied to the array's
453/// element type.
454///
455/// \param Loc The location of the entity whose type involves this
456/// array type or, if there is no such entity, the location of the
457/// type that will have array type.
458///
459/// \param Entity The name of the entity that involves the array
460/// type, if known.
461///
462/// \returns A suitable array type, if there are no errors. Otherwise,
463/// returns a NULL type.
464QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
465 Expr *ArraySize, unsigned Quals,
466 SourceLocation Loc, DeclarationName Entity) {
467 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
468 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000469 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000470 diag::err_illegal_decl_array_incomplete_type))
471 return QualType();
472
473 if (T->isFunctionType()) {
474 Diag(Loc, diag::err_illegal_decl_array_of_functions)
475 << getPrintableNameForEntity(Entity);
476 return QualType();
477 }
478
479 // C++ 8.3.2p4: There shall be no ... arrays of references ...
480 if (T->isReferenceType()) {
481 Diag(Loc, diag::err_illegal_decl_array_of_references)
482 << getPrintableNameForEntity(Entity);
483 return QualType();
484 }
485
486 if (const RecordType *EltTy = T->getAsRecordType()) {
487 // If the element type is a struct or union that contains a variadic
488 // array, accept it as a GNU extension: C99 6.7.2.1p2.
489 if (EltTy->getDecl()->hasFlexibleArrayMember())
490 Diag(Loc, diag::ext_flexible_array_in_array) << T;
491 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000492 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
493 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000494 }
495
496 // C99 6.7.5.2p1: The size expression shall have integer type.
497 if (ArraySize && !ArraySize->isTypeDependent() &&
498 !ArraySize->getType()->isIntegerType()) {
499 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
500 << ArraySize->getType() << ArraySize->getSourceRange();
501 ArraySize->Destroy(Context);
502 return QualType();
503 }
504 llvm::APSInt ConstVal(32);
505 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000506 if (ASM == ArrayType::Star)
507 T = Context.getVariableArrayType(T, 0, ASM, Quals);
508 else
509 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000510 } else if (ArraySize->isValueDependent()) {
511 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals);
512 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
513 (!T->isDependentType() && !T->isConstantSizeType())) {
514 // Per C99, a variable array is an array with either a non-constant
515 // size or an element type that has a non-constant-size
516 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals);
517 } else {
518 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
519 // have a value greater than zero.
520 if (ConstVal.isSigned()) {
521 if (ConstVal.isNegative()) {
522 Diag(ArraySize->getLocStart(),
523 diag::err_typecheck_negative_array_size)
524 << ArraySize->getSourceRange();
525 return QualType();
526 } else if (ConstVal == 0) {
527 // GCC accepts zero sized static arrays.
528 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
529 << ArraySize->getSourceRange();
530 }
531 }
532 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
533 }
534 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
535 if (!getLangOptions().C99) {
536 if (ArraySize && !ArraySize->isTypeDependent() &&
537 !ArraySize->isValueDependent() &&
538 !ArraySize->isIntegerConstantExpr(Context))
539 Diag(Loc, diag::ext_vla);
540 else if (ASM != ArrayType::Normal || Quals != 0)
541 Diag(Loc, diag::ext_c99_array_usage);
542 }
543
544 return T;
545}
546
Douglas Gregor724651c2009-02-28 01:04:19 +0000547/// \brief Build a function type.
548///
549/// This routine checks the function type according to C++ rules and
550/// under the assumption that the result type and parameter types have
551/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000552/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000553/// simpler form that is only suitable for this narrow use case.
554///
555/// \param T The return type of the function.
556///
557/// \param ParamTypes The parameter types of the function. This array
558/// will be modified to account for adjustments to the types of the
559/// function parameters.
560///
561/// \param NumParamTypes The number of parameter types in ParamTypes.
562///
563/// \param Variadic Whether this is a variadic function type.
564///
565/// \param Quals The cvr-qualifiers to be applied to the function type.
566///
567/// \param Loc The location of the entity whose type involves this
568/// function type or, if there is no such entity, the location of the
569/// type that will have function type.
570///
571/// \param Entity The name of the entity that involves the function
572/// type, if known.
573///
574/// \returns A suitable function type, if there are no
575/// errors. Otherwise, returns a NULL type.
576QualType Sema::BuildFunctionType(QualType T,
577 QualType *ParamTypes,
578 unsigned NumParamTypes,
579 bool Variadic, unsigned Quals,
580 SourceLocation Loc, DeclarationName Entity) {
581 if (T->isArrayType() || T->isFunctionType()) {
582 Diag(Loc, diag::err_func_returning_array_function) << T;
583 return QualType();
584 }
585
586 bool Invalid = false;
587 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000588 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
589 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000590 Diag(Loc, diag::err_param_with_void_type);
591 Invalid = true;
592 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000593
Douglas Gregor724651c2009-02-28 01:04:19 +0000594 ParamTypes[Idx] = ParamType;
595 }
596
597 if (Invalid)
598 return QualType();
599
600 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
601 Quals);
602}
603
Mike Stump98eb8a72009-02-04 22:31:32 +0000604/// GetTypeForDeclarator - Convert the type for the specified
605/// declarator to Type instances. Skip the outermost Skip type
606/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000607///
608/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
609/// owns the declaration of a type (e.g., the definition of a struct
610/// type), then *OwnedDecl will receive the owned declaration.
611QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip,
612 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000613 bool OmittedReturnType = false;
614
615 if (D.getContext() == Declarator::BlockLiteralContext
616 && Skip == 0
617 && !D.getDeclSpec().hasTypeSpecifier()
618 && (D.getNumTypeObjects() == 0
619 || (D.getNumTypeObjects() == 1
620 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
621 OmittedReturnType = true;
622
Chris Lattnerb23deda2007-08-28 16:40:32 +0000623 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000624 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000625 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
626 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000627
628 // Determine the type of the declarator. Not all forms of declarator
629 // have a type.
630 QualType T;
631 switch (D.getKind()) {
632 case Declarator::DK_Abstract:
633 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000634 case Declarator::DK_Operator: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000635 const DeclSpec &DS = D.getDeclSpec();
636 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000637 // We default to a dependent type initially. Can be modified by
638 // the first return statement.
639 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000640 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000641 bool isInvalid = false;
642 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000643 if (isInvalid)
644 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000645 else if (OwnedDecl && DS.isTypeSpecOwned())
646 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000647 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000648 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000649 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000650
651 case Declarator::DK_Constructor:
652 case Declarator::DK_Destructor:
653 case Declarator::DK_Conversion:
654 // Constructors and destructors don't have return types. Use
655 // "void" instead. Conversion operators will check their return
656 // types separately.
657 T = Context.VoidTy;
658 break;
659 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000660
Douglas Gregorcd281c32009-02-28 00:25:32 +0000661 // The name we're declaring, if any.
662 DeclarationName Name;
663 if (D.getIdentifier())
664 Name = D.getIdentifier();
665
Mike Stump98eb8a72009-02-04 22:31:32 +0000666 // Walk the DeclTypeInfo, building the recursive type as we go.
667 // DeclTypeInfos are ordered from the identifier out, which is
668 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000669 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
670 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 switch (DeclType.Kind) {
672 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000673 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000674 // If blocks are disabled, emit an error.
675 if (!LangOpts.Blocks)
676 Diag(DeclType.Loc, diag::err_blocks_disable);
677
Steve Naroff5618bd42008-08-27 16:04:49 +0000678 if (!T.getTypePtr()->isFunctionType())
679 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
680 else
Mike Stump7bc8d962009-05-07 21:56:17 +0000681 T = (Context.getBlockPointerType(T)
682 .getQualifiedType(DeclType.Cls.TypeQuals));
Steve Naroff5618bd42008-08-27 16:04:49 +0000683 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000685 // Verify that we're not building a pointer to pointer to function with
686 // exception specification.
687 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
688 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
689 D.setInvalidType(true);
690 // Build the type anyway.
691 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000692 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000694 case DeclaratorChunk::Reference:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000695 // Verify that we're not building a reference to pointer to function with
696 // exception specification.
697 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
698 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
699 D.setInvalidType(true);
700 // Build the type anyway.
701 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000702 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
703 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000704 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 break;
706 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000707 // Verify that we're not building an array of pointers to function with
708 // exception specification.
709 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
710 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
711 D.setInvalidType(true);
712 // Build the type anyway.
713 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000714 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000715 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 ArrayType::ArraySizeModifier ASM;
717 if (ATI.isStar)
718 ASM = ArrayType::Star;
719 else if (ATI.hasStatic)
720 ASM = ArrayType::Static;
721 else
722 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000723 if (ASM == ArrayType::Star &&
724 D.getContext() != Declarator::PrototypeContext) {
725 // FIXME: This check isn't quite right: it allows star in prototypes
726 // for function definitions, and disallows some edge cases detailed
727 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
728 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
729 ASM = ArrayType::Normal;
730 D.setInvalidType(true);
731 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000732 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 break;
734 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000735 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 // If the function declarator has a prototype (i.e. it is not () and
737 // does not have a K&R-style identifier list), then the arguments are part
738 // of the type, otherwise the argument list is ().
739 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000740
Chris Lattnercd881292007-12-19 05:31:29 +0000741 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000742 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000743 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000744 T = Context.IntTy;
745 D.setInvalidType(true);
746 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000747
Douglas Gregor402abb52009-05-28 23:31:59 +0000748 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
749 // C++ [dcl.fct]p6:
750 // Types shall not be defined in return or parameter types.
751 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
752 if (Tag->isDefinition())
753 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
754 << Context.getTypeDeclType(Tag);
755 }
756
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000757 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000758 if (getLangOptions().CPlusPlus) {
759 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
760 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +0000761 llvm::SmallVector<QualType, 4> Exceptions;
762 Exceptions.reserve(FTI.NumExceptions);
763 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei)
764 Exceptions.push_back(
765 QualType::getFromOpaquePtr(FTI.Exceptions[ei]));
766 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
767 FTI.hasExceptionSpec,
768 FTI.hasAnyExceptionSpec,
769 FTI.NumExceptions, Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +0000770 } else if (FTI.isVariadic) {
771 // We allow a zero-parameter variadic function in C if the
772 // function is marked with the "overloadable"
773 // attribute. Scan for this attribute now.
774 bool Overloadable = false;
775 for (const AttributeList *Attrs = D.getAttributes();
776 Attrs; Attrs = Attrs->getNext()) {
777 if (Attrs->getKind() == AttributeList::AT_overloadable) {
778 Overloadable = true;
779 break;
780 }
781 }
782
783 if (!Overloadable)
784 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
785 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000786 } else {
787 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000788 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000789 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000790 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000792 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 } else {
794 // Otherwise, we have a function with an argument list that is
795 // potentially variadic.
796 llvm::SmallVector<QualType, 16> ArgTys;
797
798 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000799 ParmVarDecl *Param =
800 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +0000801 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000802 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000803
804 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000805 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 // Look for 'void'. void is allowed only as a single argument to a
808 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000809 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000810 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 // If this is something like 'float(int, void)', reject it. 'void'
812 // is an incomplete type (C99 6.2.5p19) and function decls cannot
813 // have arguments of incomplete type.
814 if (FTI.NumArgs != 1 || FTI.isVariadic) {
815 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000816 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000817 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000818 } else if (FTI.ArgInfo[i].Ident) {
819 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000821 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000822 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000823 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000824 } else {
825 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000826 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000827 Diag(DeclType.Loc, diag::err_void_param_qualified);
828
829 // Do not add 'void' to the ArgTys list.
830 break;
831 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000832 } else if (!FTI.hasPrototype) {
833 if (ArgTy->isPromotableIntegerType()) {
834 ArgTy = Context.IntTy;
835 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
836 if (BTy->getKind() == BuiltinType::Float)
837 ArgTy = Context.DoubleTy;
838 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 }
840
841 ArgTys.push_back(ArgTy);
842 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000843
844 llvm::SmallVector<QualType, 4> Exceptions;
845 Exceptions.reserve(FTI.NumExceptions);
846 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei)
847 Exceptions.push_back(QualType::getFromOpaquePtr(FTI.Exceptions[ei]));
848
Jay Foadbeaaccd2009-05-21 09:52:38 +0000849 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +0000850 FTI.isVariadic, FTI.TypeQuals,
851 FTI.hasExceptionSpec,
852 FTI.hasAnyExceptionSpec,
853 FTI.NumExceptions, Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 }
855 break;
856 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000857 case DeclaratorChunk::MemberPointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000858 // Verify that we're not building a pointer to pointer to function with
859 // exception specification.
860 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
861 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
862 D.setInvalidType(true);
863 // Build the type anyway.
864 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000865 // The scope spec must refer to a class, or be dependent.
Douglas Gregore4e5b052009-03-19 00:18:19 +0000866 DeclContext *DC = computeDeclContext(DeclType.Mem.Scope());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000867 QualType ClsType;
868 // FIXME: Extend for dependent types when it's actually supported.
869 // See ActOnCXXNestedNameSpecifier.
870 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
871 ClsType = Context.getTagDeclType(RD);
872 } else {
873 if (DC) {
874 Diag(DeclType.Mem.Scope().getBeginLoc(),
875 diag::err_illegal_decl_mempointer_in_nonclass)
876 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
877 << DeclType.Mem.Scope().getRange();
878 }
879 D.setInvalidType(true);
880 ClsType = Context.IntTy;
881 }
882
883 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
884 // with reference type, or "cv void."
885 if (T->isReferenceType()) {
886 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
887 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
888 D.setInvalidType(true);
889 T = Context.IntTy;
890 }
891 if (T->isVoidType()) {
892 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
893 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
894 T = Context.IntTy;
895 }
896
897 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
898 // object or incomplete types shall not be restrict-qualified."
899 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
900 !T->isIncompleteOrObjectType()) {
901 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
902 << T;
903 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
904 }
905
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000906 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
907 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000908
909 break;
910 }
911
Douglas Gregorcd281c32009-02-28 00:25:32 +0000912 if (T.isNull()) {
913 D.setInvalidType(true);
914 T = Context.IntTy;
915 }
916
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000917 // See if there are any attributes on this declarator chunk.
918 if (const AttributeList *AL = DeclType.getAttrs())
919 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000921
922 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000923 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
924 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000925
926 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
927 // for a nonstatic member function, the function type to which a pointer
928 // to member refers, or the top-level function type of a function typedef
929 // declaration.
930 if (FnTy->getTypeQuals() != 0 &&
931 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000932 ((D.getContext() != Declarator::MemberContext &&
933 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +0000934 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000935 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000936 if (D.isFunctionDeclarator())
937 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
938 else
939 Diag(D.getIdentifierLoc(),
940 diag::err_invalid_qualified_typedef_function_type_use);
941
942 // Strip the cv-quals from the type.
943 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000944 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000945 }
946 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000947
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000948 // If there were any type attributes applied to the decl itself (not the
949 // type, apply the type attribute to the type!)
950 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000951 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000952
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 return T;
954}
955
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000956/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
957/// to member to a function with an exception specification. This means that
958/// it is invalid to add another level of indirection.
959bool Sema::CheckDistantExceptionSpec(QualType T) {
960 if (const PointerType *PT = T->getAsPointerType())
961 T = PT->getPointeeType();
962 else if (const MemberPointerType *PT = T->getAsMemberPointerType())
963 T = PT->getPointeeType();
964 else
965 return false;
966
967 const FunctionProtoType *FnT = T->getAsFunctionProtoType();
968 if (!FnT)
969 return false;
970
971 return FnT->hasExceptionSpec();
972}
973
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000975/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +0000976QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
977 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000978 QualType T = MDecl->getResultType();
979 llvm::SmallVector<QualType, 16> ArgTys;
980
Fariborz Jahanian35600022007-11-09 17:18:29 +0000981 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000982 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000984 selfTy = Context.getPointerType(selfTy);
985 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000986 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000987 ArgTys.push_back(Context.getObjCIdType());
988 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000989
Chris Lattner89951a82009-02-20 18:43:26 +0000990 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
991 E = MDecl->param_end(); PI != E; ++PI) {
992 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000993 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +0000994 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000995 ArgTys.push_back(ArgTy);
996 }
997 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000998 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000999 return T;
1000}
1001
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001002/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1003/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1004/// they point to and return true. If T1 and T2 aren't pointer types
1005/// or pointer-to-member types, or if they are not similar at this
1006/// level, returns false and leaves T1 and T2 unchanged. Top-level
1007/// qualifiers on T1 and T2 are ignored. This function will typically
1008/// be called in a loop that successively "unwraps" pointer and
1009/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001010bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +00001011 const PointerType *T1PtrType = T1->getAsPointerType(),
1012 *T2PtrType = T2->getAsPointerType();
1013 if (T1PtrType && T2PtrType) {
1014 T1 = T1PtrType->getPointeeType();
1015 T2 = T2PtrType->getPointeeType();
1016 return true;
1017 }
1018
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001019 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
1020 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001021 if (T1MPType && T2MPType &&
1022 Context.getCanonicalType(T1MPType->getClass()) ==
1023 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001024 T1 = T1MPType->getPointeeType();
1025 T2 = T2MPType->getPointeeType();
1026 return true;
1027 }
Douglas Gregor57373262008-10-22 14:17:15 +00001028 return false;
1029}
1030
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001031Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 // C99 6.7.6: Type names have no identifier. This is already validated by
1033 // the parser.
1034 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
1035
Douglas Gregor402abb52009-05-28 23:31:59 +00001036 TagDecl *OwnedTag = 0;
1037 QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001038 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001039 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001040
Douglas Gregor402abb52009-05-28 23:31:59 +00001041 if (getLangOptions().CPlusPlus) {
1042 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001043 CheckExtraCXXDefaultArguments(D);
1044
Douglas Gregor402abb52009-05-28 23:31:59 +00001045 // C++0x [dcl.type]p3:
1046 // A type-specifier-seq shall not define a class or enumeration
1047 // unless it appears in the type-id of an alias-declaration
1048 // (7.1.3).
1049 if (OwnedTag && OwnedTag->isDefinition())
1050 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1051 << Context.getTypeDeclType(OwnedTag);
1052 }
1053
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 return T.getAsOpaquePtr();
1055}
1056
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001057
1058
1059//===----------------------------------------------------------------------===//
1060// Type Attribute Processing
1061//===----------------------------------------------------------------------===//
1062
1063/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1064/// specified type. The attribute contains 1 argument, the id of the address
1065/// space for the type.
1066static void HandleAddressSpaceTypeAttribute(QualType &Type,
1067 const AttributeList &Attr, Sema &S){
1068 // If this type is already address space qualified, reject it.
1069 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1070 // for two or more different address spaces."
1071 if (Type.getAddressSpace()) {
1072 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1073 return;
1074 }
1075
1076 // Check the attribute arguments.
1077 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001078 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001079 return;
1080 }
1081 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1082 llvm::APSInt addrSpace(32);
1083 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001084 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1085 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001086 return;
1087 }
1088
1089 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001090 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001091}
1092
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001093/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1094/// specified type. The attribute contains 1 argument, weak or strong.
1095static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001096 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001097 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001098 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001099 return;
1100 }
1101
1102 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001103 if (!Attr.getParameterName()) {
1104 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1105 << "objc_gc" << 1;
1106 return;
1107 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001108 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001109 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001110 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1111 return;
1112 }
1113 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001114 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001115 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001116 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001117 else {
1118 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1119 << "objc_gc" << Attr.getParameterName();
1120 return;
1121 }
1122
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001123 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001124}
1125
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001126void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001127 // Scan through and apply attributes to this type where it makes sense. Some
1128 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1129 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001130 // apply to the decl. Here we apply type attributes and ignore the rest.
1131 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001132 // If this is an attribute we can handle, do so now, otherwise, add it to
1133 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001134 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001135 default: break;
1136 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001137 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1138 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001139 case AttributeList::AT_objc_gc:
1140 HandleObjCGCTypeAttribute(Result, *AL, *this);
1141 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001142 }
Chris Lattner232e8822008-02-21 01:08:11 +00001143 }
Chris Lattner232e8822008-02-21 01:08:11 +00001144}
1145
Douglas Gregor86447ec2009-03-09 16:13:40 +00001146/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001147///
1148/// This routine checks whether the type @p T is complete in any
1149/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001150/// type, returns false. If @p T is a class template specialization,
1151/// this routine then attempts to perform class template
1152/// instantiation. If instantiation fails, or if @p T is incomplete
1153/// and cannot be completed, issues the diagnostic @p diag (giving it
1154/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001155///
1156/// @param Loc The location in the source that the incomplete type
1157/// diagnostic should refer to.
1158///
1159/// @param T The type that this routine is examining for completeness.
1160///
1161/// @param diag The diagnostic value (e.g.,
1162/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1163/// for the error message if @p T is incomplete.
1164///
1165/// @param Range1 An optional range in the source code that will be a
1166/// part of the "incomplete type" error message.
1167///
1168/// @param Range2 An optional range in the source code that will be a
1169/// part of the "incomplete type" error message.
1170///
1171/// @param PrintType If non-NULL, the type that should be printed
1172/// instead of @p T. This parameter should be used when the type that
1173/// we're checking for incompleteness isn't the type that should be
1174/// displayed to the user, e.g., when T is a type and PrintType is a
1175/// pointer to T.
1176///
1177/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1178/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001179bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Chris Lattner1efaa952009-04-24 00:30:45 +00001180 SourceRange Range1, SourceRange Range2,
1181 QualType PrintType) {
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001182 // FIXME: Add this assertion to help us flush out problems with
1183 // checking for dependent types and type-dependent expressions.
1184 //
1185 // assert(!T->isDependentType() &&
1186 // "Can't ask whether a dependent type is complete");
1187
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001188 // If we have a complete type, we're done.
1189 if (!T->isIncompleteType())
1190 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001191
Douglas Gregord475b8d2009-03-25 21:17:03 +00001192 // If we have a class template specialization or a class member of a
1193 // class template specialization, try to instantiate it.
1194 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001195 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001196 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001197 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1198 // Update the class template specialization's location to
1199 // refer to the point of instantiation.
1200 if (Loc.isValid())
1201 ClassTemplateSpec->setLocation(Loc);
1202 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1203 /*ExplicitInstantiation=*/false);
1204 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001205 } else if (CXXRecordDecl *Rec
1206 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1207 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1208 // Find the class template specialization that surrounds this
1209 // member class.
1210 ClassTemplateSpecializationDecl *Spec = 0;
1211 for (DeclContext *Parent = Rec->getDeclContext();
1212 Parent && !Spec; Parent = Parent->getParent())
1213 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1214 assert(Spec && "Not a member of a class template specialization?");
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001215 return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
1216 /*ExplicitInstantiation=*/false);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001217 }
1218 }
1219 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001220
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001221 if (PrintType.isNull())
1222 PrintType = T;
1223
1224 // We have an incomplete type. Produce a diagnostic.
1225 Diag(Loc, diag) << PrintType << Range1 << Range2;
1226
1227 // If the type was a forward declaration of a class/struct/union
1228 // type, produce
1229 const TagType *Tag = 0;
1230 if (const RecordType *Record = T->getAsRecordType())
1231 Tag = Record;
1232 else if (const EnumType *Enum = T->getAsEnumType())
1233 Tag = Enum;
1234
1235 if (Tag && !Tag->getDecl()->isInvalidDecl())
1236 Diag(Tag->getDecl()->getLocation(),
1237 Tag->isBeingDefined() ? diag::note_type_being_defined
1238 : diag::note_forward_declaration)
1239 << QualType(Tag, 0);
1240
1241 return true;
1242}
Douglas Gregore6258932009-03-19 00:39:20 +00001243
1244/// \brief Retrieve a version of the type 'T' that is qualified by the
1245/// nested-name-specifier contained in SS.
1246QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1247 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1248 return T;
1249
Douglas Gregorab452ba2009-03-26 23:50:42 +00001250 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001251 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001252 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001253}