1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Copyright (c) 1997 James Clark
// See the file copying.txt for copying permission.
#include "stylelib.h"
#include "MacroFlowObj.h"
#include "VM.h"
#include "Insn2.h"
#include "Insn.h"
#include "Expression.h"
#include "Interpreter.h"
#include "ProcessContext.h"
#include <OpenSP/macros.h>
#ifdef DSSSL_NAMESPACE
namespace DSSSL_NAMESPACE {
#endif
MacroFlowObj::MacroFlowObj(Vector<const Identifier *> &nics,
NCVector<Owner<Expression> > &inits,
const Identifier *contentsId,
Owner<Expression> &body)
: sp_(new S(new Definition(nics, inits, contentsId, body)))
{
size_t n = sp_->def_->nics().size();
sp_->charicVals_ = new ELObj *[n];
for (size_t i = 0; i < n; i++)
sp_->charicVals_[i] = 0;
}
MacroFlowObj::MacroFlowObj(const MacroFlowObj &obj)
: CompoundFlowObj(obj), sp_(new S(obj.sp_->def_.pointer()))
{
size_t n = sp_->def_->nics().size();
sp_->charicVals_ = new ELObj *[n];
for (size_t i = 0; i < n; i++)
sp_->charicVals_[i] = obj.sp_->charicVals_[i];
}
MacroFlowObj::~MacroFlowObj()
{
delete [] sp_->charicVals_;
}
FlowObj *MacroFlowObj::copy(Collector &c) const
{
return new (c) MacroFlowObj(*this);
}
CompoundFlowObj *MacroFlowObj::asCompoundFlowObj()
{
if (sp_->def_->isCompound())
return this;
else
return 0;
}
bool MacroFlowObj::hasNonInheritedC(const Identifier *id) const
{
const Vector<const Identifier *> &nics = sp_->def_->nics();
for (size_t i = 0; i < nics.size(); i++)
if (nics[i] == id)
return 1;
return 0;
}
void MacroFlowObj::setNonInheritedC(const Identifier *id, ELObj *obj, const Location &, Interpreter &)
{
const Vector<const Identifier *> &nics = sp_->def_->nics();
for (size_t i = 0;; i++) {
if (nics[i] == id) {
sp_->charicVals_[i] = obj;
return;
}
}
}
void MacroFlowObj::traceSubObjects(Collector &c) const
{
size_t n = sp_->def_->nics().size();
for (size_t i = 0; i < n; i++)
c.trace(sp_->charicVals_[i]);
CompoundFlowObj::traceSubObjects(c);
}
class EmptyStyleObj : public StyleObj {
public:
void appendIter(StyleObjIter &) const { }
};
void MacroFlowObj::unpack(VM &vm)
{
size_t n = sp_->def_->nics().size();
vm.needStack(n + 1 + sp_->def_->isCompound());
for (size_t i = 0; i < n; i++)
*vm.sp++ = sp_->charicVals_[i];
if (sp_->def_->isCompound()) {
ELObj *tem = content();
if (!tem)
tem = new (*vm.interp) ProcessChildrenSosofoObj(vm.interp->initialProcessingMode(), location());
*vm.sp++ = tem;
}
}
void MacroFlowObj::processInner(ProcessContext &context)
{
FOTBuilder &fotb = context.currentFOTBuilder();
fotb.startSequence();
sp_->def_->process(context, this);
fotb.endSequence();
}
MacroFlowObj::Definition::Definition(Vector<const Identifier *> &charics,
NCVector<Owner<Expression> > &charicInits,
const Identifier *contentsId,
Owner<Expression> &body)
: contentsId_(contentsId)
{
charics.swap(charics_);
charicInits.swap(charicInits_);
charicInits_.resize(charics_.size());
body.swap(body_);
}
void MacroFlowObj::Definition::process(ProcessContext &context,
MacroFlowObj *macro)
{
VM &vm = context.vm();
Interpreter &interp = *vm.interp;;
if (code_.isNull())
compile(interp);
StyleStack *saveStyleStack = vm.styleStack;
vm.styleStack = &context.currentStyleStack();
unsigned saveSpecLevel = vm.specLevel;
vm.specLevel = vm.styleStack->level();
Vector<size_t> dep;
vm.actualDependencies = &dep;
ELObj *obj = context.vm().eval(code_.pointer(), 0, macro);
vm.styleStack = saveStyleStack;
vm.specLevel = saveSpecLevel;
if (!interp.isError(obj)) {
ELObjDynamicRoot protect(interp, obj);
((SosofoObj *)obj)->process(context);
}
}
class UnpackMacroFlowObjInsn : public Insn {
public:
UnpackMacroFlowObjInsn(InsnPtr next) : next_(next) { }
const Insn *execute(VM &vm) const {
((MacroFlowObj *)*--vm.sp)->unpack(vm);
return next_.pointer();
}
private:
InsnPtr next_;
};
void MacroFlowObj::Definition::compile(Interpreter &interp)
{
InsnPtr result;
result = new CheckSosofoInsn(body_->location(), result);
int nPush = charics_.size() + (contentsId_ != 0);
result = PopBindingsInsn::make(nPush, result);
BoundVarList frameVars;
for (size_t i = 0; i < charics_.size(); i++) {
if (i > 0 && charicInits_[i])
charicInits_[i]->markBoundVars(frameVars, 0);
frameVars.append(charics_[i], 0);
}
if (contentsId_)
frameVars.append(contentsId_, 0);
body_->markBoundVars(frameVars, 0);
result = Expression::optimizeCompile(body_, interp,
Environment(frameVars, BoundVarList()),
nPush, result);
for (size_t i = charics_.size(); i > 0; i--) {
int stackOffset = int(i) - nPush - 1;
if (frameVars[i - 1].boxed())
result = new BoxStackInsn(stackOffset, result);
InsnPtr ifNull = new SetKeyArgInsn(stackOffset, result);
if (charicInits_[i - 1]) {
BoundVarList f(frameVars);
f.resize(i - 1);
ifNull = Expression::optimizeCompile(charicInits_[i - 1], interp,
Environment(f, BoundVarList()),
nPush, ifNull);
}
else
ifNull = new ConstantInsn(interp.makeFalse(), ifNull);
result = new TestNullInsn(stackOffset, ifNull, result);
}
code_ = new UnpackMacroFlowObjInsn(result);
}
#ifdef DSSSL_NAMESPACE
}
#endif
|