diff options
author | 2015-11-09 16:47:18 +0000 | |
---|---|---|
committer | 2015-11-10 10:23:06 +0000 | |
commit | 363f9be22f25fb89fd245648cf8c0cfd7e8eb08e (patch) | |
tree | 365bbb678f1b4d009ba89fa0c084e63e15731623 /src/main/java/com | |
parent | 1bf1c970cad8bd2f1b7b42b1038b87e10d6a5428 (diff) |
Compile dictionary literals to byte code.
--
MOS_MIGRATED_REVID=107388016
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/DictionaryLiteral.java | 26 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java | 27 |
2 files changed, 52 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/DictionaryLiteral.java b/src/main/java/com/google/devtools/build/lib/syntax/DictionaryLiteral.java index 46026e0436..8a223ed582 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/DictionaryLiteral.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/DictionaryLiteral.java @@ -13,9 +13,19 @@ // limitations under the License. package com.google.devtools.build.lib.syntax; +import static com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils.append; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.syntax.compiler.ByteCodeMethodCalls; +import com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils; +import com.google.devtools.build.lib.syntax.compiler.DebugInfo; +import com.google.devtools.build.lib.syntax.compiler.VariableScope; + +import net.bytebuddy.implementation.bytecode.ByteCodeAppender; +import net.bytebuddy.implementation.bytecode.Duplication; +import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -115,4 +125,20 @@ public class DictionaryLiteral extends Expression { entry.value.validate(env); } } + + @Override + ByteCodeAppender compile(VariableScope scope, DebugInfo debugInfo) throws EvalException { + List<ByteCodeAppender> code = new ArrayList<>(); + append(code, ByteCodeMethodCalls.BCImmutableMap.builder); + + for (DictionaryEntryLiteral entry : entries) { + code.add(entry.key.compile(scope, debugInfo)); + append(code, Duplication.SINGLE, EvalUtils.checkValidDictKey); + code.add(entry.value.compile(scope, debugInfo)); + // add it to the builder which is already on the stack and returns itself + append(code, ByteCodeMethodCalls.BCImmutableMap.Builder.put); + } + append(code, ByteCodeMethodCalls.BCImmutableMap.Builder.build); + return ByteCodeUtils.compoundAppender(code); + } } diff --git a/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java b/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java index 8fe0346d33..5027610e29 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/compiler/ByteCodeMethodCalls.java @@ -14,9 +14,12 @@ package com.google.devtools.build.lib.syntax.compiler; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import net.bytebuddy.implementation.bytecode.StackManipulation; +import java.util.Map; + /** * Keeps often used {@link StackManipulation}s which call often needed methods from the standard * library and others. @@ -36,6 +39,28 @@ public class ByteCodeMethodCalls { } /** + * Byte code invocations for {@link ImmutableMap}. + */ + public static class BCImmutableMap { + public static final StackManipulation builder = + ByteCodeUtils.invoke(ImmutableMap.class, "builder"); + + public static final StackManipulation copyOf = + ByteCodeUtils.invoke(ImmutableMap.class, "copyOf", Map.class); + + /** + * Byte code invocations for {@link Builder}. + */ + public static class Builder { + public static final StackManipulation put = + ByteCodeUtils.invoke(ImmutableMap.Builder.class, "put", Object.class, Object.class); + + public static final StackManipulation build = + ByteCodeUtils.invoke(ImmutableMap.Builder.class, "build"); + } + } + + /** * Byte code invocations for {@link ImmutableList}. */ public static class BCImmutableList { @@ -43,7 +68,7 @@ public class ByteCodeMethodCalls { ByteCodeUtils.invoke(ImmutableList.class, "builder"); /** - * Byte code invocations for {@link ImmutableList.Builder}. + * Byte code invocations for {@link com.google.common.collect.ImmutableList.Builder}. */ public static class Builder { public static final StackManipulation build = |