blob: 9f951989a73f9cbb21a4f649247d6d957cdf9fec [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"
Sebastian Redl23c7d062009-07-07 20:29:57 +000015#include "SemaInherit.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000021#include "clang/Basic/PartialDiagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000022#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000023#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Douglas Gregor2dc0e642009-03-23 23:06:20 +000026/// \brief Perform adjustment on the parameter type of a function.
27///
28/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000029/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
30/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000031QualType Sema::adjustParameterType(QualType T) {
32 // C99 6.7.5.3p7:
33 if (T->isArrayType()) {
34 // C99 6.7.5.3p7:
35 // A declaration of a parameter as "array of type" shall be
36 // adjusted to "qualified pointer to type", where the type
37 // qualifiers (if any) are those specified within the [ and ] of
38 // the array type derivation.
39 return Context.getArrayDecayedType(T);
40 } else if (T->isFunctionType())
41 // C99 6.7.5.3p8:
42 // A declaration of a parameter as "function returning type"
43 // shall be adjusted to "pointer to function returning type", as
44 // in 6.3.2.1.
45 return Context.getPointerType(T);
46
47 return T;
48}
49
Douglas Gregor930d8b52009-01-30 22:09:00 +000050/// \brief Convert the specified declspec to the appropriate type
51/// object.
52/// \param DS the declaration specifiers
Chris Lattner3f84ad22009-04-22 05:27:59 +000053/// \param DeclLoc The location of the declarator identifier or invalid if none.
Chris Lattner5153ee62009-04-25 08:47:54 +000054/// \returns The type described by the declaration specifiers. This function
55/// never returns null.
Chris Lattner3f84ad22009-04-22 05:27:59 +000056QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
Chris Lattnereaaebc72009-04-25 08:06:05 +000057 SourceLocation DeclLoc,
58 bool &isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +000059 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
60 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000061 QualType Result;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Reid Spencer5f016e22007-07-11 17:01:13 +000063 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000064 case DeclSpec::TST_void:
65 Result = Context.VoidTy;
66 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000067 case DeclSpec::TST_char:
68 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000069 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000071 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 else {
73 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
74 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000075 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000076 }
Chris Lattner958858e2008-02-20 21:40:32 +000077 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000078 case DeclSpec::TST_wchar:
79 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
80 Result = Context.WCharTy;
81 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000082 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
83 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000084 Result = Context.getSignedWCharType();
85 } else {
86 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
87 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000088 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
89 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000090 Result = Context.getUnsignedWCharType();
91 }
92 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000093 case DeclSpec::TST_char16:
94 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
95 "Unknown TSS value");
96 Result = Context.Char16Ty;
97 break;
98 case DeclSpec::TST_char32:
99 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
100 "Unknown TSS value");
101 Result = Context.Char32Ty;
102 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000103 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000104 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000105 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000106 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000107 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000108 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000109 break;
110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattnerd658b562008-04-05 06:32:51 +0000112 // Unspecified typespec defaults to int in C90. However, the C90 grammar
113 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
114 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
115 // Note that the one exception to this is function definitions, which are
116 // allowed to be completely missing a declspec. This is handled in the
117 // parser already though by it pretending to have seen an 'int' in this
118 // case.
119 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000120 // In C89 mode, we only warn if there is a completely missing declspec
121 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000122 if (DS.isEmpty()) {
123 if (DeclLoc.isInvalid())
124 DeclLoc = DS.getSourceRange().getBegin();
Eli Friedmanfcff5772009-06-03 12:22:01 +0000125 Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000126 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000127 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
128 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000129 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000130 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000131 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
132 // "At least one type specifier shall be given in the declaration
133 // specifiers in each declaration, and in the specifier-qualifier list in
134 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000135 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000136 if (DeclLoc.isInvalid())
137 DeclLoc = DS.getSourceRange().getBegin();
138
Chris Lattnerb78d8332009-06-26 04:45:06 +0000139 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000140 Diag(DeclLoc, diag::err_missing_type_specifier)
141 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattnerb78d8332009-06-26 04:45:06 +0000143 // When this occurs in C++ code, often something is very broken with the
144 // value being declared, poison it as invalid so we don't get chains of
145 // errors.
146 isInvalid = true;
147 } else {
Eli Friedmanfcff5772009-06-03 12:22:01 +0000148 Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000149 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000150 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
153 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000154 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
156 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000157 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
158 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
159 case DeclSpec::TSW_long: Result = Context.LongTy; break;
160 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
162 } else {
163 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000164 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
165 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
166 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
167 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
169 }
Chris Lattner958858e2008-02-20 21:40:32 +0000170 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000171 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000172 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000173 case DeclSpec::TST_double:
174 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000175 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000176 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000177 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000178 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000179 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 case DeclSpec::TST_decimal32: // _Decimal32
181 case DeclSpec::TST_decimal64: // _Decimal64
182 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000183 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
184 Result = Context.IntTy;
185 isInvalid = true;
186 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000187 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 case DeclSpec::TST_enum:
189 case DeclSpec::TST_union:
190 case DeclSpec::TST_struct: {
191 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000192 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
194 DS.getTypeSpecSign() == 0 &&
195 "Can't handle qualifiers on typedef names yet!");
196 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000197 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
John McCall2191b202009-09-05 06:31:47 +0000198
199 // In C++, make an ElaboratedType.
200 if (getLangOptions().CPlusPlus) {
201 TagDecl::TagKind Tag
202 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
203 Result = Context.getElaboratedType(Result, Tag);
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Chris Lattner5153ee62009-04-25 08:47:54 +0000206 if (D->isInvalidDecl())
207 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000208 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000209 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000210 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
212 DS.getTypeSpecSign() == 0 &&
213 "Can't handle qualifiers on typedef names yet!");
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000214 Result = GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000215
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000216 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000217 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000218 // It would be nice if protocol qualifiers were only stored with the
219 // ObjCObjectPointerType. Unfortunately, this isn't possible due
220 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000221 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000222 // typedef Foo<P> T;
223 // static void func() {
224 // Foo<P> *yy;
225 // T *zz;
226 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000227 Result = Context.getObjCInterfaceType(Interface->getDecl(),
228 (ObjCProtocolDecl**)PQ,
229 DS.getNumProtocolQualifiers());
Steve Naroff14108da2009-07-10 23:34:53 +0000230 else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000231 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000232 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000233 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
234 else if (Result->isObjCClassType()) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000235 if (DeclLoc.isInvalid())
236 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000237 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000238 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000239 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000240 } else {
241 if (DeclLoc.isInvalid())
242 DeclLoc = DS.getSourceRange().getBegin();
243 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
244 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000245 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000246 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattnereaaebc72009-04-25 08:06:05 +0000249 // If this is a reference to an invalid typedef, propagate the invalidity.
250 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
251 if (TDT->getDecl()->isInvalidDecl())
252 isInvalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000255 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 }
Chris Lattner958858e2008-02-20 21:40:32 +0000257 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000258 // FIXME: Preserve type source info.
259 Result = GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000260 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000261 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000262 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000263 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000264 case DeclSpec::TST_typeofExpr: {
265 Expr *E = static_cast<Expr *>(DS.getTypeRep());
266 assert(E && "Didn't get an expression for typeof?");
267 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000268 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000269 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000270 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000271 case DeclSpec::TST_decltype: {
272 Expr *E = static_cast<Expr *>(DS.getTypeRep());
273 assert(E && "Didn't get an expression for decltype?");
274 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000275 Result = BuildDecltypeType(E);
276 if (Result.isNull()) {
277 Result = Context.IntTy;
278 isInvalid = true;
279 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000280 break;
281 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000282 case DeclSpec::TST_auto: {
283 // TypeQuals handled by caller.
284 Result = Context.UndeducedAutoTy;
285 break;
286 }
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Douglas Gregor809070a2009-02-18 17:45:20 +0000288 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000289 Result = Context.IntTy;
290 isInvalid = true;
291 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattner958858e2008-02-20 21:40:32 +0000294 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000295 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
296 if (getLangOptions().Freestanding)
297 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000298 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattner958858e2008-02-20 21:40:32 +0000301 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
302 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattner38d8b982008-02-20 22:04:11 +0000304 // See if there are any attributes on the declspec that apply to the type (as
305 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000306 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000307 ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner96b77fc2008-04-02 06:50:17 +0000309 // Apply const/volatile/restrict qualifiers to T.
310 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
311
312 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
313 // or incomplete types shall not be restrict-qualified." C++ also allows
314 // restrict-qualified references.
315 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000316 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000317 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000318 Result->getAs<PointerType>()->getPointeeType() :
319 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Douglas Gregorbad0e652009-03-24 20:32:41 +0000321 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000322 // incomplete type.
323 if (!EltTy->isIncompleteOrObjectType()) {
324 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000325 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000326 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000327 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
328 }
329 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000330 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000331 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000332 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000333 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000334 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner96b77fc2008-04-02 06:50:17 +0000337 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
338 // of a function type includes any type qualifiers, the behavior is
339 // undefined."
340 if (Result->isFunctionType() && TypeQuals) {
341 // Get some location to point at, either the C or V location.
342 SourceLocation Loc;
343 if (TypeQuals & QualType::Const)
344 Loc = DS.getConstSpecLoc();
345 else {
346 assert((TypeQuals & QualType::Volatile) &&
347 "Has CV quals but not C or V?");
348 Loc = DS.getVolatileSpecLoc();
349 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000350 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000351 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000354 // C++ [dcl.ref]p1:
355 // Cv-qualified references are ill-formed except when the
356 // cv-qualifiers are introduced through the use of a typedef
357 // (7.1.3) or of a template type argument (14.3), in which
358 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000359 // FIXME: Shouldn't we be checking SCS_typedef here?
360 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000361 TypeQuals && Result->isReferenceType()) {
362 TypeQuals &= ~QualType::Const;
363 TypeQuals &= ~QualType::Volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000364 }
365
Chris Lattner96b77fc2008-04-02 06:50:17 +0000366 Result = Result.getQualifiedType(TypeQuals);
367 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000368 return Result;
369}
370
Douglas Gregorcd281c32009-02-28 00:25:32 +0000371static std::string getPrintableNameForEntity(DeclarationName Entity) {
372 if (Entity)
373 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Douglas Gregorcd281c32009-02-28 00:25:32 +0000375 return "type name";
376}
377
378/// \brief Build a pointer type.
379///
380/// \param T The type to which we'll be building a pointer.
381///
382/// \param Quals The cvr-qualifiers to be applied to the pointer type.
383///
384/// \param Loc The location of the entity whose type involves this
385/// pointer type or, if there is no such entity, the location of the
386/// type that will have pointer type.
387///
388/// \param Entity The name of the entity that involves the pointer
389/// type, if known.
390///
391/// \returns A suitable pointer type, if there are no
392/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000393QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000394 SourceLocation Loc, DeclarationName Entity) {
395 if (T->isReferenceType()) {
396 // C++ 8.3.2p4: There shall be no ... pointers to references ...
397 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
398 << getPrintableNameForEntity(Entity);
399 return QualType();
400 }
401
402 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
403 // object or incomplete types shall not be restrict-qualified."
404 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
405 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
406 << T;
407 Quals &= ~QualType::Restrict;
408 }
409
410 // Build the pointer type.
411 return Context.getPointerType(T).getQualifiedType(Quals);
412}
413
414/// \brief Build a reference type.
415///
416/// \param T The type to which we'll be building a reference.
417///
418/// \param Quals The cvr-qualifiers to be applied to the reference type.
419///
420/// \param Loc The location of the entity whose type involves this
421/// reference type or, if there is no such entity, the location of the
422/// type that will have reference type.
423///
424/// \param Entity The name of the entity that involves the reference
425/// type, if known.
426///
427/// \returns A suitable reference type, if there are no
428/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000429QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000430 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000431 if (LValueRef) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000432 if (const RValueReferenceType *R = T->getAs<RValueReferenceType>()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000433 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
434 // reference to a type T, and attempt to create the type "lvalue
435 // reference to cv TD" creates the type "lvalue reference to T".
436 // We use the qualifiers (restrict or none) of the original reference,
437 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000438 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000439 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000440 }
441 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000442 if (T->isReferenceType()) {
443 // C++ [dcl.ref]p4: There shall be no references to references.
Mike Stump1eb44332009-09-09 15:08:12 +0000444 //
Douglas Gregorcd281c32009-02-28 00:25:32 +0000445 // According to C++ DR 106, references to references are only
446 // diagnosed when they are written directly (e.g., "int & &"),
447 // but not when they happen via a typedef:
448 //
449 // typedef int& intref;
450 // typedef intref& intref2;
451 //
452 // Parser::ParserDeclaratorInternal diagnoses the case where
453 // references are written directly; here, we handle the
454 // collapsing of references-to-references as described in C++
455 // DR 106 and amended by C++ DR 540.
456 return T;
457 }
458
459 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000460 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000461 // is ill-formed.
462 if (T->isVoidType()) {
463 Diag(Loc, diag::err_reference_to_void);
464 return QualType();
465 }
466
467 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
468 // object or incomplete types shall not be restrict-qualified."
469 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
470 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
471 << T;
472 Quals &= ~QualType::Restrict;
473 }
474
475 // C++ [dcl.ref]p1:
476 // [...] Cv-qualified references are ill-formed except when the
477 // cv-qualifiers are introduced through the use of a typedef
478 // (7.1.3) or of a template type argument (14.3), in which case
479 // the cv-qualifiers are ignored.
480 //
481 // We diagnose extraneous cv-qualifiers for the non-typedef,
482 // non-template type argument case within the parser. Here, we just
483 // ignore any extraneous cv-qualifiers.
484 Quals &= ~QualType::Const;
485 Quals &= ~QualType::Volatile;
486
487 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000488 if (LValueRef)
489 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
490 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000491}
492
493/// \brief Build an array type.
494///
495/// \param T The type of each element in the array.
496///
497/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000498///
499/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000500///
501/// \param Quals The cvr-qualifiers to be applied to the array's
502/// element type.
503///
504/// \param Loc The location of the entity whose type involves this
505/// array type or, if there is no such entity, the location of the
506/// type that will have array type.
507///
508/// \param Entity The name of the entity that involves the array
509/// type, if known.
510///
511/// \returns A suitable array type, if there are no errors. Otherwise,
512/// returns a NULL type.
513QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
514 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000515 SourceRange Brackets, DeclarationName Entity) {
516 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000517 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000518 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Mike Stump1eb44332009-09-09 15:08:12 +0000519 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000520 diag::err_illegal_decl_array_incomplete_type))
521 return QualType();
522
523 if (T->isFunctionType()) {
524 Diag(Loc, diag::err_illegal_decl_array_of_functions)
525 << getPrintableNameForEntity(Entity);
526 return QualType();
527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Douglas Gregorcd281c32009-02-28 00:25:32 +0000529 // C++ 8.3.2p4: There shall be no ... arrays of references ...
530 if (T->isReferenceType()) {
531 Diag(Loc, diag::err_illegal_decl_array_of_references)
532 << getPrintableNameForEntity(Entity);
533 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000534 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000535
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000536 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000537 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000538 << getPrintableNameForEntity(Entity);
539 return QualType();
540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Ted Kremenek6217b802009-07-29 21:53:49 +0000542 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000543 // If the element type is a struct or union that contains a variadic
544 // array, accept it as a GNU extension: C99 6.7.2.1p2.
545 if (EltTy->getDecl()->hasFlexibleArrayMember())
546 Diag(Loc, diag::ext_flexible_array_in_array) << T;
547 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000548 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
549 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Douglas Gregorcd281c32009-02-28 00:25:32 +0000552 // C99 6.7.5.2p1: The size expression shall have integer type.
553 if (ArraySize && !ArraySize->isTypeDependent() &&
554 !ArraySize->getType()->isIntegerType()) {
555 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
556 << ArraySize->getType() << ArraySize->getSourceRange();
557 ArraySize->Destroy(Context);
558 return QualType();
559 }
560 llvm::APSInt ConstVal(32);
561 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000562 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000563 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000564 else
565 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000566 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000567 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000568 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
569 (!T->isDependentType() && !T->isConstantSizeType())) {
570 // Per C99, a variable array is an array with either a non-constant
571 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000572 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000573 } else {
574 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
575 // have a value greater than zero.
576 if (ConstVal.isSigned()) {
577 if (ConstVal.isNegative()) {
578 Diag(ArraySize->getLocStart(),
579 diag::err_typecheck_negative_array_size)
580 << ArraySize->getSourceRange();
581 return QualType();
582 } else if (ConstVal == 0) {
583 // GCC accepts zero sized static arrays.
584 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
585 << ArraySize->getSourceRange();
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000588 T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize,
589 ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000590 }
591 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
592 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000593 if (ArraySize && !ArraySize->isTypeDependent() &&
594 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000595 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000596 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000597 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000598 Diag(Loc,
599 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
600 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000601 }
602
603 return T;
604}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000605
606/// \brief Build an ext-vector type.
607///
608/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000609QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000610 SourceLocation AttrLoc) {
611
612 Expr *Arg = (Expr *)ArraySize.get();
613
614 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
615 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000616 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000617 !T->isIntegerType() && !T->isRealFloatingType()) {
618 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
619 return QualType();
620 }
621
622 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
623 llvm::APSInt vecSize(32);
624 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
625 Diag(AttrLoc, diag::err_attribute_argument_not_int)
626 << "ext_vector_type" << Arg->getSourceRange();
627 return QualType();
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
630 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000631 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000632 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
633
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000634 if (vectorSize == 0) {
635 Diag(AttrLoc, diag::err_attribute_zero_size)
636 << Arg->getSourceRange();
637 return QualType();
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000640 if (!T->isDependentType())
641 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000642 }
643
644 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000645 AttrLoc);
646}
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Douglas Gregor724651c2009-02-28 01:04:19 +0000648/// \brief Build a function type.
649///
650/// This routine checks the function type according to C++ rules and
651/// under the assumption that the result type and parameter types have
652/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000653/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000654/// simpler form that is only suitable for this narrow use case.
655///
656/// \param T The return type of the function.
657///
658/// \param ParamTypes The parameter types of the function. This array
659/// will be modified to account for adjustments to the types of the
660/// function parameters.
661///
662/// \param NumParamTypes The number of parameter types in ParamTypes.
663///
664/// \param Variadic Whether this is a variadic function type.
665///
666/// \param Quals The cvr-qualifiers to be applied to the function type.
667///
668/// \param Loc The location of the entity whose type involves this
669/// function type or, if there is no such entity, the location of the
670/// type that will have function type.
671///
672/// \param Entity The name of the entity that involves the function
673/// type, if known.
674///
675/// \returns A suitable function type, if there are no
676/// errors. Otherwise, returns a NULL type.
677QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000678 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000679 unsigned NumParamTypes,
680 bool Variadic, unsigned Quals,
681 SourceLocation Loc, DeclarationName Entity) {
682 if (T->isArrayType() || T->isFunctionType()) {
683 Diag(Loc, diag::err_func_returning_array_function) << T;
684 return QualType();
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregor724651c2009-02-28 01:04:19 +0000687 bool Invalid = false;
688 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000689 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
690 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000691 Diag(Loc, diag::err_param_with_void_type);
692 Invalid = true;
693 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000694
Douglas Gregor724651c2009-02-28 01:04:19 +0000695 ParamTypes[Idx] = ParamType;
696 }
697
698 if (Invalid)
699 return QualType();
700
Mike Stump1eb44332009-09-09 15:08:12 +0000701 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000702 Quals);
703}
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Douglas Gregor949bf692009-06-09 22:17:39 +0000705/// \brief Build a member pointer type \c T Class::*.
706///
707/// \param T the type to which the member pointer refers.
708/// \param Class the class type into which the member pointer points.
709/// \param Quals Qualifiers applied to the member pointer type
710/// \param Loc the location where this type begins
711/// \param Entity the name of the entity that will have this member pointer type
712///
713/// \returns a member pointer type, if successful, or a NULL type if there was
714/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000715QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
716 unsigned Quals, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000717 DeclarationName Entity) {
718 // Verify that we're not building a pointer to pointer to function with
719 // exception specification.
720 if (CheckDistantExceptionSpec(T)) {
721 Diag(Loc, diag::err_distant_exception_spec);
722
723 // FIXME: If we're doing this as part of template instantiation,
724 // we should return immediately.
725
726 // Build the type anyway, but use the canonical type so that the
727 // exception specifiers are stripped off.
728 T = Context.getCanonicalType(T);
729 }
730
731 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
732 // with reference type, or "cv void."
733 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000734 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000735 << (Entity? Entity.getAsString() : "type name");
736 return QualType();
737 }
738
739 if (T->isVoidType()) {
740 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
741 << (Entity? Entity.getAsString() : "type name");
742 return QualType();
743 }
744
745 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
746 // object or incomplete types shall not be restrict-qualified."
747 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
748 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
749 << T;
750
751 // FIXME: If we're doing this as part of template instantiation,
752 // we should return immediately.
753 Quals &= ~QualType::Restrict;
754 }
755
756 if (!Class->isDependentType() && !Class->isRecordType()) {
757 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
758 return QualType();
759 }
760
761 return Context.getMemberPointerType(T, Class.getTypePtr())
Mike Stump1eb44332009-09-09 15:08:12 +0000762 .getQualifiedType(Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000763}
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Anders Carlsson9a917e42009-06-12 22:56:54 +0000765/// \brief Build a block pointer type.
766///
767/// \param T The type to which we'll be building a block pointer.
768///
769/// \param Quals The cvr-qualifiers to be applied to the block pointer type.
770///
771/// \param Loc The location of the entity whose type involves this
772/// block pointer type or, if there is no such entity, the location of the
773/// type that will have block pointer type.
774///
775/// \param Entity The name of the entity that involves the block pointer
776/// type, if known.
777///
778/// \returns A suitable block pointer type, if there are no
779/// errors. Otherwise, returns a NULL type.
780QualType Sema::BuildBlockPointerType(QualType T, unsigned Quals,
Mike Stump1eb44332009-09-09 15:08:12 +0000781 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000782 DeclarationName Entity) {
783 if (!T.getTypePtr()->isFunctionType()) {
784 Diag(Loc, diag::err_nonfunction_block_type);
785 return QualType();
786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Anders Carlsson9a917e42009-06-12 22:56:54 +0000788 return Context.getBlockPointerType(T).getQualifiedType(Quals);
789}
790
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000791QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
792 QualType QT = QualType::getFromOpaquePtr(Ty);
793 DeclaratorInfo *DI = 0;
794 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
795 QT = LIT->getType();
796 DI = LIT->getDeclaratorInfo();
797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000799 if (DInfo) *DInfo = DI;
800 return QT;
801}
802
Mike Stump98eb8a72009-02-04 22:31:32 +0000803/// GetTypeForDeclarator - Convert the type for the specified
804/// declarator to Type instances. Skip the outermost Skip type
805/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000806///
807/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
808/// owns the declaration of a type (e.g., the definition of a struct
809/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000810QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
811 DeclaratorInfo **DInfo, unsigned Skip,
Douglas Gregor402abb52009-05-28 23:31:59 +0000812 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000813 bool OmittedReturnType = false;
814
815 if (D.getContext() == Declarator::BlockLiteralContext
816 && Skip == 0
817 && !D.getDeclSpec().hasTypeSpecifier()
818 && (D.getNumTypeObjects() == 0
819 || (D.getNumTypeObjects() == 1
820 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
821 OmittedReturnType = true;
822
Chris Lattnerb23deda2007-08-28 16:40:32 +0000823 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000824 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000825 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
826 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000827
828 // Determine the type of the declarator. Not all forms of declarator
829 // have a type.
830 QualType T;
831 switch (D.getKind()) {
832 case Declarator::DK_Abstract:
833 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000834 case Declarator::DK_Operator: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000835 const DeclSpec &DS = D.getDeclSpec();
836 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000837 // We default to a dependent type initially. Can be modified by
838 // the first return statement.
839 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000840 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000841 bool isInvalid = false;
842 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000843 if (isInvalid)
844 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000845 else if (OwnedDecl && DS.isTypeSpecOwned())
846 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000847 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000848 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000849 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000850
851 case Declarator::DK_Constructor:
852 case Declarator::DK_Destructor:
853 case Declarator::DK_Conversion:
854 // Constructors and destructors don't have return types. Use
855 // "void" instead. Conversion operators will check their return
856 // types separately.
857 T = Context.VoidTy;
858 break;
859 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000860
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000861 if (T == Context.UndeducedAutoTy) {
862 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000864 switch (D.getContext()) {
865 case Declarator::KNRTypeListContext:
866 assert(0 && "K&R type lists aren't allowed in C++");
867 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000868 case Declarator::PrototypeContext:
869 Error = 0; // Function prototype
870 break;
871 case Declarator::MemberContext:
872 switch (cast<TagDecl>(CurContext)->getTagKind()) {
873 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
874 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
875 case TagDecl::TK_union: Error = 2; /* Union member */ break;
876 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000877 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000878 break;
879 case Declarator::CXXCatchContext:
880 Error = 4; // Exception declaration
881 break;
882 case Declarator::TemplateParamContext:
883 Error = 5; // Template parameter
884 break;
885 case Declarator::BlockLiteralContext:
886 Error = 6; // Block literal
887 break;
888 case Declarator::FileContext:
889 case Declarator::BlockContext:
890 case Declarator::ForContext:
891 case Declarator::ConditionContext:
892 case Declarator::TypeNameContext:
893 break;
894 }
895
896 if (Error != -1) {
897 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
898 << Error;
899 T = Context.IntTy;
900 D.setInvalidType(true);
901 }
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregorcd281c32009-02-28 00:25:32 +0000904 // The name we're declaring, if any.
905 DeclarationName Name;
906 if (D.getIdentifier())
907 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000909 bool ShouldBuildInfo = DInfo != 0;
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +0000910 // The QualType referring to the type as written in source code. We can't use
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000911 // T because it can change due to semantic analysis.
912 QualType SourceTy = T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000913
Mike Stump98eb8a72009-02-04 22:31:32 +0000914 // Walk the DeclTypeInfo, building the recursive type as we go.
915 // DeclTypeInfos are ordered from the identifier out, which is
916 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000917 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
918 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 switch (DeclType.Kind) {
920 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000921 case DeclaratorChunk::BlockPointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000922 if (ShouldBuildInfo) {
923 if (SourceTy->isFunctionType())
924 SourceTy = Context.getBlockPointerType(SourceTy)
925 .getQualifiedType(DeclType.Cls.TypeQuals);
926 else
927 // If not function type Context::getBlockPointerType asserts,
928 // so just give up.
929 ShouldBuildInfo = false;
930 }
931
Chris Lattner9af55002009-03-27 04:18:06 +0000932 // If blocks are disabled, emit an error.
933 if (!LangOpts.Blocks)
934 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
936 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000937 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000938 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 case DeclaratorChunk::Pointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000940 //FIXME: Use ObjCObjectPointer for info when appropriate.
941 if (ShouldBuildInfo)
942 SourceTy = Context.getPointerType(SourceTy)
943 .getQualifiedType(DeclType.Ptr.TypeQuals);
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000944 // Verify that we're not building a pointer to pointer to function with
945 // exception specification.
946 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
947 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
948 D.setInvalidType(true);
949 // Build the type anyway.
950 }
Steve Naroff14108da2009-07-10 23:34:53 +0000951 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000952 const ObjCInterfaceType *OIT = T->getAsObjCInterfaceType();
Steve Naroff14108da2009-07-10 23:34:53 +0000953 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000954 (ObjCProtocolDecl **)OIT->qual_begin(),
955 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000956 break;
957 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000958 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000960 case DeclaratorChunk::Reference:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000961 if (ShouldBuildInfo) {
962 if (DeclType.Ref.LValueRef)
963 SourceTy = Context.getLValueReferenceType(SourceTy);
964 else
965 SourceTy = Context.getRValueReferenceType(SourceTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000966 unsigned Quals = DeclType.Ref.HasRestrict ? QualType::Restrict : 0;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000967 SourceTy = SourceTy.getQualifiedType(Quals);
968 }
969
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000970 // Verify that we're not building a reference to pointer to function with
971 // exception specification.
972 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
973 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
974 D.setInvalidType(true);
975 // Build the type anyway.
976 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000977 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
978 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000979 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 break;
981 case DeclaratorChunk::Array: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000982 if (ShouldBuildInfo)
983 // We just need to get an array type, the exact type doesn't matter.
984 SourceTy = Context.getIncompleteArrayType(SourceTy, ArrayType::Normal,
985 DeclType.Arr.TypeQuals);
986
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000987 // Verify that we're not building an array of pointers to function with
988 // exception specification.
989 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
990 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
991 D.setInvalidType(true);
992 // Build the type anyway.
993 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000994 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000995 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 ArrayType::ArraySizeModifier ASM;
997 if (ATI.isStar)
998 ASM = ArrayType::Star;
999 else if (ATI.hasStatic)
1000 ASM = ArrayType::Static;
1001 else
1002 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001003 if (ASM == ArrayType::Star &&
1004 D.getContext() != Declarator::PrototypeContext) {
1005 // FIXME: This check isn't quite right: it allows star in prototypes
1006 // for function definitions, and disallows some edge cases detailed
1007 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1008 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1009 ASM = ArrayType::Normal;
1010 D.setInvalidType(true);
1011 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001012 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
1013 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 break;
1015 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001016 case DeclaratorChunk::Function: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001017 if (ShouldBuildInfo) {
1018 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1019 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001021 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1022 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
1023 if (Param)
1024 ArgTys.push_back(Param->getType());
1025 }
1026 SourceTy = Context.getFunctionType(SourceTy, ArgTys.data(),
1027 ArgTys.size(),
1028 FTI.isVariadic, FTI.TypeQuals);
1029 }
1030
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 // If the function declarator has a prototype (i.e. it is not () and
1032 // does not have a K&R-style identifier list), then the arguments are part
1033 // of the type, otherwise the argument list is ().
1034 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001035
Chris Lattnercd881292007-12-19 05:31:29 +00001036 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001037 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001038 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001039 T = Context.IntTy;
1040 D.setInvalidType(true);
1041 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001042
Douglas Gregor402abb52009-05-28 23:31:59 +00001043 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1044 // C++ [dcl.fct]p6:
1045 // Types shall not be defined in return or parameter types.
1046 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1047 if (Tag->isDefinition())
1048 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1049 << Context.getTypeDeclType(Tag);
1050 }
1051
Sebastian Redl3cc97262009-05-31 11:47:27 +00001052 // Exception specs are not allowed in typedefs. Complain, but add it
1053 // anyway.
1054 if (FTI.hasExceptionSpec &&
1055 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1056 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1057
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001058 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001059 if (getLangOptions().CPlusPlus) {
1060 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1061 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001062 llvm::SmallVector<QualType, 4> Exceptions;
1063 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001064 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001065 // FIXME: Preserve type source info.
1066 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001067 // Check that the type is valid for an exception spec, and drop it
1068 // if not.
1069 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1070 Exceptions.push_back(ET);
1071 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001072 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1073 FTI.hasExceptionSpec,
1074 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001075 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001076 } else if (FTI.isVariadic) {
1077 // We allow a zero-parameter variadic function in C if the
1078 // function is marked with the "overloadable"
1079 // attribute. Scan for this attribute now.
1080 bool Overloadable = false;
1081 for (const AttributeList *Attrs = D.getAttributes();
1082 Attrs; Attrs = Attrs->getNext()) {
1083 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1084 Overloadable = true;
1085 break;
1086 }
1087 }
1088
1089 if (!Overloadable)
1090 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1091 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001092 } else {
1093 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001094 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001095 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001096 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001098 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 } else {
1100 // Otherwise, we have a function with an argument list that is
1101 // potentially variadic.
1102 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001105 ParmVarDecl *Param =
1106 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001107 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001108 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001109
1110 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001111 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001112
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 // Look for 'void'. void is allowed only as a single argument to a
1114 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001115 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001116 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 // If this is something like 'float(int, void)', reject it. 'void'
1118 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1119 // have arguments of incomplete type.
1120 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1121 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001122 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001123 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001124 } else if (FTI.ArgInfo[i].Ident) {
1125 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001127 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001128 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001129 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001130 } else {
1131 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001132 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001133 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Chris Lattner2ff54262007-07-21 05:18:12 +00001135 // Do not add 'void' to the ArgTys list.
1136 break;
1137 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001138 } else if (!FTI.hasPrototype) {
1139 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001140 ArgTy = Context.getPromotedIntegerType(ArgTy);
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001141 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
1142 if (BTy->getKind() == BuiltinType::Float)
1143 ArgTy = Context.DoubleTy;
1144 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 ArgTys.push_back(ArgTy);
1148 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001149
1150 llvm::SmallVector<QualType, 4> Exceptions;
1151 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001152 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001153 // FIXME: Preserve type source info.
1154 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001155 // Check that the type is valid for an exception spec, and drop it if
1156 // not.
1157 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1158 Exceptions.push_back(ET);
1159 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001160
Jay Foadbeaaccd2009-05-21 09:52:38 +00001161 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001162 FTI.isVariadic, FTI.TypeQuals,
1163 FTI.hasExceptionSpec,
1164 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001165 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 }
1167 break;
1168 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001169 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001170 // Verify that we're not building a pointer to pointer to function with
1171 // exception specification.
1172 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1173 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1174 D.setInvalidType(true);
1175 // Build the type anyway.
1176 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001177 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001178 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001179 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001180 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001181 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1182 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1183 ClsType = QualType(NNS->getAsType(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001184 } else if (CXXRecordDecl *RD
Douglas Gregor949bf692009-06-09 22:17:39 +00001185 = dyn_cast_or_null<CXXRecordDecl>(
1186 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001187 ClsType = Context.getTagDeclType(RD);
1188 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001189 Diag(DeclType.Mem.Scope().getBeginLoc(),
1190 diag::err_illegal_decl_mempointer_in_nonclass)
1191 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1192 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001193 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001194 }
1195
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001196 if (ShouldBuildInfo) {
1197 QualType cls = !ClsType.isNull() ? ClsType : Context.IntTy;
1198 SourceTy = Context.getMemberPointerType(SourceTy, cls.getTypePtr())
1199 .getQualifiedType(DeclType.Mem.TypeQuals);
1200 }
1201
Douglas Gregor949bf692009-06-09 22:17:39 +00001202 if (!ClsType.isNull())
1203 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1204 DeclType.Loc, D.getIdentifier());
1205 if (T.isNull()) {
1206 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001207 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001208 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001209 break;
1210 }
1211
Douglas Gregorcd281c32009-02-28 00:25:32 +00001212 if (T.isNull()) {
1213 D.setInvalidType(true);
1214 T = Context.IntTy;
1215 }
1216
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001217 // See if there are any attributes on this declarator chunk.
1218 if (const AttributeList *AL = DeclType.getAttrs())
1219 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001221
1222 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001223 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
1224 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001225
1226 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1227 // for a nonstatic member function, the function type to which a pointer
1228 // to member refers, or the top-level function type of a function typedef
1229 // declaration.
1230 if (FnTy->getTypeQuals() != 0 &&
1231 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001232 ((D.getContext() != Declarator::MemberContext &&
1233 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001234 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1235 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001236 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001237 if (D.isFunctionDeclarator())
1238 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1239 else
1240 Diag(D.getIdentifierLoc(),
1241 diag::err_invalid_qualified_typedef_function_type_use);
1242
1243 // Strip the cv-quals from the type.
1244 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001245 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001246 }
1247 }
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001249 // If there were any type attributes applied to the decl itself (not the
1250 // type, apply the type attribute to the type!)
1251 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001252 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001253
1254 if (ShouldBuildInfo)
1255 *DInfo = GetDeclaratorInfoForDeclarator(D, SourceTy, Skip);
1256
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 return T;
1258}
1259
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001260/// \brief Create and instantiate a DeclaratorInfo with type source information.
1261///
1262/// \param T QualType referring to the type as written in source code.
1263DeclaratorInfo *
1264Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T, unsigned Skip) {
1265 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
1266 TypeLoc CurrTL = DInfo->getTypeLoc();
1267
1268 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
1269 assert(!CurrTL.isNull());
1270
1271 DeclaratorChunk &DeclType = D.getTypeObject(i);
1272 switch (DeclType.Kind) {
1273 default: assert(0 && "Unknown decltype!");
1274 case DeclaratorChunk::BlockPointer: {
1275 BlockPointerLoc &BPL = cast<BlockPointerLoc>(CurrTL);
1276 BPL.setCaretLoc(DeclType.Loc);
1277 break;
1278 }
1279 case DeclaratorChunk::Pointer: {
1280 //FIXME: ObjCObject pointers.
1281 PointerLoc &PL = cast<PointerLoc>(CurrTL);
1282 PL.setStarLoc(DeclType.Loc);
1283 break;
1284 }
1285 case DeclaratorChunk::Reference: {
1286 ReferenceLoc &RL = cast<ReferenceLoc>(CurrTL);
1287 RL.setAmpLoc(DeclType.Loc);
1288 break;
1289 }
1290 case DeclaratorChunk::Array: {
1291 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
1292 ArrayLoc &AL = cast<ArrayLoc>(CurrTL);
1293 AL.setLBracketLoc(DeclType.Loc);
1294 AL.setRBracketLoc(DeclType.EndLoc);
1295 AL.setSizeExpr(static_cast<Expr*>(ATI.NumElts));
1296 //FIXME: Star location for [*].
1297 break;
1298 }
1299 case DeclaratorChunk::Function: {
1300 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1301 FunctionLoc &FL = cast<FunctionLoc>(CurrTL);
1302 FL.setLParenLoc(DeclType.Loc);
1303 FL.setRParenLoc(DeclType.EndLoc);
1304 for (unsigned i = 0, e = FTI.NumArgs, tpi = 0; i != e; ++i) {
1305 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
1306 if (Param) {
1307 assert(tpi < FL.getNumArgs());
1308 FL.setArg(tpi++, Param);
1309 }
1310 }
1311 break;
1312 //FIXME: Exception specs.
1313 }
1314 case DeclaratorChunk::MemberPointer: {
1315 MemberPointerLoc &MPL = cast<MemberPointerLoc>(CurrTL);
1316 MPL.setStarLoc(DeclType.Loc);
1317 //FIXME: Class location.
1318 break;
1319 }
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001321 }
1322
1323 CurrTL = CurrTL.getNextTypeLoc();
1324 }
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001326 if (TypedefLoc *TL = dyn_cast<TypedefLoc>(&CurrTL)) {
1327 TL->setNameLoc(D.getDeclSpec().getTypeSpecTypeLoc());
1328 } else {
1329 //FIXME: Other typespecs.
1330 DefaultTypeSpecLoc &DTL = cast<DefaultTypeSpecLoc>(CurrTL);
Argyrios Kyrtzidis31590f92009-08-29 22:39:34 +00001331 DTL.setStartLoc(D.getDeclSpec().getSourceRange().getBegin());
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001332 }
1333
1334 return DInfo;
1335}
1336
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001337/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1338QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1339 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1340 // and Sema during declaration parsing. Try deallocating/caching them when
1341 // it's appropriate, instead of allocating them and keeping them around.
1342 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1343 new (LocT) LocInfoType(T, DInfo);
1344 assert(LocT->getTypeClass() != T->getTypeClass() &&
1345 "LocInfoType's TypeClass conflicts with an existing Type class");
1346 return QualType(LocT, 0);
1347}
1348
1349void LocInfoType::getAsStringInternal(std::string &Str,
1350 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001351 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1352 " was used directly instead of getting the QualType through"
1353 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001354}
1355
Sebastian Redlef65f062009-05-29 18:02:33 +00001356/// CheckSpecifiedExceptionType - Check if the given type is valid in an
1357/// exception specification. Incomplete types, or pointers to incomplete types
1358/// other than void are not allowed.
1359bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
1360 // FIXME: This may not correctly work with the fix for core issue 437,
1361 // where a class's own type is considered complete within its body.
1362
1363 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1364 // an incomplete type.
1365 if (T->isIncompleteType())
1366 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1367 << Range << T << /*direct*/0;
1368
1369 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1370 // an incomplete type a pointer or reference to an incomplete type, other
1371 // than (cv) void*.
Sebastian Redlef65f062009-05-29 18:02:33 +00001372 int kind;
Ted Kremenek6217b802009-07-29 21:53:49 +00001373 if (const PointerType* IT = T->getAs<PointerType>()) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001374 T = IT->getPointeeType();
1375 kind = 1;
Ted Kremenek6217b802009-07-29 21:53:49 +00001376 } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001377 T = IT->getPointeeType();
Sebastian Redl3cc97262009-05-31 11:47:27 +00001378 kind = 2;
Sebastian Redlef65f062009-05-29 18:02:33 +00001379 } else
1380 return false;
1381
1382 if (T->isIncompleteType() && !T->isVoidType())
1383 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1384 << Range << T << /*indirect*/kind;
1385
1386 return false;
1387}
1388
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001389/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
1390/// to member to a function with an exception specification. This means that
1391/// it is invalid to add another level of indirection.
1392bool Sema::CheckDistantExceptionSpec(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001393 if (const PointerType *PT = T->getAs<PointerType>())
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001394 T = PT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001395 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001396 T = PT->getPointeeType();
1397 else
1398 return false;
1399
1400 const FunctionProtoType *FnT = T->getAsFunctionProtoType();
1401 if (!FnT)
1402 return false;
1403
1404 return FnT->hasExceptionSpec();
1405}
1406
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001407/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
1408/// exception specifications. Exception specifications are equivalent if
1409/// they allow exactly the same set of exception types. It does not matter how
1410/// that is achieved. See C++ [except.spec]p2.
1411bool Sema::CheckEquivalentExceptionSpec(
1412 const FunctionProtoType *Old, SourceLocation OldLoc,
1413 const FunctionProtoType *New, SourceLocation NewLoc) {
1414 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
1415 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
1416 if (OldAny && NewAny)
1417 return false;
1418 if (OldAny || NewAny) {
1419 Diag(NewLoc, diag::err_mismatched_exception_spec);
1420 Diag(OldLoc, diag::note_previous_declaration);
1421 return true;
1422 }
1423
1424 bool Success = true;
1425 // Both have a definite exception spec. Collect the first set, then compare
1426 // to the second.
1427 llvm::SmallPtrSet<const Type*, 8> Types;
1428 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
1429 E = Old->exception_end(); I != E; ++I)
1430 Types.insert(Context.getCanonicalType(*I).getTypePtr());
1431
1432 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
1433 E = New->exception_end(); I != E && Success; ++I)
1434 Success = Types.erase(Context.getCanonicalType(*I).getTypePtr());
1435
1436 Success = Success && Types.empty();
1437
1438 if (Success) {
1439 return false;
1440 }
1441 Diag(NewLoc, diag::err_mismatched_exception_spec);
1442 Diag(OldLoc, diag::note_previous_declaration);
1443 return true;
1444}
1445
Sebastian Redl23c7d062009-07-07 20:29:57 +00001446/// CheckExceptionSpecSubset - Check whether the second function type's
1447/// exception specification is a subset (or equivalent) of the first function
1448/// type. This is used by override and pointer assignment checks.
1449bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID,
1450 const FunctionProtoType *Superset, SourceLocation SuperLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001451 const FunctionProtoType *Subset, SourceLocation SubLoc) {
Sebastian Redl23c7d062009-07-07 20:29:57 +00001452 // FIXME: As usual, we could be more specific in our error messages, but
1453 // that better waits until we've got types with source locations.
1454
1455 // If superset contains everything, we're done.
1456 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
1457 return false;
1458
1459 // It does not. If the subset contains everything, we've failed.
1460 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
1461 Diag(SubLoc, DiagID);
1462 Diag(SuperLoc, NoteID);
1463 return true;
1464 }
1465
1466 // Neither contains everything. Do a proper comparison.
1467 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
1468 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
1469 // Take one type from the subset.
1470 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
1471 bool SubIsPointer = false;
Ted Kremenek6217b802009-07-29 21:53:49 +00001472 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001473 CanonicalSubT = RefTy->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001474 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
Sebastian Redl23c7d062009-07-07 20:29:57 +00001475 CanonicalSubT = PtrTy->getPointeeType();
1476 SubIsPointer = true;
1477 }
1478 bool SubIsClass = CanonicalSubT->isRecordType();
1479 CanonicalSubT.setCVRQualifiers(0);
1480
Sebastian Redl726212f2009-07-18 14:32:15 +00001481 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
Sebastian Redl23c7d062009-07-07 20:29:57 +00001482 /*DetectVirtual=*/false);
1483
1484 bool Contained = false;
1485 // Make sure it's in the superset.
1486 for (FunctionProtoType::exception_iterator SuperI =
1487 Superset->exception_begin(), SuperE = Superset->exception_end();
1488 SuperI != SuperE; ++SuperI) {
1489 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
1490 // SubT must be SuperT or derived from it, or pointer or reference to
1491 // such types.
Ted Kremenek6217b802009-07-29 21:53:49 +00001492 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001493 CanonicalSuperT = RefTy->getPointeeType();
1494 if (SubIsPointer) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001495 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001496 CanonicalSuperT = PtrTy->getPointeeType();
1497 else {
1498 continue;
1499 }
1500 }
1501 CanonicalSuperT.setCVRQualifiers(0);
1502 // If the types are the same, move on to the next type in the subset.
1503 if (CanonicalSubT == CanonicalSuperT) {
1504 Contained = true;
1505 break;
1506 }
1507
1508 // Otherwise we need to check the inheritance.
1509 if (!SubIsClass || !CanonicalSuperT->isRecordType())
1510 continue;
1511
1512 Paths.clear();
1513 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
1514 continue;
1515
1516 if (Paths.isAmbiguous(CanonicalSuperT))
1517 continue;
1518
Sebastian Redl726212f2009-07-18 14:32:15 +00001519 if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true))
1520 continue;
Sebastian Redl23c7d062009-07-07 20:29:57 +00001521
1522 Contained = true;
1523 break;
1524 }
1525 if (!Contained) {
1526 Diag(SubLoc, DiagID);
1527 Diag(SuperLoc, NoteID);
1528 return true;
1529 }
1530 }
1531 // We've run the gauntlet.
1532 return false;
1533}
1534
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001535/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001536/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001537QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1538 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001539 QualType T = MDecl->getResultType();
1540 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Fariborz Jahanian35600022007-11-09 17:18:29 +00001542 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001543 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001544 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001545 selfTy = Context.getPointerType(selfTy);
1546 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001547 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001548 ArgTys.push_back(Context.getObjCIdType());
1549 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Chris Lattner89951a82009-02-20 18:43:26 +00001551 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1552 E = MDecl->param_end(); PI != E; ++PI) {
1553 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001554 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001555 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001556 ArgTys.push_back(ArgTy);
1557 }
1558 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001559 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001560 return T;
1561}
1562
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001563/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1564/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1565/// they point to and return true. If T1 and T2 aren't pointer types
1566/// or pointer-to-member types, or if they are not similar at this
1567/// level, returns false and leaves T1 and T2 unchanged. Top-level
1568/// qualifiers on T1 and T2 are ignored. This function will typically
1569/// be called in a loop that successively "unwraps" pointer and
1570/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001571bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001572 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1573 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001574 if (T1PtrType && T2PtrType) {
1575 T1 = T1PtrType->getPointeeType();
1576 T2 = T2PtrType->getPointeeType();
1577 return true;
1578 }
1579
Ted Kremenek6217b802009-07-29 21:53:49 +00001580 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1581 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001582 if (T1MPType && T2MPType &&
1583 Context.getCanonicalType(T1MPType->getClass()) ==
1584 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001585 T1 = T1MPType->getPointeeType();
1586 T2 = T2MPType->getPointeeType();
1587 return true;
1588 }
Douglas Gregor57373262008-10-22 14:17:15 +00001589 return false;
1590}
1591
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001592Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 // C99 6.7.6: Type names have no identifier. This is already validated by
1594 // the parser.
1595 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001597 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001598 TagDecl *OwnedTag = 0;
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001599 QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001600 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001601 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001602
Douglas Gregor402abb52009-05-28 23:31:59 +00001603 if (getLangOptions().CPlusPlus) {
1604 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001605 CheckExtraCXXDefaultArguments(D);
1606
Douglas Gregor402abb52009-05-28 23:31:59 +00001607 // C++0x [dcl.type]p3:
1608 // A type-specifier-seq shall not define a class or enumeration
1609 // unless it appears in the type-id of an alias-declaration
1610 // (7.1.3).
1611 if (OwnedTag && OwnedTag->isDefinition())
1612 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1613 << Context.getTypeDeclType(OwnedTag);
1614 }
1615
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001616 if (DInfo)
1617 T = CreateLocInfoType(T, DInfo);
1618
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 return T.getAsOpaquePtr();
1620}
1621
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001622
1623
1624//===----------------------------------------------------------------------===//
1625// Type Attribute Processing
1626//===----------------------------------------------------------------------===//
1627
1628/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1629/// specified type. The attribute contains 1 argument, the id of the address
1630/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001631static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001632 const AttributeList &Attr, Sema &S){
1633 // If this type is already address space qualified, reject it.
1634 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1635 // for two or more different address spaces."
1636 if (Type.getAddressSpace()) {
1637 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1638 return;
1639 }
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001641 // Check the attribute arguments.
1642 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001643 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001644 return;
1645 }
1646 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1647 llvm::APSInt addrSpace(32);
1648 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001649 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1650 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001651 return;
1652 }
1653
John McCallefadb772009-07-28 06:52:18 +00001654 // Bounds checking.
1655 if (addrSpace.isSigned()) {
1656 if (addrSpace.isNegative()) {
1657 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1658 << ASArgExpr->getSourceRange();
1659 return;
1660 }
1661 addrSpace.setIsSigned(false);
1662 }
1663 llvm::APSInt max(addrSpace.getBitWidth());
1664 max = QualType::MaxAddressSpace;
1665 if (addrSpace > max) {
1666 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
1667 << QualType::MaxAddressSpace << ASArgExpr->getSourceRange();
1668 return;
1669 }
1670
Mike Stump1eb44332009-09-09 15:08:12 +00001671 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001672 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001673}
1674
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001675/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1676/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001677static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001678 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001679 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001680 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001681 return;
1682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001684 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001685 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001686 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1687 << "objc_gc" << 1;
1688 return;
1689 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001690 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001691 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001692 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1693 return;
1694 }
Mike Stump1eb44332009-09-09 15:08:12 +00001695 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001696 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001697 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001698 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001699 else {
1700 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1701 << "objc_gc" << Attr.getParameterName();
1702 return;
1703 }
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001705 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001706}
1707
Mike Stump24556362009-07-25 21:26:53 +00001708/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1709/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001710static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001711 const AttributeList &Attr, Sema &S) {
1712 if (Attr.getNumArgs() != 0)
1713 return;
1714
1715 // We only apply this to a pointer to function or a pointer to block.
1716 if (!Type->isFunctionPointerType()
1717 && !Type->isBlockPointerType()
1718 && !Type->isFunctionType())
1719 return;
1720
1721 Type = S.Context.getNoReturnType(Type);
1722}
1723
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001724void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001725 // Scan through and apply attributes to this type where it makes sense. Some
1726 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1727 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001728 // apply to the decl. Here we apply type attributes and ignore the rest.
1729 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001730 // If this is an attribute we can handle, do so now, otherwise, add it to
1731 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001732 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001733 default: break;
1734 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001735 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1736 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001737 case AttributeList::AT_objc_gc:
1738 HandleObjCGCTypeAttribute(Result, *AL, *this);
1739 break;
Mike Stump24556362009-07-25 21:26:53 +00001740 case AttributeList::AT_noreturn:
1741 HandleNoReturnTypeAttribute(Result, *AL, *this);
1742 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001743 }
Chris Lattner232e8822008-02-21 01:08:11 +00001744 }
Chris Lattner232e8822008-02-21 01:08:11 +00001745}
1746
Mike Stump1eb44332009-09-09 15:08:12 +00001747/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001748///
1749/// This routine checks whether the type @p T is complete in any
1750/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001751/// type, returns false. If @p T is a class template specialization,
1752/// this routine then attempts to perform class template
1753/// instantiation. If instantiation fails, or if @p T is incomplete
1754/// and cannot be completed, issues the diagnostic @p diag (giving it
1755/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001756///
1757/// @param Loc The location in the source that the incomplete type
1758/// diagnostic should refer to.
1759///
1760/// @param T The type that this routine is examining for completeness.
1761///
Mike Stump1eb44332009-09-09 15:08:12 +00001762/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001763/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001764///
1765/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1766/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001767bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
1768 const PartialDiagnostic &PD) {
1769 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001771 // FIXME: Add this assertion to help us flush out problems with
1772 // checking for dependent types and type-dependent expressions.
1773 //
Mike Stump1eb44332009-09-09 15:08:12 +00001774 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001775 // "Can't ask whether a dependent type is complete");
1776
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001777 // If we have a complete type, we're done.
1778 if (!T->isIncompleteType())
1779 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001780
Douglas Gregord475b8d2009-03-25 21:17:03 +00001781 // If we have a class template specialization or a class member of a
1782 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001783 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001784 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001785 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001786 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1787 // Update the class template specialization's location to
1788 // refer to the point of instantiation.
1789 if (Loc.isValid())
1790 ClassTemplateSpec->setLocation(Loc);
1791 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001792 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001793 /*Complain=*/diag != 0);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001794 }
Mike Stump1eb44332009-09-09 15:08:12 +00001795 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001796 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1797 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregor357bbd02009-08-28 20:50:45 +00001798 // This record was instantiated from a class within a template.
Mike Stump1eb44332009-09-09 15:08:12 +00001799 return InstantiateClass(Loc, Rec, Pattern,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001800 getTemplateInstantiationArgs(Rec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001801 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001802 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001803 }
1804 }
1805 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001806
Douglas Gregor5842ba92009-08-24 15:23:48 +00001807 if (diag == 0)
1808 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001810 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001811 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001812
1813 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001814 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001815 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001816 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001817 Tag = Record;
1818 else if (const EnumType *Enum = T->getAsEnumType())
1819 Tag = Enum;
1820
1821 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001822 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001823 Tag->isBeingDefined() ? diag::note_type_being_defined
1824 : diag::note_forward_declaration)
1825 << QualType(Tag, 0);
1826
1827 return true;
1828}
Douglas Gregore6258932009-03-19 00:39:20 +00001829
1830/// \brief Retrieve a version of the type 'T' that is qualified by the
1831/// nested-name-specifier contained in SS.
1832QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1833 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1834 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Douglas Gregorab452ba2009-03-26 23:50:42 +00001836 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001837 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001838 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001839}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001840
1841QualType Sema::BuildTypeofExprType(Expr *E) {
1842 return Context.getTypeOfExprType(E);
1843}
1844
1845QualType Sema::BuildDecltypeType(Expr *E) {
1846 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001847 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001848 diag::err_cannot_determine_declared_type_of_overloaded_function);
1849 return QualType();
1850 }
1851 return Context.getDecltypeType(E);
1852}