Submitted by:
Reviewed by:
Misc. changes driven by getting "carbon.h" to compile...

- Added ParseCharacterConstant() hook to Action, Sema, etc.
- Added CheckAssignmentOperands() & CheckCommaOperands() to Sema.
- Added CharacterLiteral AST node.
- Fixed CallExpr instantiation - install the correct type.
- Install a bunch of temp types and annotate with FIXME's.

llvm-svn: 39416
This commit is contained in:
Steve Naroff 2007-04-26 20:39:23 +00:00
parent 82ceca595e
commit ae4143ea90
10 changed files with 185 additions and 34 deletions

View File

@ -73,8 +73,8 @@ const char *UnaryOperator::getOpcodeStr(Opcode Op) {
// Postfix Operators.
//===----------------------------------------------------------------------===//
CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs)
: Expr(CallExprClass, fn->getType()), Fn(fn), NumArgs(numargs) {
CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t)
: Expr(CallExprClass, t), Fn(fn), NumArgs(numargs) {
Args = new Expr*[numargs];
for (unsigned i = 0; i != numargs; ++i)
Args[i] = args[i];

View File

@ -166,6 +166,7 @@ public:
virtual ExprResult ParseSimplePrimaryExpr(SourceLocation Loc,
tok::TokenKind Kind);
virtual ExprResult ParseNumericConstant(const LexerToken &);
virtual ExprResult ParseCharacterConstant(const LexerToken &);
virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
ExprTy *Val);
@ -244,6 +245,10 @@ private:
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
ExprResult CheckLogicalOperands( // C99 6.5.[13,14]
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
ExprResult CheckAssignmentOperands( // C99 6.5.16
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
ExprResult CheckCommaOperands( // C99 6.5.17
Expr *lex, Expr *rex, SourceLocation OpLoc);
/// type checking unary operators (subroutines of ParseUnaryOp).
/// The unsigned arguments are really enums (UnaryOperator::Opcode)

View File

@ -97,6 +97,19 @@ Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
}
}
Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
SmallString<16> CharBuffer;
CharBuffer.resize(Tok.getLength());
const char *ThisTokBegin = &CharBuffer[0];
unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Tok.getLocation(), PP);
if (Literal.hadError())
return ExprResult(true);
return new CharacterLiteral(Literal.getValue(), Context.IntTy);
}
Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
// fast path for a single digit (which is quite common). A single digit
// cannot have a trigraph, escaped newline, radix prefix, or type suffix.
@ -146,7 +159,9 @@ Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
ExprTy *Val) {
return Val;
Expr *e = (Expr *)Val;
assert((e != 0) && "ParseParenExpr() missing expr");
return e;
}
@ -312,14 +327,24 @@ Action::ExprResult Sema::
ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
ExprTy **Args, unsigned NumArgs,
SourceLocation *CommaLocs, SourceLocation RParenLoc) {
return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
QualType qType = ((Expr *)Fn)->getType();
assert(!qType.isNull() && "no type for function call expression");
QualType canonType = qType.getCanonicalType();
QualType resultType;
if (const FunctionType *funcT = dyn_cast<FunctionType>(canonType)) {
resultType = funcT->getResultType();
}
return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs, resultType);
}
Action::ExprResult Sema::
ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
SourceLocation RParenLoc, ExprTy *Op) {
// If error parsing type, ignore.
if (Ty == 0) return true;
assert((Ty != 0) && "ParseCastExpr(): missing type");
return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
}
@ -364,7 +389,10 @@ Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
}
Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
assert((lhs != 0) && "ParseBinOp(): missing left expression");
assert((rhs != 0) && "ParseBinOp(): missing right expression");
if (BinaryOperator::isMultiplicativeOp(Opc))
return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc);
else if (BinaryOperator::isAdditiveOp(Opc))
@ -379,8 +407,12 @@ Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
return CheckBitwiseOperands(lhs, rhs, TokLoc, Opc);
else if (BinaryOperator::isLogicalOp(Opc))
return CheckLogicalOperands(lhs, rhs, TokLoc, Opc);
else if (BinaryOperator::isAssignmentOp(Opc))
return CheckAssignmentOperands(lhs, rhs, TokLoc, Opc);
else if (Opc == BinaryOperator::Comma)
return CheckCommaOperands(lhs, rhs, TokLoc);
assert(0 && "ParseBinOp() not handling all binary ops properly");
assert(0 && "ParseBinOp(): illegal binary op");
}
/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
@ -389,7 +421,14 @@ Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
SourceLocation ColonLoc,
ExprTy *Cond, ExprTy *LHS,
ExprTy *RHS) {
return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
QualType lhs = ((Expr *)LHS)->getType();
QualType rhs = ((Expr *)RHS)->getType();
assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
QualType canonType = rhs.getCanonicalType(); // TEMPORARY
return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, canonType);
}
/// UsualUnaryConversion - Performs various conversions that are common to most
@ -570,6 +609,7 @@ Action::ExprResult Sema::CheckMultiplicativeOperands(
Action::ExprResult Sema::CheckAdditiveOperands( // C99 6.5.6
Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
{
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
}
@ -636,7 +676,22 @@ Action::ExprResult Sema::CheckBitwiseOperands(
Action::ExprResult Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
{
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code);
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
}
Action::ExprResult Sema::CheckAssignmentOperands( // C99 6.5.16
Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
{
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
}
Action::ExprResult Sema::CheckCommaOperands( // C99 6.5.17
Expr *lex, Expr *rex, SourceLocation loc)
{
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, BinaryOperator::Comma, Context.IntTy);
}
Action::ExprResult
@ -725,8 +780,10 @@ Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
assert(!qType.isNull() && "no type for * expression");
QualType canonType = qType.getCanonicalType();
// FIXME: add type checking and fix result type
return new UnaryOperator(op, UnaryOperator::Deref, QualType());
return new UnaryOperator(op, UnaryOperator::Deref, Context.IntTy);
}
/// CheckArithmeticOperand - Check the arithmetic unary operators (C99 6.5.3.3).

View File

@ -215,6 +215,11 @@ void StmtPrinter::VisitDeclRefExpr(DeclRefExpr *Node) {
OS << Node->getDecl()->getName();
}
void StmtPrinter::VisitCharacterLiteral(CharacterLiteral *Node) {
// FIXME: print value.
OS << "x";
}
void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) {
// FIXME: print value.
OS << "1";

View File

@ -497,6 +497,10 @@ Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
return ParsePostfixExpressionSuffix(Res);
}
case tok::char_constant: // constant: character-constant
Res = Actions.ParseCharacterConstant(Tok);
ConsumeToken();
// These can be followed by postfix-expr pieces.
return ParsePostfixExpressionSuffix(Res);
case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]

View File

@ -166,6 +166,7 @@ public:
virtual ExprResult ParseSimplePrimaryExpr(SourceLocation Loc,
tok::TokenKind Kind);
virtual ExprResult ParseNumericConstant(const LexerToken &);
virtual ExprResult ParseCharacterConstant(const LexerToken &);
virtual ExprResult ParseParenExpr(SourceLocation L, SourceLocation R,
ExprTy *Val);
@ -244,6 +245,10 @@ private:
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
ExprResult CheckLogicalOperands( // C99 6.5.[13,14]
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
ExprResult CheckAssignmentOperands( // C99 6.5.16
Expr *lex, Expr *rex, SourceLocation OpLoc, unsigned OpCode);
ExprResult CheckCommaOperands( // C99 6.5.17
Expr *lex, Expr *rex, SourceLocation OpLoc);
/// type checking unary operators (subroutines of ParseUnaryOp).
/// The unsigned arguments are really enums (UnaryOperator::Opcode)

View File

@ -97,6 +97,19 @@ Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
}
}
Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
SmallString<16> CharBuffer;
CharBuffer.resize(Tok.getLength());
const char *ThisTokBegin = &CharBuffer[0];
unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Tok.getLocation(), PP);
if (Literal.hadError())
return ExprResult(true);
return new CharacterLiteral(Literal.getValue(), Context.IntTy);
}
Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
// fast path for a single digit (which is quite common). A single digit
// cannot have a trigraph, escaped newline, radix prefix, or type suffix.
@ -146,7 +159,9 @@ Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
ExprTy *Val) {
return Val;
Expr *e = (Expr *)Val;
assert((e != 0) && "ParseParenExpr() missing expr");
return e;
}
@ -312,14 +327,24 @@ Action::ExprResult Sema::
ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
ExprTy **Args, unsigned NumArgs,
SourceLocation *CommaLocs, SourceLocation RParenLoc) {
return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
QualType qType = ((Expr *)Fn)->getType();
assert(!qType.isNull() && "no type for function call expression");
QualType canonType = qType.getCanonicalType();
QualType resultType;
if (const FunctionType *funcT = dyn_cast<FunctionType>(canonType)) {
resultType = funcT->getResultType();
}
return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs, resultType);
}
Action::ExprResult Sema::
ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
SourceLocation RParenLoc, ExprTy *Op) {
// If error parsing type, ignore.
if (Ty == 0) return true;
assert((Ty != 0) && "ParseCastExpr(): missing type");
return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
}
@ -364,7 +389,10 @@ Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
}
Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
assert((lhs != 0) && "ParseBinOp(): missing left expression");
assert((rhs != 0) && "ParseBinOp(): missing right expression");
if (BinaryOperator::isMultiplicativeOp(Opc))
return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc);
else if (BinaryOperator::isAdditiveOp(Opc))
@ -379,8 +407,12 @@ Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
return CheckBitwiseOperands(lhs, rhs, TokLoc, Opc);
else if (BinaryOperator::isLogicalOp(Opc))
return CheckLogicalOperands(lhs, rhs, TokLoc, Opc);
else if (BinaryOperator::isAssignmentOp(Opc))
return CheckAssignmentOperands(lhs, rhs, TokLoc, Opc);
else if (Opc == BinaryOperator::Comma)
return CheckCommaOperands(lhs, rhs, TokLoc);
assert(0 && "ParseBinOp() not handling all binary ops properly");
assert(0 && "ParseBinOp(): illegal binary op");
}
/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
@ -389,7 +421,14 @@ Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
SourceLocation ColonLoc,
ExprTy *Cond, ExprTy *LHS,
ExprTy *RHS) {
return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
QualType lhs = ((Expr *)LHS)->getType();
QualType rhs = ((Expr *)RHS)->getType();
assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
QualType canonType = rhs.getCanonicalType(); // TEMPORARY
return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, canonType);
}
/// UsualUnaryConversion - Performs various conversions that are common to most
@ -570,6 +609,7 @@ Action::ExprResult Sema::CheckMultiplicativeOperands(
Action::ExprResult Sema::CheckAdditiveOperands( // C99 6.5.6
Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
{
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
}
@ -636,7 +676,22 @@ Action::ExprResult Sema::CheckBitwiseOperands(
Action::ExprResult Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
{
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code);
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
}
Action::ExprResult Sema::CheckAssignmentOperands( // C99 6.5.16
Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
{
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
}
Action::ExprResult Sema::CheckCommaOperands( // C99 6.5.17
Expr *lex, Expr *rex, SourceLocation loc)
{
// FIXME: add type checking and fix result type
return new BinaryOperator(lex, rex, BinaryOperator::Comma, Context.IntTy);
}
Action::ExprResult
@ -725,8 +780,10 @@ Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
assert(!qType.isNull() && "no type for * expression");
QualType canonType = qType.getCanonicalType();
// FIXME: add type checking and fix result type
return new UnaryOperator(op, UnaryOperator::Deref, QualType());
return new UnaryOperator(op, UnaryOperator::Deref, Context.IntTy);
}
/// CheckArithmeticOperand - Check the arithmetic unary operators (C99 6.5.3.3).

View File

@ -91,6 +91,21 @@ public:
static bool classof(const IntegerLiteral *) { return true; }
};
class CharacterLiteral : public Expr {
unsigned Value;
public:
// type should be IntTy
CharacterLiteral(unsigned value, QualType type)
: Expr(CharacterLiteralClass, type), Value(value) {
}
virtual void visit(StmtVisitor &Visitor);
static bool classof(const Stmt *T) {
return T->getStmtClass() == CharacterLiteralClass;
}
static bool classof(const CharacterLiteral *) { return true; }
};
class FloatingLiteral : public Expr {
float Value; // FIXME
public:
@ -242,7 +257,7 @@ class CallExpr : public Expr {
Expr **Args;
unsigned NumArgs;
public:
CallExpr(Expr *fn, Expr **args, unsigned numargs);
CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t);
~CallExpr() {
delete [] Args;
}
@ -299,7 +314,7 @@ class CastExpr : public Expr {
Expr *Op;
public:
CastExpr(QualType ty, Expr *op) :
Expr(CastExprClass, QualType()), Ty(ty), Op(op) {}
Expr(CastExprClass, ty), Ty(ty), Op(op) {}
CastExpr(StmtClass SC, QualType ty, Expr *op) :
Expr(SC, QualType()), Ty(ty), Op(op) {}
@ -353,6 +368,7 @@ public:
static bool isEqualityOp(Opcode Op) { return Op == EQ || Op == NE; }
static bool isBitwiseOp(Opcode Op) { return Op >= And && Op <= Or; }
static bool isLogicalOp(Opcode Op) { return Op == LAnd || Op == LOr; }
static bool isAssignmentOp(Opcode Op) { return Op >= Assign || Op<=OrAssign; }
Opcode getOpcode() const { return Opc; }
Expr *getLHS() { return LHS; }
@ -374,8 +390,8 @@ private:
class ConditionalOperator : public Expr {
Expr *Cond, *LHS, *RHS; // Left/Middle/Right hand sides.
public:
ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs)
: Expr(ConditionalOperatorClass, QualType()), Cond(cond), LHS(lhs), RHS(rhs) {}
ConditionalOperator(Expr *cond, Expr *lhs, Expr *rhs, QualType t)
: Expr(ConditionalOperatorClass, t), Cond(cond), LHS(lhs), RHS(rhs) {}
Expr *getCond() { return Cond; }
Expr *getLHS() { return LHS; }

View File

@ -46,20 +46,21 @@ STMT(33, DeclRefExpr , Expr)
STMT(34, IntegerLiteral , Expr)
STMT(35, FloatingLiteral , Expr)
STMT(36, StringLiteral , Expr)
STMT(37, ParenExpr , Expr)
STMT(38, UnaryOperator , Expr)
STMT(39, SizeOfAlignOfTypeExpr, Expr)
STMT(40, ArraySubscriptExpr , Expr)
STMT(41, CallExpr , Expr)
STMT(42, MemberExpr , Expr)
STMT(43, CastExpr , Expr)
STMT(44, BinaryOperator , Expr)
STMT(45, ConditionalOperator , Expr)
STMT(37, CharacterLiteral , Expr)
STMT(38, ParenExpr , Expr)
STMT(39, UnaryOperator , Expr)
STMT(40, SizeOfAlignOfTypeExpr, Expr)
STMT(41, ArraySubscriptExpr , Expr)
STMT(42, CallExpr , Expr)
STMT(43, MemberExpr , Expr)
STMT(44, CastExpr , Expr)
STMT(45, BinaryOperator , Expr)
STMT(46, ConditionalOperator , Expr)
// C++ Expressions.
STMT(46, CXXCastExpr , CastExpr)
STMT(47, CXXBoolLiteralExpr , Expr)
LAST_EXPR(47)
STMT(47, CXXCastExpr , CastExpr)
STMT(48, CXXBoolLiteralExpr , Expr)
LAST_EXPR(48)
#undef STMT
#undef FIRST_STMT

View File

@ -263,6 +263,7 @@ public:
tok::TokenKind Kind) {
return 0;
}
virtual ExprResult ParseCharacterConstant(const LexerToken &) { return 0; }
virtual ExprResult ParseNumericConstant(const LexerToken &) { return 0; }
/// ParseStringLiteral - The specified tokens were lexed as pasted string