Skip to content

webdsl-types.stx

pdmosses/webdsl-statix/webdslstatix/trans/static-semantics/webdsl-types.stx

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
module 

imports
  static-semantics/entities/annotations

  static-semantics/types/built-ins
  static-semantics/types/type-extensions

  static-semantics/webdsl

signature

  constructors // semantic types

    // list of argument types, return type
    // and BOOL that indicates if function is static or not
     : string * list(TYPE) * TYPE * BOOL -> TYPE
       : string * list(TYPE) -> TYPE

           : string * scope -> TYPE
     : string * scope -> TYPE

           : string * scope -> TYPE
     : string * scope -> TYPE

         : string * list(TYPE) -> TYPE

    // BOOL indicates if template is ajax-enabled or not
     : string * list(TYPE) * BOOL -> TYPE

    // access control
        : string * list(TYPE) -> TYPE
     : list(TYPE)          -> TYPE

     : TYPE -> TYPE
      : TYPE -> TYPE
      : TYPE -> TYPE

    // built-in types without scopes
     : string * scope -> TYPE
     : string * scope -> TYPE

     : list(TYPE) * scope -> TYPE

                : TYPE
                 : TYPE
     : TYPE
                 : TYPE
              : TYPE

rules // types of constant expressions

  typeOfExp(s, exp@True()) =  :-
    t == bool(s),
    @exp.type := t.

  typeOfExp(s, exp@False()) =  :-
    t == bool(s),
    @exp.type := t.

  typeOfExp(_, exp@Null()) = NULL() :-
    @exp.type := NULL().

  typeOfExp(s, Const(c)) = typeOfConst(s, c).
  typeOfPlaceholderExp(s, PHConst(c)) = typeOfConst(s, c).

   : scope * ConstValue -> TYPE
  typeOfConst(s, exp@Int(_)) =  :-
    t == int(s),
    @exp.type := t.

  typeOfConst(s, exp@Long(_)) =  :-
    t == long(s),
    @exp.type := t.

  typeOfConst(s, exp@Float(_)) =  :-
    t == float(s),
    @exp.type := t.

  typeOfConst(, exp@StringConst(str)) =  :-
    t == string(s),
    stringOk(s, str),
    @exp.type := t.

   : scope * String
  stringOk(s, String(parts)) :- stringPartsOk(s, parts).

   maps stringPartOk(*, list(*))
   : scope * StringPart
  stringPartOk(s, StringValue(_)).
  stringPartOk(s, InterpExp(exp)) :- typed(s, exp).
  stringPartOk(s, InterpValue(InterpSimpleExp(simple_exp))) :-
    inequalType(typeOfSimpleExp(s, simple_exp), UNTYPED()).

rules // type compatibility of constants

  // numeric types
  typeCompatibleB(BUILTINTYPE("Long", _), BUILTINTYPE("Int", _)) = TRUE().
  typeCompatibleB(BUILTINTYPE("Int", _), BUILTINTYPE("Long", _)) = TRUE().

  // string types
  typeCompatibleB(T1, BUILTINTYPE("String", _))      = isStringCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("Secret", _))      = isStringCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("Email", _))       = isStringCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("URL", _))         = isStringCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("WikiText", _))    = isStringCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("Text", _))        = isStringCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("Patch", _))       = isStringCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("Placeholder", _)) = isStringCompatibleTypeB(T1).

  // date types
  typeCompatibleB(T1, BUILTINTYPE("Date", _))     = isDateCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("DateTime", _)) = isDateCompatibleTypeB(T1).
  typeCompatibleB(T1, BUILTINTYPE("Time", _))     = isDateCompatibleTypeB(T1).

  // misc
  typeCompatibleB(LIST(T1), LIST(T2)) = typeCompatibleB(T1, T2).
  typeCompatibleB(SET(T1), SET(T2))   = typeCompatibleB(T1, T2).
  typeCompatibleB(REF(_), REF(_))     = TRUE().
  typeCompatibleB(T1, REF(T2))        = typeCompatibleB(T1, T2). // ref is compatible both ways
  typeCompatibleB(REF(T1), T2)        = typeCompatibleB(T1, T2). // ref is compatible both ways
  typeCompatibleB(NULL(), _) = TRUE().
  typeCompatibleB(_, BUILTINTYPE("Object", _)) = TRUE().

rules // utils

  isDateCompatibleType : TYPE
  isDateCompatibleType(t) :- isDateCompatibleTypeB(t) == TRUE().

   : TYPE -> BOOL
  isDateCompatibleTypeB(_)                          = FALSE().
  isDateCompatibleTypeB(BUILTINTYPE("Date", _))     = TRUE().
  isDateCompatibleTypeB(BUILTINTYPE("DateTime", _)) = TRUE().
  isDateCompatibleTypeB(BUILTINTYPE("Time", _))     = TRUE().
  isDateCompatibleTypeB(BUILTINTYPE("Object", _))   = TRUE().

   : TYPE
  isStringCompatibleType(t) :- isStringCompatibleTypeB(t) == TRUE().

   : TYPE -> BOOL
  isStringCompatibleTypeB(_)             = FALSE().
  isStringCompatibleTypeB(BUILTINTYPE("String", _))       = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("Secret", _))       = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("Email", _))        = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("URL", _))          = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("WikiText", _))     = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("Text", _))         = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("Patch", _))        = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("Object", _))       = TRUE().
  isStringCompatibleTypeB(BUILTINTYPE("Placeholder", _)) = TRUE().

rules // map sorts to semantic types

   : scope * Sort -> TYPE
  typeOfSortTyped(s, sort) =  :-
    t == typeOfSort(s, sort),
    t != UNTYPED() | error $[Cannot resolve type [t]].

  typesOfSorts maps typeOfSort(*, list(*)) = list(*)
   : scope * Sort -> TYPE
  typeOfSort(_, _) = UNTYPED(). // untyped by default
  typeOfSort(s, SimpleSort("Int")) = int(s).
  typeOfSort(s, SimpleSort("Float")) = float(s).
  typeOfSort(s, SimpleSort("Long")) = long(s).

  typeOfSort(s, SimpleSort("String")) = string(s).
  typeOfSort(s, SimpleSort("Secret")) = secret(s).
  typeOfSort(s, SimpleSort("Email")) = email(s).
  typeOfSort(s, SimpleSort("URL")) = url(s).
  typeOfSort(s, SimpleSort("WikiText")) = wikitext(s).
  typeOfSort(s, SimpleSort("Text")) = text(s).
  typeOfSort(s, SimpleSort("Patch")) = patch(s).
  typeOfSort(s, SimpleSort("Placeholder")) = placeholder(s).

  typeOfSort(s, SimpleSort("Date")) = date(s).
  typeOfSort(s, SimpleSort("DateTime")) = datetime(s).
  typeOfSort(s, SimpleSort("Time")) = time(s).

  typeOfSort(_, SimpleSort("Regex")) = REGEX().
  typeOfSort(s, SimpleSort("Bool")) = bool(s).
  typeOfSort(_, SimpleSort("Void")) = VOID().
  typeOfSort(s, SimpleSort("UUID")) = uuid(s).
  typeOfSort(_, SimpleSort("TemplateElements")) = TEMPLATEELEMENTS().
  typeOfSort(s, SimpleSort("File")) = file(s).
  typeOfSort(s, SimpleSort("Image")) = image(s).

  // in case it's not a built in simple sort, it must be an entity
  typeOfSort(s, SimpleSort(x)) = definedType(s, x).

  typeOfSort(s, GenericSort("List", [innerSort])) = LIST(typeOfSort(s, innerSort)).
  typeOfSort(s, GenericSort(_, _)) = UNTYPED() :- false | error $[Only lists and sets with one type parameter are supported].
  typeOfSort(s, GenericSort("Set", [innerSort])) = SET(typeOfSort(s, innerSort)).

  typeOfSort(s, RefSort(innerSort)) = REF(typeOfSort(s, innerSort)).

rules // WebDSL definition of being a simple type: built-in simple sort (no generics, no ref, no entity etc)

   : TYPE
  isBuiltInType(t) :- isBuiltInTypeB(t) == TRUE().

  : TYPE -> BOOL
  isBuiltInTypeB(_) = FALSE(). // not a simple type by default
  isBuiltInTypeB(BUILTINTYPE(_, _)) = TRUE().

rules // type related expressions

  typeOfExp(, Cast(exp, sort)) =  :- {}
    t  == typeOfExpTyped(s, exp),
    t' == typeOfSortTyped(s, sort),
    or(typeCompatibleB(t, t'), typeCompatibleB(t', t)) | error $[Type [t] cannot be cast to [t']].

  typeOfExp(, IsA(exp, sort)) = bool(s) :- {t t'}
    t  == typeOfExpTyped(s, exp),
    t' == typeOfSortTyped(s, sort).

rules // resolve reference types

   : scope * string -> TYPE
  definedType(s, ) = t :- {}
    resolveType(s, name) == [(_, (name', t))|_] | error $[Type [name] does not exist], // correct error message for tests
    @name.ref := name'.

   : scope * string -> TYPE
  definedTypeCustomError(s, ) = t :- {}
    resolveType(s, name) == [(_, (name', t))|_],
    @name.ref := name'.

   : scope * string -> TYPE
  definedTypeNoRef(s, ) = t :-
    resolveType(s, name) == [(_, (_, t))|_] | error $[Type [name] does not exist]. // correct error message for tests

   : scope * string -> TYPE
  definedTypeIfExists(s, name) = definedTypeIfExists_internal(s, resolveType(s, name)).

   : scope * list((path * (string * TYPE))) -> TYPE
  definedTypeIfExists_internal(_, [(_, (_, t))|_]) = t.
  definedTypeIfExists_internal(s, []) = entity(s).

   : scope * string -> TYPE
  variableType(s, ) = t :- {}
    resolveVar(s, x) == [(_, (x', t))|_] | error $[Variable [x] not defined], // correct error message for tests
    @x.ref := x'.

   : scope * string -> TYPE
  propertyType(s, ) = t :- {   }
    ps == withoutAnnotation(resolveProperty(s, x), OVERRIDABLE()),
    ps == [(_, (x', t))],
    @x.ref := x'.

   : scope * string -> TYPE
  propertyTypeNoRef(s, x) = t :- {  x' }
    ps == withoutAnnotation(resolveProperty(s, x), OVERRIDABLE()),
    ps == [(_, (x', t))].

   : list((path * (string * TYPE))) * string * string
  singleResult([], type, ) :- false | error $[[type] [x] is not defined] @x.
  singleResult([_ | [_ | _]], type, ) :- false | error $[[type] [x] is defined multiple times] @x.
  singleResult(_, _, _).

   : scope * string -> TYPE
  pageType(s, ) = t :- {}
    resolvePage(s, p) == [(_, (p', t@PAGE(_, _)))|_], // no fixed error message here to allow more specific error messages on higher predicates
    @p.ref := p'.

rules // utils

   : TYPE -> TYPE
  stripGenericType(t) = UNTYPED() :- false | error $[Must be a list or set, [t] given].
  stripGenericType(REF(t)) = stripGenericType(t).
  stripGenericType(LIST(t)) = t.
  stripGenericType(SET(t)) = t.

   : TYPE -> TYPE
  stripOptionalSetOrListType(t) = t.
  stripOptionalSetOrListType(LIST(t)) = t.
  stripOptionalSetOrListType(SET(t)) = t.

   maps stripRefType(list(*)) = list(*)
   : TYPE -> TYPE
  stripRefType(t) = t.
  stripRefType(REF(t)) = stripRefType(t).

   : scope * Exp
  typed(s, exp) :- inequalType(typeOfExp(s, exp), UNTYPED()).

   : TYPE * TYPE
  inequalType(T1, T2) :- equalTypeB(T1, T2) == FALSE().

   : TYPE * TYPE
  equalType(T1, T2) :- equalTypeB(T1, T2) == TRUE().

   : TYPE * TYPE -> BOOL
  equalTypeB(T, T) = TRUE().
  equalTypeB(_, _) = FALSE().

rules

   : TYPE -> string
  ppType(t) = "<unknown pretty print type>" :- try { false } | warning $[Couldn't pretty-print type [t]].
  ppType(ENTITY(e, _)) = e.
  ppType(STATICENTITY(e, _)) = e.
  ppType(NATIVECLASS(nc, _)) = nc.
  ppType(STATICNATIVECLASS(nc, _)) = nc.
  ppType(BUILTINTYPE(t, _)) = t.
  ppType(STATICBUILTINTYPE(t, _)) = t.

  ppType(REGEX()) = "Regex".
  ppType(VOID()) = "Void".
  ppType(TEMPLATEELEMENTS()) = "TemplateElements".
  ppType(NULL()) = "Null".
  ppType(UNTYPED()) = "UNTYPED".