values.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2017 Google Inc. All rights reserved.
  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 main
  15. import (
  16. "fmt"
  17. "strings"
  18. mkparser "android/soong/androidmk/parser"
  19. bpparser "github.com/google/blueprint/parser"
  20. )
  21. func stringToStringValue(s string) bpparser.Expression {
  22. return &bpparser.String{
  23. Value: s,
  24. }
  25. }
  26. func addValues(val1, val2 bpparser.Expression) (bpparser.Expression, error) {
  27. if val1 == nil {
  28. return val2, nil
  29. }
  30. if val1.Type() == bpparser.StringType && val2.Type() == bpparser.ListType {
  31. val1 = &bpparser.List{
  32. Values: []bpparser.Expression{val1},
  33. }
  34. } else if val2.Type() == bpparser.StringType && val1.Type() == bpparser.ListType {
  35. val2 = &bpparser.List{
  36. Values: []bpparser.Expression{val1},
  37. }
  38. } else if val1.Type() != val2.Type() {
  39. return nil, fmt.Errorf("cannot add mismatched types")
  40. }
  41. return &bpparser.Operator{
  42. Operator: '+',
  43. Args: [2]bpparser.Expression{val1, val2},
  44. }, nil
  45. }
  46. func makeToStringExpression(ms *mkparser.MakeString, scope mkparser.Scope) (bpparser.Expression, error) {
  47. var val bpparser.Expression
  48. var err error
  49. if ms.Strings[0] != "" {
  50. val = stringToStringValue(ms.Strings[0])
  51. }
  52. for i, s := range ms.Strings[1:] {
  53. if ret, ok := ms.Variables[i].EvalFunction(scope); ok {
  54. val, err = addValues(val, stringToStringValue(ret))
  55. } else {
  56. name := ms.Variables[i].Name
  57. if !name.Const() {
  58. return nil, fmt.Errorf("Unsupported non-const variable name %s", name.Dump())
  59. }
  60. tmp := &bpparser.Variable{
  61. Name: name.Value(nil),
  62. Value: &bpparser.String{},
  63. }
  64. if tmp.Name == "TOP" {
  65. if s[0] == '/' {
  66. s = s[1:]
  67. } else {
  68. s = "." + s
  69. }
  70. } else {
  71. val, err = addValues(val, tmp)
  72. if err != nil {
  73. return nil, err
  74. }
  75. }
  76. }
  77. if s != "" {
  78. tmp := stringToStringValue(s)
  79. val, err = addValues(val, tmp)
  80. if err != nil {
  81. return nil, err
  82. }
  83. }
  84. }
  85. return val, nil
  86. }
  87. func stringToListValue(s string) bpparser.Expression {
  88. list := strings.Fields(s)
  89. valList := make([]bpparser.Expression, len(list))
  90. for i, l := range list {
  91. valList[i] = &bpparser.String{
  92. Value: l,
  93. }
  94. }
  95. return &bpparser.List{
  96. Values: valList,
  97. }
  98. }
  99. func makeToListExpression(ms *mkparser.MakeString, scope mkparser.Scope) (bpparser.Expression, error) {
  100. fields := ms.Split(" \t")
  101. var listOfListValues []bpparser.Expression
  102. listValue := &bpparser.List{}
  103. for _, f := range fields {
  104. if len(f.Variables) == 1 && f.Strings[0] == "" && f.Strings[1] == "" {
  105. if ret, ok := f.Variables[0].EvalFunction(scope); ok {
  106. listValue.Values = append(listValue.Values, &bpparser.String{
  107. Value: ret,
  108. })
  109. } else {
  110. // Variable by itself, variable is probably a list
  111. if !f.Variables[0].Name.Const() {
  112. return nil, fmt.Errorf("unsupported non-const variable name")
  113. }
  114. if f.Variables[0].Name.Value(nil) == "TOP" {
  115. listValue.Values = append(listValue.Values, &bpparser.String{
  116. Value: ".",
  117. })
  118. } else {
  119. if len(listValue.Values) > 0 {
  120. listOfListValues = append(listOfListValues, listValue)
  121. }
  122. listOfListValues = append(listOfListValues, &bpparser.Variable{
  123. Name: f.Variables[0].Name.Value(nil),
  124. Value: &bpparser.List{},
  125. })
  126. listValue = &bpparser.List{}
  127. }
  128. }
  129. } else {
  130. s, err := makeToStringExpression(f, scope)
  131. if err != nil {
  132. return nil, err
  133. }
  134. if s == nil {
  135. continue
  136. }
  137. listValue.Values = append(listValue.Values, s)
  138. }
  139. }
  140. if len(listValue.Values) > 0 {
  141. listOfListValues = append(listOfListValues, listValue)
  142. }
  143. if len(listOfListValues) == 0 {
  144. return listValue, nil
  145. }
  146. val := listOfListValues[0]
  147. for _, tmp := range listOfListValues[1:] {
  148. var err error
  149. val, err = addValues(val, tmp)
  150. if err != nil {
  151. return nil, err
  152. }
  153. }
  154. return val, nil
  155. }
  156. func stringToBoolValue(s string) (bpparser.Expression, error) {
  157. var b bool
  158. s = strings.TrimSpace(s)
  159. switch s {
  160. case "true":
  161. b = true
  162. case "false", "":
  163. b = false
  164. case "-frtti": // HACK for LOCAL_RTTI_VALUE
  165. b = true
  166. default:
  167. return nil, fmt.Errorf("unexpected bool value %s", s)
  168. }
  169. return &bpparser.Bool{
  170. Value: b,
  171. }, nil
  172. }
  173. func makeToBoolExpression(ms *mkparser.MakeString) (bpparser.Expression, error) {
  174. if !ms.Const() {
  175. if len(ms.Variables) == 1 && ms.Strings[0] == "" && ms.Strings[1] == "" {
  176. name := ms.Variables[0].Name
  177. if !name.Const() {
  178. return nil, fmt.Errorf("unsupported non-const variable name")
  179. }
  180. return &bpparser.Variable{
  181. Name: name.Value(nil),
  182. Value: &bpparser.Bool{},
  183. }, nil
  184. } else {
  185. return nil, fmt.Errorf("non-const bool expression %s", ms.Dump())
  186. }
  187. }
  188. return stringToBoolValue(ms.Value(nil))
  189. }