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" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 20 | /// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 22 | Sema::ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | SourceLocation LAngleBracketLoc, TypeTy *Ty, |
| 24 | SourceLocation RAngleBracketLoc, |
| 25 | SourceLocation LParenLoc, ExprTy *E, |
| 26 | SourceLocation RParenLoc) { |
| 27 | CXXCastExpr::Opcode Op; |
| 28 | |
| 29 | switch (Kind) { |
| 30 | default: assert(0 && "Unknown C++ cast!"); |
| 31 | case tok::kw_const_cast: Op = CXXCastExpr::ConstCast; break; |
| 32 | case tok::kw_dynamic_cast: Op = CXXCastExpr::DynamicCast; break; |
| 33 | case tok::kw_reinterpret_cast: Op = CXXCastExpr::ReinterpretCast; break; |
| 34 | case tok::kw_static_cast: Op = CXXCastExpr::StaticCast; break; |
| 35 | } |
| 36 | |
| 37 | return new CXXCastExpr(Op, QualType::getFromOpaquePtr(Ty), (Expr*)E, OpLoc); |
| 38 | } |
| 39 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 40 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 42 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | assert((Kind != tok::kw_true || Kind != tok::kw_false) && |
| 44 | "Unknown C++ Boolean value!"); |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 45 | return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 47 | |
| 48 | /// ActOnCXXThrow - Parse throw expressions. |
| 49 | Action::ExprResult |
| 50 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { |
| 51 | return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); |
| 52 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 53 | |
| 54 | Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
| 55 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 56 | /// is a non-lvalue expression whose value is the address of the object for |
| 57 | /// which the function is called. |
| 58 | |
| 59 | if (!isa<FunctionDecl>(CurContext)) { |
| 60 | Diag(ThisLoc, diag::err_invalid_this_use); |
| 61 | return ExprResult(true); |
| 62 | } |
| 63 | |
| 64 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 65 | if (MD->isInstance()) |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 66 | return new PredefinedExpr(ThisLoc, MD->getThisType(Context), |
| 67 | PredefinedExpr::CXXThis); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 68 | |
| 69 | return Diag(ThisLoc, diag::err_invalid_this_use); |
| 70 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame^] | 71 | |
| 72 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 73 | /// Can be interpreted either as function-style casting ("int(x)") |
| 74 | /// or class type construction ("ClassType(x,y,z)") |
| 75 | /// or creation of a value-initialized type ("int()"). |
| 76 | Action::ExprResult |
| 77 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 78 | SourceLocation LParenLoc, |
| 79 | ExprTy **ExprTys, unsigned NumExprs, |
| 80 | SourceLocation *CommaLocs, |
| 81 | SourceLocation RParenLoc) { |
| 82 | assert(TypeRep && "Missing type!"); |
| 83 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
| 84 | Expr **Exprs = (Expr**)ExprTys; |
| 85 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 86 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 87 | |
| 88 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 89 | // C++ 5.2.3p1: |
| 90 | // If the simple-type-specifier specifies a class type, the class type shall |
| 91 | // be complete. |
| 92 | // |
| 93 | if (!RT->getDecl()->isDefinition()) |
| 94 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use, |
| 95 | Ty.getAsString(), FullRange); |
| 96 | |
| 97 | // "class constructors are not supported yet" |
| 98 | return Diag(TyBeginLoc, diag::err_unsupported_class_constructor, FullRange); |
| 99 | } |
| 100 | |
| 101 | // C++ 5.2.3p1: |
| 102 | // If the expression list is a single expression, the type conversion |
| 103 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 104 | // corresponding cast expression. |
| 105 | // |
| 106 | if (NumExprs == 1) { |
| 107 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
| 108 | return true; |
| 109 | return new CXXFunctionalCastExpr(Ty, TyBeginLoc, Exprs[0], RParenLoc); |
| 110 | } |
| 111 | |
| 112 | // C++ 5.2.3p1: |
| 113 | // If the expression list specifies more than a single value, the type shall |
| 114 | // be a class with a suitably declared constructor. |
| 115 | // |
| 116 | if (NumExprs > 1) |
| 117 | return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg, |
| 118 | FullRange); |
| 119 | |
| 120 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 121 | |
| 122 | // C++ 5.2.3p2: |
| 123 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 124 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 125 | // rvalue of the specified type, which is value-initialized. |
| 126 | // |
| 127 | if (Ty->isArrayType()) |
| 128 | return Diag(TyBeginLoc, diag::err_value_init_for_array_type, FullRange); |
| 129 | if (Ty->isIncompleteType() && !Ty->isVoidType()) |
| 130 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use, |
| 131 | Ty.getAsString(), FullRange); |
| 132 | |
| 133 | return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); |
| 134 | } |