2007-07-11 21:01:13 +04:00
|
|
|
//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for C++ expressions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-02-07 23:10:22 +03:00
|
|
|
#include "SemaInherit.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
2007-08-25 18:02:58 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-09-10 06:17:11 +04:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2008-10-07 03:16:35 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2008-12-03 23:26:15 +03:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-12-23 03:26:44 +03:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2008-11-19 22:09:45 +03:00
|
|
|
/// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function
|
2008-11-17 23:34:05 +03:00
|
|
|
/// name (e.g., operator void const *) as an expression. This is
|
|
|
|
/// very similar to ActOnIdentifierExpr, except that instead of
|
|
|
|
/// providing an identifier the parser provides the type of the
|
|
|
|
/// conversion function.
|
2009-01-18 21:53:16 +03:00
|
|
|
Sema::OwningExprResult
|
2008-11-19 22:09:45 +03:00
|
|
|
Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc,
|
|
|
|
TypeTy *Ty, bool HasTrailingLParen,
|
2009-02-03 23:19:35 +03:00
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
bool isAddressOfOperand) {
|
2008-11-17 23:34:05 +03:00
|
|
|
QualType ConvType = QualType::getFromOpaquePtr(Ty);
|
|
|
|
QualType ConvTypeCanon = Context.getCanonicalType(ConvType);
|
|
|
|
DeclarationName ConvName
|
|
|
|
= Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon);
|
2009-01-18 21:53:16 +03:00
|
|
|
return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen,
|
2009-02-04 18:01:18 +03:00
|
|
|
&SS, isAddressOfOperand);
|
2008-11-17 23:34:05 +03:00
|
|
|
}
|
2008-11-11 14:37:55 +03:00
|
|
|
|
2008-11-19 22:09:45 +03:00
|
|
|
/// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59526 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 17:39:36 +03:00
|
|
|
/// name (e.g., @c operator+ ) as an expression. This is very
|
|
|
|
/// similar to ActOnIdentifierExpr, except that instead of providing
|
|
|
|
/// an identifier the parser provides the kind of overloaded
|
|
|
|
/// operator that was parsed.
|
2009-01-18 21:53:16 +03:00
|
|
|
Sema::OwningExprResult
|
2008-11-19 22:09:45 +03:00
|
|
|
Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc,
|
|
|
|
OverloadedOperatorKind Op,
|
|
|
|
bool HasTrailingLParen,
|
2009-02-03 23:19:35 +03:00
|
|
|
const CXXScopeSpec &SS,
|
|
|
|
bool isAddressOfOperand) {
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59526 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 17:39:36 +03:00
|
|
|
DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op);
|
2009-02-03 23:19:35 +03:00
|
|
|
return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS,
|
2009-02-04 18:01:18 +03:00
|
|
|
isAddressOfOperand);
|
Extend DeclarationName to support C++ overloaded operators, e.g.,
operator+, directly, using the same mechanism as all other special
names.
Removed the "special" identifiers for the overloaded operators from
the identifier table and IdentifierInfo data structure. IdentifierInfo
is back to representing only real identifiers.
Added a new Action, ActOnOperatorFunctionIdExpr, that builds an
expression from an parsed operator-function-id (e.g., "operator
+"). ActOnIdentifierExpr used to do this job, but
operator-function-ids are no longer represented by IdentifierInfo's.
Extended Declarator to store overloaded operator names.
Sema::GetNameForDeclarator now knows how to turn the operator
name into a DeclarationName for the overloaded operator.
Except for (perhaps) consolidating the functionality of
ActOnIdentifier, ActOnOperatorFunctionIdExpr, and
ActOnConversionFunctionExpr into a common routine that builds an
appropriate DeclRefExpr by looking up a DeclarationName, all of the
work on normalizing declaration names should be complete with this
commit.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59526 91177308-0d34-0410-b5e6-96231b3b80d8
2008-11-18 17:39:36 +03:00
|
|
|
}
|
|
|
|
|
2008-11-11 14:37:55 +03:00
|
|
|
/// ActOnCXXTypeidOfType - Parse typeid( type-id ).
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-11-11 14:37:55 +03:00
|
|
|
Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc,
|
|
|
|
bool isType, void *TyOrExpr, SourceLocation RParenLoc) {
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63354 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-30 04:04:22 +03:00
|
|
|
NamespaceDecl *StdNs = GetStdNamespace();
|
2008-11-20 08:51:55 +03:00
|
|
|
if (!StdNs)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
|
2008-11-20 08:51:55 +03:00
|
|
|
|
|
|
|
IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info");
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63354 91177308-0d34-0410-b5e6-96231b3b80d8
2009-01-30 04:04:22 +03:00
|
|
|
Decl *TypeInfoDecl = LookupQualifiedName(StdNs, TypeInfoII, LookupTagName);
|
2008-11-11 14:37:55 +03:00
|
|
|
RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl);
|
2008-11-20 08:51:55 +03:00
|
|
|
if (!TypeInfoRecordDecl)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid));
|
2008-11-11 14:37:55 +03:00
|
|
|
|
|
|
|
QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl);
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr,
|
|
|
|
TypeInfoType.withConst(),
|
|
|
|
SourceRange(OpLoc, RParenLoc)));
|
2008-11-11 14:37:55 +03:00
|
|
|
}
|
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
/// ActOnCXXBoolLiteral - Parse {true,false} literals.
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2007-09-16 18:56:35 +04:00
|
|
|
Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
|
2008-10-24 19:36:09 +04:00
|
|
|
assert((Kind == tok::kw_true || Kind == tok::kw_false) &&
|
2007-07-11 21:01:13 +04:00
|
|
|
"Unknown C++ Boolean value!");
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true,
|
|
|
|
Context.BoolTy, OpLoc));
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-02-26 03:51:44 +03:00
|
|
|
|
|
|
|
/// ActOnCXXThrow - Parse throw expressions.
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
|
|
|
Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) {
|
|
|
|
return Owned(new (Context) CXXThrowExpr((Expr*)E.release(), Context.VoidTy,
|
|
|
|
OpLoc));
|
2008-02-26 03:51:44 +03:00
|
|
|
}
|
2008-07-01 14:37:29 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) {
|
2008-07-01 14:37:29 +04:00
|
|
|
/// C++ 9.3.2: In the body of a non-static member function, the keyword this
|
|
|
|
/// is a non-lvalue expression whose value is the address of the object for
|
|
|
|
/// which the function is called.
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
if (!isa<FunctionDecl>(CurContext))
|
|
|
|
return ExprError(Diag(ThisLoc, diag::err_invalid_this_use));
|
2008-07-01 14:37:29 +04:00
|
|
|
|
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext))
|
|
|
|
if (MD->isInstance())
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXThisExpr(ThisLoc,
|
|
|
|
MD->getThisType(Context)));
|
2008-07-01 14:37:29 +04:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(ThisLoc, diag::err_invalid_this_use));
|
2008-07-01 14:37:29 +04:00
|
|
|
}
|
2008-08-22 19:38:55 +04:00
|
|
|
|
|
|
|
/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
|
|
|
|
/// Can be interpreted either as function-style casting ("int(x)")
|
|
|
|
/// or class type construction ("ClassType(x,y,z)")
|
|
|
|
/// or creation of a value-initialized type ("int()").
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-08-22 19:38:55 +04:00
|
|
|
Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
|
|
|
|
SourceLocation LParenLoc,
|
2009-03-15 20:47:39 +03:00
|
|
|
MultiExprArg exprs,
|
2008-08-22 19:38:55 +04:00
|
|
|
SourceLocation *CommaLocs,
|
|
|
|
SourceLocation RParenLoc) {
|
|
|
|
assert(TypeRep && "Missing type!");
|
|
|
|
QualType Ty = QualType::getFromOpaquePtr(TypeRep);
|
2009-03-15 20:47:39 +03:00
|
|
|
unsigned NumExprs = exprs.size();
|
|
|
|
Expr **Exprs = (Expr**)exprs.get();
|
2008-08-22 19:38:55 +04:00
|
|
|
SourceLocation TyBeginLoc = TypeRange.getBegin();
|
|
|
|
SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
if (Ty->isDependentType() ||
|
2009-03-14 00:01:28 +03:00
|
|
|
CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) {
|
2009-03-15 20:47:39 +03:00
|
|
|
exprs.release();
|
|
|
|
return Owned(new (Context) CXXTemporaryObjectExpr(0, Ty, TyBeginLoc,
|
|
|
|
Exprs, NumExprs,
|
|
|
|
RParenLoc));
|
2009-03-14 00:01:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
// C++ [expr.type.conv]p1:
|
2008-08-22 19:38:55 +04:00
|
|
|
// If the expression list is a single expression, the type conversion
|
|
|
|
// expression is equivalent (in definedness, and if defined in meaning) to the
|
|
|
|
// corresponding cast expression.
|
|
|
|
//
|
|
|
|
if (NumExprs == 1) {
|
|
|
|
if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
|
|
|
exprs.release();
|
|
|
|
return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(),
|
|
|
|
Ty, TyBeginLoc, Exprs[0],
|
|
|
|
RParenLoc));
|
2008-08-22 19:38:55 +04:00
|
|
|
}
|
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
if (const RecordType *RT = Ty->getAsRecordType()) {
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
|
2009-03-15 20:47:39 +03:00
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
if (NumExprs > 1 || Record->hasUserDeclaredConstructor()) {
|
|
|
|
CXXConstructorDecl *Constructor
|
|
|
|
= PerformInitializationByConstructor(Ty, Exprs, NumExprs,
|
|
|
|
TypeRange.getBegin(),
|
|
|
|
SourceRange(TypeRange.getBegin(),
|
|
|
|
RParenLoc),
|
|
|
|
DeclarationName(),
|
|
|
|
IK_Direct);
|
2009-03-15 20:47:39 +03:00
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
if (!Constructor)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2009-01-16 21:33:17 +03:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
exprs.release();
|
|
|
|
return Owned(new (Context) CXXTemporaryObjectExpr(Constructor, Ty,
|
|
|
|
TyBeginLoc, Exprs,
|
|
|
|
NumExprs, RParenLoc));
|
2009-01-16 21:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall through to value-initialize an object of class type that
|
|
|
|
// doesn't have a user-declared default constructor.
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [expr.type.conv]p1:
|
2008-08-22 19:38:55 +04:00
|
|
|
// If the expression list specifies more than a single value, the type shall
|
|
|
|
// be a class with a suitably declared constructor.
|
|
|
|
//
|
|
|
|
if (NumExprs > 1)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(CommaLocs[0],
|
|
|
|
diag::err_builtin_func_cast_more_than_one_arg)
|
|
|
|
<< FullRange);
|
2008-08-22 19:38:55 +04:00
|
|
|
|
|
|
|
assert(NumExprs == 0 && "Expected 0 expressions");
|
|
|
|
|
2009-01-16 21:33:17 +03:00
|
|
|
// C++ [expr.type.conv]p2:
|
2008-08-22 19:38:55 +04:00
|
|
|
// The expression T(), where T is a simple-type-specifier for a non-array
|
|
|
|
// complete object type or the (possibly cv-qualified) void type, creates an
|
|
|
|
// rvalue of the specified type, which is value-initialized.
|
|
|
|
//
|
|
|
|
if (Ty->isArrayType())
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(TyBeginLoc,
|
|
|
|
diag::err_value_init_for_array_type) << FullRange);
|
2009-01-19 22:26:10 +03:00
|
|
|
if (!Ty->isDependentType() && !Ty->isVoidType() &&
|
2009-03-15 20:47:39 +03:00
|
|
|
RequireCompleteType(TyBeginLoc, Ty,
|
|
|
|
diag::err_invalid_incomplete_type_use, FullRange))
|
|
|
|
return ExprError();
|
2008-08-22 19:38:55 +04:00
|
|
|
|
2009-03-24 04:19:16 +03:00
|
|
|
if (RequireNonAbstractType(TyBeginLoc, Ty,
|
|
|
|
diag::err_allocation_of_abstract_type))
|
2009-03-23 22:10:31 +03:00
|
|
|
return ExprError();
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
exprs.release();
|
|
|
|
return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc));
|
2008-08-22 19:38:55 +04:00
|
|
|
}
|
2008-09-10 06:17:11 +04:00
|
|
|
|
|
|
|
|
2008-11-21 22:14:01 +03:00
|
|
|
/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.:
|
|
|
|
/// @code new (memory) int[size][4] @endcode
|
|
|
|
/// or
|
|
|
|
/// @code ::new Foo(23, "hello") @endcode
|
|
|
|
/// For the interpretation of this heap of arguments, consult the base version.
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-11-21 22:14:01 +03:00
|
|
|
Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal,
|
2009-03-15 20:47:39 +03:00
|
|
|
SourceLocation PlacementLParen, MultiExprArg PlacementArgs,
|
2008-11-21 22:14:01 +03:00
|
|
|
SourceLocation PlacementRParen, bool ParenTypeId,
|
2008-12-02 17:43:59 +03:00
|
|
|
Declarator &D, SourceLocation ConstructorLParen,
|
2009-03-15 20:47:39 +03:00
|
|
|
MultiExprArg ConstructorArgs,
|
2008-11-21 22:14:01 +03:00
|
|
|
SourceLocation ConstructorRParen)
|
|
|
|
{
|
2008-12-02 17:43:59 +03:00
|
|
|
Expr *ArraySize = 0;
|
|
|
|
unsigned Skip = 0;
|
|
|
|
// If the specified type is an array, unwrap it and save the expression.
|
|
|
|
if (D.getNumTypeObjects() > 0 &&
|
|
|
|
D.getTypeObject(0).Kind == DeclaratorChunk::Array) {
|
|
|
|
DeclaratorChunk &Chunk = D.getTypeObject(0);
|
|
|
|
if (Chunk.Arr.hasStatic)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new)
|
|
|
|
<< D.getSourceRange());
|
2008-12-02 17:43:59 +03:00
|
|
|
if (!Chunk.Arr.NumElts)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size)
|
|
|
|
<< D.getSourceRange());
|
2008-12-02 17:43:59 +03:00
|
|
|
ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts);
|
|
|
|
Skip = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip);
|
|
|
|
if (D.getInvalidType())
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-12-02 17:43:59 +03:00
|
|
|
|
|
|
|
if (CheckAllocatedType(AllocType, D))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-02-26 17:39:58 +03:00
|
|
|
QualType ResultType = AllocType->isDependentType()
|
|
|
|
? Context.DependentTy
|
|
|
|
: Context.getPointerType(AllocType);
|
2008-11-21 22:14:01 +03:00
|
|
|
|
|
|
|
// That every array dimension except the first is constant was already
|
|
|
|
// checked by the type check above.
|
2008-12-02 17:43:59 +03:00
|
|
|
|
2008-11-21 22:14:01 +03:00
|
|
|
// C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
|
|
|
|
// or enumeration type with a non-negative value."
|
2009-02-26 17:39:58 +03:00
|
|
|
if (ArraySize && !ArraySize->isTypeDependent()) {
|
2008-12-02 17:43:59 +03:00
|
|
|
QualType SizeType = ArraySize->getType();
|
|
|
|
if (!SizeType->isIntegralType() && !SizeType->isEnumeralType())
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(ArraySize->getSourceRange().getBegin(),
|
|
|
|
diag::err_array_size_not_integral)
|
|
|
|
<< SizeType << ArraySize->getSourceRange());
|
2008-12-02 17:43:59 +03:00
|
|
|
// Let's see if this is a constant < 0. If so, we reject it out of hand.
|
|
|
|
// We don't care about special rules, so we tell the machinery it's not
|
|
|
|
// evaluated - it gives us a result in more cases.
|
2009-02-26 17:39:58 +03:00
|
|
|
if (!ArraySize->isValueDependent()) {
|
|
|
|
llvm::APSInt Value;
|
|
|
|
if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) {
|
|
|
|
if (Value < llvm::APSInt(
|
|
|
|
llvm::APInt::getNullValue(Value.getBitWidth()), false))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(ArraySize->getSourceRange().getBegin(),
|
|
|
|
diag::err_typecheck_negative_array_size)
|
|
|
|
<< ArraySize->getSourceRange());
|
2009-02-26 17:39:58 +03:00
|
|
|
}
|
2008-12-02 17:43:59 +03:00
|
|
|
}
|
|
|
|
}
|
2008-11-21 22:14:01 +03:00
|
|
|
|
|
|
|
FunctionDecl *OperatorNew = 0;
|
|
|
|
FunctionDecl *OperatorDelete = 0;
|
2009-03-15 20:47:39 +03:00
|
|
|
Expr **PlaceArgs = (Expr**)PlacementArgs.get();
|
|
|
|
unsigned NumPlaceArgs = PlacementArgs.size();
|
2009-02-26 17:39:58 +03:00
|
|
|
if (!AllocType->isDependentType() &&
|
|
|
|
!Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) &&
|
|
|
|
FindAllocationFunctions(StartLoc,
|
2009-02-09 21:24:27 +03:00
|
|
|
SourceRange(PlacementLParen, PlacementRParen),
|
|
|
|
UseGlobal, AllocType, ArraySize, PlaceArgs,
|
|
|
|
NumPlaceArgs, OperatorNew, OperatorDelete))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
|
|
|
|
bool Init = ConstructorLParen.isValid();
|
|
|
|
// --- Choosing a constructor ---
|
|
|
|
// C++ 5.3.4p15
|
|
|
|
// 1) If T is a POD and there's no initializer (ConstructorLParen is invalid)
|
|
|
|
// the object is not initialized. If the object, or any part of it, is
|
|
|
|
// const-qualified, it's an error.
|
|
|
|
// 2) If T is a POD and there's an empty initializer, the object is value-
|
|
|
|
// initialized.
|
|
|
|
// 3) If T is a POD and there's one initializer argument, the object is copy-
|
|
|
|
// constructed.
|
|
|
|
// 4) If T is a POD and there's more initializer arguments, it's an error.
|
|
|
|
// 5) If T is not a POD, the initializer arguments are used as constructor
|
|
|
|
// arguments.
|
|
|
|
//
|
|
|
|
// Or by the C++0x formulation:
|
|
|
|
// 1) If there's no initializer, the object is default-initialized according
|
|
|
|
// to C++0x rules.
|
|
|
|
// 2) Otherwise, the object is direct-initialized.
|
|
|
|
CXXConstructorDecl *Constructor = 0;
|
2009-03-15 20:47:39 +03:00
|
|
|
Expr **ConsArgs = (Expr**)ConstructorArgs.get();
|
|
|
|
unsigned NumConsArgs = ConstructorArgs.size();
|
2009-02-26 17:39:58 +03:00
|
|
|
if (AllocType->isDependentType()) {
|
|
|
|
// Skip all the checks.
|
|
|
|
}
|
2009-02-09 21:24:27 +03:00
|
|
|
// FIXME: Should check for primitive/aggregate here, not record.
|
2009-02-26 17:39:58 +03:00
|
|
|
else if (const RecordType *RT = AllocType->getAsRecordType()) {
|
2008-11-21 22:14:01 +03:00
|
|
|
// FIXME: This is incorrect for when there is an empty initializer and
|
|
|
|
// no user-defined constructor. Must zero-initialize, not default-construct.
|
|
|
|
Constructor = PerformInitializationByConstructor(
|
2008-12-02 17:43:59 +03:00
|
|
|
AllocType, ConsArgs, NumConsArgs,
|
2009-02-09 21:24:27 +03:00
|
|
|
D.getSourceRange().getBegin(),
|
|
|
|
SourceRange(D.getSourceRange().getBegin(),
|
2008-12-02 17:43:59 +03:00
|
|
|
ConstructorRParen),
|
2008-11-24 08:29:24 +03:00
|
|
|
RT->getDecl()->getDeclName(),
|
2008-11-21 22:14:01 +03:00
|
|
|
NumConsArgs != 0 ? IK_Direct : IK_Default);
|
|
|
|
if (!Constructor)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
} else {
|
|
|
|
if (!Init) {
|
|
|
|
// FIXME: Check that no subpart is const.
|
2009-03-15 20:47:39 +03:00
|
|
|
if (AllocType.isConstQualified())
|
|
|
|
return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const)
|
|
|
|
<< D.getSourceRange());
|
2008-11-21 22:14:01 +03:00
|
|
|
} else if (NumConsArgs == 0) {
|
|
|
|
// Object is value-initialized. Do nothing.
|
|
|
|
} else if (NumConsArgs == 1) {
|
|
|
|
// Object is direct-initialized.
|
2008-11-24 08:29:24 +03:00
|
|
|
// FIXME: WHAT DeclarationName do we pass in here?
|
2008-12-02 17:43:59 +03:00
|
|
|
if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc,
|
2009-01-14 18:45:31 +03:00
|
|
|
DeclarationName() /*AllocType.getAsString()*/,
|
|
|
|
/*DirectInit=*/true))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
2008-11-21 22:14:01 +03:00
|
|
|
} else {
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(StartLoc,
|
|
|
|
diag::err_builtin_direct_init_more_than_one_arg)
|
|
|
|
<< SourceRange(ConstructorLParen, ConstructorRParen));
|
2008-11-21 22:14:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
PlacementArgs.release();
|
|
|
|
ConstructorArgs.release();
|
|
|
|
return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs,
|
2009-02-07 04:47:29 +03:00
|
|
|
NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init,
|
2008-11-21 22:14:01 +03:00
|
|
|
ConsArgs, NumConsArgs, OperatorDelete, ResultType,
|
2009-03-15 20:47:39 +03:00
|
|
|
StartLoc, Init ? ConstructorRParen : SourceLocation()));
|
2008-11-21 22:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckAllocatedType - Checks that a type is suitable as the allocated type
|
|
|
|
/// in a new-expression.
|
|
|
|
/// dimension off and stores the size expression in ArraySize.
|
2008-12-02 17:43:59 +03:00
|
|
|
bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D)
|
2008-11-21 22:14:01 +03:00
|
|
|
{
|
|
|
|
// C++ 5.3.4p1: "[The] type shall be a complete object type, but not an
|
|
|
|
// abstract class type or array thereof.
|
2009-03-24 22:52:54 +03:00
|
|
|
if (AllocType->isFunctionType())
|
|
|
|
return Diag(D.getSourceRange().getBegin(), diag::err_bad_new_type)
|
|
|
|
<< AllocType << 0 << D.getSourceRange();
|
|
|
|
else if (AllocType->isReferenceType())
|
|
|
|
return Diag(D.getSourceRange().getBegin(), diag::err_bad_new_type)
|
|
|
|
<< AllocType << 1 << D.getSourceRange();
|
|
|
|
else if (!AllocType->isDependentType() &&
|
|
|
|
RequireCompleteType(D.getSourceRange().getBegin(), AllocType,
|
|
|
|
diag::err_new_incomplete_type,
|
|
|
|
D.getSourceRange()))
|
|
|
|
return true;
|
|
|
|
else if (RequireNonAbstractType(D.getSourceRange().getBegin(), AllocType,
|
|
|
|
diag::err_allocation_of_abstract_type))
|
2008-11-21 22:14:01 +03:00
|
|
|
return true;
|
|
|
|
|
2008-12-02 17:43:59 +03:00
|
|
|
// Every dimension shall be of constant size.
|
|
|
|
unsigned i = 1;
|
2008-11-21 22:14:01 +03:00
|
|
|
while (const ArrayType *Array = Context.getAsArrayType(AllocType)) {
|
|
|
|
if (!Array->isConstantArrayType()) {
|
2008-12-02 17:43:59 +03:00
|
|
|
Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst)
|
|
|
|
<< static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange();
|
2008-11-21 22:14:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
AllocType = Array->getElementType();
|
2008-12-02 17:43:59 +03:00
|
|
|
++i;
|
2008-11-21 22:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-03 23:26:15 +03:00
|
|
|
/// FindAllocationFunctions - Finds the overloads of operator new and delete
|
|
|
|
/// that are appropriate for the allocation.
|
2009-02-09 21:24:27 +03:00
|
|
|
bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
|
|
|
|
bool UseGlobal, QualType AllocType,
|
|
|
|
bool IsArray, Expr **PlaceArgs,
|
|
|
|
unsigned NumPlaceArgs,
|
2008-12-03 23:26:15 +03:00
|
|
|
FunctionDecl *&OperatorNew,
|
|
|
|
FunctionDecl *&OperatorDelete)
|
|
|
|
{
|
|
|
|
// --- Choosing an allocation function ---
|
|
|
|
// C++ 5.3.4p8 - 14 & 18
|
|
|
|
// 1) If UseGlobal is true, only look in the global scope. Else, also look
|
|
|
|
// in the scope of the allocated class.
|
|
|
|
// 2) If an array size is given, look for operator new[], else look for
|
|
|
|
// operator new.
|
|
|
|
// 3) The first argument is always size_t. Append the arguments from the
|
|
|
|
// placement form.
|
|
|
|
// FIXME: Also find the appropriate delete operator.
|
|
|
|
|
|
|
|
llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs);
|
|
|
|
// We don't care about the actual value of this argument.
|
|
|
|
// FIXME: Should the Sema create the expression and embed it in the syntax
|
|
|
|
// tree? Or should the consumer just recalculate the value?
|
2009-02-07 04:47:29 +03:00
|
|
|
AllocArgs[0] = new (Context) IntegerLiteral(llvm::APInt::getNullValue(
|
2008-12-03 23:26:15 +03:00
|
|
|
Context.Target.getPointerWidth(0)),
|
|
|
|
Context.getSizeType(),
|
|
|
|
SourceLocation());
|
|
|
|
std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1);
|
|
|
|
|
|
|
|
DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName(
|
|
|
|
IsArray ? OO_Array_New : OO_New);
|
|
|
|
if (AllocType->isRecordType() && !UseGlobal) {
|
2009-02-28 04:32:25 +03:00
|
|
|
CXXRecordDecl *Record
|
|
|
|
= cast<CXXRecordDecl>(AllocType->getAsRecordType()->getDecl());
|
2008-12-05 01:20:51 +03:00
|
|
|
// FIXME: We fail to find inherited overloads.
|
2009-02-09 21:24:27 +03:00
|
|
|
if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
|
2008-12-05 01:20:51 +03:00
|
|
|
AllocArgs.size(), Record, /*AllowMissing=*/true,
|
|
|
|
OperatorNew))
|
2008-12-03 23:26:15 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!OperatorNew) {
|
|
|
|
// Didn't find a member overload. Look for a global one.
|
|
|
|
DeclareGlobalNewDelete();
|
2008-12-05 01:20:51 +03:00
|
|
|
DeclContext *TUDecl = Context.getTranslationUnitDecl();
|
2009-02-09 21:24:27 +03:00
|
|
|
if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0],
|
2008-12-05 01:20:51 +03:00
|
|
|
AllocArgs.size(), TUDecl, /*AllowMissing=*/false,
|
|
|
|
OperatorNew))
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
// FIXME: This is leaked on error. But so much is currently in Sema that it's
|
|
|
|
// easier to clean it in one go.
|
|
|
|
AllocArgs[0]->Destroy(Context);
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
/// FindAllocationOverload - Find an fitting overload for the allocation
|
|
|
|
/// function in the specified scope.
|
2009-02-09 21:24:27 +03:00
|
|
|
bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range,
|
|
|
|
DeclarationName Name, Expr** Args,
|
|
|
|
unsigned NumArgs, DeclContext *Ctx,
|
|
|
|
bool AllowMissing, FunctionDecl *&Operator)
|
2008-12-05 01:20:51 +03:00
|
|
|
{
|
2008-12-23 03:26:44 +03:00
|
|
|
DeclContext::lookup_iterator Alloc, AllocEnd;
|
2009-01-08 20:28:14 +03:00
|
|
|
llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name);
|
2008-12-23 03:26:44 +03:00
|
|
|
if (Alloc == AllocEnd) {
|
2008-12-05 01:20:51 +03:00
|
|
|
if (AllowMissing)
|
|
|
|
return false;
|
|
|
|
return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
|
2009-02-17 10:29:20 +03:00
|
|
|
<< Name << Range;
|
2008-12-05 01:20:51 +03:00
|
|
|
}
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
OverloadCandidateSet Candidates;
|
2008-12-23 03:26:44 +03:00
|
|
|
for (; Alloc != AllocEnd; ++Alloc) {
|
|
|
|
// Even member operator new/delete are implicitly treated as
|
|
|
|
// static, so don't use AddMemberCandidate.
|
|
|
|
if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc))
|
|
|
|
AddOverloadCandidate(Fn, Args, NumArgs, Candidates,
|
|
|
|
/*SuppressUserConversions=*/false);
|
2008-12-03 23:26:15 +03:00
|
|
|
}
|
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
// Do the resolution.
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
switch(BestViableFunction(Candidates, Best)) {
|
|
|
|
case OR_Success: {
|
|
|
|
// Got one!
|
|
|
|
FunctionDecl *FnDecl = Best->Function;
|
|
|
|
// The first argument is size_t, and the first parameter must be size_t,
|
|
|
|
// too. This is checked on declaration and can be assumed. (It can't be
|
|
|
|
// asserted on, though, since invalid decls are left in there.)
|
|
|
|
for (unsigned i = 1; i < NumArgs; ++i) {
|
|
|
|
// FIXME: Passing word to diagnostic.
|
|
|
|
if (PerformCopyInitialization(Args[i-1],
|
|
|
|
FnDecl->getParamDecl(i)->getType(),
|
|
|
|
"passing"))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Operator = FnDecl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OR_No_Viable_Function:
|
|
|
|
if (AllowMissing)
|
|
|
|
return false;
|
|
|
|
Diag(StartLoc, diag::err_ovl_no_viable_function_in_call)
|
2009-02-17 10:29:20 +03:00
|
|
|
<< Name << Range;
|
2008-12-05 01:20:51 +03:00
|
|
|
PrintOverloadCandidates(Candidates, /*OnlyViable=*/false);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case OR_Ambiguous:
|
|
|
|
Diag(StartLoc, diag::err_ovl_ambiguous_call)
|
2009-02-09 21:24:27 +03:00
|
|
|
<< Name << Range;
|
2008-12-05 01:20:51 +03:00
|
|
|
PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
|
|
|
|
return true;
|
2009-02-19 00:56:37 +03:00
|
|
|
|
|
|
|
case OR_Deleted:
|
|
|
|
Diag(StartLoc, diag::err_ovl_deleted_call)
|
|
|
|
<< Best->Function->isDeleted()
|
|
|
|
<< Name << Range;
|
|
|
|
PrintOverloadCandidates(Candidates, /*OnlyViable=*/true);
|
|
|
|
return true;
|
2008-12-05 01:20:51 +03:00
|
|
|
}
|
|
|
|
assert(false && "Unreachable, bad result from BestViableFunction");
|
|
|
|
return true;
|
2008-12-03 23:26:15 +03:00
|
|
|
}
|
|
|
|
|
2008-12-05 01:20:51 +03:00
|
|
|
|
2008-12-03 23:26:15 +03:00
|
|
|
/// DeclareGlobalNewDelete - Declare the global forms of operator new and
|
|
|
|
/// delete. These are:
|
|
|
|
/// @code
|
|
|
|
/// void* operator new(std::size_t) throw(std::bad_alloc);
|
|
|
|
/// void* operator new[](std::size_t) throw(std::bad_alloc);
|
|
|
|
/// void operator delete(void *) throw();
|
|
|
|
/// void operator delete[](void *) throw();
|
|
|
|
/// @endcode
|
|
|
|
/// Note that the placement and nothrow forms of new are *not* implicitly
|
|
|
|
/// declared. Their use requires including \<new\>.
|
|
|
|
void Sema::DeclareGlobalNewDelete()
|
|
|
|
{
|
|
|
|
if (GlobalNewDeleteDeclared)
|
|
|
|
return;
|
|
|
|
GlobalNewDeleteDeclared = true;
|
|
|
|
|
|
|
|
QualType VoidPtr = Context.getPointerType(Context.VoidTy);
|
|
|
|
QualType SizeT = Context.getSizeType();
|
|
|
|
|
|
|
|
// FIXME: Exception specifications are not added.
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_New),
|
|
|
|
VoidPtr, SizeT);
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_Array_New),
|
|
|
|
VoidPtr, SizeT);
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_Delete),
|
|
|
|
Context.VoidTy, VoidPtr);
|
|
|
|
DeclareGlobalAllocationFunction(
|
|
|
|
Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete),
|
|
|
|
Context.VoidTy, VoidPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DeclareGlobalAllocationFunction - Declares a single implicit global
|
|
|
|
/// allocation function if it doesn't already exist.
|
|
|
|
void Sema::DeclareGlobalAllocationFunction(DeclarationName Name,
|
|
|
|
QualType Return, QualType Argument)
|
|
|
|
{
|
|
|
|
DeclContext *GlobalCtx = Context.getTranslationUnitDecl();
|
|
|
|
|
|
|
|
// Check if this function is already declared.
|
2008-12-24 00:05:05 +03:00
|
|
|
{
|
2008-12-24 01:05:29 +03:00
|
|
|
DeclContext::lookup_iterator Alloc, AllocEnd;
|
2009-01-08 20:28:14 +03:00
|
|
|
for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name);
|
2008-12-24 00:05:05 +03:00
|
|
|
Alloc != AllocEnd; ++Alloc) {
|
|
|
|
// FIXME: Do we need to check for default arguments here?
|
|
|
|
FunctionDecl *Func = cast<FunctionDecl>(*Alloc);
|
|
|
|
if (Func->getNumParams() == 1 &&
|
2009-02-07 04:47:29 +03:00
|
|
|
Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument)
|
2008-12-03 23:26:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0);
|
|
|
|
FunctionDecl *Alloc =
|
|
|
|
FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name,
|
2009-02-25 19:33:18 +03:00
|
|
|
FnType, FunctionDecl::None, false, true,
|
2008-12-03 23:26:15 +03:00
|
|
|
SourceLocation());
|
|
|
|
Alloc->setImplicit();
|
|
|
|
ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(),
|
2009-01-20 04:17:11 +03:00
|
|
|
0, Argument, VarDecl::None, 0);
|
2009-01-14 03:42:25 +03:00
|
|
|
Alloc->setParams(Context, &Param, 1);
|
2008-12-03 23:26:15 +03:00
|
|
|
|
2008-12-24 00:05:05 +03:00
|
|
|
// FIXME: Also add this declaration to the IdentifierResolver, but
|
|
|
|
// make sure it is at the end of the chain to coincide with the
|
|
|
|
// global scope.
|
2009-01-13 02:27:07 +03:00
|
|
|
((DeclContext *)TUScope->getEntity())->addDecl(Alloc);
|
2008-12-03 23:26:15 +03:00
|
|
|
}
|
|
|
|
|
2008-11-21 22:14:01 +03:00
|
|
|
/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in:
|
|
|
|
/// @code ::delete ptr; @endcode
|
|
|
|
/// or
|
|
|
|
/// @code delete [] ptr; @endcode
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-11-21 22:14:01 +03:00
|
|
|
Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal,
|
2009-03-15 20:47:39 +03:00
|
|
|
bool ArrayForm, ExprArg Operand)
|
2008-11-21 22:14:01 +03:00
|
|
|
{
|
|
|
|
// C++ 5.3.5p1: "The operand shall have a pointer type, or a class type
|
|
|
|
// having a single conversion function to a pointer type. The result has
|
|
|
|
// type void."
|
|
|
|
// DR599 amends "pointer type" to "pointer to object type" in both cases.
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
Expr *Ex = (Expr *)Operand.get();
|
2009-02-26 17:39:58 +03:00
|
|
|
if (!Ex->isTypeDependent()) {
|
|
|
|
QualType Type = Ex->getType();
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-02-26 17:39:58 +03:00
|
|
|
if (Type->isRecordType()) {
|
|
|
|
// FIXME: Find that one conversion function and amend the type.
|
|
|
|
}
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
if (!Type->isPointerType())
|
|
|
|
return ExprError(Diag(StartLoc, diag::err_delete_operand)
|
|
|
|
<< Type << Ex->getSourceRange());
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-02-26 17:39:58 +03:00
|
|
|
QualType Pointee = Type->getAsPointerType()->getPointeeType();
|
2009-03-15 20:47:39 +03:00
|
|
|
if (!Pointee->isVoidType() &&
|
2009-03-09 19:13:40 +03:00
|
|
|
RequireCompleteType(StartLoc, Pointee, diag::warn_delete_incomplete,
|
2009-02-26 17:39:58 +03:00
|
|
|
Ex->getSourceRange()))
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
|
|
|
else if (!Pointee->isObjectType())
|
|
|
|
return ExprError(Diag(StartLoc, diag::err_delete_operand)
|
|
|
|
<< Type << Ex->getSourceRange());
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-02-26 17:39:58 +03:00
|
|
|
// FIXME: Look up the correct operator delete overload and pass a pointer
|
|
|
|
// along.
|
|
|
|
// FIXME: Check access and ambiguity of operator delete and destructor.
|
|
|
|
}
|
2008-11-21 22:14:01 +03:00
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
Operand.release();
|
|
|
|
return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm,
|
|
|
|
0, Ex, StartLoc));
|
2008-11-21 22:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-10 06:17:11 +04:00
|
|
|
/// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a
|
|
|
|
/// C++ if/switch/while/for statement.
|
|
|
|
/// e.g: "if (int x = f()) {...}"
|
2009-03-15 20:47:39 +03:00
|
|
|
Action::OwningExprResult
|
2008-09-10 06:17:11 +04:00
|
|
|
Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc,
|
|
|
|
Declarator &D,
|
|
|
|
SourceLocation EqualLoc,
|
2009-03-15 20:47:39 +03:00
|
|
|
ExprArg AssignExprVal) {
|
|
|
|
assert(AssignExprVal.get() && "Null assignment expression");
|
2008-09-10 06:17:11 +04:00
|
|
|
|
|
|
|
// C++ 6.4p2:
|
|
|
|
// The declarator shall not specify a function or an array.
|
|
|
|
// The type-specifier-seq shall not contain typedef and shall not declare a
|
|
|
|
// new class or enumeration.
|
|
|
|
|
|
|
|
assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
|
|
|
|
"Parser allowed 'typedef' as storage class of condition decl.");
|
|
|
|
|
|
|
|
QualType Ty = GetTypeForDeclarator(D, S);
|
|
|
|
|
|
|
|
if (Ty->isFunctionType()) { // The declarator shall not specify a function...
|
|
|
|
// We exit without creating a CXXConditionDeclExpr because a FunctionDecl
|
|
|
|
// would be created and CXXConditionDeclExpr wants a VarDecl.
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError(Diag(StartLoc, diag::err_invalid_use_of_function_type)
|
|
|
|
<< SourceRange(StartLoc, EqualLoc));
|
2008-09-10 06:17:11 +04:00
|
|
|
} else if (Ty->isArrayType()) { // ...or an array.
|
2008-11-19 08:27:50 +03:00
|
|
|
Diag(StartLoc, diag::err_invalid_use_of_array_type)
|
|
|
|
<< SourceRange(StartLoc, EqualLoc);
|
2008-09-10 06:17:11 +04:00
|
|
|
} else if (const RecordType *RT = Ty->getAsRecordType()) {
|
|
|
|
RecordDecl *RD = RT->getDecl();
|
|
|
|
// The type-specifier-seq shall not declare a new class...
|
|
|
|
if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD)))
|
|
|
|
Diag(RD->getLocation(), diag::err_type_defined_in_condition);
|
|
|
|
} else if (const EnumType *ET = Ty->getAsEnumType()) {
|
|
|
|
EnumDecl *ED = ET->getDecl();
|
|
|
|
// ...or enumeration.
|
|
|
|
if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED)))
|
|
|
|
Diag(ED->getLocation(), diag::err_type_defined_in_condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclTy *Dcl = ActOnDeclarator(S, D, 0);
|
|
|
|
if (!Dcl)
|
2009-03-15 20:47:39 +03:00
|
|
|
return ExprError();
|
|
|
|
AddInitializerToDecl(Dcl, move(AssignExprVal));
|
2008-09-10 06:17:11 +04:00
|
|
|
|
2008-12-11 02:01:14 +03:00
|
|
|
// Mark this variable as one that is declared within a conditional.
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>((Decl *)Dcl))
|
|
|
|
VD->setDeclaredInCondition(true);
|
|
|
|
|
2009-03-15 20:47:39 +03:00
|
|
|
return Owned(new (Context) CXXConditionDeclExpr(StartLoc, EqualLoc,
|
|
|
|
cast<VarDecl>(static_cast<Decl *>(Dcl))));
|
2008-09-10 06:17:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid.
|
|
|
|
bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) {
|
|
|
|
// C++ 6.4p4:
|
|
|
|
// The value of a condition that is an initialized declaration in a statement
|
|
|
|
// other than a switch statement is the value of the declared variable
|
|
|
|
// implicitly converted to type bool. If that conversion is ill-formed, the
|
|
|
|
// program is ill-formed.
|
|
|
|
// The value of a condition that is an expression is the value of the
|
|
|
|
// expression, implicitly converted to bool.
|
|
|
|
//
|
2009-01-14 18:45:31 +03:00
|
|
|
return PerformContextuallyConvertToBool(CondExpr);
|
2008-09-10 06:17:11 +04:00
|
|
|
}
|
2008-09-12 04:47:35 +04:00
|
|
|
|
|
|
|
/// Helper function to determine whether this is the (deprecated) C++
|
|
|
|
/// conversion from a string literal to a pointer to non-const char or
|
|
|
|
/// non-const wchar_t (for narrow and wide string literals,
|
|
|
|
/// respectively).
|
|
|
|
bool
|
|
|
|
Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) {
|
|
|
|
// Look inside the implicit cast, if it exists.
|
|
|
|
if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From))
|
|
|
|
From = Cast->getSubExpr();
|
|
|
|
|
|
|
|
// A string literal (2.13.4) that is not a wide string literal can
|
|
|
|
// be converted to an rvalue of type "pointer to char"; a wide
|
|
|
|
// string literal can be converted to an rvalue of type "pointer
|
|
|
|
// to wchar_t" (C++ 4.2p2).
|
|
|
|
if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From))
|
|
|
|
if (const PointerType *ToPtrType = ToType->getAsPointerType())
|
|
|
|
if (const BuiltinType *ToPointeeType
|
|
|
|
= ToPtrType->getPointeeType()->getAsBuiltinType()) {
|
|
|
|
// This conversion is considered only when there is an
|
|
|
|
// explicit appropriate pointer target type (C++ 4.2p2).
|
|
|
|
if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 &&
|
|
|
|
((StrLit->isWide() && ToPointeeType->isWideCharType()) ||
|
|
|
|
(!StrLit->isWide() &&
|
|
|
|
(ToPointeeType->getKind() == BuiltinType::Char_U ||
|
|
|
|
ToPointeeType->getKind() == BuiltinType::Char_S))))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-24 08:54:22 +04:00
|
|
|
|
|
|
|
/// PerformImplicitConversion - Perform an implicit conversion of the
|
|
|
|
/// expression From to the type ToType. Returns true if there was an
|
|
|
|
/// error, false otherwise. The expression From is replaced with the
|
2008-12-19 20:40:08 +03:00
|
|
|
/// converted expression. Flavor is the kind of conversion we're
|
2009-01-14 18:45:31 +03:00
|
|
|
/// performing, used in the error message. If @p AllowExplicit,
|
|
|
|
/// explicit user-defined conversions are permitted.
|
2008-10-24 08:54:22 +04:00
|
|
|
bool
|
2008-12-19 20:40:08 +03:00
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
2009-01-14 18:45:31 +03:00
|
|
|
const char *Flavor, bool AllowExplicit)
|
2008-10-24 08:54:22 +04:00
|
|
|
{
|
2009-01-14 18:45:31 +03:00
|
|
|
ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType, false,
|
|
|
|
AllowExplicit);
|
|
|
|
return PerformImplicitConversion(From, ToType, ICS, Flavor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PerformImplicitConversion - Perform an implicit conversion of the
|
|
|
|
/// expression From to the type ToType using the pre-computed implicit
|
|
|
|
/// conversion sequence ICS. Returns true if there was an error, false
|
|
|
|
/// otherwise. The expression From is replaced with the converted
|
|
|
|
/// expression. Flavor is the kind of conversion we're performing,
|
|
|
|
/// used in the error message.
|
|
|
|
bool
|
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
|
|
|
const ImplicitConversionSequence &ICS,
|
|
|
|
const char* Flavor) {
|
2008-10-24 08:54:22 +04:00
|
|
|
switch (ICS.ConversionKind) {
|
|
|
|
case ImplicitConversionSequence::StandardConversion:
|
2008-12-19 20:40:08 +03:00
|
|
|
if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor))
|
2008-10-24 08:54:22 +04:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ImplicitConversionSequence::UserDefinedConversion:
|
|
|
|
// FIXME: This is, of course, wrong. We'll need to actually call
|
|
|
|
// the constructor or conversion operator, and then cope with the
|
|
|
|
// standard conversions.
|
2009-01-14 18:45:31 +03:00
|
|
|
ImpCastExprToType(From, ToType.getNonReferenceType(),
|
2009-03-17 02:22:08 +03:00
|
|
|
ToType->isLValueReferenceType());
|
2008-10-31 19:23:19 +03:00
|
|
|
return false;
|
2008-10-24 08:54:22 +04:00
|
|
|
|
|
|
|
case ImplicitConversionSequence::EllipsisConversion:
|
|
|
|
assert(false && "Cannot perform an ellipsis conversion");
|
2008-10-31 19:23:19 +03:00
|
|
|
return false;
|
2008-10-24 08:54:22 +04:00
|
|
|
|
|
|
|
case ImplicitConversionSequence::BadConversion:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything went well.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PerformImplicitConversion - Perform an implicit conversion of the
|
|
|
|
/// expression From to the type ToType by following the standard
|
|
|
|
/// conversion sequence SCS. Returns true if there was an error, false
|
|
|
|
/// otherwise. The expression From is replaced with the converted
|
2008-12-19 20:40:08 +03:00
|
|
|
/// expression. Flavor is the context in which we're performing this
|
|
|
|
/// conversion, for use in error messages.
|
2008-10-24 08:54:22 +04:00
|
|
|
bool
|
|
|
|
Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
2008-12-19 20:40:08 +03:00
|
|
|
const StandardConversionSequence& SCS,
|
2009-01-14 18:45:31 +03:00
|
|
|
const char *Flavor) {
|
2008-10-24 08:54:22 +04:00
|
|
|
// Overall FIXME: we are recomputing too many types here and doing
|
|
|
|
// far too much extra work. What this means is that we need to keep
|
|
|
|
// track of more information that is computed when we try the
|
|
|
|
// implicit conversion initially, so that we don't need to recompute
|
|
|
|
// anything here.
|
|
|
|
QualType FromType = From->getType();
|
|
|
|
|
2008-11-03 22:09:14 +03:00
|
|
|
if (SCS.CopyConstructor) {
|
|
|
|
// FIXME: Create a temporary object by calling the copy
|
|
|
|
// constructor.
|
2009-01-16 22:38:23 +03:00
|
|
|
ImpCastExprToType(From, ToType.getNonReferenceType(),
|
2009-03-17 02:22:08 +03:00
|
|
|
ToType->isLValueReferenceType());
|
2008-11-03 22:09:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-24 08:54:22 +04:00
|
|
|
// Perform the first implicit conversion.
|
|
|
|
switch (SCS.First) {
|
|
|
|
case ICK_Identity:
|
|
|
|
case ICK_Lvalue_To_Rvalue:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Array_To_Pointer:
|
2009-02-19 00:56:37 +03:00
|
|
|
FromType = Context.getArrayDecayedType(FromType);
|
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Function_To_Pointer:
|
2009-03-13 21:40:31 +03:00
|
|
|
if (Context.getCanonicalType(FromType) == Context.OverloadTy) {
|
2008-11-10 23:40:00 +03:00
|
|
|
FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true);
|
|
|
|
if (!Fn)
|
|
|
|
return true;
|
|
|
|
|
2009-02-19 00:56:37 +03:00
|
|
|
if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin()))
|
|
|
|
return true;
|
|
|
|
|
2008-11-10 23:40:00 +03:00
|
|
|
FixOverloadedFunctionReference(From, Fn);
|
|
|
|
FromType = From->getType();
|
|
|
|
}
|
2008-10-24 08:54:22 +04:00
|
|
|
FromType = Context.getPointerType(FromType);
|
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper first standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the second implicit conversion
|
|
|
|
switch (SCS.Second) {
|
|
|
|
case ICK_Identity:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Integral_Promotion:
|
|
|
|
case ICK_Floating_Promotion:
|
2009-02-12 03:15:05 +03:00
|
|
|
case ICK_Complex_Promotion:
|
2008-10-24 08:54:22 +04:00
|
|
|
case ICK_Integral_Conversion:
|
|
|
|
case ICK_Floating_Conversion:
|
2009-02-12 03:15:05 +03:00
|
|
|
case ICK_Complex_Conversion:
|
2008-10-24 08:54:22 +04:00
|
|
|
case ICK_Floating_Integral:
|
2009-02-12 03:15:05 +03:00
|
|
|
case ICK_Complex_Real:
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64336 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-12 02:02:49 +03:00
|
|
|
case ICK_Compatible_Conversion:
|
|
|
|
// FIXME: Go deeper to get the unqualified type!
|
2008-10-24 08:54:22 +04:00
|
|
|
FromType = ToType.getUnqualifiedType();
|
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Pointer_Conversion:
|
2008-12-19 20:40:08 +03:00
|
|
|
if (SCS.IncompatibleObjC) {
|
|
|
|
// Diagnose incompatible Objective-C conversions
|
|
|
|
Diag(From->getSourceRange().getBegin(),
|
|
|
|
diag::ext_typecheck_convert_incompatible_pointer)
|
|
|
|
<< From->getType() << ToType << Flavor
|
|
|
|
<< From->getSourceRange();
|
|
|
|
}
|
|
|
|
|
2008-10-24 08:54:22 +04:00
|
|
|
if (CheckPointerConversion(From, ToType))
|
|
|
|
return true;
|
|
|
|
ImpCastExprToType(From, ToType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Pointer_Member:
|
2009-01-25 22:43:20 +03:00
|
|
|
if (CheckMemberPointerConversion(From, ToType))
|
|
|
|
return true;
|
|
|
|
ImpCastExprToType(From, ToType);
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Boolean_Conversion:
|
|
|
|
FromType = Context.BoolTy;
|
|
|
|
ImpCastExprToType(From, FromType);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper second standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (SCS.Third) {
|
|
|
|
case ICK_Identity:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICK_Qualification:
|
2009-03-17 02:22:08 +03:00
|
|
|
// FIXME: Not sure about lvalue vs rvalue here in the presence of
|
|
|
|
// rvalue references.
|
2009-01-16 22:38:23 +03:00
|
|
|
ImpCastExprToType(From, ToType.getNonReferenceType(),
|
2009-03-17 02:22:08 +03:00
|
|
|
ToType->isLValueReferenceType());
|
2008-10-24 08:54:22 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Improper second standard conversion");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:52:13 +03:00
|
|
|
Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT,
|
|
|
|
SourceLocation KWLoc,
|
|
|
|
SourceLocation LParen,
|
|
|
|
TypeTy *Ty,
|
|
|
|
SourceLocation RParen) {
|
|
|
|
// FIXME: Some of the type traits have requirements. Interestingly, only the
|
|
|
|
// __is_base_of requirement is explicitly stated to be diagnosed. Indeed,
|
|
|
|
// G++ accepts __is_pod(Incomplete) without complaints, and claims that the
|
|
|
|
// type is indeed a POD.
|
|
|
|
|
|
|
|
// There is no point in eagerly computing the value. The traits are designed
|
|
|
|
// to be used from type trait templates, so Ty will be a template parameter
|
|
|
|
// 99% of the time.
|
2009-02-07 04:47:29 +03:00
|
|
|
return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT,
|
2009-01-05 23:52:13 +03:00
|
|
|
QualType::getFromOpaquePtr(Ty),
|
|
|
|
RParen, Context.BoolTy));
|
|
|
|
}
|
2009-02-07 23:10:22 +03:00
|
|
|
|
|
|
|
QualType Sema::CheckPointerToMemberOperands(
|
|
|
|
Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect)
|
|
|
|
{
|
|
|
|
const char *OpSpelling = isIndirect ? "->*" : ".*";
|
|
|
|
// C++ 5.5p2
|
|
|
|
// The binary operator .* [p3: ->*] binds its second operand, which shall
|
|
|
|
// be of type "pointer to member of T" (where T is a completely-defined
|
|
|
|
// class type) [...]
|
|
|
|
QualType RType = rex->getType();
|
|
|
|
const MemberPointerType *MemPtr = RType->getAsMemberPointerType();
|
2009-03-24 22:52:54 +03:00
|
|
|
if (!MemPtr) {
|
2009-02-07 23:10:22 +03:00
|
|
|
Diag(Loc, diag::err_bad_memptr_rhs)
|
|
|
|
<< OpSpelling << RType << rex->getSourceRange();
|
|
|
|
return QualType();
|
2009-03-24 22:52:54 +03:00
|
|
|
} else if (RequireCompleteType(Loc, QualType(MemPtr->getClass(), 0),
|
|
|
|
diag::err_memptr_rhs_incomplete,
|
|
|
|
rex->getSourceRange()))
|
|
|
|
return QualType();
|
|
|
|
|
2009-02-07 23:10:22 +03:00
|
|
|
QualType Class(MemPtr->getClass(), 0);
|
|
|
|
|
|
|
|
// C++ 5.5p2
|
|
|
|
// [...] to its first operand, which shall be of class T or of a class of
|
|
|
|
// which T is an unambiguous and accessible base class. [p3: a pointer to
|
|
|
|
// such a class]
|
|
|
|
QualType LType = lex->getType();
|
|
|
|
if (isIndirect) {
|
|
|
|
if (const PointerType *Ptr = LType->getAsPointerType())
|
|
|
|
LType = Ptr->getPointeeType().getNonReferenceType();
|
|
|
|
else {
|
|
|
|
Diag(Loc, diag::err_bad_memptr_lhs)
|
|
|
|
<< OpSpelling << 1 << LType << lex->getSourceRange();
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Context.getCanonicalType(Class).getUnqualifiedType() !=
|
|
|
|
Context.getCanonicalType(LType).getUnqualifiedType()) {
|
|
|
|
BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false,
|
|
|
|
/*DetectVirtual=*/false);
|
|
|
|
// FIXME: Would it be useful to print full ambiguity paths,
|
|
|
|
// or is that overkill?
|
|
|
|
if (!IsDerivedFrom(LType, Class, Paths) ||
|
|
|
|
Paths.isAmbiguous(Context.getCanonicalType(Class))) {
|
|
|
|
Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling
|
|
|
|
<< (int)isIndirect << lex->getType() << lex->getSourceRange();
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ 5.5p2
|
|
|
|
// The result is an object or a function of the type specified by the
|
|
|
|
// second operand.
|
|
|
|
// The cv qualifiers are the union of those in the pointer and the left side,
|
|
|
|
// in accordance with 5.5p5 and 5.2.5.
|
|
|
|
// FIXME: This returns a dereferenced member function pointer as a normal
|
|
|
|
// function type. However, the only operation valid on such functions is
|
|
|
|
// calling them. There's also a GCC extension to get a function pointer to
|
|
|
|
// the thing, which is another complication, because this type - unlike the
|
|
|
|
// type that is the result of this expression - takes the class as the first
|
|
|
|
// argument.
|
|
|
|
// We probably need a "MemberFunctionClosureType" or something like that.
|
|
|
|
QualType Result = MemPtr->getPointeeType();
|
|
|
|
if (LType.isConstQualified())
|
|
|
|
Result.addConst();
|
|
|
|
if (LType.isVolatileQualified())
|
|
|
|
Result.addVolatile();
|
|
|
|
return Result;
|
|
|
|
}
|