| 1 |
/* |
| 2 |
* Copyright (C) 2016 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package android.databinding.tool.expr |
| 18 |
|
| 19 |
import android.databinding.tool.reflection.Callable |
| 20 |
import android.databinding.tool.solver.ExecutionPath |
| 21 |
import android.databinding.tool.writer.KCode |
| 22 |
import android.databinding.tool.writer.fieldName |
| 23 |
import android.databinding.tool.writer.isForcedToLocalize |
| 24 |
import android.databinding.tool.writer.isVariable |
| 25 |
import android.databinding.tool.writer.kcode |
| 26 |
import android.databinding.tool.writer.scopedName |
| 27 |
|
| 28 |
fun Expr.shouldLocalizeInCallbacks() = canBeEvaluatedToAVariable() && !resolvedType.isVoid && (isDynamic || isForcedToLocalize()) |
| 29 |
|
| 30 |
fun CallbackExprModel.localizeGlobalVariables(vararg ignore: Expr): KCode = kcode("// localize variables for thread safety") { |
| 31 |
// puts all variables in this model to local values. |
| 32 |
mExprMap.values.filter { it.shouldLocalizeInCallbacks() && !ignore.contains(it) }.forEach { |
| 33 |
nl("// ${it.toString()}") |
| 34 |
nl("${it.resolvedType.toJavaCode()} ${it.scopedName()} = ${if (it.isVariable()) it.fieldName else it.defaultValue};") |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
fun ExecutionPath.toCode(): KCode = kcode("") { |
| 39 |
val myExpr = expr |
| 40 |
if (myExpr != null && !isAlreadyEvaluated) { |
| 41 |
// variables are read up top |
| 42 |
val localize = myExpr.shouldLocalizeInCallbacks() && !myExpr.isVariable() |
| 43 |
// if this is not a method call (or method call via field access, don't do anything |
| 44 |
val eligible = localize || (myExpr is MethodCallExpr || |
| 45 |
(myExpr is FieldAccessExpr && myExpr.getter.type == Callable.Type.METHOD) || |
| 46 |
(myExpr is FieldAssignmentExpr)) |
| 47 |
if (eligible) { |
| 48 |
val assign = if (localize) { |
| 49 |
"${myExpr.scopedName()} = " |
| 50 |
} else { |
| 51 |
"" |
| 52 |
} |
| 53 |
if (myExpr is TernaryExpr) { |
| 54 |
// if i know the value, short circuit it |
| 55 |
if (knownValues.containsKey(myExpr.pred)) { |
| 56 |
val chosen = if (knownValues[myExpr.pred]!!) myExpr.ifTrue else myExpr.ifFalse |
| 57 |
// fast read me |
| 58 |
nl("$assign${chosen.toCode().generate()};") |
| 59 |
} else { |
| 60 |
// read me |
| 61 |
nl("$assign${myExpr.toFullCode().generate()};") |
| 62 |
} |
| 63 |
} else { |
| 64 |
// read me |
| 65 |
nl("$assign${myExpr.toFullCode().generate()};") |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
children.forEach { |
| 70 |
nl(it.toCode()) |
| 71 |
} |
| 72 |
// if i have branches, execute them |
| 73 |
val myTrue = trueBranch |
| 74 |
val myFalse = falseBranch |
| 75 |
if (myTrue != null) { |
| 76 |
val condition = with(myTrue.conditional) { |
| 77 |
if (shouldLocalizeInCallbacks()) { |
| 78 |
scopedName() |
| 79 |
} else { |
| 80 |
toFullCode().generate() |
| 81 |
} |
| 82 |
} |
| 83 |
block("if ($condition)") { |
| 84 |
nl(myTrue.path.toCode()) |
| 85 |
} |
| 86 |
if (myFalse != null) { |
| 87 |
block("else") { |
| 88 |
nl(myFalse.path.toCode()) |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |