Anders Carlsson | 16d81b8 | 2009-09-22 22:53:17 +0000 | [diff] [blame] | 1 | //===--- CGCXXExpr.cpp - Emit LLVM Code for C++ expressions ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with code generation of C++ expressions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | using namespace clang; |
| 16 | using namespace CodeGen; |
| 17 | |
| 18 | llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { |
| 19 | if (E->isArray()) { |
| 20 | ErrorUnsupported(E, "new[] expression"); |
| 21 | return llvm::UndefValue::get(ConvertType(E->getType())); |
| 22 | } |
| 23 | |
| 24 | QualType AllocType = E->getAllocatedType(); |
| 25 | FunctionDecl *NewFD = E->getOperatorNew(); |
| 26 | const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>(); |
| 27 | |
| 28 | CallArgList NewArgs; |
| 29 | |
| 30 | // The allocation size is the first argument. |
| 31 | QualType SizeTy = getContext().getSizeType(); |
| 32 | llvm::Value *AllocSize = |
| 33 | llvm::ConstantInt::get(ConvertType(SizeTy), |
| 34 | getContext().getTypeSize(AllocType) / 8); |
| 35 | |
| 36 | NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); |
| 37 | |
| 38 | // Emit the rest of the arguments. |
| 39 | // FIXME: Ideally, this should just use EmitCallArgs. |
| 40 | CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); |
| 41 | |
| 42 | // First, use the types from the function type. |
| 43 | // We start at 1 here because the first argument (the allocation size) |
| 44 | // has already been emitted. |
| 45 | for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { |
| 46 | QualType ArgType = NewFTy->getArgType(i); |
| 47 | |
| 48 | assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). |
| 49 | getTypePtr() == |
| 50 | getContext().getCanonicalType(NewArg->getType()).getTypePtr() && |
| 51 | "type mismatch in call argument!"); |
| 52 | |
| 53 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 54 | ArgType)); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | // Either we've emitted all the call args, or we have a call to a |
| 59 | // variadic function. |
| 60 | assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && |
| 61 | "Extra arguments in non-variadic function!"); |
| 62 | |
| 63 | // If we still have any arguments, emit them using the type of the argument. |
| 64 | for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); |
| 65 | NewArg != NewArgEnd; ++NewArg) { |
| 66 | QualType ArgType = NewArg->getType(); |
| 67 | NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), |
| 68 | ArgType)); |
| 69 | } |
| 70 | |
| 71 | // Emit the call to new. |
| 72 | RValue RV = |
| 73 | EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs), |
| 74 | CGM.GetAddrOfFunction(NewFD), NewArgs, NewFD); |
| 75 | |
| 76 | // If an allocation function is declared with an empty exception specification |
| 77 | // it returns null to indicate failure to allocate storage. [expr.new]p13. |
| 78 | // (We don't need to check for null when there's no new initializer and |
| 79 | // we're allocating a POD type). |
| 80 | bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && |
| 81 | !(AllocType->isPODType() && !E->hasInitializer()); |
| 82 | |
| 83 | llvm::BasicBlock *NewNull = 0; |
| 84 | llvm::BasicBlock *NewNotNull = 0; |
| 85 | llvm::BasicBlock *NewEnd = 0; |
| 86 | |
| 87 | llvm::Value *NewPtr = RV.getScalarVal(); |
| 88 | |
| 89 | if (NullCheckResult) { |
| 90 | NewNull = createBasicBlock("new.null"); |
| 91 | NewNotNull = createBasicBlock("new.notnull"); |
| 92 | NewEnd = createBasicBlock("new.end"); |
| 93 | |
| 94 | llvm::Value *IsNull = |
| 95 | Builder.CreateICmpEQ(NewPtr, |
| 96 | llvm::Constant::getNullValue(NewPtr->getType()), |
| 97 | "isnull"); |
| 98 | |
| 99 | Builder.CreateCondBr(IsNull, NewNull, NewNotNull); |
| 100 | EmitBlock(NewNotNull); |
| 101 | } |
| 102 | |
| 103 | NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); |
| 104 | |
| 105 | if (AllocType->isPODType()) { |
| 106 | if (E->getNumConstructorArgs() > 0) { |
| 107 | assert(E->getNumConstructorArgs() == 1 && |
| 108 | "Can only have one argument to initializer of POD type."); |
| 109 | |
| 110 | const Expr *Init = E->getConstructorArg(0); |
| 111 | |
| 112 | if (!hasAggregateLLVMType(AllocType)) |
| 113 | Builder.CreateStore(EmitScalarExpr(Init), NewPtr); |
| 114 | else if (AllocType->isAnyComplexType()) |
| 115 | EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified()); |
| 116 | else |
| 117 | EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); |
| 118 | } |
| 119 | } else { |
| 120 | // Call the constructor. |
| 121 | CXXConstructorDecl *Ctor = E->getConstructor(); |
| 122 | |
| 123 | EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr, |
| 124 | E->constructor_arg_begin(), |
| 125 | E->constructor_arg_end()); |
| 126 | } |
| 127 | |
| 128 | if (NullCheckResult) { |
| 129 | Builder.CreateBr(NewEnd); |
| 130 | EmitBlock(NewNull); |
| 131 | Builder.CreateBr(NewEnd); |
| 132 | EmitBlock(NewEnd); |
| 133 | |
| 134 | llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); |
| 135 | PHI->reserveOperandSpace(2); |
| 136 | PHI->addIncoming(NewPtr, NewNotNull); |
| 137 | PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull); |
| 138 | |
| 139 | NewPtr = PHI; |
| 140 | } |
| 141 | |
| 142 | return NewPtr; |
| 143 | } |
| 144 | |
| 145 | void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { |
| 146 | if (E->isArrayForm()) { |
| 147 | ErrorUnsupported(E, "delete[] expression"); |
| 148 | return; |
| 149 | }; |
| 150 | |
| 151 | QualType DeleteTy = |
| 152 | E->getArgument()->getType()->getAs<PointerType>()->getPointeeType(); |
| 153 | |
| 154 | llvm::Value *Ptr = EmitScalarExpr(E->getArgument()); |
| 155 | |
| 156 | // Null check the pointer. |
| 157 | llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); |
| 158 | llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); |
| 159 | |
| 160 | llvm::Value *IsNull = |
| 161 | Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()), |
| 162 | "isnull"); |
| 163 | |
| 164 | Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); |
| 165 | EmitBlock(DeleteNotNull); |
| 166 | |
| 167 | // Call the destructor if necessary. |
| 168 | if (const RecordType *RT = DeleteTy->getAs<RecordType>()) { |
| 169 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 170 | if (!RD->hasTrivialDestructor()) { |
| 171 | const CXXDestructorDecl *Dtor = RD->getDestructor(getContext()); |
| 172 | if (Dtor->isVirtual()) { |
| 173 | const llvm::Type *Ty = |
| 174 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(Dtor), |
| 175 | /*isVariadic=*/false); |
| 176 | |
| 177 | llvm::Value *Callee = BuildVirtualCall(Dtor, Ptr, Ty); |
| 178 | EmitCXXMemberCall(Dtor, Callee, Ptr, 0, 0); |
| 179 | } else |
| 180 | EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Call delete. |
| 186 | FunctionDecl *DeleteFD = E->getOperatorDelete(); |
| 187 | const FunctionProtoType *DeleteFTy = |
| 188 | DeleteFD->getType()->getAs<FunctionProtoType>(); |
| 189 | |
| 190 | CallArgList DeleteArgs; |
| 191 | |
| 192 | QualType ArgTy = DeleteFTy->getArgType(0); |
| 193 | llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); |
| 194 | DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy)); |
| 195 | |
| 196 | // Emit the call to delete. |
| 197 | EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(), |
| 198 | DeleteArgs), |
| 199 | CGM.GetAddrOfFunction(DeleteFD), |
| 200 | DeleteArgs, DeleteFD); |
| 201 | |
| 202 | EmitBlock(DeleteEnd); |
| 203 | } |