0 | module Text.WebIDL.Codegen.Types
2 | import Derive.Prelude
3 | import public Data.SortedMap
4 | import Text.WebIDL.Types
7 | %language ElabReflection
21 | data Kind : Type where
22 | KAlias : Identifier -> Kind
23 | KCallback : Identifier -> Kind
24 | KDictionary : Identifier -> Kind
25 | KEnum : Identifier -> Kind
26 | KInterface : (isParent : Bool) -> Identifier -> Kind
27 | KMixin : Identifier -> Kind
28 | KOther : Identifier -> Kind
30 | %runElab derive "Kind" [Eq,Show]
33 | isParent : Kind -> Bool
34 | isParent (KDictionary _) = True
35 | isParent (KInterface b _) = b
36 | isParent (KMixin _) = True
40 | ident : Kind -> Identifier
41 | ident (KAlias x) = x
42 | ident (KCallback x) = x
43 | ident (KDictionary x) = x
45 | ident (KInterface _ x) = x
46 | ident (KMixin x) = x
47 | ident (KOther x) = x
50 | kindToString : Kind -> String
51 | kindToString = value . ident
54 | data Wrapper = Direct | Opt | May | OptMay
57 | opt : Wrapper -> Wrapper
63 | may : Wrapper -> Wrapper
71 | data SimpleType : Type
76 | data SimpleType : Type where
81 | Boolean : SimpleType
89 | Interface : (isParent : Bool) -> Identifier -> SimpleType
99 | Dictionary : Identifier -> SimpleType
109 | Mixin : Identifier -> SimpleType
114 | Primitive : String -> SimpleType
117 | Unchangeable : String -> SimpleType
120 | Enum : Identifier -> SimpleType
123 | Array : CGType -> SimpleType
126 | Record : String -> CGType -> SimpleType
128 | data CGType : Type where
130 | Promise : CGType -> CGType
131 | Simple : Nullable SimpleType -> CGType
132 | Union : Nullable (List1 SimpleType) -> CGType
138 | isIndex : CGType -> Bool
139 | isIndex (Simple $
NotNull $
Primitive "String") = True
140 | isIndex (Simple $
NotNull $
Primitive "Bits32") = True
143 | namespace SimpleType
147 | safeCast : SimpleType -> Bool
148 | safeCast Undef = True
149 | safeCast Boolean = True
150 | safeCast (Interface _ x) = True
151 | safeCast (Dictionary _) = False
152 | safeCast (Mixin _) = False
153 | safeCast (Primitive x) = True
154 | safeCast (Unchangeable x) = False
155 | safeCast (Enum x) = True
156 | safeCast (Array x) = False
157 | safeCast (Record x y) = False
162 | sameArgType : SimpleType -> Bool
163 | sameArgType Undef = True
164 | sameArgType Boolean = False
165 | sameArgType (Interface b _) = not b
166 | sameArgType (Dictionary _) = False
167 | sameArgType (Mixin _) = False
168 | sameArgType (Primitive t) = True
169 | sameArgType (Unchangeable _) = True
170 | sameArgType (Enum _) = False
171 | sameArgType (Array _) = True
172 | sameArgType (Record _ _) = True
177 | sameRetType : SimpleType -> Bool
178 | sameRetType Undef = True
179 | sameRetType Boolean = False
180 | sameRetType (Interface _ _) = True
181 | sameRetType (Dictionary _) = True
182 | sameRetType (Mixin _) = True
183 | sameRetType (Primitive _) = True
184 | sameRetType (Unchangeable _) = True
185 | sameRetType (Enum _) = False
186 | sameRetType (Array _) = True
187 | sameRetType (Record _ _) = True
190 | inheritance : SimpleType -> Maybe (Identifier,Wrapper)
191 | inheritance (Interface True x) = Just (x,Direct)
192 | inheritance (Dictionary x) = Just (x,Direct)
193 | inheritance (Mixin x) = Just (x,Direct)
194 | inheritance _ = Nothing
199 | simple : SimpleType -> CGType
200 | simple = Simple . NotNull
203 | unchangeable : String -> CGType
204 | unchangeable = simple . Unchangeable
207 | iface : Bool -> Identifier -> CGType
208 | iface b = simple . Interface b
211 | mixin : Identifier -> CGType
212 | mixin = simple . Mixin
215 | dict : Identifier -> CGType
216 | dict = simple . Dictionary
220 | fromKind : Kind -> CGType
221 | fromKind (KAlias x) = unchangeable x.value
222 | fromKind (KCallback x) = unchangeable x.value
223 | fromKind (KDictionary x) = dict x
224 | fromKind (KEnum x) = Simple . NotNull $
Enum x
225 | fromKind (KInterface b x) = iface b x
226 | fromKind (KMixin x) = mixin x
227 | fromKind (KOther x) = unchangeable x.value
232 | safeCast : CGType -> Bool
233 | safeCast Any = True
234 | safeCast (Promise x) = False
235 | safeCast (Simple x) = safeCast $
nullVal x
236 | safeCast (Union _) = False
241 | sameArgType : CGType -> Bool
242 | sameArgType Any = False
243 | sameArgType (Promise x) = True
244 | sameArgType (Simple $
MaybeNull _) = False
245 | sameArgType (Simple $
NotNull x) = sameArgType x
246 | sameArgType (Union _) = False
251 | sameRetType : CGType -> Bool
252 | sameRetType Any = False
253 | sameRetType (Promise x) = True
254 | sameRetType (Simple $
MaybeNull _) = False
255 | sameRetType (Simple $
NotNull x) = sameRetType x
256 | sameRetType (Union $
MaybeNull _) = False
257 | sameRetType (Union $
NotNull xs) = not $
all safeCast xs
260 | inheritance : CGType -> Maybe (Identifier,Wrapper)
261 | inheritance (Simple $
MaybeNull x) = map may <$> inheritance x
262 | inheritance (Simple $
NotNull x) = inheritance x
263 | inheritance _ = Nothing
271 | data ReturnType : Type where
273 | Undefined : ReturnType
278 | UndefOr : CGType -> Maybe Default -> ReturnType
281 | Def : CGType -> ReturnType
285 | isUndefined : ReturnType -> Bool
286 | isUndefined Undefined = True
287 | isUndefined _ = False
289 | namespace ReturnType
292 | simple : SimpleType -> ReturnType
293 | simple = Def . simple
296 | unchangeable : String -> ReturnType
297 | unchangeable = Def . unchangeable
301 | fromKind : Kind -> ReturnType
302 | fromKind = Def . CGType.fromKind
306 | safeCast : ReturnType -> Bool
307 | safeCast Undefined = True
308 | safeCast (UndefOr x _) = safeCast x
309 | safeCast (Def x) = safeCast x
314 | sameType : ReturnType -> Bool
315 | sameType Undefined = True
316 | sameType (UndefOr _ _) = False
317 | sameType (Def x) = sameRetType x
324 | record CGConstType where
325 | constructor MkConstType
329 | record CGConst where
330 | constructor MkConst
341 | data CGArg : Type where
343 | Mandatory : ArgumentName -> CGType -> CGArg
347 | Optional : ArgumentName -> CGType -> Default -> CGArg
350 | VarArg : ArgumentName -> CGType -> CGArg
353 | argName : CGArg -> ArgumentName
354 | argName (Mandatory x _) = x
355 | argName (Optional x _ _) = x
356 | argName (VarArg x _) = x
359 | argType : CGArg -> CGType
360 | argType (Mandatory _ y) = y
361 | argType (Optional _ y _) = y
362 | argType (VarArg _ y) = y
365 | isOptional : CGArg -> Bool
366 | isOptional (Optional _ _ _) = True
367 | isOptional _ = False
373 | safeCast : CGArg -> Bool
374 | safeCast (Mandatory _ t) = safeCast t
375 | safeCast (Optional _ t _) = safeCast t
376 | safeCast (VarArg _ _) = False
381 | sameType : CGArg -> Bool
382 | sameType (Mandatory _ t) = sameArgType t
383 | sameType (Optional _ _ _) = False
384 | sameType (VarArg _ _) = False
387 | inheritance : CGArg -> Maybe (Identifier,Wrapper)
388 | inheritance (Mandatory _ t) = inheritance t
389 | inheritance (Optional _ t _) = map opt <$> inheritance t
390 | inheritance (VarArg _ _) = Nothing
402 | data CGFunction : Type where
405 | (name : AttributeName)
408 | -> (ret : ReturnType)
413 | (name : AttributeName)
415 | -> (tpe : ReturnType)
419 | StaticAttributeSet :
420 | (name : AttributeName)
426 | StaticAttributeGet :
427 | (name : AttributeName)
429 | -> (tpe : ReturnType)
433 | Getter : (obj : Kind) -> (index : CGArg) -> (tpe : ReturnType) -> CGFunction
436 | Setter : (obj : Kind) -> (index : CGArg) -> (value : CGArg) -> CGFunction
439 | Constructor : (obj : Kind) -> (args : Args) -> CGFunction
442 | DictConstructor : (obj : Kind) -> (args : Args) -> CGFunction
470 | priority : CGFunction -> (Nat,String,Nat)
471 | priority (DictConstructor n _) = (0,value (ident n),0)
472 | priority (Constructor n _) = (0,value (ident n),0)
473 | priority (StaticAttributeSet n _ _) = (1,show n,1)
474 | priority (StaticAttributeGet n _ _) = (1,show n,0)
475 | priority (Static n o _ _) = (2,n.value ++ value (ident o),0)
476 | priority (Getter _ _ _) = (3,"",0)
477 | priority (Setter _ _ _) = (3,"",1)
478 | priority (Attribute n _ _ _) = (4,show n,0)
479 | priority (AttributeGet n _ _) = (4,show n,1)
480 | priority (Regular n o _ _) = (5,n.value ++ value (ident o),0)
494 | record JSType where
495 | constructor MkJSType
496 | parent : Maybe Identifier
497 | mixins : List Identifier
503 | record Supertypes where
504 | constructor MkSupertypes
505 | parents : List Identifier
506 | mixins : List Identifier
513 | record CGDict where
517 | functions : List CGFunction
520 | record CGIface where
521 | constructor MkIface
524 | constants : List CGConst
525 | functions : List CGFunction
528 | record CGMixin where
529 | constructor MkMixin
531 | constants : List CGConst
532 | functions : List CGFunction
535 | record CGCallback where
536 | constructor MkCallback
538 | constants : List CGConst
543 | record CGDomain where
544 | constructor MkDomain
546 | callbacks : List CGCallback
547 | dicts : List CGDict
549 | ifaces : List CGIface
550 | mixins : List CGMixin
553 | domainFunctions : CGDomain -> List CGFunction
554 | domainFunctions d =
555 | (d.dicts >>= functions)
556 | ++ (d.ifaces >>= functions)
557 | ++ (d.mixins >>= functions)
575 | maxInheritance : Nat
576 | kinds : SortedMap Identifier Kind
577 | jsTypes : SortedMap Identifier JSType
578 | aliases : SortedMap Identifier (IdlTypeF ExtAttributeList Kind)
585 | data CodegenErr : Type where
586 | AnyInUnion : Domain -> CodegenErr
587 | CBInterfaceInvalidOps : Domain -> Identifier -> Nat -> CodegenErr
588 | InvalidConstType : Domain -> CodegenErr
589 | InvalidGetter : Domain -> Identifier -> CodegenErr
590 | InvalidSetter : Domain -> Identifier -> CodegenErr
591 | NullableAny : Domain -> CodegenErr
592 | NullablePromise : Domain -> CodegenErr
593 | PromiseInUnion : Domain -> CodegenErr
594 | RegularOpWithoutName : Domain -> Identifier -> CodegenErr
595 | UnresolvedAlias : Domain -> Identifier -> CodegenErr
598 | Codegen : Type -> Type
599 | Codegen = Either (List CodegenErr)