Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 17 | #include "clang/Parse/DeclSpec.h" |
Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 22 | /// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 23 | /// name (e.g., operator void const *) as an expression. This is |
| 24 | /// very similar to ActOnIdentifierExpr, except that instead of |
| 25 | /// providing an identifier the parser provides the type of the |
| 26 | /// conversion function. |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 27 | Sema::ExprResult |
| 28 | Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc, |
| 29 | TypeTy *Ty, bool HasTrailingLParen, |
| 30 | const CXXScopeSpec &SS) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 31 | QualType ConvType = QualType::getFromOpaquePtr(Ty); |
| 32 | QualType ConvTypeCanon = Context.getCanonicalType(ConvType); |
| 33 | DeclarationName ConvName |
| 34 | = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 35 | return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen, |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 36 | &SS); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 37 | } |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 39 | /// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 40 | /// name (e.g., @c operator+ ) as an expression. This is very |
| 41 | /// similar to ActOnIdentifierExpr, except that instead of providing |
| 42 | /// an identifier the parser provides the kind of overloaded |
| 43 | /// operator that was parsed. |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 44 | Sema::ExprResult |
| 45 | Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc, |
| 46 | OverloadedOperatorKind Op, |
| 47 | bool HasTrailingLParen, |
| 48 | const CXXScopeSpec &SS) { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 49 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 50 | return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS); |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 53 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). |
| 54 | Action::ExprResult |
| 55 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 56 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
| 57 | const NamespaceDecl *StdNs = GetStdNamespace(); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 58 | if (!StdNs) |
| 59 | return Diag(OpLoc, diag::err_need_header_before_typeid); |
| 60 | |
| 61 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 62 | Decl *TypeInfoDecl = LookupDecl(TypeInfoII, |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 63 | Decl::IDNS_Tag | Decl::IDNS_Ordinary, |
| 64 | 0, StdNs, /*createBuiltins=*/false); |
| 65 | RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 66 | if (!TypeInfoRecordDecl) |
| 67 | return Diag(OpLoc, diag::err_need_header_before_typeid); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 68 | |
| 69 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); |
| 70 | |
| 71 | return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(), |
| 72 | SourceRange(OpLoc, RParenLoc)); |
| 73 | } |
| 74 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 75 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 77 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 78 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | "Unknown C++ Boolean value!"); |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 80 | return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 82 | |
| 83 | /// ActOnCXXThrow - Parse throw expressions. |
| 84 | Action::ExprResult |
| 85 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { |
| 86 | return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); |
| 87 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 88 | |
| 89 | Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
| 90 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 91 | /// is a non-lvalue expression whose value is the address of the object for |
| 92 | /// which the function is called. |
| 93 | |
| 94 | if (!isa<FunctionDecl>(CurContext)) { |
| 95 | Diag(ThisLoc, diag::err_invalid_this_use); |
| 96 | return ExprResult(true); |
| 97 | } |
| 98 | |
| 99 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 100 | if (MD->isInstance()) |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 101 | return new CXXThisExpr(ThisLoc, MD->getThisType(Context)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 102 | |
| 103 | return Diag(ThisLoc, diag::err_invalid_this_use); |
| 104 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 105 | |
| 106 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 107 | /// Can be interpreted either as function-style casting ("int(x)") |
| 108 | /// or class type construction ("ClassType(x,y,z)") |
| 109 | /// or creation of a value-initialized type ("int()"). |
| 110 | Action::ExprResult |
| 111 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 112 | SourceLocation LParenLoc, |
| 113 | ExprTy **ExprTys, unsigned NumExprs, |
| 114 | SourceLocation *CommaLocs, |
| 115 | SourceLocation RParenLoc) { |
| 116 | assert(TypeRep && "Missing type!"); |
| 117 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
| 118 | Expr **Exprs = (Expr**)ExprTys; |
| 119 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 120 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 121 | |
| 122 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 123 | // C++ 5.2.3p1: |
| 124 | // If the simple-type-specifier specifies a class type, the class type shall |
| 125 | // be complete. |
| 126 | // |
| 127 | if (!RT->getDecl()->isDefinition()) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 128 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 129 | << Ty << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 130 | |
Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 131 | unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, |
| 132 | "class constructors are not supported yet"); |
| 133 | return Diag(TyBeginLoc, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // C++ 5.2.3p1: |
| 137 | // If the expression list is a single expression, the type conversion |
| 138 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 139 | // corresponding cast expression. |
| 140 | // |
| 141 | if (NumExprs == 1) { |
| 142 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
| 143 | return true; |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 144 | return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc, |
| 145 | Exprs[0], RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // C++ 5.2.3p1: |
| 149 | // If the expression list specifies more than a single value, the type shall |
| 150 | // be a class with a suitably declared constructor. |
| 151 | // |
| 152 | if (NumExprs > 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 153 | return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg) |
| 154 | << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 155 | |
| 156 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 157 | |
| 158 | // C++ 5.2.3p2: |
| 159 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 160 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 161 | // rvalue of the specified type, which is value-initialized. |
| 162 | // |
| 163 | if (Ty->isArrayType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 164 | return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 165 | if (Ty->isIncompleteType() && !Ty->isVoidType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 166 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 167 | << Ty << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 168 | |
| 169 | return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); |
| 170 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 171 | |
| 172 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 173 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 174 | /// @code new (memory) int[size][4] @endcode |
| 175 | /// or |
| 176 | /// @code ::new Foo(23, "hello") @endcode |
| 177 | /// For the interpretation of this heap of arguments, consult the base version. |
| 178 | Action::ExprResult |
| 179 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 180 | SourceLocation PlacementLParen, |
| 181 | ExprTy **PlacementArgs, unsigned NumPlaceArgs, |
| 182 | SourceLocation PlacementRParen, bool ParenTypeId, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 183 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 184 | ExprTy **ConstructorArgs, unsigned NumConsArgs, |
| 185 | SourceLocation ConstructorRParen) |
| 186 | { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 187 | // FIXME: Throughout this function, we have rather bad location information. |
| 188 | // Implementing Declarator::getSourceRange() would go a long way toward |
| 189 | // fixing that. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 190 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 191 | Expr *ArraySize = 0; |
| 192 | unsigned Skip = 0; |
| 193 | // If the specified type is an array, unwrap it and save the expression. |
| 194 | if (D.getNumTypeObjects() > 0 && |
| 195 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 196 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 197 | if (Chunk.Arr.hasStatic) |
| 198 | return Diag(Chunk.Loc, diag::err_static_illegal_in_new); |
| 199 | if (!Chunk.Arr.NumElts) |
| 200 | return Diag(Chunk.Loc, diag::err_array_new_needs_size); |
| 201 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
| 202 | Skip = 1; |
| 203 | } |
| 204 | |
| 205 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip); |
| 206 | if (D.getInvalidType()) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 207 | return true; |
| 208 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 209 | if (CheckAllocatedType(AllocType, D)) |
| 210 | return true; |
| 211 | |
| 212 | QualType ResultType = Context.getPointerType(AllocType); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 213 | |
| 214 | // That every array dimension except the first is constant was already |
| 215 | // checked by the type check above. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 216 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 217 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 218 | // or enumeration type with a non-negative value." |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 219 | if (ArraySize) { |
| 220 | QualType SizeType = ArraySize->getType(); |
| 221 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) |
| 222 | return Diag(ArraySize->getSourceRange().getBegin(), |
| 223 | diag::err_array_size_not_integral) |
| 224 | << SizeType << ArraySize->getSourceRange(); |
| 225 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 226 | // We don't care about special rules, so we tell the machinery it's not |
| 227 | // evaluated - it gives us a result in more cases. |
| 228 | llvm::APSInt Value; |
| 229 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 230 | if (Value < llvm::APSInt( |
| 231 | llvm::APInt::getNullValue(Value.getBitWidth()), false)) |
| 232 | return Diag(ArraySize->getSourceRange().getBegin(), |
| 233 | diag::err_typecheck_negative_array_size) |
| 234 | << ArraySize->getSourceRange(); |
| 235 | } |
| 236 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 237 | |
| 238 | // --- Choosing an allocation function --- |
| 239 | // C++ 5.3.4p8 - 14 & 18 |
| 240 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 241 | // in the scope of the allocated class. |
| 242 | // 2) If an array size is given, look for operator new[], else look for |
| 243 | // operator new. |
| 244 | // 3) The first argument is always size_t. Append the arguments from the |
| 245 | // placement form. |
| 246 | // FIXME: Find the correct overload of operator new. |
| 247 | // FIXME: Also find the corresponding overload of operator delete. |
| 248 | FunctionDecl *OperatorNew = 0; |
| 249 | FunctionDecl *OperatorDelete = 0; |
| 250 | Expr **PlaceArgs = (Expr**)PlacementArgs; |
| 251 | |
| 252 | bool Init = ConstructorLParen.isValid(); |
| 253 | // --- Choosing a constructor --- |
| 254 | // C++ 5.3.4p15 |
| 255 | // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) |
| 256 | // the object is not initialized. If the object, or any part of it, is |
| 257 | // const-qualified, it's an error. |
| 258 | // 2) If T is a POD and there's an empty initializer, the object is value- |
| 259 | // initialized. |
| 260 | // 3) If T is a POD and there's one initializer argument, the object is copy- |
| 261 | // constructed. |
| 262 | // 4) If T is a POD and there's more initializer arguments, it's an error. |
| 263 | // 5) If T is not a POD, the initializer arguments are used as constructor |
| 264 | // arguments. |
| 265 | // |
| 266 | // Or by the C++0x formulation: |
| 267 | // 1) If there's no initializer, the object is default-initialized according |
| 268 | // to C++0x rules. |
| 269 | // 2) Otherwise, the object is direct-initialized. |
| 270 | CXXConstructorDecl *Constructor = 0; |
| 271 | Expr **ConsArgs = (Expr**)ConstructorArgs; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 272 | if (const RecordType *RT = AllocType->getAsRecordType()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 273 | // FIXME: This is incorrect for when there is an empty initializer and |
| 274 | // no user-defined constructor. Must zero-initialize, not default-construct. |
| 275 | Constructor = PerformInitializationByConstructor( |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 276 | AllocType, ConsArgs, NumConsArgs, |
| 277 | D.getDeclSpec().getSourceRange().getBegin(), |
| 278 | SourceRange(D.getDeclSpec().getSourceRange().getBegin(), |
| 279 | ConstructorRParen), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 280 | RT->getDecl()->getDeclName(), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 281 | NumConsArgs != 0 ? IK_Direct : IK_Default); |
| 282 | if (!Constructor) |
| 283 | return true; |
| 284 | } else { |
| 285 | if (!Init) { |
| 286 | // FIXME: Check that no subpart is const. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 287 | if (AllocType.isConstQualified()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 288 | Diag(StartLoc, diag::err_new_uninitialized_const) |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 289 | << D.getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | } else if (NumConsArgs == 0) { |
| 293 | // Object is value-initialized. Do nothing. |
| 294 | } else if (NumConsArgs == 1) { |
| 295 | // Object is direct-initialized. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 296 | // FIXME: WHAT DeclarationName do we pass in here? |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 297 | if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc, |
| 298 | DeclarationName() /*AllocType.getAsString()*/)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 299 | return true; |
| 300 | } else { |
| 301 | Diag(StartLoc, diag::err_builtin_direct_init_more_than_one_arg) |
| 302 | << SourceRange(ConstructorLParen, ConstructorRParen); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
| 307 | |
| 308 | return new CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, NumPlaceArgs, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 309 | ParenTypeId, ArraySize, Constructor, Init, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 310 | ConsArgs, NumConsArgs, OperatorDelete, ResultType, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 311 | StartLoc, Init ? ConstructorRParen : SourceLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 315 | /// in a new-expression. |
| 316 | /// dimension off and stores the size expression in ArraySize. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 317 | bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 318 | { |
| 319 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 320 | // abstract class type or array thereof. |
| 321 | // FIXME: We don't have abstract types yet. |
| 322 | // FIXME: Under C++ semantics, an incomplete object type is still an object |
| 323 | // type. This code assumes the C semantics, where it's not. |
| 324 | if (!AllocType->isObjectType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 325 | unsigned type; // For the select in the message. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 326 | if (AllocType->isFunctionType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 327 | type = 0; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 328 | } else if(AllocType->isIncompleteType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 329 | type = 1; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 330 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 331 | assert(AllocType->isReferenceType() && "What else could it be?"); |
| 332 | type = 2; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 333 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 334 | SourceRange TyR = D.getDeclSpec().getSourceRange(); |
| 335 | // FIXME: This is very much a guess and won't work for, e.g., pointers. |
| 336 | if (D.getNumTypeObjects() > 0) |
| 337 | TyR.setEnd(D.getTypeObject(0).Loc); |
| 338 | Diag(TyR.getBegin(), diag::err_bad_new_type) |
| 339 | << AllocType.getAsString() << type << TyR; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 343 | // Every dimension shall be of constant size. |
| 344 | unsigned i = 1; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 345 | while (const ArrayType *Array = Context.getAsArrayType(AllocType)) { |
| 346 | if (!Array->isConstantArrayType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 347 | Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst) |
| 348 | << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 349 | return true; |
| 350 | } |
| 351 | AllocType = Array->getElementType(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame^] | 352 | ++i; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 359 | /// @code ::delete ptr; @endcode |
| 360 | /// or |
| 361 | /// @code delete [] ptr; @endcode |
| 362 | Action::ExprResult |
| 363 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
| 364 | bool ArrayForm, ExprTy *Operand) |
| 365 | { |
| 366 | // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type |
| 367 | // having a single conversion function to a pointer type. The result has |
| 368 | // type void." |
| 369 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 370 | |
| 371 | Expr *Ex = (Expr *)Operand; |
| 372 | QualType Type = Ex->getType(); |
| 373 | |
| 374 | if (Type->isRecordType()) { |
| 375 | // FIXME: Find that one conversion function and amend the type. |
| 376 | } |
| 377 | |
| 378 | if (!Type->isPointerType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 379 | Diag(StartLoc, diag::err_delete_operand) << Type << Ex->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
| 383 | QualType Pointee = Type->getAsPointerType()->getPointeeType(); |
| 384 | if (Pointee->isIncompleteType() && !Pointee->isVoidType()) |
| 385 | Diag(StartLoc, diag::warn_delete_incomplete) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 386 | << Pointee << Ex->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 387 | else if (!Pointee->isObjectType()) { |
| 388 | Diag(StartLoc, diag::err_delete_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 389 | << Type << Ex->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
| 393 | // FIXME: Look up the correct operator delete overload and pass a pointer |
| 394 | // along. |
| 395 | // FIXME: Check access and ambiguity of operator delete and destructor. |
| 396 | |
| 397 | return new CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, Ex, |
| 398 | StartLoc); |
| 399 | } |
| 400 | |
| 401 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 402 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 403 | /// C++ if/switch/while/for statement. |
| 404 | /// e.g: "if (int x = f()) {...}" |
| 405 | Action::ExprResult |
| 406 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 407 | Declarator &D, |
| 408 | SourceLocation EqualLoc, |
| 409 | ExprTy *AssignExprVal) { |
| 410 | assert(AssignExprVal && "Null assignment expression"); |
| 411 | |
| 412 | // C++ 6.4p2: |
| 413 | // The declarator shall not specify a function or an array. |
| 414 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 415 | // new class or enumeration. |
| 416 | |
| 417 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 418 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 419 | |
| 420 | QualType Ty = GetTypeForDeclarator(D, S); |
| 421 | |
| 422 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 423 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 424 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 425 | return Diag(StartLoc, diag::err_invalid_use_of_function_type) |
| 426 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 427 | } else if (Ty->isArrayType()) { // ...or an array. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 428 | Diag(StartLoc, diag::err_invalid_use_of_array_type) |
| 429 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 430 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
| 431 | RecordDecl *RD = RT->getDecl(); |
| 432 | // The type-specifier-seq shall not declare a new class... |
| 433 | if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD))) |
| 434 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); |
| 435 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 436 | EnumDecl *ED = ET->getDecl(); |
| 437 | // ...or enumeration. |
| 438 | if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED))) |
| 439 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); |
| 440 | } |
| 441 | |
| 442 | DeclTy *Dcl = ActOnDeclarator(S, D, 0); |
| 443 | if (!Dcl) |
| 444 | return true; |
| 445 | AddInitializerToDecl(Dcl, AssignExprVal); |
| 446 | |
| 447 | return new CXXConditionDeclExpr(StartLoc, EqualLoc, |
| 448 | cast<VarDecl>(static_cast<Decl *>(Dcl))); |
| 449 | } |
| 450 | |
| 451 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 452 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 453 | // C++ 6.4p4: |
| 454 | // The value of a condition that is an initialized declaration in a statement |
| 455 | // other than a switch statement is the value of the declared variable |
| 456 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 457 | // program is ill-formed. |
| 458 | // The value of a condition that is an expression is the value of the |
| 459 | // expression, implicitly converted to bool. |
| 460 | // |
| 461 | QualType Ty = CondExpr->getType(); // Save the type. |
| 462 | AssignConvertType |
| 463 | ConvTy = CheckSingleAssignmentConstraints(Context.BoolTy, CondExpr); |
| 464 | if (ConvTy == Incompatible) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 465 | return Diag(CondExpr->getLocStart(), diag::err_typecheck_bool_condition) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 466 | << Ty << CondExpr->getSourceRange(); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 467 | return false; |
| 468 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 469 | |
| 470 | /// Helper function to determine whether this is the (deprecated) C++ |
| 471 | /// conversion from a string literal to a pointer to non-const char or |
| 472 | /// non-const wchar_t (for narrow and wide string literals, |
| 473 | /// respectively). |
| 474 | bool |
| 475 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 476 | // Look inside the implicit cast, if it exists. |
| 477 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 478 | From = Cast->getSubExpr(); |
| 479 | |
| 480 | // A string literal (2.13.4) that is not a wide string literal can |
| 481 | // be converted to an rvalue of type "pointer to char"; a wide |
| 482 | // string literal can be converted to an rvalue of type "pointer |
| 483 | // to wchar_t" (C++ 4.2p2). |
| 484 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
| 485 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) |
| 486 | if (const BuiltinType *ToPointeeType |
| 487 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { |
| 488 | // This conversion is considered only when there is an |
| 489 | // explicit appropriate pointer target type (C++ 4.2p2). |
| 490 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && |
| 491 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 492 | (!StrLit->isWide() && |
| 493 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 494 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | return false; |
| 499 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 500 | |
| 501 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 502 | /// expression From to the type ToType. Returns true if there was an |
| 503 | /// error, false otherwise. The expression From is replaced with the |
| 504 | /// converted expression. |
| 505 | bool |
| 506 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType) |
| 507 | { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 508 | ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 509 | switch (ICS.ConversionKind) { |
| 510 | case ImplicitConversionSequence::StandardConversion: |
| 511 | if (PerformImplicitConversion(From, ToType, ICS.Standard)) |
| 512 | return true; |
| 513 | break; |
| 514 | |
| 515 | case ImplicitConversionSequence::UserDefinedConversion: |
| 516 | // FIXME: This is, of course, wrong. We'll need to actually call |
| 517 | // the constructor or conversion operator, and then cope with the |
| 518 | // standard conversions. |
| 519 | ImpCastExprToType(From, ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 520 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 521 | |
| 522 | case ImplicitConversionSequence::EllipsisConversion: |
| 523 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 524 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 525 | |
| 526 | case ImplicitConversionSequence::BadConversion: |
| 527 | return true; |
| 528 | } |
| 529 | |
| 530 | // Everything went well. |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 535 | /// expression From to the type ToType by following the standard |
| 536 | /// conversion sequence SCS. Returns true if there was an error, false |
| 537 | /// otherwise. The expression From is replaced with the converted |
| 538 | /// expression. |
| 539 | bool |
| 540 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 541 | const StandardConversionSequence& SCS) |
| 542 | { |
| 543 | // Overall FIXME: we are recomputing too many types here and doing |
| 544 | // far too much extra work. What this means is that we need to keep |
| 545 | // track of more information that is computed when we try the |
| 546 | // implicit conversion initially, so that we don't need to recompute |
| 547 | // anything here. |
| 548 | QualType FromType = From->getType(); |
| 549 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 550 | if (SCS.CopyConstructor) { |
| 551 | // FIXME: Create a temporary object by calling the copy |
| 552 | // constructor. |
| 553 | ImpCastExprToType(From, ToType); |
| 554 | return false; |
| 555 | } |
| 556 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 557 | // Perform the first implicit conversion. |
| 558 | switch (SCS.First) { |
| 559 | case ICK_Identity: |
| 560 | case ICK_Lvalue_To_Rvalue: |
| 561 | // Nothing to do. |
| 562 | break; |
| 563 | |
| 564 | case ICK_Array_To_Pointer: |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 565 | if (FromType->isOverloadType()) { |
| 566 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); |
| 567 | if (!Fn) |
| 568 | return true; |
| 569 | |
| 570 | FixOverloadedFunctionReference(From, Fn); |
| 571 | FromType = From->getType(); |
| 572 | } else { |
| 573 | FromType = Context.getArrayDecayedType(FromType); |
| 574 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 575 | ImpCastExprToType(From, FromType); |
| 576 | break; |
| 577 | |
| 578 | case ICK_Function_To_Pointer: |
| 579 | FromType = Context.getPointerType(FromType); |
| 580 | ImpCastExprToType(From, FromType); |
| 581 | break; |
| 582 | |
| 583 | default: |
| 584 | assert(false && "Improper first standard conversion"); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | // Perform the second implicit conversion |
| 589 | switch (SCS.Second) { |
| 590 | case ICK_Identity: |
| 591 | // Nothing to do. |
| 592 | break; |
| 593 | |
| 594 | case ICK_Integral_Promotion: |
| 595 | case ICK_Floating_Promotion: |
| 596 | case ICK_Integral_Conversion: |
| 597 | case ICK_Floating_Conversion: |
| 598 | case ICK_Floating_Integral: |
| 599 | FromType = ToType.getUnqualifiedType(); |
| 600 | ImpCastExprToType(From, FromType); |
| 601 | break; |
| 602 | |
| 603 | case ICK_Pointer_Conversion: |
| 604 | if (CheckPointerConversion(From, ToType)) |
| 605 | return true; |
| 606 | ImpCastExprToType(From, ToType); |
| 607 | break; |
| 608 | |
| 609 | case ICK_Pointer_Member: |
| 610 | // FIXME: Implement pointer-to-member conversions. |
| 611 | assert(false && "Pointer-to-member conversions are unsupported"); |
| 612 | break; |
| 613 | |
| 614 | case ICK_Boolean_Conversion: |
| 615 | FromType = Context.BoolTy; |
| 616 | ImpCastExprToType(From, FromType); |
| 617 | break; |
| 618 | |
| 619 | default: |
| 620 | assert(false && "Improper second standard conversion"); |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | switch (SCS.Third) { |
| 625 | case ICK_Identity: |
| 626 | // Nothing to do. |
| 627 | break; |
| 628 | |
| 629 | case ICK_Qualification: |
| 630 | ImpCastExprToType(From, ToType); |
| 631 | break; |
| 632 | |
| 633 | default: |
| 634 | assert(false && "Improper second standard conversion"); |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | return false; |
| 639 | } |
| 640 | |