Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
Florian Weikert | bca82ad | 2015-11-09 17:57:13 +0000 | [diff] [blame] | 16 | import static com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils.append; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | |
Florian Weikert | bca82ad | 2015-11-09 17:57:13 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.devtools.build.lib.syntax.compiler.ByteCodeMethodCalls; |
| 20 | import com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils; |
| 21 | import com.google.devtools.build.lib.syntax.compiler.DebugInfo; |
| 22 | import com.google.devtools.build.lib.syntax.compiler.DebugInfo.AstAccessors; |
| 23 | import com.google.devtools.build.lib.syntax.compiler.Variable.InternalVariable; |
| 24 | import com.google.devtools.build.lib.syntax.compiler.VariableScope; |
| 25 | |
| 26 | import net.bytebuddy.implementation.bytecode.ByteCodeAppender; |
| 27 | import net.bytebuddy.implementation.bytecode.Duplication; |
| 28 | import net.bytebuddy.implementation.bytecode.Removal; |
| 29 | |
| 30 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import java.util.LinkedHashMap; |
Florian Weikert | bca82ad | 2015-11-09 17:57:13 +0000 | [diff] [blame] | 32 | import java.util.List; |
Florian Weikert | ffd8a5a | 2015-09-18 11:51:01 +0000 | [diff] [blame] | 33 | import java.util.Map; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Syntax node for dictionary comprehension expressions. |
| 37 | */ |
Florian Weikert | ffd8a5a | 2015-09-18 11:51:01 +0000 | [diff] [blame] | 38 | public class DictComprehension extends AbstractComprehension { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | private final Expression keyExpression; |
| 40 | private final Expression valueExpression; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | |
Florian Weikert | ffd8a5a | 2015-09-18 11:51:01 +0000 | [diff] [blame] | 42 | public DictComprehension(Expression keyExpression, Expression valueExpression) { |
| 43 | super('{', '}', keyExpression, valueExpression); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | this.keyExpression = keyExpression; |
| 45 | this.valueExpression = valueExpression; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
Florian Weikert | ffd8a5a | 2015-09-18 11:51:01 +0000 | [diff] [blame] | 49 | String printExpressions() { |
| 50 | return String.format("%s: %s", keyExpression, valueExpression); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | OutputCollector createCollector() { |
| 55 | return new DictOutputCollector(); |
| 56 | } |
| 57 | |
Florian Weikert | bca82ad | 2015-11-09 17:57:13 +0000 | [diff] [blame] | 58 | @Override |
| 59 | InternalVariable compileInitialization(VariableScope scope, List<ByteCodeAppender> code) { |
| 60 | InternalVariable dict = scope.freshVariable(ImmutableMap.class); |
| 61 | append(code, ByteCodeMethodCalls.BCImmutableMap.builder); |
| 62 | code.add(dict.store()); |
| 63 | return dict; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | ByteCodeAppender compileCollector( |
| 68 | VariableScope scope, |
| 69 | InternalVariable collection, |
| 70 | DebugInfo debugInfo, |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame^] | 71 | AstAccessors debugAccessors) |
| 72 | throws EvalException { |
Florian Weikert | bca82ad | 2015-11-09 17:57:13 +0000 | [diff] [blame] | 73 | List<ByteCodeAppender> code = new ArrayList<>(); |
| 74 | append(code, collection.load()); |
| 75 | code.add(keyExpression.compile(scope, debugInfo)); |
| 76 | append(code, Duplication.SINGLE, EvalUtils.checkValidDictKey); |
| 77 | code.add(valueExpression.compile(scope, debugInfo)); |
| 78 | append(code, ByteCodeMethodCalls.BCImmutableMap.Builder.put, Removal.SINGLE); |
| 79 | return ByteCodeUtils.compoundAppender(code); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | ByteCodeAppender compileBuilding(VariableScope scope, InternalVariable collection) { |
| 84 | return new ByteCodeAppender.Simple( |
| 85 | collection.load(), ByteCodeMethodCalls.BCImmutableMap.Builder.build); |
| 86 | } |
| 87 | |
Florian Weikert | ffd8a5a | 2015-09-18 11:51:01 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Helper class that collects the intermediate results of the {@link DictComprehension} and |
| 90 | * provides access to the resulting {@link Map}. |
| 91 | */ |
| 92 | private final class DictOutputCollector implements OutputCollector { |
| 93 | private final Map<Object, Object> result; |
| 94 | |
| 95 | DictOutputCollector() { |
| 96 | // We want to keep the iteration order |
| 97 | result = new LinkedHashMap<>(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void evaluateAndCollect(Environment env) throws EvalException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | Object key = keyExpression.eval(env); |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 103 | EvalUtils.checkValidDictKey(key); |
Florian Weikert | ffd8a5a | 2015-09-18 11:51:01 +0000 | [diff] [blame] | 104 | result.put(key, valueExpression.eval(env)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 106 | |
Florian Weikert | ffd8a5a | 2015-09-18 11:51:01 +0000 | [diff] [blame] | 107 | @Override |
| 108 | public Object getResult(Environment env) throws EvalException { |
| 109 | return ImmutableMap.copyOf(result); |
| 110 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 111 | } |
| 112 | } |