Source

nsl / nsl / types.py

  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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
from collections import OrderedDict
import nsl.ast


class UnknownSymbol(Exception):
    def __init__(self, symbol):
        self.symbol = symbol

    def __str__(self):
        return "Unknown symbol: '{}'".format (self.symbol)

class UnknownFunction(Exception):
    pass

class NotMatchingFunctionOverload(Exception):
    pass

class InvalidDeclaration(Exception):
    def __init__(self, message):
        self.message = message

class Scope:
    def __init__(self, parent = None):
        # We store everything in ordered dicts so we can use
        # this for structures/classes without further modification
        self.symbols = OrderedDict ()
        self.parent = parent
        self.functions = OrderedDict ()

        self.registeredObjects = set ()

    def RegisterVariable(self, symbol, typeinfo):
        assert isinstance (typeinfo, Type), 'Expected Type instance but got {}'.format (type(typeinfo))
        assert symbol not in self.symbols

        if symbol in self.registeredObjects:
            raise InvalidDeclaration ("Cannot define variable '{}': A function with that name already exists in the current scope.".format (symbol))

        self.symbols [symbol] = typeinfo
        self.registeredObjects.add (symbol)

    def GetVariableType (self, symbol):
        if symbol in self.symbols:
            return self.symbols [symbol]
        else:
            if self.parent is not None:
                return self.parent.GetVariableType (symbol)
            else:
                raise UnknownSymbol(symbol)

    def RegisterFunction(self, functionName, typeinfo):
        if functionName in self.registeredObjects and functionName in self.symbols:
            raise InvalidDeclaration ("Cannot define function '{}': A variable with that name already exists in the current scope.".format (functionName))

        if not functionName in self.functions:
            self.functions [functionName] = []
        self.functions [functionName].append (typeinfo)
        self.registeredObjects.add (functionName)

    def GetFunctionType(self, functionName, argumentTypes):
        '''Get a matching function.

        @param argumentTypes: The type of each function parameter.'''

        # Resolve overloaded functions
        if not functionName in self.functions:
            if not self.parent is None:
                return self.parent.GetFunctionType(functionName,
                                                   argumentTypes)
            else:
                raise UnknownFunction ()

        def GetFirst(tuple):
            return tuple[0]

        def IsValidCandidate(candidate):
            return candidate[0] >= 0

        candidates = self.functions [functionName]
        ranking = list (filter (IsValidCandidate,
                          sorted ([(candidate.Match (argumentTypes), candidate)
                                   for candidate in candidates],
                          key=GetFirst)))

        if len(ranking) == 0:
            raise UnknownFunction()

        if len(ranking) == 1:
            return ranking[0][1]

        # if the first two candidates have the same score, the overload is
        # ambiguous
        if ranking[0][0] == ranking[1][0]:
            raise UnknownFunction()

        return ranking[0][1]

class Type:
    '''Base class for all type calculations.'''
    def GetName(self):
        assert False, 'Base type has no defined name'
        return 'undef'

    def IsPrimitive(self):
        return False

    def IsComplex(self):
        return False

    def IsArray(self):
        return False

    def HasSemantic(self):
        return False

    def NeedsResolve (self):
        return False

def Resolve(type, scope):
    if type.NeedsResolve ():
        result = scope.GetVariableType (type.GetName ())
        assert not isinstance (result, UnresolvedType)
        return result
    else:
        return type

def ResolveFunction(type, scope, argumentTypes):
    if type.NeedsResolve ():
        return scope.GetFunctionType (type.GetName (), argumentTypes)
    else:
        return type

class UnresolvedType:
    def __init__(self, name):
        self.name = name

    def NeedsResolve(self):
        return True

    def GetName (self):
        return self.name

def IsCompatible(left, right):
    '''Check if two types are compatible.

    All NSL numeric types are compatible. Arrays are compatible if
    they consist of the same element type and have the same size.
    Structs must have the same type to be compatible, which is
    stricter than the DX rules (which allow input structs to consume
    a subset of the output struct elements, i.e. the first n elements.'''
    if left.IsArray() and not right.IsArray ():
        return False
    elif not left.IsArray () and right.IsArray ():
        return False
    elif left.IsArray() and right.IsArray ():
        if left.GetSize() != right.GetSize ():
            return False
        else:
            return IsCompatible (left.GetType (), right.GetType ())
    elif left.IsPrimitive() and right.IsPrimitive():
        if isinstance(left, Void) or isinstance(right, Void):
            return isinstance (left, Void) and isinstance (right, Void)

        # reduce single-component vector types to scalars
        if isinstance (left, VectorType):
            if left.GetSize () == 1:
                left = left.GetType ()
        if isinstance (right, VectorType):
            if right.GetSize () == 1:
                right = right.GetType ()

        # Our primitive types are all compatible
        if isinstance(left, VectorType) and isinstance(right, VectorType):
            return left.GetSize () == right.GetSize()
        elif isinstance (left, MatrixType) and isinstance (right, MatrixType):
            return ((left.GetRows () == right.GetRows ()) and
                    (left.GetColumns () == right.GetColumns ()))
        elif left.IsScalar () and right.IsScalar ():
            return True
        else:
            return False
    elif left.IsPrimitive () and right.IsComplex ():
        return False
    elif left.IsComplex () and right.IsPrimitive ():
        return False
    else:
        # both complex
        return left == right

def Match(leftType, rightType):
    '''Match two types.
    @return How well the two types match. 0 indicates the types are
        equal, 1 indicates that the right type has to be implicitly
        converted to the left and -1 means the types are not
        convertible.'''
    if not IsCompatible(leftType, rightType):
        return -1
    elif (leftType == rightType):
        return 0
    else:
        return 1

def Promote(t1, t2):
    # TODO: scalars can get promoted to 1-element vectors
    if isinstance (t1, VectorType ) and isinstance (t2, VectorType):
        assert t1.GetSize () == t2.GetSize (), \
            'Cannot combine {} and {} to unified vector type'.format (t1, t2)
        return VectorType (Promote (t1.GetType (), t2.GetType (),
                                    t1.GetSize ()))
    if isinstance (t1, Float) or isinstance (t2, Float):
        return Float ()
    elif isinstance (t1, Integer) or isinstance (t2, Integer):
        return Integer ()
    else:
        return t1

def GetCombinedType(expr, left, right):
    '''Get the type of an expression combining two elements,
    one of type left and one of type right. This performs the standard
    type promotion rules (int->float, int->uint) and expects that both
    types are compatible to start with.

    @param expr: A binary expression'''
    assert IsCompatible (left, right), 'Incompatible types'
    # must be primitive type, we don't support operations on
    # complex types
    assert isinstance (left, PrimitiveType)
    assert isinstance (right, PrimitiveType)

    if nsl.ast.IsComparison (expr.GetOperation ()):
        return Integer ()

    if left == right:
        return left

    # otherwise, promote
    return Promote (left, right)

def IsValidInput(outputStructType, inputStructType):
    return False

class PrimitiveType(Type):
    '''Primitive, or built-in type base class.'''
    def IsPrimitive(self):
        return True

    def __eq__(self, other):
        '''All primitive types have a valid __repr__ implementation, so just
        use that.'''
        return repr(self) == repr(other)

    def __init__(self):
        self.semantic = None

    def SetSemantic(self, semantic):
        assert semantic is not None
        self.semantic = semantic

    def GetSemantic(self):
        return self.semantic

    def HasSemantic(self):
        return self.semantic is not None

    def IsScalar (self):
        return False

    def IsVector (self):
        return False

    def IsMatrix (self):
        return False

class ArrayType(Type):
    def __init__(self, elementType, arraySize):
        Type.__init__(self)
        assert arraySize > 0
        self.elementType = elementType
        self.arraySize = arraySize

    def GetSize(self):
        return self.arraySize

    def GetType(self):
        return self.elementType

    def GetName(self):
        return self.GetType().GetName () + ' [' + str(self.arraySize) + ']'

    def __str__(self):
        return '{} [{}]'.format (self.elementType, self.arraySize)

    def __repr__(self):
        return 'ArrayType ({}, {})'.format (repr(self.elementType),
                                            self.arraySize)

class ComplexType(Type):
    def IsComplex(self):
        return True

class StructType(ComplexType):
    def __init__(self, name, declarations):
        self.members = Scope ()
        self.name = name
        for (name, type) in declarations.items ():
            self.members.RegisterVariable(name, type)
        self.declarations = declarations

    def __str__(self):
        return 'struct {}'.format(self.name)

    def __repr__(self):
        return 'StructType ({}, {})'.format (repr(self.name),
                                             repr(self.declarations))

    def GetName(self):
        return self.name

    def GetMembers(self):
        return self.members

    def GetVariableType(self, variableName):
        return self.members.GetVariableType (variableName)

class ClassType(StructType):
    '''Struct type with additional support for member functions.'''
    def __init__(self, name, declarations, functions):
        StructType.__init__(self, name, declarations)
        for func in functions:
            self.members.RegisterFunction(func.GetName (), func)

    def GetFunctionType(self, functionName, argumentTypes):
        return self.members.GetFunctionType(functionName, argumentTypes)

class Function(Type):
    def __init__(self, name, returnType, arguments):
        self.returnType = returnType
        self.arguments = arguments
        self.name = name

    def Resolve(self, scope):
        self.returnType = Resolve(self.returnType, scope)
        tmpArgs = self.arguments
        self.arguments = OrderedDict ()
        counter = 0
        for arg in tmpArgs:
            if arg.HasName ():
                self.arguments [arg.GetName ()] = Resolve (arg.GetType (), scope)
            else:
                # generate an invalid name
                self.arguments ['$unnamed_arg${}'.format (counter)] = Resolve (arg.GetType (), scope)
            counter += 1

    def GetName(self):
        return self.name

    def GetMangledName(self):
        return '@{}->{}`{}'.format (self.name, str(self.returnType),
                                    ','.join ([str(arg) for arg in self.arguments.values ()]))

    def __str__(self):
        return 'function {0} ({1}) -> {2}'.format (self.name,
            ', '.join(self.arguments.keys ()), self.returnType)

    def __repr__(self):
        return 'Function (\'{}\', {}, [{}])'.format (self.name, repr(self.returnType),
                                                 ', '.join ([repr(arg) for arg in self.arguments.keys ()]))

    def Match(self, parameterList):
        '''Match the function signature against a parameter list.
        @return: A score indicating how well the function signature
            matches the argument list. 0 means all types match,
            negative numbers indicate the function does not match the
            signature at all and positive numbers indicate how many
            (implicit) conversions have to be performed to match.'''
        if (len(parameterList) != len(self.arguments)):
            return -1
        else:
            return sum([Match (e[0], e[1]) for e in zip (parameterList, self.arguments.values ())])

    def GetReturnType (self):
        '''The return type of this function, potentially unresolved.'''
        return self.returnType

    def GetArguments(self):
        return self.arguments

class Void(Type):
    def GetName(self):
        return 'void'

    def __repr__(self):
        return 'Void ()'

    def __str__(self):
        return 'void'

class ScalarType(PrimitiveType):
    def IsScalar (self):
        return True

class Float(ScalarType):
    def GetName(self):
        return 'float'

    def __repr__(self):
        return 'Float ()'

    def __str__(self):
        return 'float'

class Integer(ScalarType):
    def GetName(self):
        return 'int'

    def __repr__(self):
        return 'Integer ()'

    def __str__(self):
        return 'int'

class UnsignedInteger(ScalarType):
    def GetName(self):
        return 'uint'

    def __repr__(self):
        return 'UnsignedInteger ()'

    def __str__(self):
        return 'uint'

class VectorType(PrimitiveType):
    def __init__(self, componentType, componentCount):
        assert componentCount > 0
        assert isinstance(componentType, PrimitiveType)
        PrimitiveType.__init__(self)
        self.componentType = componentType
        self.componentCount = componentCount

    def IsVector(self):
        return True

    def GetType(self):
        return self.componentType

    def GetSize(self):
        return self.componentCount

    def GetName(self):
        return '{}{}'.format (self.componentType.GetName (),
                              self.componentCount)

    def __repr__(self):
        return 'VectorType (' + repr(self.componentType) + \
            ', {})'.format(self.componentCount)

    def __str__(self):
        return '{}{}'.format(self.componentType, self.componentCount)

class MatrixOrder:
    ROW_MAJOR       = 1
    COLUMN_MAJOR    = 2

class MatrixType(PrimitiveType):
    def __init__(self, componentType, rows, columns,
                 order = MatrixOrder.ROW_MAJOR):
        assert rows > 0 and columns > 0
        assert isinstance(componentType, PrimitiveType)
        PrimitiveType.__init__(self)
        self.componentType = componentType
        self.size = (rows, columns,)
        self.order = order

    def IsMatrix (self):
        return True

    def GetRowCount (self):
        return self.size [0]

    def GetColumnCount (self):
        return self.size [1]

    def GetType(self):
        return self.componentType

    def GetSize(self):
        return self.size

    def GetName(self):
        return str(self)

    def __repr__(self):
        return 'MatrixType (' + repr(self.componentType) + \
            ', {}, {})'.format(self.GetRowCount (), self.GetColumnCount ())

    def __str__(self):
        return '{}{}x{}'.format (self.componentType.GetName (),
                              self.GetRowCount (),
                              self.GetColumnCount ())

def BuiltinTypeFactory(typeName):
    typeDict = {
        'float'         : Float (),
        'float2'        : VectorType (Float (), 2),
        'float3'        : VectorType (Float (), 3),
        'float4'        : VectorType (Float (), 4),
        'int'           : Integer (),
        'int2'          : VectorType (Integer (), 2),
        'int3'          : VectorType (Integer (), 3),
        'int4'          : VectorType (Integer (), 4),
        'uint'          : UnsignedInteger (),
        'uint2'         : VectorType (UnsignedInteger (), 2),
        'uint3'         : VectorType (UnsignedInteger (), 3),
        'uint4'         : VectorType (UnsignedInteger (), 4),
        'matrix3x3'     : MatrixType (Float (), 3, 3),
        'matrix4x4'     : MatrixType (Float (), 4, 4),
        'void'          : Void ()
    }

    if typeName in typeDict:
        return typeDict [typeName]
    else:
        assert False, 'Unknown built-in type requested'
        return None