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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 19 | /// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 21 | Sema::ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | SourceLocation LAngleBracketLoc, TypeTy *Ty, |
| 23 | SourceLocation RAngleBracketLoc, |
| 24 | SourceLocation LParenLoc, ExprTy *E, |
| 25 | SourceLocation RParenLoc) { |
| 26 | CXXCastExpr::Opcode Op; |
| 27 | |
| 28 | switch (Kind) { |
| 29 | default: assert(0 && "Unknown C++ cast!"); |
| 30 | case tok::kw_const_cast: Op = CXXCastExpr::ConstCast; break; |
| 31 | case tok::kw_dynamic_cast: Op = CXXCastExpr::DynamicCast; break; |
| 32 | case tok::kw_reinterpret_cast: Op = CXXCastExpr::ReinterpretCast; break; |
| 33 | case tok::kw_static_cast: Op = CXXCastExpr::StaticCast; break; |
| 34 | } |
| 35 | |
| 36 | return new CXXCastExpr(Op, QualType::getFromOpaquePtr(Ty), (Expr*)E, OpLoc); |
| 37 | } |
| 38 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 39 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 41 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | assert((Kind != tok::kw_true || Kind != tok::kw_false) && |
| 43 | "Unknown C++ Boolean value!"); |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 44 | return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 46 | |
| 47 | /// ActOnCXXThrow - Parse throw expressions. |
| 48 | Action::ExprResult |
| 49 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { |
| 50 | return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); |
| 51 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame^] | 52 | |
| 53 | Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
| 54 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 55 | /// is a non-lvalue expression whose value is the address of the object for |
| 56 | /// which the function is called. |
| 57 | |
| 58 | if (!isa<FunctionDecl>(CurContext)) { |
| 59 | Diag(ThisLoc, diag::err_invalid_this_use); |
| 60 | return ExprResult(true); |
| 61 | } |
| 62 | |
| 63 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 64 | if (MD->isInstance()) |
| 65 | return new PreDefinedExpr(ThisLoc, MD->getThisType(Context), |
| 66 | PreDefinedExpr::CXXThis); |
| 67 | |
| 68 | return Diag(ThisLoc, diag::err_invalid_this_use); |
| 69 | } |