/* * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package compiler.lib.template_framework; import java.util.HashMap; import java.util.Map; /** * The {@link TemplateFrame} keeps track of the nested hashtag replacements available * inside the {@link Template}, as well as the unique id of the {@link Template} use, * and how much fuel is available for recursive {@link Template} calls. The name of * the {@link TemplateFrame} indicates that it corresponds to the structure of the * {@link Template}, whereas the {@link CodeFrame} corresponds to the structure of * the generated code. * *
* The unique id is used to deconflict names using {@link Template#$}. * *
* A {@link Template} can have multiple {@link TemplateFrame}s, if there are nested * scopes. The outermost {@link TemplateFrame} determines the id of the {@link Template} * use and performs the subtraction of fuel from the outer {@link Template}. Inner * {@link TemplateFrame}s ensure the correct availability of hashtag replacement and * {@link Template#setFuelCost} definitions, so that they are local to their scope and * nested scopes, and only escape if the scope is transparent. * *
* The hashtag replacements are a set of key-value pairs from the template arguments * and queries such as {@link Template#let} definitions. Each {@link TemplateFrame} * has such a set of hashtag replacements, and implicitly provides access to the * hashtag replacements of the outer {@link TemplateFrame}s, up to the outermost * of the current {@link Template}. If a hashtag replacement is added in a scope, * we have to traverse to outer scopes until we find one that is not transparent * for hashtags (at most it is the frame of the Template), and insert it there. * The hashtag replacent is local to that frame, and accessible for any frames nested * inside it, but not inside other Templates. The hashtag replacement disappears once * the corresponding scope is exited, i.e. the frame removed. * *
* The {@link #parent} relationship provides a trace for the use chain of templates and * their inner scopes. The {@link #fuel} is reduced over this chain to give a heuristic * on how deeply nested the code is at a given point, correlating to the runtime that * would be spent if the code was executed. The idea is that once the fuel is depleated, * we do not want to nest more deeply, so that there is a reasonable chance that the * execution of the generated code can terminate. * *
* The {@link TemplateFrame} thus implements the hashtag and {@link Template#setFuelCost} * non-transparency aspect of {@link ScopeToken}. * *
* See also {@link CodeFrame} for more explanations about the frames. Note, that while
* {@link TemplateFrame} always nests inward, even with {@link Hook#insert}, the
* {@link CodeFrame} can also jump to the {@link Hook#anchor} {@link CodeFrame} when
* using {@link Hook#insert}.
*/
class TemplateFrame {
final TemplateFrame parent;
private final boolean isInnerScope;
private final int id;
private final Map