Skip to content

Commit b9dd99d

Browse files
committed
Don't create special objects for NULL, just use NULL directly.
Also move null check into PycRef for nullable references.
1 parent 1329626 commit b9dd99d

12 files changed

+160
-155
lines changed

ASTNode.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "ASTNode.h"
22

3-
PycRef<ASTNode> Node_NULL = (ASTNode*)0;
4-
53
/* ASTNodeList */
64
void ASTNodeList::removeLast()
75
{

ASTNode.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ class ASTNode {
5252
void delRef() { internalDelRef(this); }
5353
};
5454

55-
/* A NULL node for comparison */
56-
extern PycRef<ASTNode> Node_NULL;
57-
5855

5956
class ASTNodeList : public ASTNode {
6057
public:
@@ -157,8 +154,8 @@ class ASTSlice : public ASTBinary {
157154
SLICE0, SLICE1, SLICE2, SLICE3
158155
};
159156

160-
ASTSlice(int op, PycRef<ASTNode> left = Node_NULL,
161-
PycRef<ASTNode> right = Node_NULL)
157+
ASTSlice(int op, PycRef<ASTNode> left = NULL,
158+
PycRef<ASTNode> right = NULL)
162159
: ASTBinary(left, right, op, NODE_SLICE) { }
163160
};
164161

@@ -258,16 +255,16 @@ class ASTCall : public ASTNode {
258255

259256
ASTCall(PycRef<ASTNode> func, pparam_t pparams, kwparam_t kwparams)
260257
: ASTNode(NODE_CALL), m_func(func), m_pparams(pparams), m_kwparams(kwparams),
261-
m_var(Node_NULL), m_kw(Node_NULL) { }
258+
m_var(), m_kw() { }
262259

263260
PycRef<ASTNode> func() const { return m_func; }
264261
const pparam_t& pparams() const { return m_pparams; }
265262
const kwparam_t& kwparams() const { return m_kwparams; }
266263
PycRef<ASTNode> var() const { return m_var; }
267264
PycRef<ASTNode> kw() const { return m_kw; }
268265

269-
bool hasVar() const { return m_var != Node_NULL; }
270-
bool hasKW() const { return m_kw != Node_NULL; }
266+
bool hasVar() const { return m_var != NULL; }
267+
bool hasKW() const { return m_kw != NULL; }
271268

272269
void setVar(PycRef<ASTNode> var) { m_var = var; }
273270
void setKW(PycRef<ASTNode> kw) { m_kw = kw; }
@@ -363,7 +360,7 @@ class ASTSubscr : public ASTNode {
363360

364361
class ASTPrint : public ASTNode {
365362
public:
366-
ASTPrint(PycRef<ASTNode> value, PycRef<ASTNode> stream = Node_NULL)
363+
ASTPrint(PycRef<ASTNode> value, PycRef<ASTNode> stream = NULL)
367364
: ASTNode(NODE_PRINT), m_value(value), m_stream(stream) { }
368365

369366
PycRef<ASTNode> value() const { return m_value; }

ASTree.cpp

+113-113
Large diffs are not rendered by default.

FastStack.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class FastStack {
3535

3636
void pop() {
3737
if (m_ptr > -1)
38-
m_stack[m_ptr--] = Node_NULL;
38+
m_stack[m_ptr--] = NULL;
3939
}
4040

4141
PycRef<ASTNode> top() const
4242
{
4343
if (m_ptr > -1)
4444
return m_stack[m_ptr];
4545
else
46-
return Node_NULL;
46+
return NULL;
4747
}
4848

4949
void replace(const FastStack& copy)

bytecode.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ bool Pyc::IsCompareArg(int opcode)
141141

142142
void print_const(PycRef<PycObject> obj, PycModule* mod)
143143
{
144+
if (obj == NULL) {
145+
fprintf(pyc_output, "<NULL>");
146+
return;
147+
}
148+
144149
switch (obj->type()) {
145150
case PycObject::TYPE_STRING:
146151
OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 'b' : 0);

pyc_numeric.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void PycLong::load(PycData* stream, PycModule*)
3535

3636
bool PycLong::isEqual(PycRef<PycObject> obj) const
3737
{
38-
if (type() != obj->type())
38+
if (type() != obj.type())
3939
return false;
4040

4141
PycRef<PycLong> longObj = obj.cast<PycLong>();
@@ -110,7 +110,7 @@ void PycFloat::load(PycData* stream, PycModule*)
110110

111111
bool PycFloat::isEqual(PycRef<PycObject> obj) const
112112
{
113-
if (type() != obj->type())
113+
if (type() != obj.type())
114114
return false;
115115

116116
PycRef<PycFloat> floatObj = obj.cast<PycFloat>();

pyc_numeric.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class PycInt : public PycObject {
1313

1414
bool isEqual(PycRef<PycObject> obj) const
1515
{
16-
return (type() == obj->type()) &&
16+
return (type() == obj.type()) &&
1717
(m_value == obj.cast<PycInt>()->m_value);
1818
}
1919

@@ -85,7 +85,7 @@ class PycCFloat : public PycObject {
8585

8686
bool isEqual(PycRef<PycObject> obj) const
8787
{
88-
return (type() == obj->type()) &&
88+
return (type() == obj.type()) &&
8989
(m_value == obj.cast<PycCFloat>()->m_value);
9090
}
9191

pyc_object.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "data.h"
66
#include <cstdio>
77

8-
PycRef<PycObject> Pyc_NULL = (PycObject*)0;
98
PycRef<PycObject> Pyc_None = new PycObject(PycObject::TYPE_NONE);
109
PycRef<PycObject> Pyc_Ellipsis = new PycObject(PycObject::TYPE_ELLIPSIS);
1110
PycRef<PycObject> Pyc_StopIteration = new PycObject(PycObject::TYPE_STOPITER);
@@ -16,7 +15,7 @@ PycRef<PycObject> CreateObject(int type)
1615
{
1716
switch (type) {
1817
case PycObject::TYPE_NULL:
19-
return Pyc_NULL;
18+
return NULL;
2019
case PycObject::TYPE_NONE:
2120
return Pyc_None;
2221
case PycObject::TYPE_FALSE:
@@ -65,7 +64,7 @@ PycRef<PycObject> CreateObject(int type)
6564
return new PycSet(type);
6665
default:
6766
fprintf(stderr, "CreateObject: Got unsupported type 0x%X\n", type);
68-
return Pyc_NULL;
67+
return NULL;
6968
}
7069
}
7170

@@ -79,7 +78,7 @@ PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod)
7978
obj = mod->getRef(index);
8079
} else {
8180
obj = CreateObject(type & 0x7F);
82-
if (obj != Pyc_NULL) {
81+
if (obj != NULL) {
8382
if (type & 0x80)
8483
mod->refObject(obj);
8584
obj->load(stream, mod);

pyc_object.h

+10-9
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ class PycRef {
5353
_Obj* operator->() const { return m_obj; }
5454
operator _Obj*() const { return m_obj; }
5555

56+
inline int type() const;
57+
5658
/* This is just for coding convenience -- no type checking is done! */
5759
template <class _Cast>
5860
PycRef<_Cast> cast() const { return static_cast<_Cast*>(m_obj); }
5961

62+
bool isIdent(const _Obj* obj) { return m_obj == obj; }
63+
6064
private:
6165
_Obj* m_obj;
6266
};
@@ -106,33 +110,30 @@ class PycObject {
106110
PycObject(int type = TYPE_UNKNOWN) : m_refs(0), m_type(type) { }
107111
virtual ~PycObject() { }
108112

109-
int type() const { return internalGetType(this); }
113+
int type() const { return m_type; }
110114

111115
virtual bool isEqual(PycRef<PycObject> obj) const
112-
{ return (this == (PycObject*)obj); }
116+
{ return obj.isIdent(this); }
113117

114118
virtual void load(PycData*, PycModule*) { }
115119

116120
private:
117121
int m_refs;
118122
int m_type;
119123

120-
// Hack to make clang happy :(
121-
static int internalGetType(const PycObject *obj)
122-
{
123-
return obj ? obj->m_type : TYPE_NULL;
124-
}
125-
126124
public:
127125
void addRef() { ++m_refs; }
128126
void delRef() { if (--m_refs == 0) delete this; }
129127
};
130128

129+
template <class _Obj>
130+
int PycRef<_Obj>::type() const
131+
{ return m_obj ? m_obj->type() : PycObject::TYPE_NULL; }
132+
131133
PycRef<PycObject> CreateObject(int type);
132134
PycRef<PycObject> LoadObject(PycData* stream, PycModule* mod);
133135

134136
/* Static Singleton objects */
135-
extern PycRef<PycObject> Pyc_NULL;
136137
extern PycRef<PycObject> Pyc_None;
137138
extern PycRef<PycObject> Pyc_Ellipsis;
138139
extern PycRef<PycObject> Pyc_StopIteration;

pyc_sequence.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void PycTuple::load(PycData* stream, PycModule* mod)
1717

1818
bool PycTuple::isEqual(PycRef<PycObject> obj) const
1919
{
20-
if (type() != obj->type())
20+
if (type() != obj.type())
2121
return false;
2222

2323
PycRef<PycTuple> tupleObj = obj.cast<PycTuple>();
@@ -44,7 +44,7 @@ void PycList::load(PycData* stream, PycModule* mod)
4444

4545
bool PycList::isEqual(PycRef<PycObject> obj) const
4646
{
47-
if (type() != obj->type())
47+
if (type() != obj.type())
4848
return false;
4949

5050
PycRef<PycList> listObj = obj.cast<PycList>();
@@ -67,7 +67,7 @@ void PycDict::load(PycData* stream, PycModule* mod)
6767
PycRef<PycObject> key, val;
6868
for (;;) {
6969
key = LoadObject(stream, mod);
70-
if (key == Pyc_NULL)
70+
if (key == NULL)
7171
break;
7272
val = LoadObject(stream, mod);
7373
m_keys.push_back(key);
@@ -77,7 +77,7 @@ void PycDict::load(PycData* stream, PycModule* mod)
7777

7878
bool PycDict::isEqual(PycRef<PycObject> obj) const
7979
{
80-
if (type() != obj->type())
80+
if (type() != obj.type())
8181
return false;
8282

8383
PycRef<PycDict> dictObj = obj.cast<PycDict>();
@@ -111,7 +111,7 @@ PycRef<PycObject> PycDict::get(PycRef<PycObject> key) const
111111
return *vi;
112112
++ki, ++vi;
113113
}
114-
return Pyc_NULL; // Disassembly shouldn't get non-existant keys
114+
return NULL; // Disassembly shouldn't get non-existant keys
115115
}
116116

117117

@@ -125,7 +125,7 @@ void PycSet::load(PycData* stream, PycModule* mod)
125125

126126
bool PycSet::isEqual(PycRef<PycObject> obj) const
127127
{
128-
if (type() != obj->type())
128+
if (type() != obj.type())
129129
return false;
130130

131131
PycRef<PycSet> setObj = obj.cast<PycSet>();

pyc_string.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void PycString::load(PycData* stream, PycModule* mod)
8484

8585
bool PycString::isEqual(PycRef<PycObject> obj) const
8686
{
87-
if (type() != obj->type())
87+
if (type() != obj.type())
8888
return false;
8989

9090
PycRef<PycString> strObj = obj.cast<PycString>();

pycdas.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ static void iprintf(int indent, const char* fmt, ...)
6565

6666
void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
6767
{
68+
if (obj == NULL) {
69+
iprintf(indent, "<NULL>");
70+
return;
71+
}
72+
6873
switch (obj->type()) {
6974
case PycObject::TYPE_CODE:
7075
case PycObject::TYPE_CODE2:
@@ -81,31 +86,31 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
8186
iprintf(indent + 1, "Flags: 0x%08X", codeObj->flags());
8287
print_coflags(codeObj->flags());
8388

84-
if (codeObj->names() != Pyc_NULL) {
89+
if (codeObj->names() != NULL) {
8590
iprintf(indent + 1, "[Names]\n");
8691
for (int i=0; i<codeObj->names()->size(); i++)
8792
output_object(codeObj->names()->get(i), mod, indent + 2);
8893
}
8994

90-
if (codeObj->varNames() != Pyc_NULL) {
95+
if (codeObj->varNames() != NULL) {
9196
iprintf(indent + 1, "[Var Names]\n");
9297
for (int i=0; i<codeObj->varNames()->size(); i++)
9398
output_object(codeObj->varNames()->get(i), mod, indent + 2);
9499
}
95100

96-
if (codeObj->freeVars() != Pyc_NULL) {
101+
if (codeObj->freeVars() != NULL) {
97102
iprintf(indent + 1, "[Free Vars]\n");
98103
for (int i=0; i<codeObj->freeVars()->size(); i++)
99104
output_object(codeObj->freeVars()->get(i), mod, indent + 2);
100105
}
101106

102-
if (codeObj->cellVars() != Pyc_NULL) {
107+
if (codeObj->cellVars() != NULL) {
103108
iprintf(indent + 1, "[Cell Vars]\n");
104109
for (int i=0; i<codeObj->cellVars()->size(); i++)
105110
output_object(codeObj->cellVars()->get(i), mod, indent + 2);
106111
}
107112

108-
if (codeObj->consts() != Pyc_NULL) {
113+
if (codeObj->consts() != NULL) {
109114
iprintf(indent + 1, "[Constants]\n");
110115
for (int i=0; i<codeObj->consts()->size(); i++)
111116
output_object(codeObj->consts()->get(i), mod, indent + 2);

0 commit comments

Comments
 (0)