All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.painless.symbol.ScriptScope Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.painless.symbol;

import org.elasticsearch.painless.CompilerSettings;
import org.elasticsearch.painless.ScriptClassInfo;
import org.elasticsearch.painless.lookup.PainlessLookup;
import org.elasticsearch.painless.node.ANode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * Stores information for use across the entirety of compilation.
 */
public class ScriptScope extends Decorator {

    protected final PainlessLookup painlessLookup;
    protected final CompilerSettings compilerSettings;
    protected final ScriptClassInfo scriptClassInfo;
    protected final String scriptName;
    protected final String scriptSource;

    protected final FunctionTable functionTable = new FunctionTable();
    protected int syntheticCounter = 0;

    protected boolean deterministic = true;
    protected List docFields = new ArrayList<>();
    protected Set usedVariables = Collections.emptySet();
    protected Map staticConstants = new HashMap<>();

    public ScriptScope(PainlessLookup painlessLookup, CompilerSettings compilerSettings,
                      ScriptClassInfo scriptClassInfo, String scriptName, String scriptSource, int nodeCount) {

        super(nodeCount);

        this.painlessLookup = Objects.requireNonNull(painlessLookup);
        this.compilerSettings = Objects.requireNonNull(compilerSettings);
        this.scriptClassInfo = Objects.requireNonNull(scriptClassInfo);
        this.scriptName = Objects.requireNonNull(scriptName);
        this.scriptSource = Objects.requireNonNull(scriptName);

        staticConstants.put("$NAME", scriptName);
        staticConstants.put("$SOURCE", scriptSource);
        staticConstants.put("$DEFINITION", painlessLookup);
        staticConstants.put("$FUNCTIONS", functionTable);
        staticConstants.put("$COMPILERSETTINGS", compilerSettings.asMap());
    }

    public PainlessLookup getPainlessLookup() {
        return painlessLookup;
    }

    public CompilerSettings getCompilerSettings() {
        return compilerSettings;
    }

    public ScriptClassInfo getScriptClassInfo() {
        return scriptClassInfo;
    }

    public String getScriptName() {
        return scriptName;
    }

    public String getScriptSource() {
        return scriptSource;
    }

    public FunctionTable getFunctionTable() {
        return functionTable;
    }

    /**
     * Returns a unique identifier for generating the name of a synthetic value.
     */
    public String getNextSyntheticName(String prefix) {
        return prefix + "$synthetic$" + syntheticCounter++;
    }

    public void markNonDeterministic(boolean nondeterministic) {
        this.deterministic &= !nondeterministic;
    }

    public boolean isDeterministic() {
        return deterministic;
    }

    /**
     * Document fields read or written using constant strings
     */
    public List docFields() {
        return Collections.unmodifiableList(docFields);
    }

    public void addDocField(String field) {
        docFields.add(field);
    }

    public void setUsedVariables(Set usedVariables) {
        this.usedVariables = usedVariables;
    }

    public Set getUsedVariables() {
        return Collections.unmodifiableSet(usedVariables);
    }

    public void addStaticConstant(String name, Object constant) {
        staticConstants.put(name, constant);
    }

    public Map getStaticConstants() {
        return Collections.unmodifiableMap(staticConstants);
    }

    public  T putDecoration(ANode node, T decoration) {
        return put(node.getIdentifier(), decoration);
    }

    public  T removeDecoration(ANode node, Class type) {
        return remove(node.getIdentifier(), type);
    }

    public  T getDecoration(ANode node, Class type) {
        return get(node.getIdentifier(), type);
    }

    public boolean hasDecoration(ANode node, Class type) {
        return has(node.getIdentifier(), type);
    }

    public  boolean copyDecoration(ANode originalNode, ANode targetNode, Class type) {
        return copy(originalNode.getIdentifier(), targetNode.getIdentifier(), type);
    }

    public boolean setCondition(ANode node, Class type) {
        return set(node.getIdentifier(), type);
    }

    public boolean deleteCondition(ANode node, Class type) {
        return delete(node.getIdentifier(), type);
    }

    public boolean getCondition(ANode node, Class type) {
        return exists(node.getIdentifier(), type);
    }

    public boolean replicateCondition(ANode originalNode, ANode targetNode, Class type) {
        return replicate(originalNode.getIdentifier(), targetNode.getIdentifier(), type);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy