-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCallbackType.zu
124 lines (105 loc) · 2.93 KB
/
CallbackType.zu
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
#
# The Zimbu compiler written in Zimbu
#
# CallbackType class.
#
# A Type used for callback<>
#
# Copyright 2013 Bram Moolenaar All Rights Reserved.
# Licensed under the Apache License, Version 2.0. See the LICENSE file or
# obtain a copy at: http://www.apache.org/licenses/LICENSE-2.0
#
IMPORT.PROTO "zui.proto"
IMPORT "ReferenceType.zu"
IMPORT "Declaration.zu"
IMPORT "MethodType.zu"
IMPORT "SContext.zu"
IMPORT "TargetLang.zu"
IMPORT "Type.zu"
CLASS CallbackType EXTENDS ReferenceType @items=public
# The method at the start of the type spec.
MethodType $methodType
# The extra arguments (MethodType useArguments and autoArguments together)
list<Declaration.C> $arguments
# The method actually called. Arguments are those of $methodType plus
# $arguments.
MethodType $calledMethodType
TargetLang $written # Languages for which this item was written.
NEW(Type.Enum type, string name) @replace
PARENT.NEW(type, name)
$arguments = NEW()
}
# Make a copy of this Type. Every subclass must redefine it.
FUNC $copyType() Type @replace @default
CallbackType ret = NEW($ttype, $name)
$copyMethodValues(ret)
RETURN ret
}
# Copy the values of this object into |ret|.
PROC $copyMethodValues(CallbackType ret) @default
$copyReferenceValues(ret)
ret.methodType = $methodType
ret.arguments = $arguments
ret.written = $written
}
# Return something nicer than "ref".
FUNC $typeName(bool long) string @replace @default
string s = "callback<"
IF $methodType == NIL
s ..= "unknown"
ELSE
s ..= $methodType.typeName(long)
}
FOR arg IN $arguments
s ..= ", " .. arg.type.typeName(long)
}
s ..= ">"
RETURN s
}
# Add an argument |type|
PROC $addArgument(Declaration type)
IF $arguments == NIL
$arguments = NEW()
}
$arguments.add(type)
}
FUNC $typeDefined() bool @replace
IF $methodType == NIL || !$methodType.typeDefined()
RETURN FALSE
}
IF $arguments == NIL
RETURN FALSE
}
FOR arg IN $arguments
IF !arg.type.typeDefined()
RETURN FALSE
}
}
RETURN TRUE
}
# Return the name for the type.
FUNC $getTypeName(SContext ctx) string @replace
RETURN "cb"
}
# Return TRUE for types that use managed memory. This excludes pointers to
# callbacks, these are in static memory. Also exclude references, e.g. an
# "&undef" argument.
FUNC $isManaged() bool @replace
RETURN TRUE
}
# Return the method type as the caller sees it, without the USE arguments.
FUNC $getMethod() MethodType @replace @default
RETURN $methodType
}
SHARED
PROC generateCallbacks(SContext ctx)
FOR cb IN ctx.topScope.allCallbacks
IF ctx.gen.isDeclUsed(cb)
# Declare the structure to store the info and produce an alloc
# function.
ctx.gen.writeCallbackDecl(cb, cb.methodType.zuiPos, ctx)
}
}
}
}
}