B :`r@s`dZddlZddlmZddlmZejr6ddlm Z ej dej dd d Z Gd d d eZ dS) aThe optimizer tries to constant fold expressions and modify the AST in place so that it should be faster to evaluate. Because the AST does not contain all the scoping information and the compiler has to find that out, we cannot do all the optimizations we want. For example, loop unrolling doesn't work because unrolled loops would have a different scope. The solution would be a second syntax tree that stored the scoping rules. N)nodes)NodeTransformer) Environmentr)node environmentreturncCst|}ttj||S)z^The context hint can be used to perform an static optimization based on the context given.) OptimizertcastrNodevisit)rrZ optimizerrA/opt/alt/python37/lib/python3.7/site-packages/jinja2/optimizer.pyoptimizesrcs@eZdZdddddZejejejejdfdd ZZ S) r zt.Optional[Environment]N)rrcCs ||_dS)N)r)selfrrrr__init__szOptimizer.__init__)rargskwargsrcsdtj|f||}t|tjr`y(tjj||r8|dnd|j|j dStj k r^YnX|S)Nr)linenor) super generic_visit isinstancerExprZConstZfrom_untrustedZas_constrrZ Impossible)rrrr) __class__rrrs  zOptimizer.generic_visit) __name__ __module__ __qualname__rrr r Anyr __classcell__rr)rrr s r ) __doc__typingr rvisitorr TYPE_CHECKINGrrr rr rrrr s