| 1 |
// Copyright (c) 1996 James Clark |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#include "stylelib.h" |
| 5 |
#include "Insn.h" |
| 6 |
#include "Insn2.h" |
| 7 |
#include "Interpreter.h" |
| 8 |
#include "InterpreterMessages.h" |
| 9 |
#include "ELObjMessageArg.h" |
| 10 |
#include "Expression.h" |
| 11 |
#include "Style.h" |
| 12 |
#include <OpenSP/macros.h> |
| 13 |
|
| 14 |
#ifdef DSSSL_NAMESPACE |
| 15 |
namespace DSSSL_NAMESPACE { |
| 16 |
#endif |
| 17 |
|
| 18 |
Expression::Expression(const Location &loc) |
| 19 |
: loc_(loc) |
| 20 |
{ |
| 21 |
} |
| 22 |
|
| 23 |
void Expression::optimize(Interpreter &, const Environment &, Owner<Expression> &) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
ELObj *Expression::constantValue() const |
| 28 |
{ |
| 29 |
return 0; |
| 30 |
} |
| 31 |
|
| 32 |
const Identifier *Expression::keyword() const |
| 33 |
{ |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
void Expression::markBoundVars(BoundVarList &, bool) |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
ConstantExpression::ConstantExpression(ELObj *obj, const Location &loc) |
| 42 |
: Expression(loc), obj_(obj) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
InsnPtr |
| 47 |
ConstantExpression::compile(Interpreter &interp, const Environment &env, |
| 48 |
int stackPos, const InsnPtr &next) |
| 49 |
{ |
| 50 |
return new ConstantInsn(obj_, new ResolveQuantitiesInsn(location(), next)); |
| 51 |
} |
| 52 |
|
| 53 |
void ConstantExpression::optimize(Interpreter &interp, const Environment &, |
| 54 |
Owner<Expression> &expr) |
| 55 |
{ |
| 56 |
ELObj *tem = obj_->resolveQuantities(0, interp, location()); |
| 57 |
if (tem) { |
| 58 |
interp.makePermanent(tem); |
| 59 |
expr = new ResolvedConstantExpression(tem, location()); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
bool ConstantExpression::canEval(bool) const |
| 64 |
{ |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
const Identifier *ConstantExpression::keyword() const |
| 69 |
{ |
| 70 |
const KeywordObj *k = obj_->asKeyword(); |
| 71 |
if (k) |
| 72 |
return k->identifier(); |
| 73 |
else |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
ResolvedConstantExpression::ResolvedConstantExpression(ELObj *obj, const Location &loc) |
| 78 |
: Expression(loc), obj_(obj) |
| 79 |
{ |
| 80 |
} |
| 81 |
|
| 82 |
InsnPtr |
| 83 |
ResolvedConstantExpression::compile(Interpreter &interp, const Environment &env, |
| 84 |
int stackPos, const InsnPtr &next) |
| 85 |
{ |
| 86 |
return new ConstantInsn(obj_, next); |
| 87 |
} |
| 88 |
|
| 89 |
ELObj *ResolvedConstantExpression::constantValue() const |
| 90 |
{ |
| 91 |
return obj_; |
| 92 |
} |
| 93 |
|
| 94 |
bool ResolvedConstantExpression::canEval(bool) const |
| 95 |
{ |
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
CallExpression::CallExpression(Owner<Expression> &op, |
| 100 |
NCVector<Owner<Expression> > &args, |
| 101 |
const Location &loc) |
| 102 |
: Expression(loc) |
| 103 |
{ |
| 104 |
op.swap(op_); |
| 105 |
args.swap(args_); |
| 106 |
} |
| 107 |
|
| 108 |
bool CallExpression::canEval(bool) const |
| 109 |
{ |
| 110 |
if (!op_->canEval(1)) |
| 111 |
return 0; |
| 112 |
for (size_t i = 0; i < args_.size(); i++) |
| 113 |
if (!args_[i]->canEval(1)) |
| 114 |
return 0; |
| 115 |
return 1; |
| 116 |
} |
| 117 |
|
| 118 |
int CallExpression::nArgs() |
| 119 |
{ |
| 120 |
return args_.size(); |
| 121 |
} |
| 122 |
|
| 123 |
InsnPtr CallExpression::compile(Interpreter &interp, const Environment &env, |
| 124 |
int stackPos, const InsnPtr &next) |
| 125 |
{ |
| 126 |
op_->optimize(interp, env, op_); |
| 127 |
ELObj *value = op_->constantValue(); |
| 128 |
InsnPtr result; |
| 129 |
if (value) { |
| 130 |
FunctionObj *func = value->asFunction(); |
| 131 |
if (!func) { |
| 132 |
interp.setNextLocation(location()); |
| 133 |
interp.message(InterpreterMessages::callNonFunction, |
| 134 |
ELObjMessageArg(value, interp)); |
| 135 |
return new ErrorInsn; |
| 136 |
} |
| 137 |
if (nArgs() < func->nRequiredArgs()) { |
| 138 |
interp.setNextLocation(location()); |
| 139 |
interp.message(InterpreterMessages::missingArg); |
| 140 |
return new ErrorInsn; |
| 141 |
} |
| 142 |
if (nArgs() - func->nRequiredArgs() > func->nOptionalArgs()) { |
| 143 |
if (func->nKeyArgs()) { |
| 144 |
if ((nArgs() - func->nRequiredArgs() - func->nOptionalArgs()) & 1) { |
| 145 |
interp.setNextLocation(location()); |
| 146 |
interp.message(InterpreterMessages::oddKeyArgs); |
| 147 |
args_.resize(func->nRequiredArgs() + func->nOptionalArgs()); |
| 148 |
} |
| 149 |
} |
| 150 |
else if (!func->restArg()) { |
| 151 |
interp.setNextLocation(location()); |
| 152 |
interp.message(InterpreterMessages::tooManyArgs); |
| 153 |
args_.resize(func->nRequiredArgs() + func->nOptionalArgs()); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
int callerArgs; |
| 158 |
if (!next.isNull() && next->isReturn(callerArgs) && !interp.debugMode()) |
| 159 |
result = func->makeTailCallInsn(nArgs(), interp, location(), callerArgs); |
| 160 |
else |
| 161 |
result = func->makeCallInsn(nArgs(), interp, location(), next); |
| 162 |
} |
| 163 |
else { |
| 164 |
int n = nArgs(); |
| 165 |
int callerArgs; |
| 166 |
if (!next.isNull() && next->isReturn(callerArgs) && !interp.debugMode()) |
| 167 |
result = new TailApplyInsn(callerArgs, n, location()); |
| 168 |
else |
| 169 |
result = new ApplyInsn(n, location(), next); |
| 170 |
result = op_->compile(interp, env, stackPos + n, result); |
| 171 |
} |
| 172 |
for (size_t i = args_.size(); i > 0; i--) |
| 173 |
result = optimizeCompile(args_[i - 1], interp, env, |
| 174 |
stackPos + i - 1, result); |
| 175 |
return result; |
| 176 |
} |
| 177 |
|
| 178 |
void CallExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 179 |
{ |
| 180 |
op_->markBoundVars(vars, shared); |
| 181 |
for (size_t i = 0; i < args_.size(); i++) |
| 182 |
args_[i]->markBoundVars(vars, shared); |
| 183 |
} |
| 184 |
|
| 185 |
VariableExpression::VariableExpression(const Identifier *ident, |
| 186 |
const Location &loc) |
| 187 |
: Expression(loc), ident_(ident), isTop_(0) |
| 188 |
{ |
| 189 |
} |
| 190 |
|
| 191 |
InsnPtr VariableExpression::compile(Interpreter &interp, |
| 192 |
const Environment &env, |
| 193 |
int stackPos, |
| 194 |
const InsnPtr &next) |
| 195 |
{ |
| 196 |
bool isFrame; |
| 197 |
int index; |
| 198 |
unsigned flags; |
| 199 |
if (env.lookup(ident_, isFrame, index, flags)) { |
| 200 |
bool boxed = BoundVar::flagsBoxed(flags); |
| 201 |
InsnPtr tem; |
| 202 |
int n; |
| 203 |
if (isFrame |
| 204 |
&& !next.isNull() |
| 205 |
&& next->isPopBindings(n, tem) |
| 206 |
&& n == 1 |
| 207 |
&& index - stackPos == -1) { |
| 208 |
if (flags & BoundVar::uninitFlag) |
| 209 |
tem = new CheckInitInsn(ident_, location(), tem); |
| 210 |
// This happens with named let. |
| 211 |
if (boxed) |
| 212 |
return new UnboxInsn(tem); |
| 213 |
else |
| 214 |
return tem; |
| 215 |
} |
| 216 |
if (flags & BoundVar::uninitFlag) |
| 217 |
tem = new CheckInitInsn(ident_, location(), next); |
| 218 |
else |
| 219 |
tem = next; |
| 220 |
if (boxed) |
| 221 |
tem = new UnboxInsn(tem); |
| 222 |
if (isFrame) |
| 223 |
return new StackRefInsn(index - stackPos, index, tem); |
| 224 |
else |
| 225 |
return new ClosureRefInsn(index, tem); |
| 226 |
} |
| 227 |
isTop_ = 1; |
| 228 |
Location loc; |
| 229 |
unsigned part; |
| 230 |
if (!ident_->defined(part, loc)) { |
| 231 |
interp.setNextLocation(location()); |
| 232 |
interp.message(InterpreterMessages::undefinedVariableReference, |
| 233 |
StringMessageArg(ident_->name())); |
| 234 |
return new ErrorInsn; |
| 235 |
} |
| 236 |
ident_->requireFeature(interp, location()); |
| 237 |
ELObj *val = ident_->computeValue(0, interp); |
| 238 |
if (!val) |
| 239 |
return new TopRefInsn(ident_, next); |
| 240 |
if (interp.isError(val)) |
| 241 |
return new ErrorInsn; |
| 242 |
return new ConstantInsn(val, next); |
| 243 |
} |
| 244 |
|
| 245 |
void VariableExpression::optimize(Interpreter &interp, const Environment &env, |
| 246 |
Owner<Expression> &expr) |
| 247 |
{ |
| 248 |
bool isFrame; |
| 249 |
int index; |
| 250 |
unsigned flags; |
| 251 |
if (env.lookup(ident_, isFrame, index, flags)) |
| 252 |
return; |
| 253 |
isTop_ = 1; |
| 254 |
Location loc; |
| 255 |
unsigned part; |
| 256 |
if (ident_->defined(part, loc)) { |
| 257 |
ident_->requireFeature(interp, location()); |
| 258 |
ELObj *obj = ident_->computeValue(0, interp); |
| 259 |
if (obj && !interp.isError(obj)) { |
| 260 |
interp.makePermanent(obj); |
| 261 |
expr = new ConstantExpression(obj, location()); |
| 262 |
expr->optimize(interp, env, expr); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
bool VariableExpression::canEval(bool) const |
| 268 |
{ |
| 269 |
return !isTop_ || ident_->evaluated(); |
| 270 |
} |
| 271 |
|
| 272 |
void VariableExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 273 |
{ |
| 274 |
vars.mark(ident_, BoundVar::usedFlag | (shared ? BoundVar::sharedFlag : 0)); |
| 275 |
} |
| 276 |
|
| 277 |
IfExpression::IfExpression(Owner<Expression> &test, |
| 278 |
Owner<Expression> &consequent, |
| 279 |
Owner<Expression> &alternate, |
| 280 |
const Location &loc) |
| 281 |
: Expression(loc) |
| 282 |
{ |
| 283 |
test.swap(test_); |
| 284 |
consequent.swap(consequent_); |
| 285 |
alternate.swap(alternate_); |
| 286 |
} |
| 287 |
|
| 288 |
bool IfExpression::canEval(bool maybeCall) const |
| 289 |
{ |
| 290 |
return (test_->canEval(maybeCall) |
| 291 |
&& consequent_->canEval(maybeCall) |
| 292 |
&& alternate_->canEval(maybeCall)); |
| 293 |
} |
| 294 |
|
| 295 |
void IfExpression::optimize(Interpreter &interp, const Environment &env, |
| 296 |
Owner<Expression> &expr) |
| 297 |
{ |
| 298 |
test_->optimize(interp, env, test_); |
| 299 |
ELObj *obj = test_->constantValue(); |
| 300 |
if (obj) { |
| 301 |
if (obj->isTrue()) { |
| 302 |
expr = consequent_.extract(); |
| 303 |
expr->optimize(interp, env, expr); |
| 304 |
} |
| 305 |
else { |
| 306 |
expr = alternate_.extract(); |
| 307 |
expr->optimize(interp, env, expr); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
InsnPtr IfExpression::compile(Interpreter &interp, const Environment &env, |
| 313 |
int stackPos, const InsnPtr &next) |
| 314 |
{ |
| 315 |
alternate_->optimize(interp, env, alternate_); |
| 316 |
if (alternate_->constantValue() == interp.makeFalse()) |
| 317 |
return test_->compile(interp, env, stackPos, |
| 318 |
new AndInsn(optimizeCompile(consequent_, |
| 319 |
interp, env, |
| 320 |
stackPos, next), |
| 321 |
next)); |
| 322 |
else |
| 323 |
return test_->compile(interp, env, stackPos, |
| 324 |
new TestInsn(optimizeCompile(consequent_, |
| 325 |
interp, env, |
| 326 |
stackPos, next), |
| 327 |
alternate_->compile(interp, env, |
| 328 |
stackPos, next))); |
| 329 |
} |
| 330 |
|
| 331 |
void IfExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 332 |
{ |
| 333 |
test_->markBoundVars(vars, shared); |
| 334 |
consequent_->markBoundVars(vars, shared); |
| 335 |
alternate_->markBoundVars(vars, shared); |
| 336 |
} |
| 337 |
|
| 338 |
OrExpression::OrExpression(Owner<Expression> &test1, |
| 339 |
Owner<Expression> &test2, |
| 340 |
const Location &loc) |
| 341 |
: Expression(loc) |
| 342 |
{ |
| 343 |
test1.swap(test1_); |
| 344 |
test2.swap(test2_); |
| 345 |
} |
| 346 |
|
| 347 |
bool OrExpression::canEval(bool maybeCall) const |
| 348 |
{ |
| 349 |
return (test1_->canEval(maybeCall) |
| 350 |
&& test2_->canEval(maybeCall)); |
| 351 |
} |
| 352 |
|
| 353 |
void OrExpression::optimize(Interpreter &interp, const Environment &env, |
| 354 |
Owner<Expression> &expr) |
| 355 |
{ |
| 356 |
test1_->optimize(interp, env, test1_); |
| 357 |
ELObj *obj = test1_->constantValue(); |
| 358 |
if (obj) { |
| 359 |
if (obj->isTrue()) |
| 360 |
expr = test1_.extract(); |
| 361 |
else { |
| 362 |
expr = test2_.extract(); |
| 363 |
expr->optimize(interp, env, expr); |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
InsnPtr OrExpression::compile(Interpreter &interp, const Environment &env, |
| 369 |
int stackPos, const InsnPtr &next) |
| 370 |
{ |
| 371 |
return test1_->compile(interp, env, stackPos, |
| 372 |
new OrInsn(optimizeCompile(test2_, interp, env, |
| 373 |
stackPos, next), |
| 374 |
next)); |
| 375 |
} |
| 376 |
|
| 377 |
void OrExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 378 |
{ |
| 379 |
test1_->markBoundVars(vars, shared); |
| 380 |
test2_->markBoundVars(vars, shared); |
| 381 |
} |
| 382 |
|
| 383 |
CondFailExpression::CondFailExpression(const Location &loc) |
| 384 |
: Expression(loc) |
| 385 |
{ |
| 386 |
} |
| 387 |
|
| 388 |
InsnPtr CondFailExpression::compile(Interpreter &, const Environment &, |
| 389 |
int, const InsnPtr &) |
| 390 |
{ |
| 391 |
return new CondFailInsn(location()); |
| 392 |
} |
| 393 |
|
| 394 |
bool CondFailExpression::canEval(bool maybeCall) const |
| 395 |
{ |
| 396 |
return 1; |
| 397 |
} |
| 398 |
|
| 399 |
CaseExpression::CaseExpression(Owner<Expression> &key, |
| 400 |
NCVector<Case> &cases, |
| 401 |
Owner<Expression> &elseClause, |
| 402 |
const Location &loc) |
| 403 |
: Expression(loc) |
| 404 |
{ |
| 405 |
key.swap(key_); |
| 406 |
cases.swap(cases_); |
| 407 |
elseClause.swap(else_); |
| 408 |
} |
| 409 |
|
| 410 |
InsnPtr CaseExpression::compile(Interpreter &interp, |
| 411 |
const Environment &env, int stackPos, |
| 412 |
const InsnPtr &next) |
| 413 |
{ |
| 414 |
InsnPtr finish; |
| 415 |
if (else_) |
| 416 |
finish = new PopInsn(else_->compile(interp, env, stackPos, next)); |
| 417 |
else |
| 418 |
finish = new CaseFailInsn(location()); |
| 419 |
for (size_t i = 0; i < cases_.size(); i++) { |
| 420 |
InsnPtr match = cases_[i].expr->compile(interp, env, stackPos, next); |
| 421 |
for (size_t j = 0; j < nResolved_[i]; j++) |
| 422 |
finish = new CaseInsn(cases_[i].datums[j], match, finish); |
| 423 |
} |
| 424 |
// FIXME handle unresolved quantities |
| 425 |
return key_->compile(interp, env, stackPos, finish); |
| 426 |
} |
| 427 |
|
| 428 |
void CaseExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 429 |
{ |
| 430 |
key_->markBoundVars(vars, shared); |
| 431 |
for (size_t i = 0; i < cases_.size(); i++) |
| 432 |
cases_[i].expr->markBoundVars(vars, shared); |
| 433 |
if (else_) |
| 434 |
else_->markBoundVars(vars, shared); |
| 435 |
} |
| 436 |
|
| 437 |
bool CaseExpression::canEval(bool maybeCall) const |
| 438 |
{ |
| 439 |
if (!key_->canEval(maybeCall)) |
| 440 |
return 0; |
| 441 |
if (else_ && !else_->canEval(maybeCall)) |
| 442 |
return 0; |
| 443 |
for (size_t i = 0; i < cases_.size(); i++) { |
| 444 |
if (!cases_[i].expr->canEval(maybeCall)) |
| 445 |
return 0; |
| 446 |
if (nResolved_[i] == cases_[i].datums.size()) |
| 447 |
return 0; |
| 448 |
} |
| 449 |
return 1; |
| 450 |
} |
| 451 |
|
| 452 |
void CaseExpression::optimize(Interpreter &interp, const Environment &env, |
| 453 |
Owner<Expression> &expr) |
| 454 |
{ |
| 455 |
key_->optimize(interp, env, key_); |
| 456 |
ELObj *k = key_->constantValue(); |
| 457 |
nResolved_.assign(cases_.size(), 0); |
| 458 |
bool unresolved = 0; |
| 459 |
for (size_t i = 0; i < cases_.size(); i++) { |
| 460 |
cases_[i].expr->optimize(interp, env, cases_[i].expr); |
| 461 |
int nResolved = 0; |
| 462 |
for (size_t j = 0; j < cases_[i].datums.size(); j++) { |
| 463 |
ELObj *tem = cases_[i].datums[j]->resolveQuantities(0, interp, |
| 464 |
location()); |
| 465 |
if (tem) { |
| 466 |
if (k && ELObj::eqv(*k, *tem)) { |
| 467 |
expr = cases_[i].expr.extract(); |
| 468 |
return; |
| 469 |
} |
| 470 |
if (j != nResolved) |
| 471 |
cases_[i].datums[j] = cases_[i].datums[nResolved]; |
| 472 |
cases_[i].datums[nResolved++] = tem; |
| 473 |
} |
| 474 |
else |
| 475 |
unresolved = 1; |
| 476 |
} |
| 477 |
nResolved_[i] = nResolved; |
| 478 |
} |
| 479 |
if (else_) { |
| 480 |
else_->optimize(interp, env, else_); |
| 481 |
if (k && !unresolved) |
| 482 |
expr = else_.extract(); |
| 483 |
} |
| 484 |
else if (k && !unresolved) { |
| 485 |
interp.setNextLocation(location()); |
| 486 |
interp.message(InterpreterMessages::caseFail, ELObjMessageArg(k, interp)); |
| 487 |
} |
| 488 |
if (unresolved) { |
| 489 |
interp.setNextLocation(location()); |
| 490 |
interp.message(InterpreterMessages::caseUnresolvedQuantities); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
LambdaExpression::LambdaExpression(Vector<const Identifier *> &formals, |
| 496 |
NCVector<Owner<Expression> > &inits, |
| 497 |
int nOptional, |
| 498 |
bool hasRest, |
| 499 |
int nKey, |
| 500 |
Owner<Expression> &body, |
| 501 |
const Location &loc) |
| 502 |
: Expression(loc) |
| 503 |
{ |
| 504 |
formals.swap(formals_); |
| 505 |
inits.swap(inits_); |
| 506 |
body.swap(body_); |
| 507 |
sig_.nRequiredArgs = formals_.size() - nOptional - nKey - hasRest; |
| 508 |
sig_.nOptionalArgs = nOptional; |
| 509 |
sig_.restArg = hasRest; |
| 510 |
sig_.nKeyArgs = nKey; |
| 511 |
sig_.keys = formals_.begin() + formals_.size() - nKey; |
| 512 |
} |
| 513 |
|
| 514 |
bool LambdaExpression::canEval(bool maybeCall) const |
| 515 |
{ |
| 516 |
if (!maybeCall) |
| 517 |
return 1; |
| 518 |
if (!body_->canEval(1)) |
| 519 |
return 0; |
| 520 |
for (size_t i = 0; i < inits_.size(); i++) |
| 521 |
if (inits_[i] && !inits_[i]->canEval(1)) |
| 522 |
return 0; |
| 523 |
return 1; |
| 524 |
} |
| 525 |
|
| 526 |
InsnPtr LambdaExpression::compile(Interpreter &interp, const Environment &env, |
| 527 |
int stackPos, const InsnPtr &next) |
| 528 |
{ |
| 529 |
BoundVarList boundVars; |
| 530 |
env.boundVars(boundVars); |
| 531 |
markBoundVars(boundVars, 0); |
| 532 |
boundVars.removeUnused(); |
| 533 |
BoundVarList formalVars(formals_, sig_.nRequiredArgs); |
| 534 |
for (int i = 0; i < sig_.nOptionalArgs + sig_.nKeyArgs; i++) { |
| 535 |
if (inits_[i]) |
| 536 |
inits_[i]->markBoundVars(formalVars, 0); |
| 537 |
formalVars.append(formals_[sig_.nRequiredArgs + i], 0); |
| 538 |
} |
| 539 |
if (sig_.restArg) |
| 540 |
formalVars.append(formals_.back(), 0); |
| 541 |
ASSERT(formalVars.size() == formals_.size()); |
| 542 |
body_->markBoundVars(formalVars, 0); |
| 543 |
InsnPtr code |
| 544 |
= optimizeCompile(body_, interp, |
| 545 |
Environment(formalVars, boundVars), formals_.size(), |
| 546 |
new ReturnInsn(formals_.size())); |
| 547 |
if (sig_.nOptionalArgs || sig_.restArg || sig_.nKeyArgs) { |
| 548 |
Vector<InsnPtr> entryPoints(sig_.nOptionalArgs |
| 549 |
+ (sig_.restArg || sig_.nKeyArgs) |
| 550 |
+ 1); |
| 551 |
// First entry point is for only required args supplied |
| 552 |
// Next entry is for one optional arg supplied, no rest arg |
| 553 |
// Next entry is for two optional args, no rest arg |
| 554 |
// .. |
| 555 |
// Next entry is for all optional args, no other args |
| 556 |
// Last entry is for all optional args supplied, and other args |
| 557 |
// (for this entry point all args are pushed; unspecified |
| 558 |
// keyword args are bound to 0). |
| 559 |
entryPoints.back() = code; |
| 560 |
// Box the rest arg if necessary. |
| 561 |
if (sig_.restArg && formalVars.back().boxed()) |
| 562 |
entryPoints.back() = new BoxStackInsn(-1 - sig_.nKeyArgs, entryPoints.back()); |
| 563 |
if (sig_.nKeyArgs) { |
| 564 |
// For each keyword argument test whether it is 0, |
| 565 |
// and if so initialize it. |
| 566 |
for (int i = sig_.nOptionalArgs + sig_.nKeyArgs - 1; |
| 567 |
i >= sig_.nOptionalArgs; |
| 568 |
i--) { |
| 569 |
int offset = i - (sig_.nOptionalArgs + sig_.nKeyArgs); |
| 570 |
InsnPtr &next = entryPoints.back(); |
| 571 |
InsnPtr set(new SetKeyArgInsn(offset, next)); |
| 572 |
if (formalVars[sig_.nRequiredArgs + i].boxed()) |
| 573 |
set = new BoxInsn(set); |
| 574 |
if (inits_[i]) { |
| 575 |
BoundVarList f(formalVars); |
| 576 |
f.resize(sig_.nRequiredArgs + i + sig_.restArg); |
| 577 |
set = optimizeCompile(inits_[i], interp, |
| 578 |
Environment(f, boundVars), |
| 579 |
formals_.size(), |
| 580 |
set); |
| 581 |
} |
| 582 |
else |
| 583 |
set = new ConstantInsn(interp.makeFalse(), set); |
| 584 |
next = new TestNullInsn(offset, set, next); |
| 585 |
} |
| 586 |
} |
| 587 |
if (sig_.restArg || sig_.nKeyArgs) { |
| 588 |
for (int i = sig_.nOptionalArgs + sig_.nKeyArgs - 1; |
| 589 |
i >= sig_.nOptionalArgs; |
| 590 |
i--) { |
| 591 |
if (formalVars[sig_.nRequiredArgs + i].boxed()) |
| 592 |
code = new BoxInsn(code); |
| 593 |
if (inits_[i]) { |
| 594 |
BoundVarList f(formalVars); |
| 595 |
f.resize(sig_.nRequiredArgs + i + sig_.restArg); |
| 596 |
code = optimizeCompile(inits_[i], interp, |
| 597 |
Environment(f, boundVars), |
| 598 |
f.size(), |
| 599 |
code); |
| 600 |
} |
| 601 |
else |
| 602 |
code = new ConstantInsn(interp.makeFalse(), code); |
| 603 |
} |
| 604 |
if (sig_.restArg) { |
| 605 |
if (formalVars.back().boxed()) |
| 606 |
code = new BoxInsn(code); |
| 607 |
code = new ConstantInsn(interp.makeNil(), code); |
| 608 |
} |
| 609 |
entryPoints[sig_.nOptionalArgs] = code; |
| 610 |
} |
| 611 |
for (int i = sig_.nOptionalArgs - 1; i >= 0; i--) { |
| 612 |
InsnPtr tem(entryPoints[i + 1]); |
| 613 |
if (formalVars[sig_.nRequiredArgs + i].boxed()) |
| 614 |
tem = new BoxInsn(tem); |
| 615 |
if (inits_[i]) { |
| 616 |
BoundVarList f(formalVars); |
| 617 |
f.resize(sig_.nRequiredArgs + i); |
| 618 |
entryPoints[i] |
| 619 |
= optimizeCompile(inits_[i], interp, |
| 620 |
Environment(f, boundVars), |
| 621 |
f.size(), |
| 622 |
tem); |
| 623 |
} |
| 624 |
else |
| 625 |
entryPoints[i] = new ConstantInsn(interp.makeFalse(), |
| 626 |
tem); |
| 627 |
} |
| 628 |
for (int i = 0; i < sig_.nOptionalArgs; i++) { |
| 629 |
if (formalVars[sig_.nRequiredArgs + i].boxed()) { |
| 630 |
for (int j = i; j < sig_.nOptionalArgs; j++) |
| 631 |
entryPoints[j + 1] = new BoxArgInsn(i + sig_.nRequiredArgs, entryPoints[j + 1]); |
| 632 |
if (sig_.nKeyArgs || sig_.restArg) |
| 633 |
entryPoints.back() = new BoxStackInsn(i - sig_.nKeyArgs - sig_.restArg |
| 634 |
- sig_.nOptionalArgs, |
| 635 |
entryPoints.back()); |
| 636 |
} |
| 637 |
} |
| 638 |
code = new VarargsInsn(sig_, entryPoints, location()); |
| 639 |
} |
| 640 |
for (int i = 0; i < sig_.nRequiredArgs; i++) |
| 641 |
if (formalVars[i].boxed()) |
| 642 |
code = new BoxArgInsn(i, code); |
| 643 |
return compilePushVars(interp, env, stackPos, boundVars, 0, |
| 644 |
new ClosureInsn(&sig_, code, |
| 645 |
boundVars.size(), next)); |
| 646 |
} |
| 647 |
|
| 648 |
InsnPtr Expression::compilePushVars(Interpreter &interp, |
| 649 |
const Environment &env, |
| 650 |
int stackPos, |
| 651 |
const BoundVarList &vars, |
| 652 |
size_t varIndex, |
| 653 |
const InsnPtr &next) |
| 654 |
{ |
| 655 |
if (varIndex >= vars.size()) |
| 656 |
return next; |
| 657 |
bool isFrame; |
| 658 |
int index; |
| 659 |
unsigned flags; |
| 660 |
if (!env.lookup(vars[varIndex].ident, isFrame, index, flags)) |
| 661 |
CANNOT_HAPPEN(); |
| 662 |
if (isFrame) |
| 663 |
return new FrameRefInsn(index, |
| 664 |
compilePushVars(interp, env, stackPos + 1, vars, |
| 665 |
varIndex + 1, next)); |
| 666 |
else |
| 667 |
return new ClosureRefInsn(index, |
| 668 |
compilePushVars(interp, env, stackPos + 1, vars, |
| 669 |
varIndex + 1, next)); |
| 670 |
} |
| 671 |
|
| 672 |
void LambdaExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 673 |
{ |
| 674 |
for (int i = 0; i < sig_.nOptionalArgs + sig_.nKeyArgs; i++) |
| 675 |
if (inits_[i]) { |
| 676 |
Vector<const Identifier *> f(formals_); |
| 677 |
f.resize(sig_.nRequiredArgs + i |
| 678 |
+ (sig_.restArg && i >= sig_.nOptionalArgs)); |
| 679 |
vars.rebind(f); |
| 680 |
inits_[i]->markBoundVars(vars, 1); |
| 681 |
vars.unbind(f); |
| 682 |
} |
| 683 |
vars.rebind(formals_); |
| 684 |
body_->markBoundVars(vars, 1); |
| 685 |
vars.unbind(formals_); |
| 686 |
} |
| 687 |
|
| 688 |
LetExpression::LetExpression(Vector<const Identifier *> &vars, |
| 689 |
NCVector<Owner<Expression> > &inits, |
| 690 |
Owner<Expression> &body, |
| 691 |
const Location &loc) |
| 692 |
: Expression(loc) |
| 693 |
{ |
| 694 |
vars.swap(vars_); |
| 695 |
inits.swap(inits_); |
| 696 |
body.swap(body_); |
| 697 |
} |
| 698 |
|
| 699 |
bool LetExpression::canEval(bool maybeCall) const |
| 700 |
{ |
| 701 |
if (!body_->canEval(maybeCall)) |
| 702 |
return 0; |
| 703 |
for (size_t i = 0; i < inits_.size(); i++) |
| 704 |
if (!inits_[i]->canEval(1)) |
| 705 |
return 0; |
| 706 |
return 1; |
| 707 |
} |
| 708 |
|
| 709 |
InsnPtr LetExpression::compile(Interpreter &interp, const Environment &env, |
| 710 |
int stackPos, const InsnPtr &next) |
| 711 |
{ |
| 712 |
int nVars = vars_.size(); |
| 713 |
Environment bodyEnv(env); |
| 714 |
BoundVarList boundVars(vars_); |
| 715 |
body_->markBoundVars(boundVars, 0); |
| 716 |
bodyEnv.augmentFrame(boundVars, stackPos); |
| 717 |
return compileInits(interp, env, boundVars, 0, stackPos, |
| 718 |
optimizeCompile(body_, interp, bodyEnv, |
| 719 |
stackPos + nVars, |
| 720 |
PopBindingsInsn::make(nVars, next))); |
| 721 |
} |
| 722 |
|
| 723 |
InsnPtr LetExpression::compileInits(Interpreter &interp, |
| 724 |
const Environment &env, |
| 725 |
const BoundVarList &initVars, |
| 726 |
size_t initIndex, |
| 727 |
int stackPos, |
| 728 |
const InsnPtr &next) |
| 729 |
{ |
| 730 |
if (initIndex >= inits_.size()) |
| 731 |
return next; |
| 732 |
InsnPtr tem = compileInits(interp, env, initVars, initIndex + 1, stackPos + 1, next); |
| 733 |
if (initVars[initIndex].boxed()) |
| 734 |
tem = new BoxInsn(tem); |
| 735 |
return optimizeCompile(inits_[initIndex], interp, env, stackPos, tem); |
| 736 |
} |
| 737 |
|
| 738 |
void LetExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 739 |
{ |
| 740 |
for (size_t i = 0; i < inits_.size(); i++) |
| 741 |
inits_[i]->markBoundVars(vars, shared); |
| 742 |
vars.rebind(vars_); |
| 743 |
body_->markBoundVars(vars, shared); |
| 744 |
vars.unbind(vars_); |
| 745 |
} |
| 746 |
|
| 747 |
LetStarExpression::LetStarExpression(Vector<const Identifier *> &vars, |
| 748 |
NCVector<Owner<Expression> > &inits, |
| 749 |
Owner<Expression> &body, |
| 750 |
const Location &loc) |
| 751 |
: LetExpression(vars, inits, body, loc) |
| 752 |
{ |
| 753 |
} |
| 754 |
|
| 755 |
InsnPtr LetStarExpression::compile(Interpreter &interp, |
| 756 |
const Environment &env, |
| 757 |
int stackPos, |
| 758 |
const InsnPtr &next) |
| 759 |
{ |
| 760 |
int nVars = vars_.size(); |
| 761 |
Environment bodyEnv(env); |
| 762 |
BoundVarList vars; |
| 763 |
for (int i = 0; i < nVars; i++) { |
| 764 |
if (i > 0) |
| 765 |
inits_[i]->markBoundVars(vars, 0); |
| 766 |
vars.append(vars_[i], 0); |
| 767 |
} |
| 768 |
body_->markBoundVars(vars, 0); |
| 769 |
bodyEnv.augmentFrame(vars, stackPos); |
| 770 |
return compileInits(interp, env, vars, 0, stackPos, |
| 771 |
optimizeCompile(body_, interp, bodyEnv, |
| 772 |
stackPos + vars_.size(), |
| 773 |
PopBindingsInsn::make(nVars, next))); |
| 774 |
} |
| 775 |
|
| 776 |
InsnPtr LetStarExpression::compileInits(Interpreter &interp, |
| 777 |
const Environment &env, |
| 778 |
const BoundVarList &initVars, |
| 779 |
size_t initIndex, |
| 780 |
int stackPos, |
| 781 |
const InsnPtr &next) |
| 782 |
{ |
| 783 |
if (initIndex >= inits_.size()) |
| 784 |
return next; |
| 785 |
Environment nextEnv(env); |
| 786 |
BoundVarList vars; |
| 787 |
vars.append(initVars[initIndex].ident, initVars[initIndex].flags); |
| 788 |
nextEnv.augmentFrame(vars, stackPos); |
| 789 |
InsnPtr tem |
| 790 |
= compileInits(interp, nextEnv, initVars, initIndex + 1, stackPos + 1, next); |
| 791 |
if (initVars[initIndex].boxed()) |
| 792 |
tem = new BoxInsn(tem); |
| 793 |
return optimizeCompile(inits_[initIndex], interp, env, stackPos, tem); |
| 794 |
} |
| 795 |
|
| 796 |
LetrecExpression::LetrecExpression(Vector<const Identifier *> &vars, |
| 797 |
NCVector<Owner<Expression> > &inits, |
| 798 |
Owner<Expression> &body, |
| 799 |
const Location &loc) |
| 800 |
: Expression(loc) |
| 801 |
{ |
| 802 |
vars.swap(vars_); |
| 803 |
inits.swap(inits_); |
| 804 |
body.swap(body_); |
| 805 |
} |
| 806 |
|
| 807 |
bool LetrecExpression::canEval(bool maybeCall) const |
| 808 |
{ |
| 809 |
if (!body_->canEval(maybeCall)) |
| 810 |
return 0; |
| 811 |
for (size_t i = 0; i < inits_.size(); i++) |
| 812 |
if (!inits_[i]->canEval(1)) |
| 813 |
return 0; |
| 814 |
return 1; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
InsnPtr LetrecExpression::compile(Interpreter &interp, const Environment &env, |
| 819 |
int stackPos, const InsnPtr &next) |
| 820 |
{ |
| 821 |
int nVars = vars_.size(); |
| 822 |
|
| 823 |
BoundVarList vars(vars_, nVars, BoundVar::assignedFlag); |
| 824 |
|
| 825 |
Environment bodyEnv(env); |
| 826 |
for (size_t i = 0; i < nVars; i++) |
| 827 |
inits_[i]->markBoundVars(vars, 0); |
| 828 |
body_->markBoundVars(vars, 0); |
| 829 |
bodyEnv.augmentFrame(vars, stackPos); |
| 830 |
InsnPtr tem = optimizeCompile(body_, interp, bodyEnv, stackPos + nVars, |
| 831 |
PopBindingsInsn::make(nVars, next)); |
| 832 |
|
| 833 |
for (size_t i = 0; i < nVars; i++) |
| 834 |
vars[i].flags |= BoundVar::uninitFlag; |
| 835 |
|
| 836 |
for (int i = 0; i < nVars; i++) { |
| 837 |
if (vars[i].boxed()) |
| 838 |
tem = new SetBoxInsn(nVars, tem); |
| 839 |
else |
| 840 |
tem = new SetImmediateInsn(nVars, tem); |
| 841 |
} |
| 842 |
tem = compileInits(interp, bodyEnv, 0, stackPos + nVars, tem); |
| 843 |
for (int i = nVars; i > 0; --i) { |
| 844 |
if (vars[i - 1].boxed()) |
| 845 |
tem = new BoxInsn(tem); |
| 846 |
tem = new ConstantInsn(0, tem); |
| 847 |
} |
| 848 |
return tem; |
| 849 |
} |
| 850 |
|
| 851 |
InsnPtr LetrecExpression::compileInits(Interpreter &interp, |
| 852 |
const Environment &env, |
| 853 |
size_t initIndex, |
| 854 |
int stackPos, |
| 855 |
const InsnPtr &next) |
| 856 |
{ |
| 857 |
if (initIndex >= inits_.size()) |
| 858 |
return next; |
| 859 |
return optimizeCompile(inits_[initIndex], interp, env, stackPos, |
| 860 |
compileInits(interp, env, initIndex + 1, |
| 861 |
stackPos + 1, next)); |
| 862 |
} |
| 863 |
|
| 864 |
void LetrecExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 865 |
{ |
| 866 |
vars.rebind(vars_); |
| 867 |
for (size_t i = 0; i < inits_.size(); i++) |
| 868 |
inits_[i]->markBoundVars(vars, shared); |
| 869 |
body_->markBoundVars(vars, shared); |
| 870 |
vars.unbind(vars_); |
| 871 |
} |
| 872 |
|
| 873 |
QuasiquoteExpression::QuasiquoteExpression(NCVector<Owner<Expression> > &members, |
| 874 |
Vector<PackedBoolean> &spliced, |
| 875 |
Type type, |
| 876 |
const Location &loc) |
| 877 |
: Expression(loc), spliced_(spliced), type_(type) |
| 878 |
{ |
| 879 |
members.swap(members_); |
| 880 |
} |
| 881 |
|
| 882 |
InsnPtr QuasiquoteExpression::compile(Interpreter &interp, const Environment &env, |
| 883 |
int stackPos, const InsnPtr &next) |
| 884 |
{ |
| 885 |
InsnPtr tem(next); |
| 886 |
size_t n = members_.size(); |
| 887 |
if (type_ == vectorType) { |
| 888 |
bool splicy = 0; |
| 889 |
for (size_t i = 0; i < n; i++) { |
| 890 |
if (spliced_[i]) { |
| 891 |
splicy = 1; |
| 892 |
break; |
| 893 |
} |
| 894 |
} |
| 895 |
if (!splicy) { |
| 896 |
tem = new VectorInsn(n, tem); |
| 897 |
for (size_t i = n; i > 0; i--) |
| 898 |
tem = members_[i - 1]->compile(interp, env, stackPos + (i - 1), tem); |
| 899 |
return tem; |
| 900 |
} |
| 901 |
tem = new ListToVectorInsn(tem); |
| 902 |
} |
| 903 |
else if (type_ == improperType) |
| 904 |
n--; |
| 905 |
for (size_t i = 0; i < n; i++) { |
| 906 |
if (spliced_[i]) |
| 907 |
tem = new AppendInsn(location(), tem); |
| 908 |
else |
| 909 |
tem = new ConsInsn(tem); |
| 910 |
tem = members_[i]->compile(interp, env, stackPos + 1, tem); |
| 911 |
} |
| 912 |
if (type_ == improperType) |
| 913 |
tem = members_.back()->compile(interp, env, stackPos, tem); |
| 914 |
else |
| 915 |
tem = new ConstantInsn(interp.makeNil(), tem); |
| 916 |
return tem; |
| 917 |
} |
| 918 |
|
| 919 |
void QuasiquoteExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 920 |
{ |
| 921 |
for (size_t i = 0; i < members_.size(); i++) |
| 922 |
members_[i]->markBoundVars(vars, shared); |
| 923 |
} |
| 924 |
|
| 925 |
bool QuasiquoteExpression::canEval(bool maybeCall) const |
| 926 |
{ |
| 927 |
for (size_t i = 0; i < members_.size(); i++) |
| 928 |
if (!members_[i]->canEval(maybeCall)) |
| 929 |
return 0; |
| 930 |
return 1; |
| 931 |
} |
| 932 |
|
| 933 |
void QuasiquoteExpression::optimize(Interpreter &interp, const Environment &env, Owner<Expression> &expr) |
| 934 |
{ |
| 935 |
for (size_t i = 0; i < members_.size(); i++) |
| 936 |
members_[i]->optimize(interp, env, members_[i]); |
| 937 |
if (type_ == vectorType) |
| 938 |
return; |
| 939 |
if (members_.size() == 0) { |
| 940 |
expr = new ResolvedConstantExpression(interp.makeNil(), location()); |
| 941 |
return; |
| 942 |
} |
| 943 |
ELObj *tail = members_.back()->constantValue(); |
| 944 |
if (!tail) |
| 945 |
return; |
| 946 |
ASSERT(!(spliced_.back() && type_ == improperType)); |
| 947 |
if (type_ != improperType && !spliced_.back()) { |
| 948 |
tail = interp.makePair(tail, interp.makeNil()); |
| 949 |
interp.makePermanent(tail); |
| 950 |
} |
| 951 |
for (size_t i = members_.size() - 1; i-- > 0;) { |
| 952 |
ELObj *tem = members_[i]->constantValue(); |
| 953 |
// FIXME optimize splice as well |
| 954 |
if (!tem || spliced_[i]) { |
| 955 |
members_.resize(i + 2); |
| 956 |
type_ = improperType; |
| 957 |
members_[i + 1] = new ResolvedConstantExpression(tail, location()); |
| 958 |
return; |
| 959 |
} |
| 960 |
tail = interp.makePair(tem, tail); |
| 961 |
interp.makePermanent(tail); |
| 962 |
} |
| 963 |
expr = new ResolvedConstantExpression(tail, location()); |
| 964 |
} |
| 965 |
|
| 966 |
SequenceExpression::SequenceExpression(NCVector<Owner<Expression> > &sequence, |
| 967 |
const Location &loc) |
| 968 |
: Expression(loc) |
| 969 |
{ |
| 970 |
ASSERT(sequence.size() > 0); |
| 971 |
sequence.swap(sequence_); |
| 972 |
} |
| 973 |
|
| 974 |
InsnPtr |
| 975 |
SequenceExpression::compile(Interpreter &interp, const Environment &env, |
| 976 |
int stackPos, const InsnPtr &next) |
| 977 |
{ |
| 978 |
// FIXME optimize this |
| 979 |
InsnPtr result(sequence_.back()->compile(interp, env, stackPos, next)); |
| 980 |
for (size_t i = sequence_.size() - 1; i > 0; i--) |
| 981 |
result = sequence_[i - 1]->compile(interp, env, stackPos, |
| 982 |
new PopInsn(result)); |
| 983 |
return result; |
| 984 |
} |
| 985 |
|
| 986 |
void SequenceExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 987 |
{ |
| 988 |
for (size_t i = 0; i < sequence_.size(); i++) |
| 989 |
sequence_[i]->markBoundVars(vars, shared); |
| 990 |
} |
| 991 |
|
| 992 |
bool SequenceExpression::canEval(bool maybeCall) const |
| 993 |
{ |
| 994 |
for (size_t i = 0; i < sequence_.size(); i++) |
| 995 |
if (!sequence_[i]->canEval(maybeCall)) |
| 996 |
return 0; |
| 997 |
return 1; |
| 998 |
} |
| 999 |
|
| 1000 |
void SequenceExpression::optimize(Interpreter &interp, const Environment &env, |
| 1001 |
Owner<Expression> &expr) |
| 1002 |
{ |
| 1003 |
size_t j = 0; |
| 1004 |
for (size_t i = 0;; i++) { |
| 1005 |
if (j != i) |
| 1006 |
sequence_[j].swap(sequence_[i]); |
| 1007 |
sequence_[j]->optimize(interp, env, sequence_[j]); |
| 1008 |
if (i == sequence_.size() - 1) |
| 1009 |
break; |
| 1010 |
if (!sequence_[j]->constantValue()) |
| 1011 |
j++; |
| 1012 |
} |
| 1013 |
if (j == 0) |
| 1014 |
sequence_[0].swap(expr); |
| 1015 |
else |
| 1016 |
sequence_.resize(j + 1); |
| 1017 |
} |
| 1018 |
|
| 1019 |
AssignmentExpression::AssignmentExpression(const Identifier *var, |
| 1020 |
Owner<Expression> &value, |
| 1021 |
const Location &loc) |
| 1022 |
: Expression(loc), var_(var) |
| 1023 |
{ |
| 1024 |
value.swap(value_); |
| 1025 |
} |
| 1026 |
|
| 1027 |
InsnPtr AssignmentExpression::compile(Interpreter &interp, const Environment &env, |
| 1028 |
int stackPos, const InsnPtr &next) |
| 1029 |
{ |
| 1030 |
bool isFrame; |
| 1031 |
int index; |
| 1032 |
unsigned flags; |
| 1033 |
if (!env.lookup(var_, isFrame, index, flags)) { |
| 1034 |
interp.setNextLocation(location()); |
| 1035 |
unsigned part; |
| 1036 |
Location loc; |
| 1037 |
if (var_->defined(part, loc)) |
| 1038 |
interp.message(InterpreterMessages::topLevelAssignment, |
| 1039 |
StringMessageArg(var_->name())); |
| 1040 |
else |
| 1041 |
interp.message(InterpreterMessages::undefinedVariableReference, |
| 1042 |
StringMessageArg(var_->name())); |
| 1043 |
return new ErrorInsn; |
| 1044 |
} |
| 1045 |
InsnPtr result; |
| 1046 |
if (flags & BoundVar::uninitFlag) |
| 1047 |
result = new CheckInitInsn(var_, location(), next); |
| 1048 |
else |
| 1049 |
result = next; |
| 1050 |
if (isFrame) { |
| 1051 |
if (BoundVar::flagsBoxed(flags)) |
| 1052 |
result = new StackSetBoxInsn(index - (stackPos + 1), index, location(), result); |
| 1053 |
else |
| 1054 |
result = new StackSetInsn(index - (stackPos + 1), index, result); |
| 1055 |
} |
| 1056 |
else { |
| 1057 |
ASSERT(BoundVar::flagsBoxed(flags)); |
| 1058 |
result = new ClosureSetBoxInsn(index, location(), result); |
| 1059 |
} |
| 1060 |
return optimizeCompile(value_, interp, env, stackPos, result); |
| 1061 |
} |
| 1062 |
|
| 1063 |
void AssignmentExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 1064 |
{ |
| 1065 |
vars.mark(var_, |
| 1066 |
(BoundVar::usedFlag |
| 1067 |
|BoundVar::assignedFlag |
| 1068 |
|(shared ? BoundVar::sharedFlag : 0))); |
| 1069 |
value_->markBoundVars(vars, shared); |
| 1070 |
} |
| 1071 |
|
| 1072 |
bool AssignmentExpression::canEval(bool maybeCall) const |
| 1073 |
{ |
| 1074 |
return value_->canEval(maybeCall); |
| 1075 |
} |
| 1076 |
|
| 1077 |
WithModeExpression::WithModeExpression(const ProcessingMode *mode, Owner<Expression> &expr, |
| 1078 |
const Location &loc) |
| 1079 |
: Expression(loc), mode_(mode) |
| 1080 |
{ |
| 1081 |
expr.swap(expr_); |
| 1082 |
} |
| 1083 |
|
| 1084 |
InsnPtr WithModeExpression::compile(Interpreter &interp, const Environment &env, int stackPos, |
| 1085 |
const InsnPtr &next) |
| 1086 |
{ |
| 1087 |
if (!mode_->defined()) { |
| 1088 |
interp.setNextLocation(location()); |
| 1089 |
interp.message(InterpreterMessages::undefinedMode, StringMessageArg(mode_->name())); |
| 1090 |
} |
| 1091 |
return new PushModeInsn(mode_, |
| 1092 |
optimizeCompile(expr_, interp, env, stackPos, |
| 1093 |
new PopModeInsn(next))); |
| 1094 |
} |
| 1095 |
|
| 1096 |
void WithModeExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 1097 |
{ |
| 1098 |
expr_->markBoundVars(vars, shared); |
| 1099 |
} |
| 1100 |
|
| 1101 |
bool WithModeExpression::canEval(bool maybeCall) const |
| 1102 |
{ |
| 1103 |
return expr_->canEval(maybeCall); |
| 1104 |
} |
| 1105 |
|
| 1106 |
StyleExpression::StyleExpression(Vector<const Identifier *> &keys, |
| 1107 |
NCVector<Owner<Expression> > &exprs, |
| 1108 |
const Location &loc) |
| 1109 |
: Expression(loc) |
| 1110 |
{ |
| 1111 |
keys.swap(keys_); |
| 1112 |
exprs.swap(exprs_); |
| 1113 |
} |
| 1114 |
|
| 1115 |
// Interpret keywords in the following order of preference |
| 1116 |
// 1. non-inherited characteristics |
| 1117 |
// 2. DSSSL defined meaning (use: content-map:) etc |
| 1118 |
// 3. inherited characteristics |
| 1119 |
|
| 1120 |
InsnPtr StyleExpression::compile(Interpreter &interp, const Environment &env, |
| 1121 |
int stackPos, const InsnPtr &next) |
| 1122 |
{ |
| 1123 |
Vector<ConstPtr<InheritedC> > ics; |
| 1124 |
Vector<ConstPtr<InheritedC> > forceIcs; |
| 1125 |
Vector<const Identifier *> forceKeys(keys_.size()); |
| 1126 |
for (size_t i = 0; i < keys_.size(); i++) { |
| 1127 |
forceKeys[i] = 0; |
| 1128 |
if (keys_[i]->name().size() > 6) { |
| 1129 |
StringC prefix(keys_[i]->name().data(), 6); |
| 1130 |
if (prefix == interp.makeStringC("force!")) { |
| 1131 |
StringC name(keys_[i]->name().data() + 6, keys_[i]->name().size() - 6); |
| 1132 |
forceKeys[i] = interp.lookup(name); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
} |
| 1136 |
bool hasUse = 0; |
| 1137 |
size_t useIndex; |
| 1138 |
BoundVarList boundVars; |
| 1139 |
env.boundVars(boundVars); |
| 1140 |
for (size_t i = 0; i < keys_.size(); i++) { |
| 1141 |
Identifier::SyntacticKey sk; |
| 1142 |
if (forceKeys[i] |
| 1143 |
&& maybeStyleKeyword(forceKeys[i]) |
| 1144 |
&& !forceKeys[i]->inheritedC().isNull()) { |
| 1145 |
forceIcs.resize(forceIcs.size() + 1); |
| 1146 |
exprs_[i]->markBoundVars(boundVars, 0); |
| 1147 |
} |
| 1148 |
else if (maybeStyleKeyword(keys_[i]) |
| 1149 |
&& !(keys_[i]->syntacticKey(sk) && sk == Identifier::keyUse) |
| 1150 |
&& !keys_[i]->inheritedC().isNull()) { |
| 1151 |
ics.resize(ics.size() + 1); |
| 1152 |
exprs_[i]->markBoundVars(boundVars, 0); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
// FIXME optimize case where ics.size() == 0 |
| 1156 |
boundVars.removeUnused(); |
| 1157 |
BoundVarList noVars; |
| 1158 |
Environment newEnv(noVars, boundVars); |
| 1159 |
size_t j = 0; |
| 1160 |
size_t k = 0; |
| 1161 |
for (size_t i = 0; i < keys_.size(); i++) { |
| 1162 |
Identifier::SyntacticKey sk; |
| 1163 |
if (forceKeys[i] |
| 1164 |
&& maybeStyleKeyword(forceKeys[i]) |
| 1165 |
&& !forceKeys[i]->inheritedC().isNull()) { |
| 1166 |
exprs_[i]->optimize(interp, newEnv, exprs_[i]); |
| 1167 |
ELObj *val = exprs_[i]->constantValue(); |
| 1168 |
if (val) { |
| 1169 |
interp.makePermanent(val); |
| 1170 |
forceIcs[k] = forceKeys[i]->inheritedC()->make(val, exprs_[i]->location(), interp); |
| 1171 |
if (forceIcs[k].isNull()) |
| 1172 |
forceIcs.resize(forceIcs.size() - 1); |
| 1173 |
else |
| 1174 |
k++; |
| 1175 |
} |
| 1176 |
else { |
| 1177 |
forceIcs[k++] = new VarInheritedC(forceKeys[i]->inheritedC(), |
| 1178 |
exprs_[i]->compile(interp, newEnv, 0, InsnPtr()), |
| 1179 |
exprs_[i]->location()); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
else if (!maybeStyleKeyword(keys_[i])) |
| 1183 |
; |
| 1184 |
else if (keys_[i]->syntacticKey(sk) && sk == Identifier::keyUse) { |
| 1185 |
if (!hasUse) { |
| 1186 |
hasUse = 1; |
| 1187 |
useIndex = i; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
else if (!keys_[i]->inheritedC().isNull()) { |
| 1191 |
exprs_[i]->optimize(interp, newEnv, exprs_[i]); |
| 1192 |
ELObj *val = exprs_[i]->constantValue(); |
| 1193 |
if (val) { |
| 1194 |
interp.makePermanent(val); |
| 1195 |
ics[j] = keys_[i]->inheritedC()->make(val, exprs_[i]->location(), interp); |
| 1196 |
if (ics[j].isNull()) |
| 1197 |
ics.resize(ics.size() - 1); |
| 1198 |
else |
| 1199 |
j++; |
| 1200 |
} |
| 1201 |
else { |
| 1202 |
ics[j++] = new VarInheritedC(keys_[i]->inheritedC(), |
| 1203 |
exprs_[i]->compile(interp, newEnv, 0, InsnPtr()), |
| 1204 |
exprs_[i]->location()); |
| 1205 |
} |
| 1206 |
} |
| 1207 |
else |
| 1208 |
unknownStyleKeyword(keys_[i], interp, location()); |
| 1209 |
} |
| 1210 |
InsnPtr result |
| 1211 |
= compilePushVars(interp, env, stackPos + hasUse, boundVars, 0, |
| 1212 |
new VarStyleInsn(new StyleSpec(forceIcs, ics), |
| 1213 |
boundVars.size(), hasUse, |
| 1214 |
new MaybeOverrideStyleInsn(next))); |
| 1215 |
if (!hasUse) |
| 1216 |
return result; |
| 1217 |
else { |
| 1218 |
result = new CheckStyleInsn(location(), result); |
| 1219 |
return optimizeCompile(exprs_[useIndex], interp, env, stackPos, result); |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
void StyleExpression::markBoundVars(BoundVarList &vars, bool shared) |
| 1224 |
{ |
| 1225 |
for (size_t i = 0; i < exprs_.size(); i++) |
| 1226 |
exprs_[i]->markBoundVars(vars, 1); |
| 1227 |
} |
| 1228 |
|
| 1229 |
bool StyleExpression::canEval(bool maybeCall) const |
| 1230 |
{ |
| 1231 |
for (size_t i = 0; i < exprs_.size(); i++) |
| 1232 |
if (!exprs_[i]->canEval(maybeCall)) |
| 1233 |
return 0; |
| 1234 |
return 1; |
| 1235 |
} |
| 1236 |
|
| 1237 |
void StyleExpression::unknownStyleKeyword(const Identifier *ident, Interpreter &interp, |
| 1238 |
const Location &loc) const |
| 1239 |
{ |
| 1240 |
interp.setNextLocation(loc); |
| 1241 |
StringC tem(ident->name()); |
| 1242 |
tem += Char(':'); |
| 1243 |
interp.message(InterpreterMessages::invalidStyleKeyword, |
| 1244 |
StringMessageArg(tem)); |
| 1245 |
} |
| 1246 |
|
| 1247 |
bool StyleExpression::maybeStyleKeyword(const Identifier *ident) const |
| 1248 |
{ |
| 1249 |
return 1; |
| 1250 |
} |
| 1251 |
|
| 1252 |
MakeExpression::MakeExpression(const Identifier *foc, |
| 1253 |
Vector<const Identifier *> &keys, |
| 1254 |
NCVector<Owner<Expression> > &exprs, |
| 1255 |
const Location &loc) |
| 1256 |
: foc_(foc), StyleExpression(keys, exprs, loc) |
| 1257 |
{ |
| 1258 |
} |
| 1259 |
|
| 1260 |
InsnPtr MakeExpression::compile(Interpreter &interp, const Environment &env, int stackPos, |
| 1261 |
const InsnPtr &next) |
| 1262 |
{ |
| 1263 |
FlowObj *flowObj = foc_->flowObj(); |
| 1264 |
if (!flowObj) { |
| 1265 |
interp.setNextLocation(location()); |
| 1266 |
interp.message(InterpreterMessages::unknownFlowObjectClass, |
| 1267 |
StringMessageArg(foc_->name())); |
| 1268 |
flowObj = new (interp) SequenceFlowObj; |
| 1269 |
interp.makePermanent(flowObj); |
| 1270 |
} |
| 1271 |
foc_->requireFeature(interp, location(), 1); |
| 1272 |
Owner<Expression> *contentMapExpr = 0; |
| 1273 |
InsnPtr rest(next); |
| 1274 |
for (size_t i = 0; i < keys_.size(); i++) { |
| 1275 |
Identifier::SyntacticKey syn; |
| 1276 |
if (!flowObj->hasNonInheritedC(keys_[i]) && keys_[i]->syntacticKey(syn)) { |
| 1277 |
if (syn == Identifier::keyLabel) |
| 1278 |
rest = optimizeCompile(exprs_[i], |
| 1279 |
interp, |
| 1280 |
env, |
| 1281 |
stackPos + 1, |
| 1282 |
new LabelSosofoInsn(exprs_[i]->location(), rest)); |
| 1283 |
else if (syn == Identifier::keyContentMap) |
| 1284 |
contentMapExpr = &exprs_[i]; |
| 1285 |
} |
| 1286 |
} |
| 1287 |
flowObj = applyConstNonInheritedCs(flowObj, interp, env); |
| 1288 |
size_t nContent = exprs_.size() - keys_.size(); |
| 1289 |
CompoundFlowObj *cFlowObj = flowObj->asCompoundFlowObj(); |
| 1290 |
if (!cFlowObj && nContent > 0) { |
| 1291 |
interp.setNextLocation(location()); |
| 1292 |
interp.message(InterpreterMessages::atomicContent, |
| 1293 |
StringMessageArg(foc_->name())); |
| 1294 |
nContent = 0; |
| 1295 |
} |
| 1296 |
rest = compileNonInheritedCs(interp, env, stackPos + 1, rest); |
| 1297 |
for (size_t i = 0; i < keys_.size(); i++) { |
| 1298 |
if (flowObj->hasPseudoNonInheritedC(keys_[i]) |
| 1299 |
&& !exprs_[i]->constantValue()) { |
| 1300 |
rest = exprs_[i]->compile(interp, env, stackPos + 1, |
| 1301 |
new SetPseudoNonInheritedCInsn(keys_[i], |
| 1302 |
exprs_[i]->location(), |
| 1303 |
rest)); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
// FIXME optimize case where there are no non-inherited styles. |
| 1307 |
rest = StyleExpression::compile(interp, env, stackPos + 1, new SetStyleInsn(rest)); |
| 1308 |
if (nContent == 0 && !contentMapExpr) { |
| 1309 |
if (cFlowObj) |
| 1310 |
return new SetDefaultContentInsn(cFlowObj, location(), rest); |
| 1311 |
else |
| 1312 |
return new CopyFlowObjInsn(flowObj, location(), rest); |
| 1313 |
} |
| 1314 |
rest = new SetContentInsn(cFlowObj, location(), rest); |
| 1315 |
if (contentMapExpr) { |
| 1316 |
rest = optimizeCompile(*contentMapExpr, interp, env, stackPos + 1, |
| 1317 |
new ContentMapSosofoInsn((*contentMapExpr)->location(), rest)); |
| 1318 |
if (nContent == 0) |
| 1319 |
return new MakeDefaultContentInsn(location(), rest); |
| 1320 |
} |
| 1321 |
// FIXME get rid of CheckSosofoInsn if we can guarantee result is a SosofoObj. |
| 1322 |
if (nContent == 1) |
| 1323 |
return optimizeCompile(exprs_.back(), interp, env, stackPos, |
| 1324 |
new CheckSosofoInsn(exprs_.back()->location(), rest)); |
| 1325 |
rest = new SosofoAppendInsn(nContent, rest); |
| 1326 |
for (size_t i = 1; i <= nContent; i++) |
| 1327 |
rest = optimizeCompile(exprs_[exprs_.size() - i], interp, env, stackPos + nContent - i, |
| 1328 |
new CheckSosofoInsn(exprs_[exprs_.size() - i]->location(), rest)); |
| 1329 |
return rest; |
| 1330 |
} |
| 1331 |
|
| 1332 |
FlowObj *MakeExpression::applyConstNonInheritedCs(FlowObj *flowObj, Interpreter &interp, |
| 1333 |
const Environment &env) |
| 1334 |
{ |
| 1335 |
FlowObj *result = flowObj; |
| 1336 |
for (size_t i = 0; i < keys_.size(); i++) |
| 1337 |
if (flowObj->hasNonInheritedC(keys_[i]) |
| 1338 |
|| flowObj->hasPseudoNonInheritedC(keys_[i])) { |
| 1339 |
exprs_[i]->optimize(interp, env, exprs_[i]); |
| 1340 |
ELObj *val = exprs_[i]->constantValue(); |
| 1341 |
if (val) { |
| 1342 |
if (result == flowObj) { |
| 1343 |
result = flowObj->copy(interp); |
| 1344 |
interp.makePermanent(result); |
| 1345 |
} |
| 1346 |
result->setNonInheritedC(keys_[i], val, exprs_[i]->location(), interp); |
| 1347 |
} |
| 1348 |
} |
| 1349 |
return result; |
| 1350 |
} |
| 1351 |
|
| 1352 |
// Have a copied flow object on the stack. |
| 1353 |
// Generate code to set its non-constant, non-inherited characteristics. |
| 1354 |
|
| 1355 |
InsnPtr MakeExpression::compileNonInheritedCs(Interpreter &interp, const Environment &env, |
| 1356 |
int stackPos, const InsnPtr &next) |
| 1357 |
{ |
| 1358 |
FlowObj *flowObj = foc_->flowObj(); |
| 1359 |
if (!flowObj) |
| 1360 |
return next; |
| 1361 |
bool gotOne = flowObj->isCharacter(); |
| 1362 |
BoundVarList boundVars; |
| 1363 |
env.boundVars(boundVars); |
| 1364 |
for (size_t i = 0; i < keys_.size(); i++) { |
| 1365 |
if (flowObj->hasNonInheritedC(keys_[i]) && !exprs_[i]->constantValue()) { |
| 1366 |
exprs_[i]->markBoundVars(boundVars, 0); |
| 1367 |
gotOne = 1; |
| 1368 |
} |
| 1369 |
} |
| 1370 |
if (!gotOne) |
| 1371 |
return next; |
| 1372 |
boundVars.removeUnused(); |
| 1373 |
BoundVarList noVars; |
| 1374 |
Environment newEnv(noVars, boundVars); |
| 1375 |
InsnPtr code; |
| 1376 |
for (size_t i = 0; i < keys_.size(); i++) |
| 1377 |
if (flowObj->hasNonInheritedC(keys_[i]) && !exprs_[i]->constantValue()) |
| 1378 |
code = exprs_[i]->compile(interp, newEnv, 1, |
| 1379 |
new SetNonInheritedCInsn(keys_[i], |
| 1380 |
exprs_[i]->location(), |
| 1381 |
code)); |
| 1382 |
InsnPtr rest(new SetNonInheritedCsSosofoInsn(code, boundVars.size(), next)); |
| 1383 |
if (flowObj->isCharacter()) |
| 1384 |
rest = new SetImplicitCharInsn(location(), rest); |
| 1385 |
return compilePushVars(interp, env, stackPos, boundVars, 0, rest); |
| 1386 |
} |
| 1387 |
|
| 1388 |
void MakeExpression::unknownStyleKeyword(const Identifier *ident, Interpreter &interp, |
| 1389 |
const Location &loc) const |
| 1390 |
{ |
| 1391 |
FlowObj *flowObj = foc_->flowObj(); |
| 1392 |
if (!flowObj) |
| 1393 |
return; |
| 1394 |
Identifier::SyntacticKey key; |
| 1395 |
if (ident->syntacticKey(key)) { |
| 1396 |
switch(key) { |
| 1397 |
case Identifier::keyLabel: |
| 1398 |
case Identifier::keyContentMap: |
| 1399 |
return; |
| 1400 |
default: |
| 1401 |
break; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
if (flowObj->hasNonInheritedC(ident) |
| 1405 |
|| flowObj->hasPseudoNonInheritedC(ident)) |
| 1406 |
return; |
| 1407 |
interp.setNextLocation(loc); |
| 1408 |
StringC tem(ident->name()); |
| 1409 |
tem += Char(':'); |
| 1410 |
interp.message(InterpreterMessages::invalidMakeKeyword, |
| 1411 |
StringMessageArg(tem), StringMessageArg(foc_->name())); |
| 1412 |
} |
| 1413 |
|
| 1414 |
bool MakeExpression::maybeStyleKeyword(const Identifier *ident) const |
| 1415 |
{ |
| 1416 |
FlowObj *flowObj = foc_->flowObj(); |
| 1417 |
if (!flowObj) |
| 1418 |
return 1; |
| 1419 |
return (!flowObj->hasNonInheritedC(ident) |
| 1420 |
&& !flowObj->hasPseudoNonInheritedC(ident)); |
| 1421 |
} |
| 1422 |
|
| 1423 |
Environment::Environment() |
| 1424 |
: closureVars_(0) |
| 1425 |
{ |
| 1426 |
} |
| 1427 |
|
| 1428 |
Environment::Environment(const BoundVarList &frameVars, |
| 1429 |
const BoundVarList &closureVars) |
| 1430 |
: closureVars_(&closureVars) |
| 1431 |
{ |
| 1432 |
FrameVarList *tem = new FrameVarList; |
| 1433 |
frameVarList_ = tem; |
| 1434 |
tem->vars = &frameVars; |
| 1435 |
tem->stackPos = 0; |
| 1436 |
} |
| 1437 |
|
| 1438 |
void Environment::boundVars(BoundVarList &result) const |
| 1439 |
{ |
| 1440 |
if (closureVars_) { |
| 1441 |
for (size_t i = 0; i < closureVars_->size(); i++) |
| 1442 |
result.append((*closureVars_)[i].ident, |
| 1443 |
(*closureVars_)[i].flags); |
| 1444 |
} |
| 1445 |
for (const FrameVarList *f = frameVarList_.pointer(); |
| 1446 |
f; |
| 1447 |
f = f->next.pointer()) { |
| 1448 |
for (size_t i = 0; i < f->vars->size(); i++) |
| 1449 |
result.append((*f->vars)[i].ident, (*f->vars)[i].flags); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
bool Environment::lookup(const Identifier *ident, |
| 1454 |
bool &isFrame, int &index, unsigned &flags) |
| 1455 |
const |
| 1456 |
{ |
| 1457 |
for (const FrameVarList *p = frameVarList_.pointer(); |
| 1458 |
p; |
| 1459 |
p = p->next.pointer()) { |
| 1460 |
for (size_t i = 0; i < p->vars->size(); i++) |
| 1461 |
if ((*p->vars)[i].ident == ident) { |
| 1462 |
isFrame = true; |
| 1463 |
index = i + p->stackPos; |
| 1464 |
flags = (*p->vars)[i].flags; |
| 1465 |
return true; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
if (closureVars_) { |
| 1469 |
for (size_t i = 0; i < closureVars_->size(); i++) |
| 1470 |
if ((*closureVars_)[i].ident == ident) { |
| 1471 |
isFrame = false; |
| 1472 |
index = i; |
| 1473 |
flags = (*closureVars_)[i].flags; |
| 1474 |
return true; |
| 1475 |
} |
| 1476 |
} |
| 1477 |
return false; |
| 1478 |
} |
| 1479 |
|
| 1480 |
void Environment::augmentFrame(const BoundVarList &vars, int stackPos) |
| 1481 |
{ |
| 1482 |
FrameVarList *tem = new FrameVarList; |
| 1483 |
tem->stackPos = stackPos; |
| 1484 |
tem->vars = &vars; |
| 1485 |
tem->next = frameVarList_; |
| 1486 |
frameVarList_ = tem; |
| 1487 |
} |
| 1488 |
|
| 1489 |
BoundVarList::BoundVarList(const Vector<const Identifier *> &idents) |
| 1490 |
: Vector<BoundVar>(idents.size()) |
| 1491 |
{ |
| 1492 |
for (size_t i = 0; i < size(); i++) { |
| 1493 |
BoundVar &tem = (*this)[i]; |
| 1494 |
tem.ident = idents[i]; |
| 1495 |
tem.reboundCount = 0; |
| 1496 |
tem.flags = 0; |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
BoundVarList::BoundVarList(const Vector<const Identifier *> &idents, size_t n, |
| 1501 |
unsigned flags) |
| 1502 |
: Vector<BoundVar>(n) |
| 1503 |
{ |
| 1504 |
for (size_t i = 0; i < n; i++) { |
| 1505 |
BoundVar &tem = (*this)[i]; |
| 1506 |
tem.ident = idents[i]; |
| 1507 |
tem.reboundCount = 0; |
| 1508 |
tem.flags = (flags & ~BoundVar::usedFlag); |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
void BoundVarList::append(const Identifier *id, unsigned flags) |
| 1513 |
{ |
| 1514 |
resize(size() + 1); |
| 1515 |
BoundVar &tem = back(); |
| 1516 |
tem.ident = id; |
| 1517 |
tem.flags = (flags & ~BoundVar::usedFlag); |
| 1518 |
tem.reboundCount = 0; |
| 1519 |
} |
| 1520 |
|
| 1521 |
void BoundVarList::remove(const Vector<const Identifier *> &idents) |
| 1522 |
{ |
| 1523 |
size_t j = 0; |
| 1524 |
for (size_t i = 0; i < size(); i++) { |
| 1525 |
const Identifier *ident = (*this)[i].ident; |
| 1526 |
for (size_t k = 0;; k++) { |
| 1527 |
if (k >= idents.size()) { |
| 1528 |
if (j != i) |
| 1529 |
(*this)[j] = (*this)[i]; |
| 1530 |
j++; |
| 1531 |
break; |
| 1532 |
} |
| 1533 |
if (idents[k] == ident) |
| 1534 |
break; // skip it |
| 1535 |
} |
| 1536 |
} |
| 1537 |
resize(j); |
| 1538 |
} |
| 1539 |
|
| 1540 |
void BoundVarList::removeUnused() |
| 1541 |
{ |
| 1542 |
size_t j = 0; |
| 1543 |
for (size_t i = 0; i < size(); i++) { |
| 1544 |
if ((*this)[i].flags & BoundVar::usedFlag) { |
| 1545 |
if (j != i) |
| 1546 |
(*this)[j] = (*this)[i]; |
| 1547 |
j++; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
resize(j); |
| 1551 |
} |
| 1552 |
|
| 1553 |
void BoundVarList::mark(const Identifier *ident, unsigned flags) |
| 1554 |
{ |
| 1555 |
BoundVar *bv = find(ident); |
| 1556 |
if (bv && !bv->reboundCount) |
| 1557 |
bv->flags |= flags; |
| 1558 |
} |
| 1559 |
|
| 1560 |
void BoundVarList::rebind(const Vector<const Identifier *> &idents) |
| 1561 |
{ |
| 1562 |
for (size_t i = 0; i < idents.size(); i++) { |
| 1563 |
BoundVar *bv = find(idents[i]); |
| 1564 |
if (bv) |
| 1565 |
bv->reboundCount += 1; |
| 1566 |
} |
| 1567 |
} |
| 1568 |
|
| 1569 |
void BoundVarList::unbind(const Vector<const Identifier *> &idents) |
| 1570 |
{ |
| 1571 |
for (size_t i = 0; i < idents.size(); i++) { |
| 1572 |
BoundVar *bv = find(idents[i]); |
| 1573 |
if (bv) |
| 1574 |
bv->reboundCount -= 1; |
| 1575 |
} |
| 1576 |
} |
| 1577 |
|
| 1578 |
BoundVar *BoundVarList::find(const Identifier *ident) |
| 1579 |
{ |
| 1580 |
for (size_t i = 0; i < size(); i++) |
| 1581 |
if ((*this)[i].ident == ident) |
| 1582 |
return &(*this)[i]; |
| 1583 |
return 0; |
| 1584 |
} |
| 1585 |
|
| 1586 |
#ifdef DSSSL_NAMESPACE |
| 1587 |
} |
| 1588 |
#endif |