blob: 8195aba967a2519b9fd14d3d449611df4efb5633 [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000020#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000021#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Douglas Gregor2dc0e642009-03-23 23:06:20 +000024/// \brief Perform adjustment on the parameter type of a function.
25///
26/// This routine adjusts the given parameter type @p T to the actual
27/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
28/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
29QualType Sema::adjustParameterType(QualType T) {
30 // C99 6.7.5.3p7:
31 if (T->isArrayType()) {
32 // C99 6.7.5.3p7:
33 // A declaration of a parameter as "array of type" shall be
34 // adjusted to "qualified pointer to type", where the type
35 // qualifiers (if any) are those specified within the [ and ] of
36 // the array type derivation.
37 return Context.getArrayDecayedType(T);
38 } else if (T->isFunctionType())
39 // C99 6.7.5.3p8:
40 // A declaration of a parameter as "function returning type"
41 // shall be adjusted to "pointer to function returning type", as
42 // in 6.3.2.1.
43 return Context.getPointerType(T);
44
45 return T;
46}
47
Douglas Gregor930d8b52009-01-30 22:09:00 +000048/// \brief Convert the specified declspec to the appropriate type
49/// object.
50/// \param DS the declaration specifiers
Chris Lattner3f84ad22009-04-22 05:27:59 +000051/// \param DeclLoc The location of the declarator identifier or invalid if none.
Chris Lattner5153ee62009-04-25 08:47:54 +000052/// \returns The type described by the declaration specifiers. This function
53/// never returns null.
Chris Lattner3f84ad22009-04-22 05:27:59 +000054QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
Chris Lattnereaaebc72009-04-25 08:06:05 +000055 SourceLocation DeclLoc,
56 bool &isInvalid) {
Reid Spencer5f016e22007-07-11 17:01:13 +000057 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
58 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000059 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000060
61 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000062 case DeclSpec::TST_void:
63 Result = Context.VoidTy;
64 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 case DeclSpec::TST_char:
66 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000067 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000069 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 else {
71 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
72 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000073 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
Chris Lattner958858e2008-02-20 21:40:32 +000075 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000076 case DeclSpec::TST_wchar:
77 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
78 Result = Context.WCharTy;
79 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000080 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
81 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000082 Result = Context.getSignedWCharType();
83 } else {
84 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
85 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000086 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
87 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000088 Result = Context.getUnsignedWCharType();
89 }
90 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000091 case DeclSpec::TST_char16:
92 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
93 "Unknown TSS value");
94 Result = Context.Char16Ty;
95 break;
96 case DeclSpec::TST_char32:
97 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
98 "Unknown TSS value");
99 Result = Context.Char32Ty;
100 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000101 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000102 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000103 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Steve Naroff14108da2009-07-10 23:34:53 +0000104 Result = Context.getObjCObjectPointerType(QualType(),
105 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000106 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000107 break;
108 }
109
Chris Lattnerd658b562008-04-05 06:32:51 +0000110 // Unspecified typespec defaults to int in C90. However, the C90 grammar
111 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
112 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
113 // Note that the one exception to this is function definitions, which are
114 // allowed to be completely missing a declspec. This is handled in the
115 // parser already though by it pretending to have seen an 'int' in this
116 // case.
117 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000118 // In C89 mode, we only warn if there is a completely missing declspec
119 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000120 if (DS.isEmpty()) {
121 if (DeclLoc.isInvalid())
122 DeclLoc = DS.getSourceRange().getBegin();
Eli Friedmanfcff5772009-06-03 12:22:01 +0000123 Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000124 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000125 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
126 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000127 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000128 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000129 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
130 // "At least one type specifier shall be given in the declaration
131 // specifiers in each declaration, and in the specifier-qualifier list in
132 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000133 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000134 if (DeclLoc.isInvalid())
135 DeclLoc = DS.getSourceRange().getBegin();
136
Chris Lattnerb78d8332009-06-26 04:45:06 +0000137 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000138 Diag(DeclLoc, diag::err_missing_type_specifier)
139 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000140
141 // When this occurs in C++ code, often something is very broken with the
142 // value being declared, poison it as invalid so we don't get chains of
143 // errors.
144 isInvalid = true;
145 } else {
Eli Friedmanfcff5772009-06-03 12:22:01 +0000146 Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000147 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000148 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000149 }
150
151 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000152 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
154 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000155 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
156 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
157 case DeclSpec::TSW_long: Result = Context.LongTy; break;
158 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
160 } else {
161 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000162 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
163 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
164 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
165 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
167 }
Chris Lattner958858e2008-02-20 21:40:32 +0000168 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000169 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000170 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000171 case DeclSpec::TST_double:
172 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000173 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000174 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000175 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000176 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000177 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 case DeclSpec::TST_decimal32: // _Decimal32
179 case DeclSpec::TST_decimal64: // _Decimal64
180 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000181 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
182 Result = Context.IntTy;
183 isInvalid = true;
184 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000185 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 case DeclSpec::TST_enum:
187 case DeclSpec::TST_union:
188 case DeclSpec::TST_struct: {
189 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000190 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
192 DS.getTypeSpecSign() == 0 &&
193 "Can't handle qualifiers on typedef names yet!");
194 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000195 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner5153ee62009-04-25 08:47:54 +0000196
197 if (D->isInvalidDecl())
198 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000199 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000201 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
203 DS.getTypeSpecSign() == 0 &&
204 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000205 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000206
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000207 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump390b4cc2009-05-16 07:39:55 +0000208 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so we have
209 // this "hack" for now...
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000210 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
Steve Naroff14108da2009-07-10 23:34:53 +0000211 // FIXME: Remove ObjCQualifiedInterfaceType (by moving the list of
212 // protocols 'up' to ObjCInterfaceType).
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000213 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
214 (ObjCProtocolDecl**)PQ,
215 DS.getNumProtocolQualifiers());
Steve Naroff14108da2009-07-10 23:34:53 +0000216 else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000217 // id<protocol-list>
Steve Naroff14108da2009-07-10 23:34:53 +0000218 Result = Context.getObjCObjectPointerType(QualType(),
219 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
220 else if (Result->isObjCClassType()) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000221 if (DeclLoc.isInvalid())
222 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000223 // Class<protocol-list>
Chris Lattner3f84ad22009-04-22 05:27:59 +0000224 Diag(DeclLoc, diag::err_qualified_class_unsupported)
225 << DS.getSourceRange();
226 } else {
227 if (DeclLoc.isInvalid())
228 DeclLoc = DS.getSourceRange().getBegin();
229 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
230 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000231 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000232 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000233 }
Chris Lattnereaaebc72009-04-25 08:06:05 +0000234
235 // If this is a reference to an invalid typedef, propagate the invalidity.
236 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
237 if (TDT->getDecl()->isInvalidDecl())
238 isInvalid = true;
239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000241 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 }
Chris Lattner958858e2008-02-20 21:40:32 +0000243 case DeclSpec::TST_typeofType:
244 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
245 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000246 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000247 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000248 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000249 case DeclSpec::TST_typeofExpr: {
250 Expr *E = static_cast<Expr *>(DS.getTypeRep());
251 assert(E && "Didn't get an expression for typeof?");
252 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000253 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000254 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000255 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000256 case DeclSpec::TST_decltype: {
257 Expr *E = static_cast<Expr *>(DS.getTypeRep());
258 assert(E && "Didn't get an expression for decltype?");
259 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000260 Result = BuildDecltypeType(E);
261 if (Result.isNull()) {
262 Result = Context.IntTy;
263 isInvalid = true;
264 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000265 break;
266 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000267 case DeclSpec::TST_auto: {
268 // TypeQuals handled by caller.
269 Result = Context.UndeducedAutoTy;
270 break;
271 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000272
Douglas Gregor809070a2009-02-18 17:45:20 +0000273 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000274 Result = Context.IntTy;
275 isInvalid = true;
276 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 }
Chris Lattner958858e2008-02-20 21:40:32 +0000278
279 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000280 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
281 if (getLangOptions().Freestanding)
282 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000283 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000284 }
Chris Lattner958858e2008-02-20 21:40:32 +0000285
286 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
287 "FIXME: imaginary types not supported yet!");
288
Chris Lattner38d8b982008-02-20 22:04:11 +0000289 // See if there are any attributes on the declspec that apply to the type (as
290 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000291 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000292 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000293
Chris Lattner96b77fc2008-04-02 06:50:17 +0000294 // Apply const/volatile/restrict qualifiers to T.
295 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
296
297 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
298 // or incomplete types shall not be restrict-qualified." C++ also allows
299 // restrict-qualified references.
300 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000301 if (Result->isPointerType() || Result->isReferenceType()) {
302 QualType EltTy = Result->isPointerType() ?
303 Result->getAsPointerType()->getPointeeType() :
304 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000305
Douglas Gregorbad0e652009-03-24 20:32:41 +0000306 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000307 // incomplete type.
308 if (!EltTy->isIncompleteOrObjectType()) {
309 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000310 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000311 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000312 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
313 }
314 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000315 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000316 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000317 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000318 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000319 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000320 }
321
322 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
323 // of a function type includes any type qualifiers, the behavior is
324 // undefined."
325 if (Result->isFunctionType() && TypeQuals) {
326 // Get some location to point at, either the C or V location.
327 SourceLocation Loc;
328 if (TypeQuals & QualType::Const)
329 Loc = DS.getConstSpecLoc();
330 else {
331 assert((TypeQuals & QualType::Volatile) &&
332 "Has CV quals but not C or V?");
333 Loc = DS.getVolatileSpecLoc();
334 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000335 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000336 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000337 }
338
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000339 // C++ [dcl.ref]p1:
340 // Cv-qualified references are ill-formed except when the
341 // cv-qualifiers are introduced through the use of a typedef
342 // (7.1.3) or of a template type argument (14.3), in which
343 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000344 // FIXME: Shouldn't we be checking SCS_typedef here?
345 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000346 TypeQuals && Result->isReferenceType()) {
347 TypeQuals &= ~QualType::Const;
348 TypeQuals &= ~QualType::Volatile;
349 }
350
Chris Lattner96b77fc2008-04-02 06:50:17 +0000351 Result = Result.getQualifiedType(TypeQuals);
352 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000353 return Result;
354}
355
Douglas Gregorcd281c32009-02-28 00:25:32 +0000356static std::string getPrintableNameForEntity(DeclarationName Entity) {
357 if (Entity)
358 return Entity.getAsString();
359
360 return "type name";
361}
362
363/// \brief Build a pointer type.
364///
365/// \param T The type to which we'll be building a pointer.
366///
367/// \param Quals The cvr-qualifiers to be applied to the pointer type.
368///
369/// \param Loc The location of the entity whose type involves this
370/// pointer type or, if there is no such entity, the location of the
371/// type that will have pointer type.
372///
373/// \param Entity The name of the entity that involves the pointer
374/// type, if known.
375///
376/// \returns A suitable pointer type, if there are no
377/// errors. Otherwise, returns a NULL type.
378QualType Sema::BuildPointerType(QualType T, unsigned Quals,
379 SourceLocation Loc, DeclarationName Entity) {
380 if (T->isReferenceType()) {
381 // C++ 8.3.2p4: There shall be no ... pointers to references ...
382 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
383 << getPrintableNameForEntity(Entity);
384 return QualType();
385 }
386
387 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
388 // object or incomplete types shall not be restrict-qualified."
389 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
390 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
391 << T;
392 Quals &= ~QualType::Restrict;
393 }
394
395 // Build the pointer type.
396 return Context.getPointerType(T).getQualifiedType(Quals);
397}
398
399/// \brief Build a reference type.
400///
401/// \param T The type to which we'll be building a reference.
402///
403/// \param Quals The cvr-qualifiers to be applied to the reference type.
404///
405/// \param Loc The location of the entity whose type involves this
406/// reference type or, if there is no such entity, the location of the
407/// type that will have reference type.
408///
409/// \param Entity The name of the entity that involves the reference
410/// type, if known.
411///
412/// \returns A suitable reference type, if there are no
413/// errors. Otherwise, returns a NULL type.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000414QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000415 SourceLocation Loc, DeclarationName Entity) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000416 if (LValueRef) {
417 if (const RValueReferenceType *R = T->getAsRValueReferenceType()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000418 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
419 // reference to a type T, and attempt to create the type "lvalue
420 // reference to cv TD" creates the type "lvalue reference to T".
421 // We use the qualifiers (restrict or none) of the original reference,
422 // not the new ones. This is consistent with GCC.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000423 return Context.getLValueReferenceType(R->getPointeeType()).
Sebastian Redldfe292d2009-03-22 21:28:55 +0000424 getQualifiedType(T.getCVRQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000425 }
426 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000427 if (T->isReferenceType()) {
428 // C++ [dcl.ref]p4: There shall be no references to references.
429 //
430 // According to C++ DR 106, references to references are only
431 // diagnosed when they are written directly (e.g., "int & &"),
432 // but not when they happen via a typedef:
433 //
434 // typedef int& intref;
435 // typedef intref& intref2;
436 //
437 // Parser::ParserDeclaratorInternal diagnoses the case where
438 // references are written directly; here, we handle the
439 // collapsing of references-to-references as described in C++
440 // DR 106 and amended by C++ DR 540.
441 return T;
442 }
443
444 // C++ [dcl.ref]p1:
445 // A declarator that specifies the type “reference to cv void”
446 // is ill-formed.
447 if (T->isVoidType()) {
448 Diag(Loc, diag::err_reference_to_void);
449 return QualType();
450 }
451
452 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
453 // object or incomplete types shall not be restrict-qualified."
454 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
455 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
456 << T;
457 Quals &= ~QualType::Restrict;
458 }
459
460 // C++ [dcl.ref]p1:
461 // [...] Cv-qualified references are ill-formed except when the
462 // cv-qualifiers are introduced through the use of a typedef
463 // (7.1.3) or of a template type argument (14.3), in which case
464 // the cv-qualifiers are ignored.
465 //
466 // We diagnose extraneous cv-qualifiers for the non-typedef,
467 // non-template type argument case within the parser. Here, we just
468 // ignore any extraneous cv-qualifiers.
469 Quals &= ~QualType::Const;
470 Quals &= ~QualType::Volatile;
471
472 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000473 if (LValueRef)
474 return Context.getLValueReferenceType(T).getQualifiedType(Quals);
475 return Context.getRValueReferenceType(T).getQualifiedType(Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000476}
477
478/// \brief Build an array type.
479///
480/// \param T The type of each element in the array.
481///
482/// \param ASM C99 array size modifier (e.g., '*', 'static').
483///
484/// \param ArraySize Expression describing the size of the array.
485///
486/// \param Quals The cvr-qualifiers to be applied to the array's
487/// element type.
488///
489/// \param Loc The location of the entity whose type involves this
490/// array type or, if there is no such entity, the location of the
491/// type that will have array type.
492///
493/// \param Entity The name of the entity that involves the array
494/// type, if known.
495///
496/// \returns A suitable array type, if there are no errors. Otherwise,
497/// returns a NULL type.
498QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
499 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000500 SourceRange Brackets, DeclarationName Entity) {
501 SourceLocation Loc = Brackets.getBegin();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000502 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
503 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor86447ec2009-03-09 16:13:40 +0000504 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000505 diag::err_illegal_decl_array_incomplete_type))
506 return QualType();
507
508 if (T->isFunctionType()) {
509 Diag(Loc, diag::err_illegal_decl_array_of_functions)
510 << getPrintableNameForEntity(Entity);
511 return QualType();
512 }
513
514 // C++ 8.3.2p4: There shall be no ... arrays of references ...
515 if (T->isReferenceType()) {
516 Diag(Loc, diag::err_illegal_decl_array_of_references)
517 << getPrintableNameForEntity(Entity);
518 return QualType();
519 }
520
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000521 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
522 Diag(Loc, diag::err_illegal_decl_array_of_auto)
523 << getPrintableNameForEntity(Entity);
524 return QualType();
525 }
526
Douglas Gregorcd281c32009-02-28 00:25:32 +0000527 if (const RecordType *EltTy = T->getAsRecordType()) {
528 // If the element type is a struct or union that contains a variadic
529 // array, accept it as a GNU extension: C99 6.7.2.1p2.
530 if (EltTy->getDecl()->hasFlexibleArrayMember())
531 Diag(Loc, diag::ext_flexible_array_in_array) << T;
532 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000533 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
534 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000535 }
536
537 // C99 6.7.5.2p1: The size expression shall have integer type.
538 if (ArraySize && !ArraySize->isTypeDependent() &&
539 !ArraySize->getType()->isIntegerType()) {
540 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
541 << ArraySize->getType() << ArraySize->getSourceRange();
542 ArraySize->Destroy(Context);
543 return QualType();
544 }
545 llvm::APSInt ConstVal(32);
546 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000547 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000548 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000549 else
550 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000551 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000552 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000553 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
554 (!T->isDependentType() && !T->isConstantSizeType())) {
555 // Per C99, a variable array is an array with either a non-constant
556 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000557 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000558 } else {
559 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
560 // have a value greater than zero.
561 if (ConstVal.isSigned()) {
562 if (ConstVal.isNegative()) {
563 Diag(ArraySize->getLocStart(),
564 diag::err_typecheck_negative_array_size)
565 << ArraySize->getSourceRange();
566 return QualType();
567 } else if (ConstVal == 0) {
568 // GCC accepts zero sized static arrays.
569 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
570 << ArraySize->getSourceRange();
571 }
572 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000573 T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize,
574 ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000575 }
576 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
577 if (!getLangOptions().C99) {
578 if (ArraySize && !ArraySize->isTypeDependent() &&
579 !ArraySize->isValueDependent() &&
580 !ArraySize->isIntegerConstantExpr(Context))
581 Diag(Loc, diag::ext_vla);
582 else if (ASM != ArrayType::Normal || Quals != 0)
583 Diag(Loc, diag::ext_c99_array_usage);
584 }
585
586 return T;
587}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000588
589/// \brief Build an ext-vector type.
590///
591/// Run the required checks for the extended vector type.
592QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
593 SourceLocation AttrLoc) {
594
595 Expr *Arg = (Expr *)ArraySize.get();
596
597 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
598 // in conjunction with complex types (pointers, arrays, functions, etc.).
599 if (!T->isDependentType() &&
600 !T->isIntegerType() && !T->isRealFloatingType()) {
601 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
602 return QualType();
603 }
604
605 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
606 llvm::APSInt vecSize(32);
607 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
608 Diag(AttrLoc, diag::err_attribute_argument_not_int)
609 << "ext_vector_type" << Arg->getSourceRange();
610 return QualType();
611 }
612
613 // unlike gcc's vector_size attribute, the size is specified as the
614 // number of elements, not the number of bytes.
615 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
616
617 if (vectorSize == 0) {
618 Diag(AttrLoc, diag::err_attribute_zero_size)
619 << Arg->getSourceRange();
620 return QualType();
621 }
622
623 if (!T->isDependentType())
624 return Context.getExtVectorType(T, vectorSize);
625 }
626
627 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
628 AttrLoc);
629}
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630
Douglas Gregor724651c2009-02-28 01:04:19 +0000631/// \brief Build a function type.
632///
633/// This routine checks the function type according to C++ rules and
634/// under the assumption that the result type and parameter types have
635/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000636/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000637/// simpler form that is only suitable for this narrow use case.
638///
639/// \param T The return type of the function.
640///
641/// \param ParamTypes The parameter types of the function. This array
642/// will be modified to account for adjustments to the types of the
643/// function parameters.
644///
645/// \param NumParamTypes The number of parameter types in ParamTypes.
646///
647/// \param Variadic Whether this is a variadic function type.
648///
649/// \param Quals The cvr-qualifiers to be applied to the function type.
650///
651/// \param Loc The location of the entity whose type involves this
652/// function type or, if there is no such entity, the location of the
653/// type that will have function type.
654///
655/// \param Entity The name of the entity that involves the function
656/// type, if known.
657///
658/// \returns A suitable function type, if there are no
659/// errors. Otherwise, returns a NULL type.
660QualType Sema::BuildFunctionType(QualType T,
661 QualType *ParamTypes,
662 unsigned NumParamTypes,
663 bool Variadic, unsigned Quals,
664 SourceLocation Loc, DeclarationName Entity) {
665 if (T->isArrayType() || T->isFunctionType()) {
666 Diag(Loc, diag::err_func_returning_array_function) << T;
667 return QualType();
668 }
669
670 bool Invalid = false;
671 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000672 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
673 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000674 Diag(Loc, diag::err_param_with_void_type);
675 Invalid = true;
676 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000677
Douglas Gregor724651c2009-02-28 01:04:19 +0000678 ParamTypes[Idx] = ParamType;
679 }
680
681 if (Invalid)
682 return QualType();
683
684 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
685 Quals);
686}
Douglas Gregor949bf692009-06-09 22:17:39 +0000687
688/// \brief Build a member pointer type \c T Class::*.
689///
690/// \param T the type to which the member pointer refers.
691/// \param Class the class type into which the member pointer points.
692/// \param Quals Qualifiers applied to the member pointer type
693/// \param Loc the location where this type begins
694/// \param Entity the name of the entity that will have this member pointer type
695///
696/// \returns a member pointer type, if successful, or a NULL type if there was
697/// an error.
698QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
699 unsigned Quals, SourceLocation Loc,
700 DeclarationName Entity) {
701 // Verify that we're not building a pointer to pointer to function with
702 // exception specification.
703 if (CheckDistantExceptionSpec(T)) {
704 Diag(Loc, diag::err_distant_exception_spec);
705
706 // FIXME: If we're doing this as part of template instantiation,
707 // we should return immediately.
708
709 // Build the type anyway, but use the canonical type so that the
710 // exception specifiers are stripped off.
711 T = Context.getCanonicalType(T);
712 }
713
714 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
715 // with reference type, or "cv void."
716 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000717 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000718 << (Entity? Entity.getAsString() : "type name");
719 return QualType();
720 }
721
722 if (T->isVoidType()) {
723 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
724 << (Entity? Entity.getAsString() : "type name");
725 return QualType();
726 }
727
728 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
729 // object or incomplete types shall not be restrict-qualified."
730 if ((Quals & QualType::Restrict) && !T->isIncompleteOrObjectType()) {
731 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
732 << T;
733
734 // FIXME: If we're doing this as part of template instantiation,
735 // we should return immediately.
736 Quals &= ~QualType::Restrict;
737 }
738
739 if (!Class->isDependentType() && !Class->isRecordType()) {
740 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
741 return QualType();
742 }
743
744 return Context.getMemberPointerType(T, Class.getTypePtr())
745 .getQualifiedType(Quals);
746}
Anders Carlsson9a917e42009-06-12 22:56:54 +0000747
748/// \brief Build a block pointer type.
749///
750/// \param T The type to which we'll be building a block pointer.
751///
752/// \param Quals The cvr-qualifiers to be applied to the block pointer type.
753///
754/// \param Loc The location of the entity whose type involves this
755/// block pointer type or, if there is no such entity, the location of the
756/// type that will have block pointer type.
757///
758/// \param Entity The name of the entity that involves the block pointer
759/// type, if known.
760///
761/// \returns A suitable block pointer type, if there are no
762/// errors. Otherwise, returns a NULL type.
763QualType Sema::BuildBlockPointerType(QualType T, unsigned Quals,
764 SourceLocation Loc,
765 DeclarationName Entity) {
766 if (!T.getTypePtr()->isFunctionType()) {
767 Diag(Loc, diag::err_nonfunction_block_type);
768 return QualType();
769 }
770
771 return Context.getBlockPointerType(T).getQualifiedType(Quals);
772}
773
Mike Stump98eb8a72009-02-04 22:31:32 +0000774/// GetTypeForDeclarator - Convert the type for the specified
775/// declarator to Type instances. Skip the outermost Skip type
776/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000777///
778/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
779/// owns the declaration of a type (e.g., the definition of a struct
780/// type), then *OwnedDecl will receive the owned declaration.
781QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip,
782 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000783 bool OmittedReturnType = false;
784
785 if (D.getContext() == Declarator::BlockLiteralContext
786 && Skip == 0
787 && !D.getDeclSpec().hasTypeSpecifier()
788 && (D.getNumTypeObjects() == 0
789 || (D.getNumTypeObjects() == 1
790 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
791 OmittedReturnType = true;
792
Chris Lattnerb23deda2007-08-28 16:40:32 +0000793 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000794 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000795 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
796 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000797
798 // Determine the type of the declarator. Not all forms of declarator
799 // have a type.
800 QualType T;
801 switch (D.getKind()) {
802 case Declarator::DK_Abstract:
803 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000804 case Declarator::DK_Operator: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000805 const DeclSpec &DS = D.getDeclSpec();
806 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000807 // We default to a dependent type initially. Can be modified by
808 // the first return statement.
809 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000810 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000811 bool isInvalid = false;
812 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000813 if (isInvalid)
814 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000815 else if (OwnedDecl && DS.isTypeSpecOwned())
816 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000817 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000818 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000819 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000820
821 case Declarator::DK_Constructor:
822 case Declarator::DK_Destructor:
823 case Declarator::DK_Conversion:
824 // Constructors and destructors don't have return types. Use
825 // "void" instead. Conversion operators will check their return
826 // types separately.
827 T = Context.VoidTy;
828 break;
829 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000830
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000831 if (T == Context.UndeducedAutoTy) {
832 int Error = -1;
833
834 switch (D.getContext()) {
835 case Declarator::KNRTypeListContext:
836 assert(0 && "K&R type lists aren't allowed in C++");
837 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000838 case Declarator::PrototypeContext:
839 Error = 0; // Function prototype
840 break;
841 case Declarator::MemberContext:
842 switch (cast<TagDecl>(CurContext)->getTagKind()) {
843 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
844 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
845 case TagDecl::TK_union: Error = 2; /* Union member */ break;
846 case TagDecl::TK_class: Error = 3; /* Class member */ break;
847 }
848 break;
849 case Declarator::CXXCatchContext:
850 Error = 4; // Exception declaration
851 break;
852 case Declarator::TemplateParamContext:
853 Error = 5; // Template parameter
854 break;
855 case Declarator::BlockLiteralContext:
856 Error = 6; // Block literal
857 break;
858 case Declarator::FileContext:
859 case Declarator::BlockContext:
860 case Declarator::ForContext:
861 case Declarator::ConditionContext:
862 case Declarator::TypeNameContext:
863 break;
864 }
865
866 if (Error != -1) {
867 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
868 << Error;
869 T = Context.IntTy;
870 D.setInvalidType(true);
871 }
872 }
873
Douglas Gregorcd281c32009-02-28 00:25:32 +0000874 // The name we're declaring, if any.
875 DeclarationName Name;
876 if (D.getIdentifier())
877 Name = D.getIdentifier();
878
Mike Stump98eb8a72009-02-04 22:31:32 +0000879 // Walk the DeclTypeInfo, building the recursive type as we go.
880 // DeclTypeInfos are ordered from the identifier out, which is
881 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000882 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
883 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 switch (DeclType.Kind) {
885 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000886 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000887 // If blocks are disabled, emit an error.
888 if (!LangOpts.Blocks)
889 Diag(DeclType.Loc, diag::err_blocks_disable);
890
Anders Carlsson9a917e42009-06-12 22:56:54 +0000891 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
892 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000893 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000895 // Verify that we're not building a pointer to pointer to function with
896 // exception specification.
897 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
898 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
899 D.setInvalidType(true);
900 // Build the type anyway.
901 }
Steve Naroff14108da2009-07-10 23:34:53 +0000902 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
903 const ObjCInterfaceType *OIT = T->getAsObjCInterfaceType();
904 T = Context.getObjCObjectPointerType(T,
905 (ObjCProtocolDecl **)OIT->qual_begin(),
906 OIT->getNumProtocols());
907 break;
908 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000909 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000911 case DeclaratorChunk::Reference:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000912 // Verify that we're not building a reference to pointer to function with
913 // exception specification.
914 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
915 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
916 D.setInvalidType(true);
917 // Build the type anyway.
918 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000919 T = BuildReferenceType(T, DeclType.Ref.LValueRef,
920 DeclType.Ref.HasRestrict ? QualType::Restrict : 0,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000921 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 break;
923 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000924 // Verify that we're not building an array of pointers to function with
925 // exception specification.
926 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
927 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
928 D.setInvalidType(true);
929 // Build the type anyway.
930 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000931 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000932 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 ArrayType::ArraySizeModifier ASM;
934 if (ATI.isStar)
935 ASM = ArrayType::Star;
936 else if (ATI.hasStatic)
937 ASM = ArrayType::Static;
938 else
939 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000940 if (ASM == ArrayType::Star &&
941 D.getContext() != Declarator::PrototypeContext) {
942 // FIXME: This check isn't quite right: it allows star in prototypes
943 // for function definitions, and disallows some edge cases detailed
944 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
945 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
946 ASM = ArrayType::Normal;
947 D.setInvalidType(true);
948 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000949 T = BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals,
950 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 break;
952 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000953 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 // If the function declarator has a prototype (i.e. it is not () and
955 // does not have a K&R-style identifier list), then the arguments are part
956 // of the type, otherwise the argument list is ().
957 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +0000958
Chris Lattnercd881292007-12-19 05:31:29 +0000959 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000960 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000961 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000962 T = Context.IntTy;
963 D.setInvalidType(true);
964 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000965
Douglas Gregor402abb52009-05-28 23:31:59 +0000966 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
967 // C++ [dcl.fct]p6:
968 // Types shall not be defined in return or parameter types.
969 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
970 if (Tag->isDefinition())
971 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
972 << Context.getTypeDeclType(Tag);
973 }
974
Sebastian Redl3cc97262009-05-31 11:47:27 +0000975 // Exception specs are not allowed in typedefs. Complain, but add it
976 // anyway.
977 if (FTI.hasExceptionSpec &&
978 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
979 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
980
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000981 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000982 if (getLangOptions().CPlusPlus) {
983 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
984 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +0000985 llvm::SmallVector<QualType, 4> Exceptions;
986 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +0000987 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
988 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
989 // Check that the type is valid for an exception spec, and drop it
990 // if not.
991 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
992 Exceptions.push_back(ET);
993 }
Sebastian Redl465226e2009-05-27 22:11:52 +0000994 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
995 FTI.hasExceptionSpec,
996 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +0000997 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +0000998 } else if (FTI.isVariadic) {
999 // We allow a zero-parameter variadic function in C if the
1000 // function is marked with the "overloadable"
1001 // attribute. Scan for this attribute now.
1002 bool Overloadable = false;
1003 for (const AttributeList *Attrs = D.getAttributes();
1004 Attrs; Attrs = Attrs->getNext()) {
1005 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1006 Overloadable = true;
1007 break;
1008 }
1009 }
1010
1011 if (!Overloadable)
1012 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1013 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001014 } else {
1015 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001016 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001017 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001018 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001020 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 } else {
1022 // Otherwise, we have a function with an argument list that is
1023 // potentially variadic.
1024 llvm::SmallVector<QualType, 16> ArgTys;
1025
1026 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001027 ParmVarDecl *Param =
1028 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001029 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001030 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001031
1032 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001033 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001034
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 // Look for 'void'. void is allowed only as a single argument to a
1036 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001037 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001038 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 // If this is something like 'float(int, void)', reject it. 'void'
1040 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1041 // have arguments of incomplete type.
1042 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1043 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001044 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001045 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001046 } else if (FTI.ArgInfo[i].Ident) {
1047 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001049 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001050 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001051 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001052 } else {
1053 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +00001054 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001055 Diag(DeclType.Loc, diag::err_void_param_qualified);
1056
1057 // Do not add 'void' to the ArgTys list.
1058 break;
1059 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001060 } else if (!FTI.hasPrototype) {
1061 if (ArgTy->isPromotableIntegerType()) {
1062 ArgTy = Context.IntTy;
1063 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
1064 if (BTy->getKind() == BuiltinType::Float)
1065 ArgTy = Context.DoubleTy;
1066 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 }
1068
1069 ArgTys.push_back(ArgTy);
1070 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001071
1072 llvm::SmallVector<QualType, 4> Exceptions;
1073 Exceptions.reserve(FTI.NumExceptions);
Sebastian Redlef65f062009-05-29 18:02:33 +00001074 for(unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1075 QualType ET = QualType::getFromOpaquePtr(FTI.Exceptions[ei].Ty);
1076 // Check that the type is valid for an exception spec, and drop it if
1077 // not.
1078 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1079 Exceptions.push_back(ET);
1080 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001081
Jay Foadbeaaccd2009-05-21 09:52:38 +00001082 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001083 FTI.isVariadic, FTI.TypeQuals,
1084 FTI.hasExceptionSpec,
1085 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001086 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 }
1088 break;
1089 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001090 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001091 // Verify that we're not building a pointer to pointer to function with
1092 // exception specification.
1093 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1094 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1095 D.setInvalidType(true);
1096 // Build the type anyway.
1097 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001098 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001099 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001100 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
1101 NestedNameSpecifier *NNS
1102 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1103 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1104 ClsType = QualType(NNS->getAsType(), 0);
1105 } else if (CXXRecordDecl *RD
1106 = dyn_cast_or_null<CXXRecordDecl>(
1107 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001108 ClsType = Context.getTagDeclType(RD);
1109 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001110 Diag(DeclType.Mem.Scope().getBeginLoc(),
1111 diag::err_illegal_decl_mempointer_in_nonclass)
1112 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1113 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001114 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001115 }
1116
Douglas Gregor949bf692009-06-09 22:17:39 +00001117 if (!ClsType.isNull())
1118 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1119 DeclType.Loc, D.getIdentifier());
1120 if (T.isNull()) {
1121 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001122 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001123 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001124 break;
1125 }
1126
Douglas Gregorcd281c32009-02-28 00:25:32 +00001127 if (T.isNull()) {
1128 D.setInvalidType(true);
1129 T = Context.IntTy;
1130 }
1131
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001132 // See if there are any attributes on this declarator chunk.
1133 if (const AttributeList *AL = DeclType.getAttrs())
1134 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001136
1137 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001138 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
1139 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001140
1141 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1142 // for a nonstatic member function, the function type to which a pointer
1143 // to member refers, or the top-level function type of a function typedef
1144 // declaration.
1145 if (FnTy->getTypeQuals() != 0 &&
1146 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001147 ((D.getContext() != Declarator::MemberContext &&
1148 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregore4e5b052009-03-19 00:18:19 +00001149 !computeDeclContext(D.getCXXScopeSpec())->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001150 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001151 if (D.isFunctionDeclarator())
1152 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1153 else
1154 Diag(D.getIdentifierLoc(),
1155 diag::err_invalid_qualified_typedef_function_type_use);
1156
1157 // Strip the cv-quals from the type.
1158 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001159 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001160 }
1161 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001162
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001163 // If there were any type attributes applied to the decl itself (not the
1164 // type, apply the type attribute to the type!)
1165 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001166 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 return T;
1169}
1170
Sebastian Redlef65f062009-05-29 18:02:33 +00001171/// CheckSpecifiedExceptionType - Check if the given type is valid in an
1172/// exception specification. Incomplete types, or pointers to incomplete types
1173/// other than void are not allowed.
1174bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
1175 // FIXME: This may not correctly work with the fix for core issue 437,
1176 // where a class's own type is considered complete within its body.
1177
1178 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1179 // an incomplete type.
1180 if (T->isIncompleteType())
1181 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1182 << Range << T << /*direct*/0;
1183
1184 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1185 // an incomplete type a pointer or reference to an incomplete type, other
1186 // than (cv) void*.
Sebastian Redlef65f062009-05-29 18:02:33 +00001187 int kind;
1188 if (const PointerType* IT = T->getAsPointerType()) {
1189 T = IT->getPointeeType();
1190 kind = 1;
Sebastian Redlef65f062009-05-29 18:02:33 +00001191 } else if (const ReferenceType* IT = T->getAsReferenceType()) {
1192 T = IT->getPointeeType();
Sebastian Redl3cc97262009-05-31 11:47:27 +00001193 kind = 2;
Sebastian Redlef65f062009-05-29 18:02:33 +00001194 } else
1195 return false;
1196
1197 if (T->isIncompleteType() && !T->isVoidType())
1198 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1199 << Range << T << /*indirect*/kind;
1200
1201 return false;
1202}
1203
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001204/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
1205/// to member to a function with an exception specification. This means that
1206/// it is invalid to add another level of indirection.
1207bool Sema::CheckDistantExceptionSpec(QualType T) {
1208 if (const PointerType *PT = T->getAsPointerType())
1209 T = PT->getPointeeType();
1210 else if (const MemberPointerType *PT = T->getAsMemberPointerType())
1211 T = PT->getPointeeType();
1212 else
1213 return false;
1214
1215 const FunctionProtoType *FnT = T->getAsFunctionProtoType();
1216 if (!FnT)
1217 return false;
1218
1219 return FnT->hasExceptionSpec();
1220}
1221
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001222/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
1223/// exception specifications. Exception specifications are equivalent if
1224/// they allow exactly the same set of exception types. It does not matter how
1225/// that is achieved. See C++ [except.spec]p2.
1226bool Sema::CheckEquivalentExceptionSpec(
1227 const FunctionProtoType *Old, SourceLocation OldLoc,
1228 const FunctionProtoType *New, SourceLocation NewLoc) {
1229 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
1230 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
1231 if (OldAny && NewAny)
1232 return false;
1233 if (OldAny || NewAny) {
1234 Diag(NewLoc, diag::err_mismatched_exception_spec);
1235 Diag(OldLoc, diag::note_previous_declaration);
1236 return true;
1237 }
1238
1239 bool Success = true;
1240 // Both have a definite exception spec. Collect the first set, then compare
1241 // to the second.
1242 llvm::SmallPtrSet<const Type*, 8> Types;
1243 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
1244 E = Old->exception_end(); I != E; ++I)
1245 Types.insert(Context.getCanonicalType(*I).getTypePtr());
1246
1247 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
1248 E = New->exception_end(); I != E && Success; ++I)
1249 Success = Types.erase(Context.getCanonicalType(*I).getTypePtr());
1250
1251 Success = Success && Types.empty();
1252
1253 if (Success) {
1254 return false;
1255 }
1256 Diag(NewLoc, diag::err_mismatched_exception_spec);
1257 Diag(OldLoc, diag::note_previous_declaration);
1258 return true;
1259}
1260
Sebastian Redl23c7d062009-07-07 20:29:57 +00001261/// CheckExceptionSpecSubset - Check whether the second function type's
1262/// exception specification is a subset (or equivalent) of the first function
1263/// type. This is used by override and pointer assignment checks.
1264bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID,
1265 const FunctionProtoType *Superset, SourceLocation SuperLoc,
1266 const FunctionProtoType *Subset, SourceLocation SubLoc)
1267{
1268 // FIXME: As usual, we could be more specific in our error messages, but
1269 // that better waits until we've got types with source locations.
1270
1271 // If superset contains everything, we're done.
1272 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
1273 return false;
1274
1275 // It does not. If the subset contains everything, we've failed.
1276 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
1277 Diag(SubLoc, DiagID);
1278 Diag(SuperLoc, NoteID);
1279 return true;
1280 }
1281
1282 // Neither contains everything. Do a proper comparison.
1283 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
1284 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
1285 // Take one type from the subset.
1286 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
1287 bool SubIsPointer = false;
1288 if (const ReferenceType *RefTy = CanonicalSubT->getAsReferenceType())
1289 CanonicalSubT = RefTy->getPointeeType();
1290 if (const PointerType *PtrTy = CanonicalSubT->getAsPointerType()) {
1291 CanonicalSubT = PtrTy->getPointeeType();
1292 SubIsPointer = true;
1293 }
1294 bool SubIsClass = CanonicalSubT->isRecordType();
1295 CanonicalSubT.setCVRQualifiers(0);
1296
1297 BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
1298 /*DetectVirtual=*/false);
1299
1300 bool Contained = false;
1301 // Make sure it's in the superset.
1302 for (FunctionProtoType::exception_iterator SuperI =
1303 Superset->exception_begin(), SuperE = Superset->exception_end();
1304 SuperI != SuperE; ++SuperI) {
1305 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
1306 // SubT must be SuperT or derived from it, or pointer or reference to
1307 // such types.
1308 if (const ReferenceType *RefTy = CanonicalSuperT->getAsReferenceType())
1309 CanonicalSuperT = RefTy->getPointeeType();
1310 if (SubIsPointer) {
1311 if (const PointerType *PtrTy = CanonicalSuperT->getAsPointerType())
1312 CanonicalSuperT = PtrTy->getPointeeType();
1313 else {
1314 continue;
1315 }
1316 }
1317 CanonicalSuperT.setCVRQualifiers(0);
1318 // If the types are the same, move on to the next type in the subset.
1319 if (CanonicalSubT == CanonicalSuperT) {
1320 Contained = true;
1321 break;
1322 }
1323
1324 // Otherwise we need to check the inheritance.
1325 if (!SubIsClass || !CanonicalSuperT->isRecordType())
1326 continue;
1327
1328 Paths.clear();
1329 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
1330 continue;
1331
1332 if (Paths.isAmbiguous(CanonicalSuperT))
1333 continue;
1334
1335 // FIXME: Check base access. Don't forget to enable path recording.
1336
1337 Contained = true;
1338 break;
1339 }
1340 if (!Contained) {
1341 Diag(SubLoc, DiagID);
1342 Diag(SuperLoc, NoteID);
1343 return true;
1344 }
1345 }
1346 // We've run the gauntlet.
1347 return false;
1348}
1349
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001350/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001351/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001352QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1353 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001354 QualType T = MDecl->getResultType();
1355 llvm::SmallVector<QualType, 16> ArgTys;
1356
Fariborz Jahanian35600022007-11-09 17:18:29 +00001357 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001358 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001359 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001360 selfTy = Context.getPointerType(selfTy);
1361 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001362 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001363 ArgTys.push_back(Context.getObjCIdType());
1364 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +00001365
Chris Lattner89951a82009-02-20 18:43:26 +00001366 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1367 E = MDecl->param_end(); PI != E; ++PI) {
1368 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001369 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001370 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001371 ArgTys.push_back(ArgTy);
1372 }
1373 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001374 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001375 return T;
1376}
1377
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001378/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1379/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1380/// they point to and return true. If T1 and T2 aren't pointer types
1381/// or pointer-to-member types, or if they are not similar at this
1382/// level, returns false and leaves T1 and T2 unchanged. Top-level
1383/// qualifiers on T1 and T2 are ignored. This function will typically
1384/// be called in a loop that successively "unwraps" pointer and
1385/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001386bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +00001387 const PointerType *T1PtrType = T1->getAsPointerType(),
1388 *T2PtrType = T2->getAsPointerType();
1389 if (T1PtrType && T2PtrType) {
1390 T1 = T1PtrType->getPointeeType();
1391 T2 = T2PtrType->getPointeeType();
1392 return true;
1393 }
1394
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001395 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
1396 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001397 if (T1MPType && T2MPType &&
1398 Context.getCanonicalType(T1MPType->getClass()) ==
1399 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001400 T1 = T1MPType->getPointeeType();
1401 T2 = T2MPType->getPointeeType();
1402 return true;
1403 }
Douglas Gregor57373262008-10-22 14:17:15 +00001404 return false;
1405}
1406
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001407Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 // C99 6.7.6: Type names have no identifier. This is already validated by
1409 // the parser.
1410 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
1411
Douglas Gregor402abb52009-05-28 23:31:59 +00001412 TagDecl *OwnedTag = 0;
1413 QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001414 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001415 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001416
Douglas Gregor402abb52009-05-28 23:31:59 +00001417 if (getLangOptions().CPlusPlus) {
1418 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001419 CheckExtraCXXDefaultArguments(D);
1420
Douglas Gregor402abb52009-05-28 23:31:59 +00001421 // C++0x [dcl.type]p3:
1422 // A type-specifier-seq shall not define a class or enumeration
1423 // unless it appears in the type-id of an alias-declaration
1424 // (7.1.3).
1425 if (OwnedTag && OwnedTag->isDefinition())
1426 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1427 << Context.getTypeDeclType(OwnedTag);
1428 }
1429
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 return T.getAsOpaquePtr();
1431}
1432
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001433
1434
1435//===----------------------------------------------------------------------===//
1436// Type Attribute Processing
1437//===----------------------------------------------------------------------===//
1438
1439/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1440/// specified type. The attribute contains 1 argument, the id of the address
1441/// space for the type.
1442static void HandleAddressSpaceTypeAttribute(QualType &Type,
1443 const AttributeList &Attr, Sema &S){
1444 // If this type is already address space qualified, reject it.
1445 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1446 // for two or more different address spaces."
1447 if (Type.getAddressSpace()) {
1448 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1449 return;
1450 }
1451
1452 // Check the attribute arguments.
1453 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001454 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001455 return;
1456 }
1457 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1458 llvm::APSInt addrSpace(32);
1459 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001460 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1461 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001462 return;
1463 }
1464
1465 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001466 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001467}
1468
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001469/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1470/// specified type. The attribute contains 1 argument, weak or strong.
1471static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001472 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001473 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001474 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001475 return;
1476 }
1477
1478 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001479 if (!Attr.getParameterName()) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1481 << "objc_gc" << 1;
1482 return;
1483 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001484 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001485 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001486 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1487 return;
1488 }
1489 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001490 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001491 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001492 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001493 else {
1494 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1495 << "objc_gc" << Attr.getParameterName();
1496 return;
1497 }
1498
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001499 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001500}
1501
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001502void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001503 // Scan through and apply attributes to this type where it makes sense. Some
1504 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1505 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001506 // apply to the decl. Here we apply type attributes and ignore the rest.
1507 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001508 // If this is an attribute we can handle, do so now, otherwise, add it to
1509 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001510 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001511 default: break;
1512 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001513 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1514 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001515 case AttributeList::AT_objc_gc:
1516 HandleObjCGCTypeAttribute(Result, *AL, *this);
1517 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001518 }
Chris Lattner232e8822008-02-21 01:08:11 +00001519 }
Chris Lattner232e8822008-02-21 01:08:11 +00001520}
1521
Douglas Gregor86447ec2009-03-09 16:13:40 +00001522/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001523///
1524/// This routine checks whether the type @p T is complete in any
1525/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001526/// type, returns false. If @p T is a class template specialization,
1527/// this routine then attempts to perform class template
1528/// instantiation. If instantiation fails, or if @p T is incomplete
1529/// and cannot be completed, issues the diagnostic @p diag (giving it
1530/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001531///
1532/// @param Loc The location in the source that the incomplete type
1533/// diagnostic should refer to.
1534///
1535/// @param T The type that this routine is examining for completeness.
1536///
1537/// @param diag The diagnostic value (e.g.,
1538/// @c diag::err_typecheck_decl_incomplete_type) that will be used
1539/// for the error message if @p T is incomplete.
1540///
1541/// @param Range1 An optional range in the source code that will be a
1542/// part of the "incomplete type" error message.
1543///
1544/// @param Range2 An optional range in the source code that will be a
1545/// part of the "incomplete type" error message.
1546///
1547/// @param PrintType If non-NULL, the type that should be printed
1548/// instead of @p T. This parameter should be used when the type that
1549/// we're checking for incompleteness isn't the type that should be
1550/// displayed to the user, e.g., when T is a type and PrintType is a
1551/// pointer to T.
1552///
1553/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1554/// @c false otherwise.
Douglas Gregor86447ec2009-03-09 16:13:40 +00001555bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, unsigned diag,
Chris Lattner1efaa952009-04-24 00:30:45 +00001556 SourceRange Range1, SourceRange Range2,
1557 QualType PrintType) {
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001558 // FIXME: Add this assertion to help us flush out problems with
1559 // checking for dependent types and type-dependent expressions.
1560 //
1561 // assert(!T->isDependentType() &&
1562 // "Can't ask whether a dependent type is complete");
1563
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001564 // If we have a complete type, we're done.
1565 if (!T->isIncompleteType())
1566 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001567
Douglas Gregord475b8d2009-03-25 21:17:03 +00001568 // If we have a class template specialization or a class member of a
1569 // class template specialization, try to instantiate it.
1570 if (const RecordType *Record = T->getAsRecordType()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001571 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001572 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001573 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
1574 // Update the class template specialization's location to
1575 // refer to the point of instantiation.
1576 if (Loc.isValid())
1577 ClassTemplateSpec->setLocation(Loc);
1578 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
1579 /*ExplicitInstantiation=*/false);
1580 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001581 } else if (CXXRecordDecl *Rec
1582 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1583 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
1584 // Find the class template specialization that surrounds this
1585 // member class.
1586 ClassTemplateSpecializationDecl *Spec = 0;
1587 for (DeclContext *Parent = Rec->getDeclContext();
1588 Parent && !Spec; Parent = Parent->getParent())
1589 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
1590 assert(Spec && "Not a member of a class template specialization?");
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001591 return InstantiateClass(Loc, Rec, Pattern, Spec->getTemplateArgs(),
1592 /*ExplicitInstantiation=*/false);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001593 }
1594 }
1595 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001596
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001597 if (PrintType.isNull())
1598 PrintType = T;
1599
1600 // We have an incomplete type. Produce a diagnostic.
1601 Diag(Loc, diag) << PrintType << Range1 << Range2;
1602
1603 // If the type was a forward declaration of a class/struct/union
1604 // type, produce
1605 const TagType *Tag = 0;
1606 if (const RecordType *Record = T->getAsRecordType())
1607 Tag = Record;
1608 else if (const EnumType *Enum = T->getAsEnumType())
1609 Tag = Enum;
1610
1611 if (Tag && !Tag->getDecl()->isInvalidDecl())
1612 Diag(Tag->getDecl()->getLocation(),
1613 Tag->isBeingDefined() ? diag::note_type_being_defined
1614 : diag::note_forward_declaration)
1615 << QualType(Tag, 0);
1616
1617 return true;
1618}
Douglas Gregore6258932009-03-19 00:39:20 +00001619
1620/// \brief Retrieve a version of the type 'T' that is qualified by the
1621/// nested-name-specifier contained in SS.
1622QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1623 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1624 return T;
1625
Douglas Gregorab452ba2009-03-26 23:50:42 +00001626 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001627 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001628 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001629}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001630
1631QualType Sema::BuildTypeofExprType(Expr *E) {
1632 return Context.getTypeOfExprType(E);
1633}
1634
1635QualType Sema::BuildDecltypeType(Expr *E) {
1636 if (E->getType() == Context.OverloadTy) {
1637 Diag(E->getLocStart(),
1638 diag::err_cannot_determine_declared_type_of_overloaded_function);
1639 return QualType();
1640 }
1641 return Context.getDecltypeType(E);
1642}