|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of PHP-CFG, a Control flow graph implementation for PHP |
| 5 | + * |
| 6 | + * @copyright 2015 Anthony Ferrara. All rights reserved |
| 7 | + * @license MIT See LICENSE at the root of the project for more info |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHPCfg\ParserHandler\Batch; |
| 11 | + |
| 12 | +use PHPCfg\Assertion; |
| 13 | +use PHPCfg\Op; |
| 14 | +use PHPCfg\Operand; |
| 15 | +use PHPCfg\ParserHandler; |
| 16 | +use PhpParser\Node; |
| 17 | +use PhpParser\Node\Expr; |
| 18 | +use PhpParser\Node\Expr\BinaryOp as AstBinaryOp; |
| 19 | + |
| 20 | +class BinaryOp extends ParserHandler |
| 21 | +{ |
| 22 | + private const MAP = [ |
| 23 | + 'Expr_BinaryOp_LogicalAnd' => '', |
| 24 | + 'Expr_BinaryOp_LogicalOr' => '', |
| 25 | + 'Expr_BinaryOp_BooleanAnd' => '', |
| 26 | + 'Expr_BinaryOp_BooleanOr' => '', |
| 27 | + 'Expr_BinaryOp_BitwiseAnd' => Op\Expr\BinaryOp\BitwiseAnd::class, |
| 28 | + 'Expr_BinaryOp_BitwiseOr' => Op\Expr\BinaryOp\BitwiseOr::class, |
| 29 | + 'Expr_BinaryOp_BitwiseXor' => Op\Expr\BinaryOp\BitwiseXor::class, |
| 30 | + 'Expr_BinaryOp_Coalesce' => Op\Expr\BinaryOp\Coalesce::class, |
| 31 | + 'Expr_BinaryOp_Concat' => Op\Expr\BinaryOp\Concat::class, |
| 32 | + 'Expr_BinaryOp_Div' => Op\Expr\BinaryOp\Div::class, |
| 33 | + 'Expr_BinaryOp_Equal' => Op\Expr\BinaryOp\Equal::class, |
| 34 | + 'Expr_BinaryOp_Greater' => Op\Expr\BinaryOp\Greater::class, |
| 35 | + 'Expr_BinaryOp_GreaterOrEqual' => Op\Expr\BinaryOp\GreaterOrEqual::class, |
| 36 | + 'Expr_BinaryOp_Identical' => Op\Expr\BinaryOp\Identical::class, |
| 37 | + 'Expr_BinaryOp_LogicalXor' => Op\Expr\BinaryOp\LogicalXor::class, |
| 38 | + 'Expr_BinaryOp_Minus' => Op\Expr\BinaryOp\Minus::class, |
| 39 | + 'Expr_BinaryOp_Mod' => Op\Expr\BinaryOp\Mod::class, |
| 40 | + 'Expr_BinaryOp_Mul' => Op\Expr\BinaryOp\Mul::class, |
| 41 | + 'Expr_BinaryOp_NotEqual' => Op\Expr\BinaryOp\NotEqual::class, |
| 42 | + 'Expr_BinaryOp_NotIdentical' => Op\Expr\BinaryOp\NotIdentical::class, |
| 43 | + 'Expr_BinaryOp_Plus' => Op\Expr\BinaryOp\Plus::class, |
| 44 | + 'Expr_BinaryOp_Pow' => Op\Expr\BinaryOp\Pow::class, |
| 45 | + 'Expr_BinaryOp_ShiftLeft' => Op\Expr\BinaryOp\ShiftLeft::class, |
| 46 | + 'Expr_BinaryOp_ShiftRight' => Op\Expr\BinaryOp\ShiftRight::class, |
| 47 | + 'Expr_BinaryOp_Smaller' => Op\Expr\BinaryOp\Smaller::class, |
| 48 | + 'Expr_BinaryOp_SmallerOrEqual' => Op\Expr\BinaryOp\SmallerOrEqual::class, |
| 49 | + 'Expr_BinaryOp_Spaceship' => Op\Expr\BinaryOp\Spaceship::class, |
| 50 | + ]; |
| 51 | + |
| 52 | + public function supports(Node $expr): bool |
| 53 | + { |
| 54 | + return isset(self::MAP[$expr->getType()]); |
| 55 | + } |
| 56 | + |
| 57 | + public function handleExpr(Expr $expr): Operand |
| 58 | + { |
| 59 | + $type = $expr->getType(); |
| 60 | + if (!isset(self::MAP[$type])) { |
| 61 | + throw new \RuntimeException("Unknown unary expression type $type"); |
| 62 | + } |
| 63 | + |
| 64 | + if ($expr instanceof AstBinaryOp\LogicalAnd || $expr instanceof AstBinaryOp\BooleanAnd) { |
| 65 | + return $this->parseShortCircuiting($expr, false); |
| 66 | + } |
| 67 | + if ($expr instanceof AstBinaryOp\LogicalOr || $expr instanceof AstBinaryOp\BooleanOr) { |
| 68 | + return $this->parseShortCircuiting($expr, true); |
| 69 | + } |
| 70 | + |
| 71 | + $class = self::MAP[$type]; |
| 72 | + |
| 73 | + $left = $this->parser->readVariable($this->parser->parseExprNode($expr->left)); |
| 74 | + $right = $this->parser->readVariable($this->parser->parseExprNode($expr->right)); |
| 75 | + if (empty($class)) { |
| 76 | + throw new RuntimeException('BinaryOp Not Found: ' . $expr->getType()); |
| 77 | + } |
| 78 | + return $this->addExpr(new $class($left, $right, $this->mapAttributes($expr))); |
| 79 | + } |
| 80 | + |
| 81 | + private function parseShortCircuiting(AstBinaryOp $expr, $isOr): Operand |
| 82 | + { |
| 83 | + $result = new Operand\Temporary(); |
| 84 | + $longBlock = $this->createBlockWithCatchTarget(); |
| 85 | + $endBlock = $this->createBlockWithCatchTarget(); |
| 86 | + |
| 87 | + $left = $this->parser->readVariable($this->parser->parseExprNode($expr->left)); |
| 88 | + $if = $isOr ? $endBlock : $longBlock; |
| 89 | + $else = $isOr ? $longBlock : $endBlock; |
| 90 | + |
| 91 | + $this->addOp(new Op\Stmt\JumpIf($left, $if, $else)); |
| 92 | + $longBlock->addParent($this->block()); |
| 93 | + $endBlock->addParent($this->block()); |
| 94 | + |
| 95 | + $this->block($longBlock); |
| 96 | + $right = $this->parser->readVariable($this->parser->parseExprNode($expr->right)); |
| 97 | + $boolCast = new Op\Expr\Cast\Bool_($right); |
| 98 | + $this->addOp($boolCast); |
| 99 | + $this->addOp(new Op\Stmt\Jump($endBlock)); |
| 100 | + $endBlock->addParent($this->block()); |
| 101 | + |
| 102 | + $this->block($endBlock); |
| 103 | + $phi = new Op\Phi($result, ['block' => $this->block()]); |
| 104 | + $phi->addOperand(new Operand\Literal($isOr)); |
| 105 | + $phi->addOperand($boolCast->result); |
| 106 | + $this->block()->phi[] = $phi; |
| 107 | + |
| 108 | + $mode = $isOr ? Assertion::MODE_UNION : Assertion::MODE_INTERSECTION; |
| 109 | + foreach ($left->assertions as $assert) { |
| 110 | + $result->addAssertion($assert['var'], $assert['assertion'], $mode); |
| 111 | + } |
| 112 | + foreach ($right->assertions as $assert) { |
| 113 | + $result->addAssertion($assert['var'], $assert['assertion'], $mode); |
| 114 | + } |
| 115 | + |
| 116 | + return $result; |
| 117 | + } |
| 118 | +} |
0 commit comments