summary refs log tree commit diff stats
path: root/compiler/semcall.nim
blob: f20349c0fe143bf775a85f74ed3c0b6cfced39bc (plain) (blame)
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
#
#
#           The Nimrod Compiler
#        (c) Copyright 2011 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements semantic checking for calls. 

proc sameMethodDispatcher(a, b: PSym): bool = 
  result = false
  if a.kind == skMethod and b.kind == skMethod: 
    var aa = lastSon(a.ast)
    var bb = lastSon(b.ast)
    if aa.kind == nkSym and bb.kind == nkSym and aa.sym == bb.sym: 
      result = true
  
proc semDirectCallWithBinding(c: PContext, n, f: PNode, filter: TSymKinds,
                              initialBinding: PNode): PNode = 
  var
    o: TOverloadIter
    x, y, z: TCandidate
  #Message(n.info, warnUser, renderTree(n))
  var sym = initOverloadIter(o, c, f)
  result = nil
  if sym == nil: return 
  initCandidate(x, sym, initialBinding)
  initCandidate(y, sym, initialBinding)

  while sym != nil: 
    if sym.kind in filter: 
      initCandidate(z, sym, initialBinding)
      z.calleeSym = sym
      matches(c, n, z)
      if z.state == csMatch: 
        case x.state
        of csEmpty, csNoMatch: x = z
        of csMatch: 
          var cmp = cmpCandidates(x, z)
          if cmp < 0: x = z # z is better than x
          elif cmp == 0: y = z # z is as good as x
          else: nil
    sym = nextOverloadIter(o, c, f)
  if x.state == csEmpty: 
    # no overloaded proc found
    # do not generate an error yet; the semantic checking will check for
    # an overloaded () operator
  elif y.state == csMatch and cmpCandidates(x, y) == 0 and
      not sameMethodDispatcher(x.calleeSym, y.calleeSym): 
    if x.state != csMatch: 
      InternalError(n.info, "x.state is not csMatch") 
    LocalError(n.Info, errGenerated, msgKindToString(errAmbiguousCallXYZ) % [
      getProcHeader(x.calleeSym), getProcHeader(y.calleeSym), 
      x.calleeSym.Name.s])
  else: 
    # only one valid interpretation found:
    markUsed(n, x.calleeSym)
    if x.calleeSym.ast == nil: 
      internalError(n.info, "calleeSym.ast is nil") # XXX: remove this check!
    if x.calleeSym.ast.sons[genericParamsPos].kind != nkEmpty: 
      # a generic proc!
      x.calleeSym = generateInstance(c, x.calleeSym, x.bindings, n.info)
      x.callee = x.calleeSym.typ
    result = x.call
    result.sons[0] = newSymNode(x.calleeSym)
    result.typ = x.callee.sons[0]
        
proc semDirectCall(c: PContext, n: PNode, filter: TSymKinds): PNode = 
  # process the bindings once:
  var initialBinding: PNode
  var f = n.sons[0]
  if f.kind == nkBracketExpr:
    # fill in the bindings:
    initialBinding = f
    f = f.sons[0]
  else: 
    initialBinding = nil
  result = semDirectCallWithBinding(c, n, f, filter, initialBinding)

proc explicitGenericInstError(n: PNode): PNode =
  LocalError(n.info, errCannotInstantiateX, renderTree(n))
  result = n

proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
  var x: TCandidate
  initCandidate(x, s, n)
  var newInst = generateInstance(c, s, x.bindings, n.info)
  markUsed(n, s)
  result = newSymNode(newInst, n.info)

proc explicitGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode = 
  assert n.kind == nkBracketExpr
  for i in 1..sonsLen(n)-1:
    n.sons[i].typ = semTypeNode(c, n.sons[i], nil)
  var s = s
  var a = n.sons[0]
  if a.kind == nkSym:
    # common case; check the only candidate has the right
    # number of generic type parameters:
    if safeLen(s.ast.sons[genericParamsPos]) != n.len-1:
      return explicitGenericInstError(n)
    result = explicitGenericSym(c, n, s)
  elif a.kind == nkSymChoice:
    # choose the generic proc with the proper number of type parameters.
    # XXX I think this could be improved by reusing sigmatch.ParamTypesMatch.
    # It's good enough for now.
    result = newNodeI(nkSymChoice, n.info)
    for i in countup(0, len(a)-1): 
      var candidate = a.sons[i].sym
      if candidate.kind in {skProc, skMethod, skConverter, skIterator}: 
        # if suffices that the candidate has the proper number of generic 
        # type parameters:
        if safeLen(candidate.ast.sons[genericParamsPos]) == n.len-1:
          result.add(explicitGenericSym(c, n, candidate))
    # get rid of nkSymChoice if not ambigious:
    if result.len == 1: result = result[0]
    # candidateCount != 1: return explicitGenericInstError(n)
  else:
    assert false
                                 
                                                   


                                                     
                                                   


                        
                                  







                                                        




                                          
              



                                                



                                                                          

                                                                                                




                                                                                     

                   


                                                                                   



















                                                                             




                                        
# test the new time module
discard """
  file: "ttime.nim"
"""

import
  times, strutils

# $ date --date='@2147483647'
# Tue 19 Jan 03:14:07 GMT 2038

var t = getGMTime(fromSeconds(2147483647))
doAssert t.format("ddd dd MMM hh:mm:ss ZZZ yyyy") == "Tue 19 Jan 03:14:07 UTC 2038"
doAssert t.format("ddd ddMMMhh:mm:ssZZZyyyy") == "Tue 19Jan03:14:07UTC2038"

doAssert t.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
  " ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
  "19 19 Tue Tuesday 3 03 3 03 14 14 1 01 Jan January 7 07 A AM 8 38 038 2038 02038 0 00 00:00 UTC"

doAssert t.format("yyyyMMddhhmmss") == "20380119031407"

var t2 = getGMTime(fromSeconds(160070789)) # Mon 27 Jan 16:06:29 GMT 1975
doAssert t2.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
  " ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
  "27 27 Mon Monday 4 04 16 16 6 06 1 01 Jan January 29 29 P PM 5 75 975 1975 01975 0 00 00:00 UTC"

when not defined(JS):
  when sizeof(Time) == 8:
    var t3 = getGMTime(fromSeconds(889067643645)) # Fri  7 Jun 19:20:45 BST 30143
    doAssert t3.format("d dd ddd dddd h hh H HH m mm M MM MMM MMMM s" &
      " ss t tt y yy yyy yyyy yyyyy z zz zzz ZZZ") ==
      "7 07 Fri Friday 6 06 18 18 20 20 6 06 Jun June 45 45 P PM 3 43 143 0143 30143 0 00 00:00 UTC"
    doAssert t3.format(":,[]()-/") == ":,[]()-/"

var t4 = getGMTime(fromSeconds(876124714)) # Mon  6 Oct 08:58:34 BST 1997
doAssert t4.format("M MM MMM MMMM") == "10 10 Oct October"

# Interval tests
doAssert((t4 - initInterval(years = 2)).format("yyyy") == "1995")
doAssert((t4 - initInterval(years = 7, minutes = 34, seconds = 24)).format("yyyy mm ss") == "1990 24 10")

var s = "Tuesday at 09:04am on Dec 15, 2015"
var f = "dddd at hh:mmtt on MMM d, yyyy"
doAssert($s.parse(f) == "Tue Dec 15 09:04:00 2015")
# ANSIC       = "Mon Jan _2 15:04:05 2006"
s = "Thu Jan 12 15:04:05 2006"
f = "ddd MMM dd HH:mm:ss yyyy"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
s = "Thu Jan 12 15:04:05 MST 2006"
f = "ddd MMM dd HH:mm:ss ZZZ yyyy"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
s = "Thu Jan 12 15:04:05 -07:00 2006"
f = "ddd MMM dd HH:mm:ss zzz yyyy"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# RFC822      = "02 Jan 06 15:04 MST"
s = "12 Jan 16 15:04 MST"
f = "dd MMM yy HH:mm ZZZ"
doAssert($s.parse(f) == "Tue Jan 12 15:04:00 2016")
# RFC822Z     = "02 Jan 06 15:04 -0700" # RFC822 with numeric zone
s = "12 Jan 16 15:04 -07:00"
f = "dd MMM yy HH:mm zzz"
doAssert($s.parse(f) == "Tue Jan 12 15:04:00 2016")
# RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
s = "Monday, 12-Jan-06 15:04:05 MST"
f = "dddd, dd-MMM-yy HH:mm:ss ZZZ"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
s = "Thu, 12 Jan 2006 15:04:05 MST"
f = "ddd, dd MMM yyyy HH:mm:ss ZZZ"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" # RFC1123 with numeric zone
s = "Thu, 12 Jan 2006 15:04:05 -07:00"
f = "ddd, dd MMM yyyy HH:mm:ss zzz"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# RFC3339     = "2006-01-02T15:04:05Z07:00"
s = "2006-01-12T15:04:05Z-07:00"
f = "yyyy-MM-ddTHH:mm:ssZzzz"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
f = "yyyy-MM-dd'T'HH:mm:ss'Z'zzz"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
s = "2006-01-12T15:04:05.999999999Z-07:00"
f = "yyyy-MM-ddTHH:mm:ss.999999999Zzzz"
doAssert($s.parse(f) == "Thu Jan 12 15:04:05 2006")
# Kitchen     = "3:04PM"
s = "3:04PM"
f = "h:mmtt"
doAssert "15:04:00" in $s.parse(f)
#when not defined(testing):
#  echo "Kitchen: " & $s.parse(f)
#  var ti = timeToTimeInfo(getTime())
#  echo "Todays date after decoding: ", ti
#  var tint = timeToTimeInterval(getTime())
#  echo "Todays date after decoding to interval: ", tint

# checking dayOfWeek matches known days
doAssert getDayOfWeek(21, 9, 1900) == dFri
doAssert getDayOfWeek(1, 1, 1970) == dThu
doAssert getDayOfWeek(21, 9, 1970) == dMon
doAssert getDayOfWeek(1, 1, 2000) == dSat
doAssert getDayOfWeek(1, 1, 2021) == dFri
# Julian tests
doAssert getDayOfWeekJulian(21, 9, 1900) == dFri
doAssert getDayOfWeekJulian(21, 9, 1970) == dMon
doAssert getDayOfWeekJulian(1, 1, 2000) == dSat
doAssert getDayOfWeekJulian(1, 1, 2021) == dFri

# toSeconds tests with GM and Local timezones
#var t4 = getGMTime(fromSeconds(876124714)) # Mon  6 Oct 08:58:34 BST 1997
var t4L = getLocalTime(fromSeconds(876124714))
doAssert toSeconds(timeInfoToTime(t4L)) == 876124714    # fromSeconds is effectively "localTime"
doAssert toSeconds(timeInfoToTime(t4L)) + t4L.timezone.float == toSeconds(timeInfoToTime(t4))

# adding intervals
var
  a1L = toSeconds(timeInfoToTime(t4L + initInterval(hours = 1))) + t4L.timezone.float
  a1G = toSeconds(timeInfoToTime(t4)) + 60.0 * 60.0
doAssert a1L == a1G

# subtracting intervals
a1L = toSeconds(timeInfoToTime(t4L - initInterval(hours = 1))) + t4L.timezone.float
a1G = toSeconds(timeInfoToTime(t4)) - (60.0 * 60.0)
doAssert a1L == a1G

# add/subtract TimeIntervals and Time/TimeInfo
doAssert getTime() - 1.seconds == getTime() - 3.seconds + 2.seconds
doAssert getTime() + 65.seconds == getTime() + 1.minutes + 5.seconds
doAssert getTime() + 60.minutes == getTime() + 1.hours
doAssert getTime() + 24.hours == getTime() + 1.days
doAssert getTime() + 13.months == getTime() + 1.years + 1.months
var
  ti1 = getTime() + 1.years
ti1 -= 1.years
doAssert ti1 == getTime()
ti1 += 1.days
doAssert ti1 == getTime() + 1.days

# overflow of TimeIntervals on initalisation
doAssert initInterval(milliseconds = 25000) == initInterval(seconds = 25)
doAssert initInterval(seconds = 65) == initInterval(seconds = 5, minutes = 1)
doAssert initInterval(hours = 25) == initInterval(hours = 1, days = 1)
doAssert initInterval(months = 13) == initInterval(months = 1, years = 1)

# Bug with adding a day to a Time
let day = 24.hours
let tomorrow = getTime() + day
doAssert tomorrow - getTime() == 60*60*24