blob: 86e334580c432a2ed6069bfdec3c2278fbb4655f [file] [log] [blame]
Chris Lattner29375652006-12-04 18:06:35 +00001//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner29375652006-12-04 18:06:35 +00007//
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 Naroffaac94152007-08-25 14:02:58 +000016#include "clang/AST/ASTContext.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000017#include "clang/Basic/Diagnostic.h"
Chris Lattner29375652006-12-04 18:06:35 +000018using namespace clang;
19
Steve Naroff66356bd2007-09-16 14:56:35 +000020/// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's.
Chris Lattner29375652006-12-04 18:06:35 +000021Action::ExprResult
Steve Naroff66356bd2007-09-16 14:56:35 +000022Sema::ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind,
Chris Lattner29375652006-12-04 18:06:35 +000023 SourceLocation LAngleBracketLoc, TypeTy *Ty,
24 SourceLocation RAngleBracketLoc,
25 SourceLocation LParenLoc, ExprTy *E,
26 SourceLocation RParenLoc) {
27 CXXCastExpr::Opcode Op;
28
29 switch (Kind) {
Bill Wendling4073ed52007-02-13 01:51:42 +000030 default: assert(0 && "Unknown C++ cast!");
Chris Lattner29375652006-12-04 18:06:35 +000031 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
Bill Wendlinga6930032007-06-29 18:21:34 +000037 return new CXXCastExpr(Op, QualType::getFromOpaquePtr(Ty), (Expr*)E, OpLoc);
Chris Lattner29375652006-12-04 18:06:35 +000038}
Bill Wendling4073ed52007-02-13 01:51:42 +000039
Steve Naroff66356bd2007-09-16 14:56:35 +000040/// ActOnCXXBoolLiteral - Parse {true,false} literals.
Bill Wendling4073ed52007-02-13 01:51:42 +000041Action::ExprResult
Steve Naroff66356bd2007-09-16 14:56:35 +000042Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) {
Bill Wendling4073ed52007-02-13 01:51:42 +000043 assert((Kind != tok::kw_true || Kind != tok::kw_false) &&
Bill Wendlingbf313b02007-02-13 20:09:46 +000044 "Unknown C++ Boolean value!");
Steve Naroffaac94152007-08-25 14:02:58 +000045 return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc);
Bill Wendling4073ed52007-02-13 01:51:42 +000046}
Chris Lattnerb7e656b2008-02-26 00:51:44 +000047
48/// ActOnCXXThrow - Parse throw expressions.
49Action::ExprResult
50Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) {
51 return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc);
52}
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000053
54Action::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 Lattner6307f192008-08-10 01:53:14 +000066 return new PredefinedExpr(ThisLoc, MD->getThisType(Context),
67 PredefinedExpr::CXXThis);
Argyrios Kyrtzidised983422008-07-01 10:37:29 +000068
69 return Diag(ThisLoc, diag::err_invalid_this_use);
70}