aboutsummaryrefslogtreecommitdiffstats
path: root/androidmk/cmd/androidmk/values.go
blob: ce2f279650162bc35542fc398fb8d668f723356c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main

import (
	"fmt"
	"strings"

	mkparser "android/soong/androidmk/parser"

	bpparser "github.com/google/blueprint/parser"
)

func stringToStringValue(s string) *bpparser.Value {
	return &bpparser.Value{
		Type:        bpparser.String,
		StringValue: s,
	}
}

func addValues(val1, val2 *bpparser.Value) (*bpparser.Value, error) {
	if val1 == nil {
		return val2, nil
	}

	if val1.Type == bpparser.String && val2.Type == bpparser.List {
		val1 = &bpparser.Value{
			Type:      bpparser.List,
			ListValue: []bpparser.Value{*val1},
		}
	} else if val2.Type == bpparser.String && val1.Type == bpparser.List {
		val2 = &bpparser.Value{
			Type:      bpparser.List,
			ListValue: []bpparser.Value{*val1},
		}
	} else if val1.Type != val2.Type {
		return nil, fmt.Errorf("cannot add mismatched types")
	}

	return &bpparser.Value{
		Type: val1.Type,
		Expression: &bpparser.Expression{
			Operator: '+',
			Args:     [2]bpparser.Value{*val1, *val2},
		},
	}, nil
}

func makeToStringExpression(ms *mkparser.MakeString, scope mkparser.Scope) (*bpparser.Value, error) {
	var val *bpparser.Value
	var err error

	if ms.Strings[0] != "" {
		val = stringToStringValue(ms.Strings[0])
	}

	for i, s := range ms.Strings[1:] {
		if ret, ok := ms.Variables[i].EvalFunction(scope); ok {
			val, err = addValues(val, stringToStringValue(ret))
		} else {
			name := ms.Variables[i].Name
			if !name.Const() {
				return nil, fmt.Errorf("Unsupported non-const variable name %s", name.Dump())
			}
			tmp := &bpparser.Value{
				Type:     bpparser.String,
				Variable: name.Value(nil),
			}

			val, err = addValues(val, tmp)
			if err != nil {
				return nil, err
			}
		}

		if s != "" {
			tmp := stringToStringValue(s)
			val, err = addValues(val, tmp)
			if err != nil {
				return nil, err
			}
		}
	}

	return val, nil
}

func stringToListValue(s string) *bpparser.Value {
	list := strings.Fields(s)
	valList := make([]bpparser.Value, len(list))
	for i, l := range list {
		valList[i] = bpparser.Value{
			Type:        bpparser.String,
			StringValue: l,
		}
	}
	return &bpparser.Value{
		Type:      bpparser.List,
		ListValue: valList,
	}

}

func makeToListExpression(ms *mkparser.MakeString, scope mkparser.Scope) (*bpparser.Value, error) {
	fields := ms.Split(" \t")

	var listOfListValues []*bpparser.Value

	listValue := &bpparser.Value{
		Type: bpparser.List,
	}

	for _, f := range fields {
		if len(f.Variables) == 1 && f.Strings[0] == "" && f.Strings[1] == "" {
			if ret, ok := f.Variables[0].EvalFunction(scope); ok {
				listValue.ListValue = append(listValue.ListValue, bpparser.Value{
					Type:        bpparser.String,
					StringValue: ret,
				})
			} else {
				// Variable by itself, variable is probably a list
				if !f.Variables[0].Name.Const() {
					return nil, fmt.Errorf("unsupported non-const variable name")
				}
				if len(listValue.ListValue) > 0 {
					listOfListValues = append(listOfListValues, listValue)
				}
				listOfListValues = append(listOfListValues, &bpparser.Value{
					Type:     bpparser.List,
					Variable: f.Variables[0].Name.Value(nil),
				})
				listValue = &bpparser.Value{
					Type: bpparser.List,
				}
			}
		} else {
			s, err := makeToStringExpression(f, scope)
			if err != nil {
				return nil, err
			}
			if s == nil {
				continue
			}

			listValue.ListValue = append(listValue.ListValue, *s)
		}
	}

	if len(listValue.ListValue) > 0 {
		listOfListValues = append(listOfListValues, listValue)
	}

	if len(listOfListValues) == 0 {
		return listValue, nil
	}

	val := listOfListValues[0]
	for _, tmp := range listOfListValues[1:] {
		var err error
		val, err = addValues(val, tmp)
		if err != nil {
			return nil, err
		}
	}

	return val, nil
}

func stringToBoolValue(s string) (*bpparser.Value, error) {
	var b bool
	s = strings.TrimSpace(s)
	switch s {
	case "true":
		b = true
	case "false", "":
		b = false
	case "-frtti": // HACK for LOCAL_RTTI_VALUE
		b = true
	default:
		return nil, fmt.Errorf("unexpected bool value %s", s)
	}
	return &bpparser.Value{
		Type:      bpparser.Bool,
		BoolValue: b,
	}, nil
}

func makeToBoolExpression(ms *mkparser.MakeString) (*bpparser.Value, error) {
	if !ms.Const() {
		if len(ms.Variables) == 1 && ms.Strings[0] == "" && ms.Strings[1] == "" {
			name := ms.Variables[0].Name
			if !name.Const() {
				return nil, fmt.Errorf("unsupported non-const variable name")
			}
			return &bpparser.Value{
				Type:     bpparser.Bool,
				Variable: name.Value(nil),
			}, nil
		} else {
			return nil, fmt.Errorf("non-const bool expression %s", ms.Dump())
		}
	}

	return stringToBoolValue(ms.Value(nil))
}