-
Notifications
You must be signed in to change notification settings - Fork 9
/
Macros.hx
161 lines (141 loc) · 4.17 KB
/
Macros.hx
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
package edge.core.macro;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using thx.macro.MacroFields;
import Type in RType;
class Macros {
public static function getVarAsFunctionArgs(fields : Array<Field>) : Array<FunctionArg> {
return fields
.map(function(field) return switch field.kind {
case FVar(t, _) if(!field.isStatic()):
{ name : field.name, type : t, opt : null, value : null }
case _:
null;
})
.filter(function(field) return field != null);
}
public static function createVarField(name : String, type : ComplexType) : Field {
return {
name: name,
kind: FVar(type, null),
pos: Context.currentPos()
};
}
public static function createFunctionField(name : String, ?args : Array<FunctionArg>, ?ret : ComplexType, ?expr : Expr) : Field {
return {
name: name,
access: [APublic],
kind: FFun({
ret : null != ret ? ret : macro : Void,
expr : null != expr ? expr : macro {},
args : null != args ? args : []
}),
pos: Context.currentPos()
};
}
public static function makeVarsPublic(fields : Array<Field>) {
fields.map(function(field) switch field.kind {
case FVar(_, _) if(!field.isPublic()):
field.access.push(APublic);
case _:
});
}
public static function makePublic(fields : Array<Field>, name : String) {
var field = findField(fields, name);
if(null == field) return;
makeFieldPublic(field);
}
public static function makeFieldPublic(field : Field) {
if(isPublic(field)) return;
field.access.push(APublic);
}
public static function isPublic(field : Field) {
for(a in field.access)
if(RType.enumEq(a, APublic))
return true;
return false;
}
public static function hasField(fields : Array<Field>, name : String)
return null != findField(fields, name);
public static function findField(fields : Array<Field>, name : String) {
for(field in fields)
if(field.name == name)
return field;
return null;
}
public static function findClassField(fields : Array<ClassField>, name : String) {
for(field in fields) {
if(field.name == name)
return field;
}
return null;
}
public static function hasClassField(fields : Array<ClassField>, name : String)
return findClassField(fields, name) != null;
public static function isFieldInHirearchy(type : ClassType, name : String) : Bool {
if(name == "new") {
if(null != type.constructor)
return true;
} else {
if(hasClassField(type.fields.get(), name))
return true;
}
var superClass = type.superClass;
if(null == superClass) {
return false;
}
return isFieldInHirearchy(superClass.t.get(), name);
}
public static function hasVarField(fields : Array<Field>, fieldName : String) {
for(field in fields)
if(field.name == fieldName && switch field.kind {
case FVar(_, _): true;
case _: false;
})
return true;
return false;
}
public static function hasFunField(fields : Array<Field>, fieldName : String) {
for(field in fields)
if(field.name == fieldName && switch field.kind {
case FFun(_): true;
case _: false;
})
return true;
return false;
}
public static function fieldHasMeta(field : Field, name : String) {
if(field.meta == null) return false;
for(meta in field.meta)
if(meta.name == name)
return true;
return false;
}
inline public static function clsName()
return Context.getLocalClass().toString();
public static function appendExprToFieldFunction(field : Field, expr : Expr) {
switch field.kind {
case FFun(o):
var exprs = [o.expr, expr];
o.expr = macro $b{exprs};
case _:
}
}
public static function fieldFunctionHasArguments(field : Field) {
switch field.kind {
case FFun(o):
return o.args.length > 0;
case _:
return false;
}
}
public static function fieldFunctionArguments(field : Field) {
switch field.kind {
case FFun(o):
return o.args;
case _:
return null;
}
}
}