Search in sources :

Example 11 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class SkylarkAspectFactory method createAspect.

private ConfiguredAspect createAspect(Object aspectSkylarkObject, AspectDescriptor aspectDescriptor, RuleContext ruleContext) throws EvalException {
    ConfiguredAspect.Builder builder = new ConfiguredAspect.Builder(aspectDescriptor, ruleContext);
    if (aspectSkylarkObject instanceof Iterable) {
        addDeclaredProviders(builder, (Iterable) aspectSkylarkObject);
    } else {
        SkylarkClassObject struct = (SkylarkClassObject) aspectSkylarkObject;
        Location loc = struct.getCreationLoc();
        for (String key : struct.getKeys()) {
            if (key.equals("output_groups")) {
                addOutputGroups(struct.getValue(key), loc, builder);
            } else if (key.equals("providers")) {
                Object value = struct.getValue(key);
                Iterable providers = SkylarkType.cast(value, Iterable.class, loc, "The value for \"providers\" should be a list of declared providers, " + "got %s instead", EvalUtils.getDataTypeName(value, false));
                addDeclaredProviders(builder, providers);
            } else {
                builder.addSkylarkTransitiveInfo(key, struct.getValue(key), loc);
            }
        }
    }
    ConfiguredAspect configuredAspect = builder.build();
    SkylarkProviderValidationUtil.checkOrphanArtifacts(ruleContext);
    return configuredAspect;
}
Also used : ConfiguredAspect(com.google.devtools.build.lib.analysis.ConfiguredAspect) SkylarkRuleConfiguredTargetBuilder(com.google.devtools.build.lib.rules.SkylarkRuleConfiguredTargetBuilder) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Location(com.google.devtools.build.lib.events.Location)

Example 12 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class AugmentedAssignmentStatement method doExec.

@Override
void doExec(Environment env) throws EvalException, InterruptedException {
    Location loc = getLocation();
    lvalue.assign(env, loc, expression, operator);
}
Also used : Location(com.google.devtools.build.lib.events.Location)

Example 13 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class IndexExpression method eval.

/**
   * This method can be used instead of eval(Environment) if we want to avoid `obj` being evaluated
   * several times.
   */
Object eval(Environment env, Object objValue) throws EvalException, InterruptedException {
    Object keyValue = key.eval(env);
    Location loc = getLocation();
    if (objValue instanceof SkylarkIndexable) {
        Object result = ((SkylarkIndexable) objValue).getIndex(keyValue, loc);
        return SkylarkType.convertToSkylark(result, env);
    } else if (objValue instanceof String) {
        String string = (String) objValue;
        int index = EvalUtils.getSequenceIndex(keyValue, string.length(), loc);
        return string.substring(index, index + 1);
    }
    throw new EvalException(loc, Printer.format("type '%s' has no operator [](%s)", EvalUtils.getDataTypeName(objValue), EvalUtils.getDataTypeName(keyValue)));
}
Also used : Location(com.google.devtools.build.lib.events.Location)

Example 14 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class DictionaryLiteral method doEval.

@Override
Object doEval(Environment env) throws EvalException, InterruptedException {
    SkylarkDict<Object, Object> dict = SkylarkDict.<Object, Object>of(env);
    Location loc = getLocation();
    for (DictionaryEntryLiteral entry : entries) {
        if (entry == null) {
            throw new EvalException(loc, "null expression in " + this);
        }
        Object key = entry.key.eval(env);
        Object val = entry.value.eval(env);
        dict.put(key, val, loc, env);
    }
    return dict;
}
Also used : Location(com.google.devtools.build.lib.events.Location)

Example 15 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class FuncallExpression method invokeObjectMethod.

/**
   * Call a method depending on the type of an object it is called on.
   *
   * <p>Public for reflection by the compiler and access from generated byte code.
   *
   * @param positionals The first object is expected to be the object the method is called on.
   * @param call the original expression that caused this call, needed for rules especially
   */
public Object invokeObjectMethod(String method, ImmutableList<Object> positionals, ImmutableMap<String, Object> keyWordArgs, FuncallExpression call, Environment env) throws EvalException, InterruptedException {
    Location location = call.getLocation();
    Object value = positionals.get(0);
    ImmutableList<Object> positionalArgs = positionals.subList(1, positionals.size());
    BaseFunction function = Runtime.getFunction(EvalUtils.getSkylarkType(value.getClass()), method);
    if (function != null) {
        if (!isNamespace(value.getClass())) {
            // Use self as an implicit parameter in front.
            positionalArgs = positionals;
        }
        return function.call(positionalArgs, ImmutableMap.<String, Object>copyOf(keyWordArgs), call, env);
    } else if (value instanceof ClassObject) {
        Object fieldValue = ((ClassObject) value).getValue(method);
        if (fieldValue == null) {
            throw new EvalException(location, String.format("struct has no method '%s'", method));
        }
        if (!(fieldValue instanceof BaseFunction)) {
            throw new EvalException(location, String.format("struct field '%s' is not a function", method));
        }
        function = (BaseFunction) fieldValue;
        return function.call(positionalArgs, ImmutableMap.<String, Object>copyOf(keyWordArgs), call, env);
    } else {
        // When calling a Java method, the name is not in the Environment,
        // so evaluating 'func' would fail.
        Class<?> objClass;
        Object obj;
        if (value instanceof Class<?>) {
            // Static call
            obj = null;
            objClass = (Class<?>) value;
        } else {
            obj = value;
            objClass = value.getClass();
        }
        Pair<MethodDescriptor, List<Object>> javaMethod = call.findJavaMethod(objClass, method, positionalArgs, keyWordArgs);
        if (javaMethod.first.getAnnotation().structField()) {
            // Not a method but a callable attribute
            try {
                return callFunction(javaMethod.first.getMethod().invoke(obj), env);
            } catch (IllegalAccessException e) {
                throw new EvalException(getLocation(), "method invocation failed: " + e);
            } catch (InvocationTargetException e) {
                if (e.getCause() instanceof FuncallException) {
                    throw new EvalException(getLocation(), e.getCause().getMessage());
                } else if (e.getCause() != null) {
                    throw new EvalExceptionWithJavaCause(getLocation(), e.getCause());
                } else {
                    // This is unlikely to happen
                    throw new EvalException(getLocation(), "method invocation failed: " + e);
                }
            }
        }
        return callMethod(javaMethod.first, method, obj, javaMethod.second.toArray(), location, env);
    }
}
Also used : EvalExceptionWithJavaCause(com.google.devtools.build.lib.syntax.EvalException.EvalExceptionWithJavaCause) InvocationTargetException(java.lang.reflect.InvocationTargetException) Location(com.google.devtools.build.lib.events.Location) Pair(com.google.devtools.build.lib.util.Pair)

Aggregations

Location (com.google.devtools.build.lib.events.Location)19 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)4 Test (org.junit.Test)4 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)3 EvalException (com.google.devtools.build.lib.syntax.EvalException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 List (java.util.List)2 ImmutableList (com.google.common.collect.ImmutableList)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 TransitiveInfoProvider (com.google.devtools.build.lib.analysis.TransitiveInfoProvider)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 Event (com.google.devtools.build.lib.events.Event)1 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)1 Target (com.google.devtools.build.lib.packages.Target)1 SkylarkRuleConfiguredTargetBuilder (com.google.devtools.build.lib.rules.SkylarkRuleConfiguredTargetBuilder)1 InstrumentationSpec (com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec)1 InstrumentedFilesProvider (com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider)1