2006-08-23 06:42:10 +00:00
|
|
|
//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Expr class and subclasses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/Expr.h"
|
2006-11-04 06:21:51 +00:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2006-08-24 04:40:38 +00:00
|
|
|
#include "clang/Lex/IdentifierTable.h"
|
2006-08-23 06:42:10 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2006-08-24 04:56:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Primary Expressions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-26 22:17:12 +00:00
|
|
|
StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
|
2007-05-17 01:16:00 +00:00
|
|
|
bool Wide, QualType t, SourceLocation l) :
|
2007-03-23 22:27:02 +00:00
|
|
|
Expr(StringLiteralClass, t) {
|
2007-02-21 23:46:25 +00:00
|
|
|
// OPTIMIZE: could allocate this appended to the StringLiteral.
|
2006-10-06 05:22:26 +00:00
|
|
|
char *AStrData = new char[byteLength];
|
|
|
|
memcpy(AStrData, strData, byteLength);
|
|
|
|
StrData = AStrData;
|
|
|
|
ByteLength = byteLength;
|
2006-11-04 18:52:07 +00:00
|
|
|
IsWide = Wide;
|
2007-05-17 01:16:00 +00:00
|
|
|
Loc = l;
|
2006-10-06 05:22:26 +00:00
|
|
|
}
|
|
|
|
|
2007-02-21 23:46:25 +00:00
|
|
|
StringLiteral::~StringLiteral() {
|
2006-10-06 05:22:26 +00:00
|
|
|
delete[] StrData;
|
|
|
|
}
|
|
|
|
|
2006-11-05 23:54:51 +00:00
|
|
|
bool UnaryOperator::isPostfix(Opcode Op) {
|
|
|
|
switch (Op) {
|
|
|
|
case PostInc:
|
|
|
|
case PostDec:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-23 06:42:10 +00:00
|
|
|
/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
|
|
|
|
/// corresponds to, e.g. "sizeof" or "[pre]++".
|
|
|
|
const char *UnaryOperator::getOpcodeStr(Opcode Op) {
|
|
|
|
switch (Op) {
|
2006-10-25 05:45:55 +00:00
|
|
|
default: assert(0 && "Unknown unary operator");
|
2006-11-05 23:54:51 +00:00
|
|
|
case PostInc: return "++";
|
|
|
|
case PostDec: return "--";
|
|
|
|
case PreInc: return "++";
|
|
|
|
case PreDec: return "--";
|
2006-08-23 06:42:10 +00:00
|
|
|
case AddrOf: return "&";
|
|
|
|
case Deref: return "*";
|
|
|
|
case Plus: return "+";
|
|
|
|
case Minus: return "-";
|
|
|
|
case Not: return "~";
|
|
|
|
case LNot: return "!";
|
|
|
|
case Real: return "__real";
|
|
|
|
case Imag: return "__imag";
|
2006-08-24 06:10:04 +00:00
|
|
|
case SizeOf: return "sizeof";
|
|
|
|
case AlignOf: return "alignof";
|
2006-10-25 05:45:55 +00:00
|
|
|
case Extension: return "__extension__";
|
2006-08-23 06:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-24 04:56:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Postfix Operators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-08-24 04:40:38 +00:00
|
|
|
|
2007-05-17 01:16:00 +00:00
|
|
|
CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
|
|
|
|
SourceLocation l)
|
2007-04-26 20:39:23 +00:00
|
|
|
: Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
|
2006-08-24 04:40:38 +00:00
|
|
|
Args = new Expr*[numargs];
|
|
|
|
for (unsigned i = 0; i != numargs; ++i)
|
|
|
|
Args[i] = args[i];
|
2007-05-17 01:16:00 +00:00
|
|
|
Loc = l;
|
2006-08-24 04:40:38 +00:00
|
|
|
}
|
|
|
|
|
2006-08-23 06:42:10 +00:00
|
|
|
/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
|
|
|
|
/// corresponds to, e.g. "<<=".
|
|
|
|
const char *BinaryOperator::getOpcodeStr(Opcode Op) {
|
|
|
|
switch (Op) {
|
|
|
|
default: assert(0 && "Unknown binary operator");
|
|
|
|
case Mul: return "*";
|
|
|
|
case Div: return "/";
|
|
|
|
case Rem: return "%";
|
|
|
|
case Add: return "+";
|
|
|
|
case Sub: return "-";
|
|
|
|
case Shl: return "<<";
|
|
|
|
case Shr: return ">>";
|
|
|
|
case LT: return "<";
|
|
|
|
case GT: return ">";
|
|
|
|
case LE: return "<=";
|
|
|
|
case GE: return ">=";
|
|
|
|
case EQ: return "==";
|
|
|
|
case NE: return "!=";
|
|
|
|
case And: return "&";
|
|
|
|
case Xor: return "^";
|
|
|
|
case Or: return "|";
|
|
|
|
case LAnd: return "&&";
|
|
|
|
case LOr: return "||";
|
|
|
|
case Assign: return "=";
|
|
|
|
case MulAssign: return "*=";
|
|
|
|
case DivAssign: return "/=";
|
|
|
|
case RemAssign: return "%=";
|
|
|
|
case AddAssign: return "+=";
|
|
|
|
case SubAssign: return "-=";
|
|
|
|
case ShlAssign: return "<<=";
|
|
|
|
case ShrAssign: return ">>=";
|
|
|
|
case AndAssign: return "&=";
|
|
|
|
case XorAssign: return "^=";
|
|
|
|
case OrAssign: return "|=";
|
|
|
|
case Comma: return ",";
|
|
|
|
}
|
|
|
|
}
|
2007-04-19 23:00:49 +00:00
|
|
|
|
2007-05-14 17:19:29 +00:00
|
|
|
/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
|
|
|
|
/// incomplete type other than void. Nonarray expressions that can be lvalues:
|
2007-04-19 23:00:49 +00:00
|
|
|
/// - name, where name must be a variable
|
|
|
|
/// - e[i]
|
|
|
|
/// - (e), where e must be an lvalue
|
|
|
|
/// - e.name, where e must be an lvalue
|
|
|
|
/// - e->name
|
2007-05-07 00:24:15 +00:00
|
|
|
/// - *e, the type of e cannot be a function type
|
2007-04-19 23:00:49 +00:00
|
|
|
/// - string-constant
|
|
|
|
///
|
2007-05-14 17:19:29 +00:00
|
|
|
bool Expr::isLvalue() {
|
|
|
|
// first, check the type (C99 6.3.2.1)
|
|
|
|
if (!TR->isObjectType())
|
|
|
|
return false;
|
|
|
|
if (TR->isIncompleteType() && TR->isVoidType())
|
|
|
|
return false;
|
|
|
|
|
2007-05-14 18:14:51 +00:00
|
|
|
// the type looks fine, now check the expression
|
2007-04-19 23:00:49 +00:00
|
|
|
switch (getStmtClass()) {
|
2007-05-14 17:19:29 +00:00
|
|
|
case StringLiteralClass: // C99 6.5.1p4
|
2007-04-19 23:00:49 +00:00
|
|
|
return true;
|
2007-05-14 18:14:51 +00:00
|
|
|
case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
|
2007-04-19 23:00:49 +00:00
|
|
|
return true;
|
2007-05-14 17:19:29 +00:00
|
|
|
case DeclRefExprClass: // C99 6.5.1p2
|
2007-05-14 18:14:51 +00:00
|
|
|
return isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl());
|
2007-05-14 17:19:29 +00:00
|
|
|
case MemberExprClass: // C99 6.5.2.3p4
|
2007-04-19 23:00:49 +00:00
|
|
|
const MemberExpr *m = cast<MemberExpr>(this);
|
2007-05-14 18:14:51 +00:00
|
|
|
return m->isArrow() ? true : m->getBase()->isLvalue();
|
2007-05-14 17:19:29 +00:00
|
|
|
case UnaryOperatorClass: // C99 6.5.3p4
|
2007-05-14 18:14:51 +00:00
|
|
|
return cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref;
|
2007-05-14 17:19:29 +00:00
|
|
|
case ParenExprClass: // C99 6.5.1p5
|
|
|
|
return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
|
2007-04-19 23:00:49 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
|
2007-05-14 17:19:29 +00:00
|
|
|
/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
|
|
|
|
/// does not have an incomplete type, does not have a const-qualified type, and
|
|
|
|
/// if it is a structure or union, does not have any member (including,
|
|
|
|
/// recursively, any member or element of all contained aggregates or unions)
|
|
|
|
/// with a const-qualified type.
|
|
|
|
bool Expr::isModifiableLvalue() {
|
|
|
|
if (!isLvalue())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (TR.isConstQualified())
|
|
|
|
return false;
|
|
|
|
if (TR->isArrayType())
|
|
|
|
return false;
|
|
|
|
if (TR->isIncompleteType())
|
|
|
|
return false;
|
|
|
|
if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType()))
|
|
|
|
return r->isModifiableLvalue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-15 20:29:32 +00:00
|
|
|
/// isConstantExpr - this recursive routine will test if an expression is
|
|
|
|
/// either a constant expression (isIntConst == false) or an integer constant
|
|
|
|
/// expression (isIntConst == true). Note: With the introduction of VLA's in
|
|
|
|
/// C99 the result of the sizeof operator is no longer always a constant
|
|
|
|
/// expression. The generalization of the wording to include any subexpression
|
|
|
|
/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
|
|
|
|
/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
|
|
|
|
/// "0 || f()" can be treated as a constant expression. In C90 this expression,
|
|
|
|
/// occurring in a context requiring a constant, would have been a constraint
|
|
|
|
/// violation. FIXME: This routine currently implements C90 semantics.
|
|
|
|
/// To properly implement C99 semantics this routine will need to evaluate
|
|
|
|
/// expressions involving operators previously mentioned.
|
|
|
|
bool Expr::isConstantExpr(bool isIntConst) const {
|
2007-05-08 21:09:37 +00:00
|
|
|
switch (getStmtClass()) {
|
|
|
|
case IntegerLiteralClass:
|
|
|
|
case CharacterLiteralClass:
|
|
|
|
return true;
|
2007-05-15 20:29:32 +00:00
|
|
|
case FloatingLiteralClass:
|
|
|
|
case StringLiteralClass:
|
|
|
|
return isIntConst ? false : true;
|
2007-05-08 21:09:37 +00:00
|
|
|
case DeclRefExprClass:
|
|
|
|
return isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl());
|
|
|
|
case UnaryOperatorClass:
|
2007-05-15 02:32:35 +00:00
|
|
|
const UnaryOperator *uop = cast<UnaryOperator>(this);
|
|
|
|
if (uop->isIncrementDecrementOp()) // C99 6.6p3
|
|
|
|
return false;
|
|
|
|
// C99 6.5.3.4p2: otherwise, the operand is *not* evaluated and the result
|
|
|
|
// is an integer constant. This effective ignores any subexpression that
|
|
|
|
// isn't actually a constant expression (what an odd language:-)
|
|
|
|
if (uop->isSizeOfAlignOfOp())
|
2007-05-15 20:29:32 +00:00
|
|
|
return uop->getSubExpr()->getType()->isConstantSizeType();
|
|
|
|
return uop->getSubExpr()->isConstantExpr(isIntConst);
|
2007-05-08 21:09:37 +00:00
|
|
|
case BinaryOperatorClass:
|
2007-05-15 02:32:35 +00:00
|
|
|
const BinaryOperator *bop = cast<BinaryOperator>(this);
|
|
|
|
// C99 6.6p3: shall not contain assignment, increment/decrement,
|
|
|
|
// function call, or comma operators, *except* when they are contained
|
|
|
|
// within a subexpression that is not evaluated.
|
|
|
|
if (bop->isAssignmentOp() || bop->getOpcode() == BinaryOperator::Comma)
|
|
|
|
return false;
|
2007-05-15 20:29:32 +00:00
|
|
|
return bop->getLHS()->isConstantExpr(isIntConst) &&
|
|
|
|
bop->getRHS()->isConstantExpr(isIntConst);
|
2007-05-08 21:09:37 +00:00
|
|
|
case ParenExprClass:
|
|
|
|
return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr();
|
2007-05-15 02:32:35 +00:00
|
|
|
case CastExprClass:
|
|
|
|
const CastExpr *castExpr = cast<CastExpr>(this);
|
|
|
|
// C99 6.6p6: shall only convert arithmetic types to integer types.
|
|
|
|
if (!castExpr->getSubExpr()->getType()->isArithmeticType())
|
|
|
|
return false;
|
|
|
|
if (!castExpr->getDestType()->isIntegerType())
|
|
|
|
return false;
|
|
|
|
// allow floating constants that are the immediate operands of casts.
|
|
|
|
if (castExpr->getSubExpr()->isConstantExpr() ||
|
|
|
|
isa<FloatingLiteral>(castExpr->getSubExpr()))
|
|
|
|
return true;
|
|
|
|
return false;
|
2007-05-08 21:09:37 +00:00
|
|
|
case SizeOfAlignOfTypeExprClass:
|
2007-05-15 02:32:35 +00:00
|
|
|
const SizeOfAlignOfTypeExpr *sizeExpr = cast<SizeOfAlignOfTypeExpr>(this);
|
|
|
|
if (sizeExpr->isSizeOf())
|
|
|
|
return sizeExpr->getArgumentType()->isConstantSizeType();
|
|
|
|
return true; // alignof will always evaluate to a constant
|
|
|
|
case ConditionalOperatorClass:
|
|
|
|
const ConditionalOperator *condExpr = cast<ConditionalOperator>(this);
|
2007-05-15 20:29:32 +00:00
|
|
|
return condExpr->getCond()->isConstantExpr(isIntConst) &&
|
|
|
|
condExpr->getLHS()->isConstantExpr(isIntConst) &&
|
|
|
|
condExpr->getRHS()->isConstantExpr(isIntConst);
|
2007-05-08 21:09:37 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug #:
Submitted by:
Reviewed by:
Implemented type checking for compound assignments (*=, /=, etc.).
This encouraged me to do a fairly dramatic refactoring of the Check* functions.
(since I wanted to reuse the existing work, rather than duplicate the logic).
For example, I changed all the Check* functions to return a QualType (instead
of returning an Expr). This had a very nice side benefit...there is now
only one instantiation point for BinaryOperator()! (A property I've always
wanted...separating type checking from AST building is *much* nicer). Another
change is to remove "code" from all the Check* functions (this allowed
me to remove the weird comment about enums/unsigned:-). Removing the
code forced me to add a few functions, however. For example,
< ExprResult CheckAdditiveOperands( // C99 6.5.6
< Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
> inline QualType CheckAdditionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
> inline QualType CheckSubtractionOperands( // C99 6.5.6
> Expr *lex, Expr *rex, SourceLocation OpLoc);
While this isn't as terse, it more closely reflects the differences in
the typechecking logic. For example, I disliked having to check the code again
in CheckMultiplicativeOperands/CheckAdditiveOperands.
Created the following helper functions:
- Expr::isNullPointerConstant().
- SemaExpr.cpp: static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode().
This was purely asethetic, since ParseBinOp() is now larger. I didn't feel
like looking at 2 huge switch statements. ParseBinOp() now avoids using
any of the BinaryOperator predicates (since I switched to a switch statement:-)
Only one regret (minor). I couldn't figure out how to avoid having two assign functions,
CheckCompoundAssignmentOperands, CheckSimpleAssignmentOperands. Conceptually,
the two functions make sense. Unfortunately, their implementation contains a lot of
duplication (thought they aren't that be in the first place).
llvm-svn: 39433
2007-05-04 21:54:46 +00:00
|
|
|
bool Expr::isNullPointerConstant() const {
|
|
|
|
const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(this);
|
|
|
|
if (!constant || constant->getValue() != 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|