00001 package edu.ksu.cis.bandera.specification.predicate.ast;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import edu.ksu.cis.bandera.specification.predicate.exception.*;
00036 import edu.ksu.cis.bandera.specification.predicate.analysis.*;
00037 import edu.ksu.cis.bandera.specification.predicate.node.*;
00038 import java.util.*;
00039 public class Simplifier extends DepthFirstAdapter {
00040 private static Simplifier simplifier = new Simplifier();
00041 private PExp exp;
00042 private Vector exceptions = new Vector();
00043
00044
00045
00046
00047
00048 private ANameExp buildName(String name) {
00049 StringTokenizer tokenizer = new StringTokenizer(name, ". ");
00050 PName result = new ASimpleName(new TId(tokenizer.nextToken()));
00051
00052 while (tokenizer.hasMoreTokens()) {
00053 result = new AQualifiedName(result, new TDot(), new TId(tokenizer.nextToken()));
00054 }
00055
00056 return new ANameExp(result);
00057 }
00058
00059
00060
00061
00062
00063
00064 private PExp buildNavigation(PExp baseExp, String navExp) {
00065 StringTokenizer tokenizer = new StringTokenizer(navExp, " ");
00066
00067 do {
00068 String token = tokenizer.nextToken();
00069 if (".".equals(token)) {
00070 baseExp = new ANavigationExp(baseExp,
00071 new AStrongObjectNavigation(new TDot(), new TId(tokenizer.nextToken())));
00072 }
00073
00074
00075
00076 } while (tokenizer.hasMoreTokens());
00077 return baseExp;
00078 }
00079
00080
00081
00082
00083 public void caseAAndAndExp(AAndAndExp node) {
00084 node.getAndExp().apply(this);
00085 PExp left = exp;
00086 node.getInclusiveOrExp().apply(this);
00087 exp = new ABinaryExp(left, new AAndBinOp(node.getAnd()), exp);
00088 }
00089
00090
00091
00092
00093 public void caseAAndBitAndExp(AAndBitAndExp node) {
00094 node.getBitAndExp().apply(this);
00095 PExp left = exp;
00096 node.getEqExp().apply(this);
00097 exp = new ABinaryExp(left, new ABitAndBinOp(node.getBitAnd()), exp);
00098 }
00099
00100
00101
00102
00103 public void caseABitComplementUnaryNotPlusMinusExp(ABitComplementUnaryNotPlusMinusExp node) {
00104 node.getUnaryExp().apply(this);
00105 exp = new ABitComplementExp(node.getBitComplement(), exp);
00106 }
00107
00108
00109
00110
00111 public void caseAComplementUnaryNotPlusMinusExp(AComplementUnaryNotPlusMinusExp node) {
00112 node.getUnaryExp().apply(this);
00113 exp = new AComplementExp(node.getNot(), exp);
00114 }
00115
00116
00117
00118
00119 public void caseAConditionalExp(AConditionalExp node) {
00120 node.getConditionalExp().apply(this);
00121 node.replaceBy(exp);
00122 }
00123
00124
00125
00126
00127 public void caseAEqEqExp(AEqEqExp node) {
00128 node.getEqExp().apply(this);
00129 PExp left = exp;
00130 node.getRelExp().apply(this);
00131 exp = new ABinaryExp(left, new AEqualBinOp(node.getEqual()), exp);
00132 }
00133
00134
00135
00136
00137 public void caseAGreaterEqualRelExp(AGreaterEqualRelExp node) {
00138 node.getRelExp().apply(this);
00139 PExp left = exp;
00140 node.getShiftExp().apply(this);
00141 exp = new ABinaryExp(left, new AGreaterEqualBinOp(node.getGreaterEqual()), exp);
00142 }
00143
00144
00145
00146
00147 public void caseAGreaterRelExp(AGreaterRelExp node) {
00148 node.getRelExp().apply(this);
00149 PExp left = exp;
00150 node.getShiftExp().apply(this);
00151 exp = new ABinaryExp(left, new AGreaterBinOp(node.getGreater()), exp);
00152 }
00153
00154
00155
00156
00157 public void caseAInstanceofRelExp(AInstanceofRelExp node) {
00158 node.getRelExp().apply(this);
00159 if (node.getType().toString().indexOf("..") >= 0) {
00160 exceptions.add(new WeedException("Invalid type name " + node.getType()));
00161 } else {
00162 exp = new AInstanceofExp(exp, node.getInstanceof(), node.getType());
00163 }
00164 }
00165
00166
00167
00168 public void caseALeftShiftExp(ALeftShiftExp node) {
00169 node.getShiftExp().apply(this);
00170 PExp left = exp;
00171 node.getAddExp().apply(this);
00172 exp = new ABinaryExp(left, new AShiftLeftBinOp(node.getShiftLeft()), exp);
00173 }
00174
00175
00176
00177
00178 public void caseALessEqualRelExp(ALessEqualRelExp node) {
00179 node.getRelExp().apply(this);
00180 PExp left = exp;
00181 node.getShiftExp().apply(this);
00182 exp = new ABinaryExp(left, new ALessEqualBinOp(node.getLessEqual()), exp);
00183 }
00184
00185
00186
00187
00188 public void caseALessRelExp(ALessRelExp node) {
00189 node.getRelExp().apply(this);
00190 PExp left = exp;
00191 node.getShiftExp().apply(this);
00192 exp = new ABinaryExp(left, new ALessBinOp(node.getLess()), exp);
00193 }
00194
00195
00196
00197
00198 public void caseALiteralPrimaryExp(ALiteralPrimaryExp node) {
00199 exp = new ALiteralExp(node.getLiteral());
00200 }
00201
00202
00203
00204
00205 public void caseAMinusAddExp(AMinusAddExp node) {
00206 node.getAddExp().apply(this);
00207 PExp left = exp;
00208 node.getMultExp().apply(this);
00209 exp = new ABinaryExp(left, new AMinusBinOp(node.getMinus()), exp);
00210 }
00211
00212
00213
00214
00215 public void caseAMinusUnaryExp(AMinusUnaryExp node) {
00216 node.getUnaryExp().apply(this);
00217 exp = new ANegativeExp(node.getMinus(), exp);
00218 }
00219
00220
00221
00222
00223 public void caseANamePostfixExp(ANamePostfixExp node) {
00224 String nameExp = node.getName().toString().trim();
00225 int i = nameExp.indexOf("..");
00226 if (i >= 0) {
00227 String navigationExp = nameExp.substring(i);
00228 nameExp = nameExp.substring(0, i);
00229 exp = buildNavigation(buildName(nameExp), navigationExp);
00230 } else {
00231 exp = new ANameExp(node.getName());
00232 }
00233 }
00234
00235
00236
00237
00238 public void caseANeqEqExp(ANeqEqExp node) {
00239 node.getEqExp().apply(this);
00240 PExp left = exp;
00241 node.getRelExp().apply(this);
00242 exp = new ABinaryExp(left, new ANotEqualBinOp(node.getNotEqual()), exp);
00243 }
00244
00245
00246
00247
00248 public void caseAOrInclusiveOrExp(AOrInclusiveOrExp node) {
00249 node.getInclusiveOrExp().apply(this);
00250 PExp left = exp;
00251 node.getExclusiveOrExp().apply(this);
00252 exp = new ABinaryExp(left, new ABitOrBinOp(node.getBitOr()), exp);
00253 }
00254
00255
00256
00257
00258 public void caseAOrOrExp(AOrOrExp node) {
00259 node.getOrExp().apply(this);
00260 PExp left = exp;
00261 node.getAndExp().apply(this);
00262 exp = new ABinaryExp(left, new AOrBinOp(node.getOr()), exp);
00263 }
00264
00265
00266
00267
00268 public void caseAParenPrimaryExp(AParenPrimaryExp node) {
00269 node.getExp().apply(this);
00270 exp = new AParenExp(node.getLParen(), exp, node.getRParen());
00271 }
00272
00273
00274
00275
00276 public void caseAPlusAddExp(APlusAddExp node) {
00277 node.getAddExp().apply(this);
00278 PExp left = exp;
00279 node.getMultExp().apply(this);
00280 exp = new ABinaryExp(left, new APlusBinOp(node.getPlus()), exp);
00281 }
00282
00283
00284
00285
00286 public void caseAQuestionConditionalExp(AQuestionConditionalExp node) {
00287 node.getOrExp().apply(this);
00288 PExp condition = exp;
00289 node.getTrueExp().apply(this);
00290 PExp trueExp = exp;
00291 node.getFalseExp().apply(this);
00292 exp = new AQuestionExp(condition, node.getQuestion(), trueExp, node.getColon(), exp);
00293 }
00294
00295
00296
00297
00298 public void caseAReturnValuePrimaryExp(AReturnValuePrimaryExp node) {
00299 exp = new AReturnValueExp(node.getRetVal());
00300 }
00301
00302
00303
00304 public void caseASignedRightShiftExp(ASignedRightShiftExp node) {
00305 node.getShiftExp().apply(this);
00306 PExp left = exp;
00307 node.getAddExp().apply(this);
00308 exp = new ABinaryExp(left, new ASignedShiftRightBinOp(node.getSignedShiftRight()), exp);
00309 }
00310
00311
00312
00313
00314 public void caseAStrongArrayNavigation(AStrongArrayNavigation node) {
00315 PExp nav = exp;
00316 node.getExp().apply(this);
00317 node.setExp(exp);
00318 exp = new ANavigationExp(exp, node);
00319 }
00320
00321
00322
00323
00324 public void caseAStrongDivMultExp(AStrongDivMultExp node) {
00325 node.getMultExp().apply(this);
00326 PExp left = exp;
00327 node.getUnaryExp().apply(this);
00328 exp = new ABinaryExp(left, new AStrongDivBinOp(node.getStrongDiv()), exp);
00329 }
00330
00331
00332
00333
00334 public void caseAStrongExpArrayAccess(AStrongExpArrayAccess node) {
00335 node.getPrimaryExp().apply(this);
00336 PExp baseExp = exp;
00337 node.getExp().apply(this);
00338 exp = new ANavigationExp(baseExp, new AStrongArrayNavigation(node.getLBracket(),
00339 exp, node.getRBracket()));
00340 }
00341
00342
00343
00344
00345 public void caseAStrongExpCastExp(AStrongExpCastExp node) {
00346 node.getExp().apply(this);
00347 ANameExp name = (ANameExp) exp;
00348 if (name.toString().indexOf("..") >= 0) {
00349 exceptions.add(new WeedException("Invalid type name " + name));
00350 } else {
00351 node.getUnaryNotPlusMinusExp().apply(this);
00352 exp = new AStrongCastExp(node.getLParen(), name.getName(), new LinkedList(), node.getRParen(), exp);
00353 }
00354 }
00355
00356
00357
00358
00359 public void caseAStrongModMultExp(AStrongModMultExp node) {
00360 node.getMultExp().apply(this);
00361 PExp left = exp;
00362 node.getUnaryExp().apply(this);
00363 exp = new ABinaryExp(left, new AStrongModBinOp(node.getStrongMod()), exp);
00364 }
00365
00366
00367
00368
00369 public void caseAStrongNameArrayAccess(AStrongNameArrayAccess node) {
00370 String nameExp = node.getName().toString().trim();
00371 int i = nameExp.indexOf("..");
00372 if (i >= 0) {
00373 String navigationExp = nameExp.substring(i);
00374 nameExp = nameExp.substring(0, i);
00375 exp = buildNavigation(buildName(nameExp), navigationExp);
00376 } else {
00377 exp = new ANameExp(node.getName());
00378 }
00379 PExp baseExp = exp;
00380 node.getExp().apply(this);
00381 exp = new ANavigationExp(baseExp, new AStrongArrayNavigation(node.getLBracket(),
00382 exp, node.getRBracket()));
00383 }
00384
00385
00386
00387
00388 public void caseAStrongNameCastExp(AStrongNameCastExp node) {
00389 if (node.getName().toString().indexOf("..") >= 0) {
00390 exceptions.add(new WeedException("Invalid type name in cast expression"));
00391 } else {
00392 node.getUnaryExp().apply(this);
00393 exp = new AStrongCastExp(node.getLParen(), node.getName(), node.getDim(),
00394 node.getRParen(), exp);
00395 }
00396 }
00397
00398
00399
00400
00401 public void caseAStrongObjectFieldAccess(AStrongObjectFieldAccess node) {
00402 node.getPrimaryExp().apply(this);
00403 exp = new ANavigationExp(exp, new AStrongObjectNavigation(node.getDot(), node.getId()));
00404 }
00405
00406
00407
00408
00409 public void caseAStrongObjectNavigation(AStrongObjectNavigation node) {
00410 exp = new ANavigationExp(exp, node);
00411 }
00412
00413
00414
00415
00416 public void caseAStrongPrimitiveCastExp(AStrongPrimitiveCastExp node) {
00417 node.getUnaryExp().apply(this);
00418 PName name = new ASimpleName(new TId(node.getPrimitiveType().toString().trim()));
00419 exp = new AStrongCastExp(node.getLParen(), name, node.getDim(), node.getRParen(), exp);
00420 }
00421
00422
00423
00424
00425 public void caseAThisPrimaryExp(AThisPrimaryExp node) {
00426 exp = new AThisExp(node.getThis());
00427 }
00428
00429
00430
00431
00432 public void caseATimesMultExp(ATimesMultExp node) {
00433 node.getMultExp().apply(this);
00434 PExp left = exp;
00435 node.getUnaryExp().apply(this);
00436 exp = new ABinaryExp(left, new ATimesBinOp(node.getStar()), exp);
00437 }
00438
00439
00440
00441 public void caseAUnsignedRightShiftExp(AUnsignedRightShiftExp node) {
00442 node.getShiftExp().apply(this);
00443 PExp left = exp;
00444 node.getAddExp().apply(this);
00445 exp = new ABinaryExp(left, new AUnsignedShiftRightBinOp(node.getUnsignedShiftRight()), exp);
00446 }
00447
00448
00449
00450
00451 public void caseAXorExclusiveOrExp(AXorExclusiveOrExp node) {
00452 node.getExclusiveOrExp().apply(this);
00453 PExp left = exp;
00454 node.getBitAndExp().apply(this);
00455 exp = new ABinaryExp(left, new ABitXorBinOp(node.getBitXor()), exp);
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552 public static Vector getExceptions() {
00553 return simplifier.exceptions;
00554 }
00555
00556
00557
00558 public static void reset() {
00559 simplifier.exceptions = new Vector();
00560 }
00561
00562
00563
00564
00565
00566 public static Node simplify(Node node) {
00567 node.apply(simplifier);
00568 return node;
00569 }
00570 }