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;
}
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);
}
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)));
}
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;
}
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);
}
}
Aggregations