blob: 92352cd6163664f2b4df43908be2360ab4e3e3fe [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000015#include "clang/AST/StmtVisitor.h"
Chris Lattnere165d942006-08-24 04:40:38 +000016#include "clang/Lex/IdentifierTable.h"
Chris Lattner1b926492006-08-23 06:42:10 +000017using namespace llvm;
18using namespace clang;
19
Chris Lattner0eedafe2006-08-24 04:56:27 +000020//===----------------------------------------------------------------------===//
Chris Lattner5e9a8782006-11-04 06:21:51 +000021// Visitor Implementation.
22//===----------------------------------------------------------------------===//
23
24#define MAKE_VISITOR(CLASS) \
Chris Lattner9ea960a2006-11-04 07:16:04 +000025 void CLASS::visit(StmtVisitor &V) { return V.Visit##CLASS(this); }
Chris Lattner5e9a8782006-11-04 06:21:51 +000026
Chris Lattner469d3572006-11-04 06:32:52 +000027MAKE_VISITOR(Expr)
Chris Lattner5e9a8782006-11-04 06:21:51 +000028MAKE_VISITOR(DeclRefExpr)
29MAKE_VISITOR(IntegerConstant)
30MAKE_VISITOR(FloatingConstant)
31MAKE_VISITOR(StringExpr)
32MAKE_VISITOR(ParenExpr)
33MAKE_VISITOR(UnaryOperator)
34MAKE_VISITOR(SizeOfAlignOfTypeExpr)
35MAKE_VISITOR(ArraySubscriptExpr)
36MAKE_VISITOR(CallExpr)
37MAKE_VISITOR(MemberExpr)
38MAKE_VISITOR(CastExpr)
39MAKE_VISITOR(BinaryOperator)
40MAKE_VISITOR(ConditionalOperator)
41
42#undef MAKE_VISITOR
43
44//===----------------------------------------------------------------------===//
Chris Lattner0eedafe2006-08-24 04:56:27 +000045// Primary Expressions.
46//===----------------------------------------------------------------------===//
47
Chris Lattnerd3e98952006-10-06 05:22:26 +000048
49StringExpr::StringExpr(const char *strData, unsigned byteLength, bool Wide) {
50 // OPTIMIZE: could allocate this appended to the StringExpr.
51 char *AStrData = new char[byteLength];
52 memcpy(AStrData, strData, byteLength);
53 StrData = AStrData;
54 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000055 IsWide = Wide;
Chris Lattnerd3e98952006-10-06 05:22:26 +000056}
57
58StringExpr::~StringExpr() {
59 delete[] StrData;
60}
61
Chris Lattner1b926492006-08-23 06:42:10 +000062/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
63/// corresponds to, e.g. "sizeof" or "[pre]++".
64const char *UnaryOperator::getOpcodeStr(Opcode Op) {
65 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000066 default: assert(0 && "Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +000067 case PostInc: return "[post]++";
68 case PostDec: return "[post]--";
69 case PreInc: return "[pre]++";
70 case PreDec: return "[pre]--";
71 case AddrOf: return "&";
72 case Deref: return "*";
73 case Plus: return "+";
74 case Minus: return "-";
75 case Not: return "~";
76 case LNot: return "!";
77 case Real: return "__real";
78 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000079 case SizeOf: return "sizeof";
80 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000081 case Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +000082 }
83}
84
Chris Lattner0eedafe2006-08-24 04:56:27 +000085//===----------------------------------------------------------------------===//
86// Postfix Operators.
87//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000088
Chris Lattnere165d942006-08-24 04:40:38 +000089CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs)
90 : Fn(fn), NumArgs(numargs) {
91 Args = new Expr*[numargs];
92 for (unsigned i = 0; i != numargs; ++i)
93 Args[i] = args[i];
94}
95
Chris Lattner1b926492006-08-23 06:42:10 +000096/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
97/// corresponds to, e.g. "<<=".
98const char *BinaryOperator::getOpcodeStr(Opcode Op) {
99 switch (Op) {
100 default: assert(0 && "Unknown binary operator");
101 case Mul: return "*";
102 case Div: return "/";
103 case Rem: return "%";
104 case Add: return "+";
105 case Sub: return "-";
106 case Shl: return "<<";
107 case Shr: return ">>";
108 case LT: return "<";
109 case GT: return ">";
110 case LE: return "<=";
111 case GE: return ">=";
112 case EQ: return "==";
113 case NE: return "!=";
114 case And: return "&";
115 case Xor: return "^";
116 case Or: return "|";
117 case LAnd: return "&&";
118 case LOr: return "||";
119 case Assign: return "=";
120 case MulAssign: return "*=";
121 case DivAssign: return "/=";
122 case RemAssign: return "%=";
123 case AddAssign: return "+=";
124 case SubAssign: return "-=";
125 case ShlAssign: return "<<=";
126 case ShrAssign: return ">>=";
127 case AndAssign: return "&=";
128 case XorAssign: return "^=";
129 case OrAssign: return "|=";
130 case Comma: return ",";
131 }
132}