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" |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 24 | /// ActOnCXXConversionFunctionExpr - Parse a C++ conversion function |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 25 | /// name (e.g., operator void const *) as an expression. This is |
| 26 | /// very similar to ActOnIdentifierExpr, except that instead of |
| 27 | /// providing an identifier the parser provides the type of the |
| 28 | /// conversion function. |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 29 | Sema::ExprResult |
| 30 | Sema::ActOnCXXConversionFunctionExpr(Scope *S, SourceLocation OperatorLoc, |
| 31 | TypeTy *Ty, bool HasTrailingLParen, |
| 32 | const CXXScopeSpec &SS) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 33 | QualType ConvType = QualType::getFromOpaquePtr(Ty); |
| 34 | QualType ConvTypeCanon = Context.getCanonicalType(ConvType); |
| 35 | DeclarationName ConvName |
| 36 | = Context.DeclarationNames.getCXXConversionFunctionName(ConvTypeCanon); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 37 | return ActOnDeclarationNameExpr(S, OperatorLoc, ConvName, HasTrailingLParen, |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 38 | &SS); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 39 | } |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 41 | /// ActOnCXXOperatorFunctionIdExpr - Parse a C++ overloaded operator |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 42 | /// name (e.g., @c operator+ ) as an expression. This is very |
| 43 | /// similar to ActOnIdentifierExpr, except that instead of providing |
| 44 | /// an identifier the parser provides the kind of overloaded |
| 45 | /// operator that was parsed. |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 46 | Sema::ExprResult |
| 47 | Sema::ActOnCXXOperatorFunctionIdExpr(Scope *S, SourceLocation OperatorLoc, |
| 48 | OverloadedOperatorKind Op, |
| 49 | bool HasTrailingLParen, |
| 50 | const CXXScopeSpec &SS) { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 51 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 487a75a | 2008-11-19 19:09:45 +0000 | [diff] [blame] | 52 | return ActOnDeclarationNameExpr(S, OperatorLoc, Name, HasTrailingLParen, &SS); |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 55 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). |
| 56 | Action::ExprResult |
| 57 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 58 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
| 59 | const NamespaceDecl *StdNs = GetStdNamespace(); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 60 | if (!StdNs) |
| 61 | return Diag(OpLoc, diag::err_need_header_before_typeid); |
| 62 | |
| 63 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 64 | Decl *TypeInfoDecl = LookupDecl(TypeInfoII, |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 65 | Decl::IDNS_Tag | Decl::IDNS_Ordinary, |
| 66 | 0, StdNs, /*createBuiltins=*/false); |
| 67 | RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); |
Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 68 | if (!TypeInfoRecordDecl) |
| 69 | return Diag(OpLoc, diag::err_need_header_before_typeid); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 70 | |
| 71 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); |
| 72 | |
| 73 | return new CXXTypeidExpr(isType, TyOrExpr, TypeInfoType.withConst(), |
| 74 | SourceRange(OpLoc, RParenLoc)); |
| 75 | } |
| 76 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 77 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 79 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 80 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | "Unknown C++ Boolean value!"); |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 82 | return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 84 | |
| 85 | /// ActOnCXXThrow - Parse throw expressions. |
| 86 | Action::ExprResult |
| 87 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { |
| 88 | return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); |
| 89 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 90 | |
| 91 | Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
| 92 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 93 | /// is a non-lvalue expression whose value is the address of the object for |
| 94 | /// which the function is called. |
| 95 | |
| 96 | if (!isa<FunctionDecl>(CurContext)) { |
| 97 | Diag(ThisLoc, diag::err_invalid_this_use); |
| 98 | return ExprResult(true); |
| 99 | } |
| 100 | |
| 101 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 102 | if (MD->isInstance()) |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 103 | return new CXXThisExpr(ThisLoc, MD->getThisType(Context)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 104 | |
| 105 | return Diag(ThisLoc, diag::err_invalid_this_use); |
| 106 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 107 | |
| 108 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 109 | /// Can be interpreted either as function-style casting ("int(x)") |
| 110 | /// or class type construction ("ClassType(x,y,z)") |
| 111 | /// or creation of a value-initialized type ("int()"). |
| 112 | Action::ExprResult |
| 113 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 114 | SourceLocation LParenLoc, |
| 115 | ExprTy **ExprTys, unsigned NumExprs, |
| 116 | SourceLocation *CommaLocs, |
| 117 | SourceLocation RParenLoc) { |
| 118 | assert(TypeRep && "Missing type!"); |
| 119 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
| 120 | Expr **Exprs = (Expr**)ExprTys; |
| 121 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 122 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 123 | |
| 124 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 125 | // C++ 5.2.3p1: |
| 126 | // If the simple-type-specifier specifies a class type, the class type shall |
| 127 | // be complete. |
| 128 | // |
| 129 | if (!RT->getDecl()->isDefinition()) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 130 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 131 | << Ty << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 132 | |
Argyrios Kyrtzidis | 4021a84 | 2008-10-06 23:16:35 +0000 | [diff] [blame] | 133 | unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, |
| 134 | "class constructors are not supported yet"); |
| 135 | return Diag(TyBeginLoc, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // C++ 5.2.3p1: |
| 139 | // If the expression list is a single expression, the type conversion |
| 140 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 141 | // corresponding cast expression. |
| 142 | // |
| 143 | if (NumExprs == 1) { |
| 144 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
| 145 | return true; |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 146 | return new CXXFunctionalCastExpr(Ty.getNonReferenceType(), Ty, TyBeginLoc, |
| 147 | Exprs[0], RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // C++ 5.2.3p1: |
| 151 | // If the expression list specifies more than a single value, the type shall |
| 152 | // be a class with a suitably declared constructor. |
| 153 | // |
| 154 | if (NumExprs > 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 155 | return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg) |
| 156 | << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 157 | |
| 158 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 159 | |
| 160 | // C++ 5.2.3p2: |
| 161 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 162 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 163 | // rvalue of the specified type, which is value-initialized. |
| 164 | // |
| 165 | if (Ty->isArrayType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 166 | return Diag(TyBeginLoc, diag::err_value_init_for_array_type) << FullRange; |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 167 | if (!Ty->isDependentType() && Ty->isIncompleteType() && !Ty->isVoidType()) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 168 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 169 | << Ty << FullRange; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 170 | |
| 171 | return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); |
| 172 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 173 | |
| 174 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 175 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 176 | /// @code new (memory) int[size][4] @endcode |
| 177 | /// or |
| 178 | /// @code ::new Foo(23, "hello") @endcode |
| 179 | /// For the interpretation of this heap of arguments, consult the base version. |
| 180 | Action::ExprResult |
| 181 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 182 | SourceLocation PlacementLParen, |
| 183 | ExprTy **PlacementArgs, unsigned NumPlaceArgs, |
| 184 | SourceLocation PlacementRParen, bool ParenTypeId, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 185 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 186 | ExprTy **ConstructorArgs, unsigned NumConsArgs, |
| 187 | SourceLocation ConstructorRParen) |
| 188 | { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 189 | // FIXME: Throughout this function, we have rather bad location information. |
| 190 | // Implementing Declarator::getSourceRange() would go a long way toward |
| 191 | // fixing that. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 192 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 193 | Expr *ArraySize = 0; |
| 194 | unsigned Skip = 0; |
| 195 | // If the specified type is an array, unwrap it and save the expression. |
| 196 | if (D.getNumTypeObjects() > 0 && |
| 197 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 198 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 199 | if (Chunk.Arr.hasStatic) |
| 200 | return Diag(Chunk.Loc, diag::err_static_illegal_in_new); |
| 201 | if (!Chunk.Arr.NumElts) |
| 202 | return Diag(Chunk.Loc, diag::err_array_new_needs_size); |
| 203 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
| 204 | Skip = 1; |
| 205 | } |
| 206 | |
| 207 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, Skip); |
| 208 | if (D.getInvalidType()) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 209 | return true; |
| 210 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 211 | if (CheckAllocatedType(AllocType, D)) |
| 212 | return true; |
| 213 | |
| 214 | QualType ResultType = Context.getPointerType(AllocType); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 215 | |
| 216 | // That every array dimension except the first is constant was already |
| 217 | // checked by the type check above. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 218 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 219 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 220 | // or enumeration type with a non-negative value." |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 221 | if (ArraySize) { |
| 222 | QualType SizeType = ArraySize->getType(); |
| 223 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) |
| 224 | return Diag(ArraySize->getSourceRange().getBegin(), |
| 225 | diag::err_array_size_not_integral) |
| 226 | << SizeType << ArraySize->getSourceRange(); |
| 227 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 228 | // We don't care about special rules, so we tell the machinery it's not |
| 229 | // evaluated - it gives us a result in more cases. |
| 230 | llvm::APSInt Value; |
| 231 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 232 | if (Value < llvm::APSInt( |
| 233 | llvm::APInt::getNullValue(Value.getBitWidth()), false)) |
| 234 | return Diag(ArraySize->getSourceRange().getBegin(), |
| 235 | diag::err_typecheck_negative_array_size) |
| 236 | << ArraySize->getSourceRange(); |
| 237 | } |
| 238 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 239 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 240 | FunctionDecl *OperatorNew = 0; |
| 241 | FunctionDecl *OperatorDelete = 0; |
| 242 | Expr **PlaceArgs = (Expr**)PlacementArgs; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 243 | if (FindAllocationFunctions(StartLoc, UseGlobal, AllocType, ArraySize, |
| 244 | PlaceArgs, NumPlaceArgs, OperatorNew, |
| 245 | OperatorDelete)) |
| 246 | return true; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 247 | |
| 248 | bool Init = ConstructorLParen.isValid(); |
| 249 | // --- Choosing a constructor --- |
| 250 | // C++ 5.3.4p15 |
| 251 | // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) |
| 252 | // the object is not initialized. If the object, or any part of it, is |
| 253 | // const-qualified, it's an error. |
| 254 | // 2) If T is a POD and there's an empty initializer, the object is value- |
| 255 | // initialized. |
| 256 | // 3) If T is a POD and there's one initializer argument, the object is copy- |
| 257 | // constructed. |
| 258 | // 4) If T is a POD and there's more initializer arguments, it's an error. |
| 259 | // 5) If T is not a POD, the initializer arguments are used as constructor |
| 260 | // arguments. |
| 261 | // |
| 262 | // Or by the C++0x formulation: |
| 263 | // 1) If there's no initializer, the object is default-initialized according |
| 264 | // to C++0x rules. |
| 265 | // 2) Otherwise, the object is direct-initialized. |
| 266 | CXXConstructorDecl *Constructor = 0; |
| 267 | Expr **ConsArgs = (Expr**)ConstructorArgs; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 268 | if (const RecordType *RT = AllocType->getAsRecordType()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 269 | // FIXME: This is incorrect for when there is an empty initializer and |
| 270 | // no user-defined constructor. Must zero-initialize, not default-construct. |
| 271 | Constructor = PerformInitializationByConstructor( |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 272 | AllocType, ConsArgs, NumConsArgs, |
| 273 | D.getDeclSpec().getSourceRange().getBegin(), |
| 274 | SourceRange(D.getDeclSpec().getSourceRange().getBegin(), |
| 275 | ConstructorRParen), |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 276 | RT->getDecl()->getDeclName(), |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 277 | NumConsArgs != 0 ? IK_Direct : IK_Default); |
| 278 | if (!Constructor) |
| 279 | return true; |
| 280 | } else { |
| 281 | if (!Init) { |
| 282 | // FIXME: Check that no subpart is const. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 283 | if (AllocType.isConstQualified()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 284 | Diag(StartLoc, diag::err_new_uninitialized_const) |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 285 | << D.getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | } else if (NumConsArgs == 0) { |
| 289 | // Object is value-initialized. Do nothing. |
| 290 | } else if (NumConsArgs == 1) { |
| 291 | // Object is direct-initialized. |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 292 | // FIXME: WHAT DeclarationName do we pass in here? |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 293 | if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame^] | 294 | DeclarationName() /*AllocType.getAsString()*/, |
| 295 | /*DirectInit=*/true)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 296 | return true; |
| 297 | } else { |
| 298 | Diag(StartLoc, diag::err_builtin_direct_init_more_than_one_arg) |
| 299 | << SourceRange(ConstructorLParen, ConstructorRParen); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
| 304 | |
| 305 | return new CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, NumPlaceArgs, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 306 | ParenTypeId, ArraySize, Constructor, Init, |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 307 | ConsArgs, NumConsArgs, OperatorDelete, ResultType, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 308 | StartLoc, Init ? ConstructorRParen : SourceLocation()); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 312 | /// in a new-expression. |
| 313 | /// dimension off and stores the size expression in ArraySize. |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 314 | bool Sema::CheckAllocatedType(QualType AllocType, const Declarator &D) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 315 | { |
| 316 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 317 | // abstract class type or array thereof. |
| 318 | // FIXME: We don't have abstract types yet. |
| 319 | // FIXME: Under C++ semantics, an incomplete object type is still an object |
| 320 | // type. This code assumes the C semantics, where it's not. |
| 321 | if (!AllocType->isObjectType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 322 | unsigned type; // For the select in the message. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 323 | if (AllocType->isFunctionType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 324 | type = 0; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 325 | } else if(AllocType->isIncompleteType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 326 | type = 1; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 327 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 328 | assert(AllocType->isReferenceType() && "What else could it be?"); |
| 329 | type = 2; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 330 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 331 | SourceRange TyR = D.getDeclSpec().getSourceRange(); |
| 332 | // FIXME: This is very much a guess and won't work for, e.g., pointers. |
| 333 | if (D.getNumTypeObjects() > 0) |
| 334 | TyR.setEnd(D.getTypeObject(0).Loc); |
| 335 | Diag(TyR.getBegin(), diag::err_bad_new_type) |
| 336 | << AllocType.getAsString() << type << TyR; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 340 | // Every dimension shall be of constant size. |
| 341 | unsigned i = 1; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 342 | while (const ArrayType *Array = Context.getAsArrayType(AllocType)) { |
| 343 | if (!Array->isConstantArrayType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 344 | Diag(D.getTypeObject(i).Loc, diag::err_new_array_nonconst) |
| 345 | << static_cast<Expr*>(D.getTypeObject(i).Arr.NumElts)->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | AllocType = Array->getElementType(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 349 | ++i; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | return false; |
| 353 | } |
| 354 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 355 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 356 | /// that are appropriate for the allocation. |
| 357 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, bool UseGlobal, |
| 358 | QualType AllocType, bool IsArray, |
| 359 | Expr **PlaceArgs, unsigned NumPlaceArgs, |
| 360 | FunctionDecl *&OperatorNew, |
| 361 | FunctionDecl *&OperatorDelete) |
| 362 | { |
| 363 | // --- Choosing an allocation function --- |
| 364 | // C++ 5.3.4p8 - 14 & 18 |
| 365 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 366 | // in the scope of the allocated class. |
| 367 | // 2) If an array size is given, look for operator new[], else look for |
| 368 | // operator new. |
| 369 | // 3) The first argument is always size_t. Append the arguments from the |
| 370 | // placement form. |
| 371 | // FIXME: Also find the appropriate delete operator. |
| 372 | |
| 373 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
| 374 | // We don't care about the actual value of this argument. |
| 375 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 376 | // tree? Or should the consumer just recalculate the value? |
| 377 | AllocArgs[0] = new IntegerLiteral(llvm::APInt::getNullValue( |
| 378 | Context.Target.getPointerWidth(0)), |
| 379 | Context.getSizeType(), |
| 380 | SourceLocation()); |
| 381 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 382 | |
| 383 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 384 | IsArray ? OO_Array_New : OO_New); |
| 385 | if (AllocType->isRecordType() && !UseGlobal) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 386 | CXXRecordDecl *Record = cast<CXXRecordType>(AllocType->getAsRecordType()) |
| 387 | ->getDecl(); |
| 388 | // FIXME: We fail to find inherited overloads. |
| 389 | if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0], |
| 390 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 391 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 392 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 393 | } |
| 394 | if (!OperatorNew) { |
| 395 | // Didn't find a member overload. Look for a global one. |
| 396 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 397 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
| 398 | if (FindAllocationOverload(StartLoc, NewName, &AllocArgs[0], |
| 399 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 400 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 401 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 404 | // FIXME: This is leaked on error. But so much is currently in Sema that it's |
| 405 | // easier to clean it in one go. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 406 | AllocArgs[0]->Destroy(Context); |
| 407 | return false; |
| 408 | } |
| 409 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 410 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 411 | /// function in the specified scope. |
| 412 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, DeclarationName Name, |
| 413 | Expr** Args, unsigned NumArgs, |
| 414 | DeclContext *Ctx, bool AllowMissing, |
| 415 | FunctionDecl *&Operator) |
| 416 | { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 417 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 418 | llvm::tie(Alloc, AllocEnd) = Ctx->lookup(Name); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 419 | if (Alloc == AllocEnd) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 420 | if (AllowMissing) |
| 421 | return false; |
| 422 | // FIXME: Bad location information. |
| 423 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
| 424 | << Name << 0; |
| 425 | } |
| 426 | |
| 427 | OverloadCandidateSet Candidates; |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 428 | for (; Alloc != AllocEnd; ++Alloc) { |
| 429 | // Even member operator new/delete are implicitly treated as |
| 430 | // static, so don't use AddMemberCandidate. |
| 431 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) |
| 432 | AddOverloadCandidate(Fn, Args, NumArgs, Candidates, |
| 433 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // Do the resolution. |
| 437 | OverloadCandidateSet::iterator Best; |
| 438 | switch(BestViableFunction(Candidates, Best)) { |
| 439 | case OR_Success: { |
| 440 | // Got one! |
| 441 | FunctionDecl *FnDecl = Best->Function; |
| 442 | // The first argument is size_t, and the first parameter must be size_t, |
| 443 | // too. This is checked on declaration and can be assumed. (It can't be |
| 444 | // asserted on, though, since invalid decls are left in there.) |
| 445 | for (unsigned i = 1; i < NumArgs; ++i) { |
| 446 | // FIXME: Passing word to diagnostic. |
| 447 | if (PerformCopyInitialization(Args[i-1], |
| 448 | FnDecl->getParamDecl(i)->getType(), |
| 449 | "passing")) |
| 450 | return true; |
| 451 | } |
| 452 | Operator = FnDecl; |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | case OR_No_Viable_Function: |
| 457 | if (AllowMissing) |
| 458 | return false; |
| 459 | // FIXME: Bad location information. |
| 460 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
| 461 | << Name << (unsigned)Candidates.size(); |
| 462 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/false); |
| 463 | return true; |
| 464 | |
| 465 | case OR_Ambiguous: |
| 466 | // FIXME: Bad location information. |
| 467 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
| 468 | << Name; |
| 469 | PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); |
| 470 | return true; |
| 471 | } |
| 472 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 473 | return true; |
| 474 | } |
| 475 | |
| 476 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 477 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 478 | /// delete. These are: |
| 479 | /// @code |
| 480 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 481 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 482 | /// void operator delete(void *) throw(); |
| 483 | /// void operator delete[](void *) throw(); |
| 484 | /// @endcode |
| 485 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 486 | /// declared. Their use requires including \<new\>. |
| 487 | void Sema::DeclareGlobalNewDelete() |
| 488 | { |
| 489 | if (GlobalNewDeleteDeclared) |
| 490 | return; |
| 491 | GlobalNewDeleteDeclared = true; |
| 492 | |
| 493 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 494 | QualType SizeT = Context.getSizeType(); |
| 495 | |
| 496 | // FIXME: Exception specifications are not added. |
| 497 | DeclareGlobalAllocationFunction( |
| 498 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
| 499 | VoidPtr, SizeT); |
| 500 | DeclareGlobalAllocationFunction( |
| 501 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
| 502 | VoidPtr, SizeT); |
| 503 | DeclareGlobalAllocationFunction( |
| 504 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 505 | Context.VoidTy, VoidPtr); |
| 506 | DeclareGlobalAllocationFunction( |
| 507 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 508 | Context.VoidTy, VoidPtr); |
| 509 | } |
| 510 | |
| 511 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 512 | /// allocation function if it doesn't already exist. |
| 513 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
| 514 | QualType Return, QualType Argument) |
| 515 | { |
| 516 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 517 | |
| 518 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 519 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 520 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 521 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 522 | Alloc != AllocEnd; ++Alloc) { |
| 523 | // FIXME: Do we need to check for default arguments here? |
| 524 | FunctionDecl *Func = cast<FunctionDecl>(*Alloc); |
| 525 | if (Func->getNumParams() == 1 && |
| 526 | Context.getCanonicalType(Func->getParamDecl(0)->getType()) == Argument) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 527 | return; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0); |
| 532 | FunctionDecl *Alloc = |
| 533 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, |
| 534 | FnType, FunctionDecl::None, false, 0, |
| 535 | SourceLocation()); |
| 536 | Alloc->setImplicit(); |
| 537 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
| 538 | 0, Argument, VarDecl::None, 0, 0); |
Ted Kremenek | fc76761 | 2009-01-14 00:42:25 +0000 | [diff] [blame] | 539 | Alloc->setParams(Context, &Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 540 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 541 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 542 | // make sure it is at the end of the chain to coincide with the |
| 543 | // global scope. |
Douglas Gregor | 482b77d | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 544 | ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 547 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 548 | /// @code ::delete ptr; @endcode |
| 549 | /// or |
| 550 | /// @code delete [] ptr; @endcode |
| 551 | Action::ExprResult |
| 552 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
| 553 | bool ArrayForm, ExprTy *Operand) |
| 554 | { |
| 555 | // C++ 5.3.5p1: "The operand shall have a pointer type, or a class type |
| 556 | // having a single conversion function to a pointer type. The result has |
| 557 | // type void." |
| 558 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 559 | |
| 560 | Expr *Ex = (Expr *)Operand; |
| 561 | QualType Type = Ex->getType(); |
| 562 | |
| 563 | if (Type->isRecordType()) { |
| 564 | // FIXME: Find that one conversion function and amend the type. |
| 565 | } |
| 566 | |
| 567 | if (!Type->isPointerType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 568 | Diag(StartLoc, diag::err_delete_operand) << Type << Ex->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 569 | return true; |
| 570 | } |
| 571 | |
| 572 | QualType Pointee = Type->getAsPointerType()->getPointeeType(); |
| 573 | if (Pointee->isIncompleteType() && !Pointee->isVoidType()) |
| 574 | Diag(StartLoc, diag::warn_delete_incomplete) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 575 | << Pointee << Ex->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 576 | else if (!Pointee->isObjectType()) { |
| 577 | Diag(StartLoc, diag::err_delete_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 578 | << Type << Ex->getSourceRange(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 579 | return true; |
| 580 | } |
| 581 | |
| 582 | // FIXME: Look up the correct operator delete overload and pass a pointer |
| 583 | // along. |
| 584 | // FIXME: Check access and ambiguity of operator delete and destructor. |
| 585 | |
| 586 | return new CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 0, Ex, |
| 587 | StartLoc); |
| 588 | } |
| 589 | |
| 590 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 591 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 592 | /// C++ if/switch/while/for statement. |
| 593 | /// e.g: "if (int x = f()) {...}" |
| 594 | Action::ExprResult |
| 595 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 596 | Declarator &D, |
| 597 | SourceLocation EqualLoc, |
| 598 | ExprTy *AssignExprVal) { |
| 599 | assert(AssignExprVal && "Null assignment expression"); |
| 600 | |
| 601 | // C++ 6.4p2: |
| 602 | // The declarator shall not specify a function or an array. |
| 603 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 604 | // new class or enumeration. |
| 605 | |
| 606 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 607 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 608 | |
| 609 | QualType Ty = GetTypeForDeclarator(D, S); |
| 610 | |
| 611 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 612 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 613 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 614 | return Diag(StartLoc, diag::err_invalid_use_of_function_type) |
| 615 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 616 | } else if (Ty->isArrayType()) { // ...or an array. |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 617 | Diag(StartLoc, diag::err_invalid_use_of_array_type) |
| 618 | << SourceRange(StartLoc, EqualLoc); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 619 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
| 620 | RecordDecl *RD = RT->getDecl(); |
| 621 | // The type-specifier-seq shall not declare a new class... |
| 622 | if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD))) |
| 623 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); |
| 624 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 625 | EnumDecl *ED = ET->getDecl(); |
| 626 | // ...or enumeration. |
| 627 | if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED))) |
| 628 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); |
| 629 | } |
| 630 | |
| 631 | DeclTy *Dcl = ActOnDeclarator(S, D, 0); |
| 632 | if (!Dcl) |
| 633 | return true; |
Sebastian Redl | 798d119 | 2008-12-13 16:23:55 +0000 | [diff] [blame] | 634 | AddInitializerToDecl(Dcl, ExprArg(*this, AssignExprVal)); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 635 | |
Douglas Gregor | caaf29a | 2008-12-10 23:01:14 +0000 | [diff] [blame] | 636 | // Mark this variable as one that is declared within a conditional. |
| 637 | if (VarDecl *VD = dyn_cast<VarDecl>((Decl *)Dcl)) |
| 638 | VD->setDeclaredInCondition(true); |
| 639 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 640 | return new CXXConditionDeclExpr(StartLoc, EqualLoc, |
| 641 | cast<VarDecl>(static_cast<Decl *>(Dcl))); |
| 642 | } |
| 643 | |
| 644 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 645 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 646 | // C++ 6.4p4: |
| 647 | // The value of a condition that is an initialized declaration in a statement |
| 648 | // other than a switch statement is the value of the declared variable |
| 649 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 650 | // program is ill-formed. |
| 651 | // The value of a condition that is an expression is the value of the |
| 652 | // expression, implicitly converted to bool. |
| 653 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame^] | 654 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 655 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 656 | |
| 657 | /// Helper function to determine whether this is the (deprecated) C++ |
| 658 | /// conversion from a string literal to a pointer to non-const char or |
| 659 | /// non-const wchar_t (for narrow and wide string literals, |
| 660 | /// respectively). |
| 661 | bool |
| 662 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 663 | // Look inside the implicit cast, if it exists. |
| 664 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 665 | From = Cast->getSubExpr(); |
| 666 | |
| 667 | // A string literal (2.13.4) that is not a wide string literal can |
| 668 | // be converted to an rvalue of type "pointer to char"; a wide |
| 669 | // string literal can be converted to an rvalue of type "pointer |
| 670 | // to wchar_t" (C++ 4.2p2). |
| 671 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
| 672 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) |
| 673 | if (const BuiltinType *ToPointeeType |
| 674 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { |
| 675 | // This conversion is considered only when there is an |
| 676 | // explicit appropriate pointer target type (C++ 4.2p2). |
| 677 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && |
| 678 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 679 | (!StrLit->isWide() && |
| 680 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 681 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | return false; |
| 686 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 687 | |
| 688 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 689 | /// expression From to the type ToType. Returns true if there was an |
| 690 | /// error, false otherwise. The expression From is replaced with the |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 691 | /// converted expression. Flavor is the kind of conversion we're |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame^] | 692 | /// performing, used in the error message. If @p AllowExplicit, |
| 693 | /// explicit user-defined conversions are permitted. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 694 | bool |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 695 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame^] | 696 | const char *Flavor, bool AllowExplicit) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 697 | { |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame^] | 698 | ImplicitConversionSequence ICS = TryImplicitConversion(From, ToType, false, |
| 699 | AllowExplicit); |
| 700 | return PerformImplicitConversion(From, ToType, ICS, Flavor); |
| 701 | } |
| 702 | |
| 703 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 704 | /// expression From to the type ToType using the pre-computed implicit |
| 705 | /// conversion sequence ICS. Returns true if there was an error, false |
| 706 | /// otherwise. The expression From is replaced with the converted |
| 707 | /// expression. Flavor is the kind of conversion we're performing, |
| 708 | /// used in the error message. |
| 709 | bool |
| 710 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 711 | const ImplicitConversionSequence &ICS, |
| 712 | const char* Flavor) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 713 | switch (ICS.ConversionKind) { |
| 714 | case ImplicitConversionSequence::StandardConversion: |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 715 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 716 | return true; |
| 717 | break; |
| 718 | |
| 719 | case ImplicitConversionSequence::UserDefinedConversion: |
| 720 | // FIXME: This is, of course, wrong. We'll need to actually call |
| 721 | // the constructor or conversion operator, and then cope with the |
| 722 | // standard conversions. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame^] | 723 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
| 724 | ToType->isReferenceType()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 725 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 726 | |
| 727 | case ImplicitConversionSequence::EllipsisConversion: |
| 728 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 729 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 730 | |
| 731 | case ImplicitConversionSequence::BadConversion: |
| 732 | return true; |
| 733 | } |
| 734 | |
| 735 | // Everything went well. |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 740 | /// expression From to the type ToType by following the standard |
| 741 | /// conversion sequence SCS. Returns true if there was an error, false |
| 742 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 743 | /// expression. Flavor is the context in which we're performing this |
| 744 | /// conversion, for use in error messages. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 745 | bool |
| 746 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 747 | const StandardConversionSequence& SCS, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame^] | 748 | const char *Flavor) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 749 | // Overall FIXME: we are recomputing too many types here and doing |
| 750 | // far too much extra work. What this means is that we need to keep |
| 751 | // track of more information that is computed when we try the |
| 752 | // implicit conversion initially, so that we don't need to recompute |
| 753 | // anything here. |
| 754 | QualType FromType = From->getType(); |
| 755 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 756 | if (SCS.CopyConstructor) { |
| 757 | // FIXME: Create a temporary object by calling the copy |
| 758 | // constructor. |
| 759 | ImpCastExprToType(From, ToType); |
| 760 | return false; |
| 761 | } |
| 762 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 763 | // Perform the first implicit conversion. |
| 764 | switch (SCS.First) { |
| 765 | case ICK_Identity: |
| 766 | case ICK_Lvalue_To_Rvalue: |
| 767 | // Nothing to do. |
| 768 | break; |
| 769 | |
| 770 | case ICK_Array_To_Pointer: |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 771 | if (FromType->isOverloadType()) { |
| 772 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); |
| 773 | if (!Fn) |
| 774 | return true; |
| 775 | |
| 776 | FixOverloadedFunctionReference(From, Fn); |
| 777 | FromType = From->getType(); |
| 778 | } else { |
| 779 | FromType = Context.getArrayDecayedType(FromType); |
| 780 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 781 | ImpCastExprToType(From, FromType); |
| 782 | break; |
| 783 | |
| 784 | case ICK_Function_To_Pointer: |
| 785 | FromType = Context.getPointerType(FromType); |
| 786 | ImpCastExprToType(From, FromType); |
| 787 | break; |
| 788 | |
| 789 | default: |
| 790 | assert(false && "Improper first standard conversion"); |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | // Perform the second implicit conversion |
| 795 | switch (SCS.Second) { |
| 796 | case ICK_Identity: |
| 797 | // Nothing to do. |
| 798 | break; |
| 799 | |
| 800 | case ICK_Integral_Promotion: |
| 801 | case ICK_Floating_Promotion: |
| 802 | case ICK_Integral_Conversion: |
| 803 | case ICK_Floating_Conversion: |
| 804 | case ICK_Floating_Integral: |
| 805 | FromType = ToType.getUnqualifiedType(); |
| 806 | ImpCastExprToType(From, FromType); |
| 807 | break; |
| 808 | |
| 809 | case ICK_Pointer_Conversion: |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 810 | if (SCS.IncompatibleObjC) { |
| 811 | // Diagnose incompatible Objective-C conversions |
| 812 | Diag(From->getSourceRange().getBegin(), |
| 813 | diag::ext_typecheck_convert_incompatible_pointer) |
| 814 | << From->getType() << ToType << Flavor |
| 815 | << From->getSourceRange(); |
| 816 | } |
| 817 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 818 | if (CheckPointerConversion(From, ToType)) |
| 819 | return true; |
| 820 | ImpCastExprToType(From, ToType); |
| 821 | break; |
| 822 | |
| 823 | case ICK_Pointer_Member: |
| 824 | // FIXME: Implement pointer-to-member conversions. |
| 825 | assert(false && "Pointer-to-member conversions are unsupported"); |
| 826 | break; |
| 827 | |
| 828 | case ICK_Boolean_Conversion: |
| 829 | FromType = Context.BoolTy; |
| 830 | ImpCastExprToType(From, FromType); |
| 831 | break; |
| 832 | |
| 833 | default: |
| 834 | assert(false && "Improper second standard conversion"); |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | switch (SCS.Third) { |
| 839 | case ICK_Identity: |
| 840 | // Nothing to do. |
| 841 | break; |
| 842 | |
| 843 | case ICK_Qualification: |
| 844 | ImpCastExprToType(From, ToType); |
| 845 | break; |
| 846 | |
| 847 | default: |
| 848 | assert(false && "Improper second standard conversion"); |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | return false; |
| 853 | } |
| 854 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 855 | Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, |
| 856 | SourceLocation KWLoc, |
| 857 | SourceLocation LParen, |
| 858 | TypeTy *Ty, |
| 859 | SourceLocation RParen) { |
| 860 | // FIXME: Some of the type traits have requirements. Interestingly, only the |
| 861 | // __is_base_of requirement is explicitly stated to be diagnosed. Indeed, |
| 862 | // G++ accepts __is_pod(Incomplete) without complaints, and claims that the |
| 863 | // type is indeed a POD. |
| 864 | |
| 865 | // There is no point in eagerly computing the value. The traits are designed |
| 866 | // to be used from type trait templates, so Ty will be a template parameter |
| 867 | // 99% of the time. |
| 868 | return Owned(new UnaryTypeTraitExpr(KWLoc, OTT, |
| 869 | QualType::getFromOpaquePtr(Ty), |
| 870 | RParen, Context.BoolTy)); |
| 871 | } |