summary refs log tree commit diff stats
path: root/compiler
Commit message (Expand)AuthorAgeFilesLines
* first steps to deprecate 'nil' statementAraq2012-09-097-32/+29
* implemented 'pure' enumsAraq2012-09-081-5/+6
* tmatrix.nim works nowAraq2012-09-083-21/+38
* term rewriting improvementsAraq2012-09-089-136/+238
* activated tests for tr macrosAraq2012-09-063-22/+28
* bugfixes and improvements for term rewriting macrosAraq2012-09-043-13/+24
* further improvements for term rewriting macrosAraq2012-09-042-22/+13
* term rewriting macros fully implemented; still buggyAraq2012-09-0317-118/+658
* next steps towards term rewriting macros; simple examples workAraq2012-08-3011-53/+73
* first steps towards term rewriting macrosAraq2012-08-3015-54/+260
* parameter passing works the same for macros and templates; use callsite magic...Araq2012-08-288-30/+54
* implemented generic templatesAraq2012-08-275-24/+36
* distinguish properly between nkOpen and nkClosedSymChoiceAraq2012-08-2612-42/+66
* bindSym suffices; no 'bind' for macros anymoreAraq2012-08-255-58/+40
* objects with no ancestor are not implicitely finalAraq2012-08-244-4/+12
* implemented 'bind' for macrosAraq2012-08-243-0/+55
* implemented backslash for continuation commentsAraq2012-08-231-6/+16
* bugfix for methods (comment in #185)Araq2012-08-222-4/+10
* documented hygienic templates; made tests green; fixed system.clampAraq2012-08-221-5/+14
* breaking change: hygienic templates (undocumented)Araq2012-08-201-1/+3
* next steps to hygienic templatesAraq2012-08-2015-74/+91
* first steps to make templates hygienicAraq2012-08-2018-177/+464
* bycopy/byref for object/tuple typesAraq2012-08-163-15/+27
* openarray/varargs split; breaks bootstrappingAraq2012-08-166-42/+45
* bugfix: typo in actors.nimAraq2012-08-153-0/+15
* next steps for tyVarargs/tyOpenArray splitAraq2012-08-1413-31/+44
* more bugfixes for closuresAraq2012-08-142-5/+6
* made tests green againAraq2012-08-145-5/+16
* first steps for varargs supportAraq2012-08-132-2/+15
* top level closures should work; transf is not a pass anymore; next steps for ...Araq2012-08-1313-222/+287
* borrow works with genericsAraq2012-08-063-41/+57
* make idetools output full pathsAraq2012-08-052-1/+8
* bugfix: dynlib for variablesAraq2012-08-051-2/+3
* idetools: '--def' now works in 'x.f'Araq2012-08-053-9/+9
* idetools: 'usages' and 'def' should work now; documented js backendAraq2012-08-0512-108/+171
* idetools: don't suggest module names or not visible object fieldsAraq2012-08-032-18/+21
* idetools improvements (2)Araq2012-08-033-1/+3
* idetools improvementsAraq2012-08-0310-35/+53
* Merge branch 'master' of github.com:Araq/NimrodAraq2012-08-022-9/+16
|\
| * DLL fixes for windows; fixes #175Zahary Karadjov2012-07-312-9/+16
* | idetools improvements; preparation of first class iterators; fixes #183Araq2012-08-0212-36/+95
|/
* made compiler more robust for idetools; implemented idetools.usagesAraq2012-07-3010-46/+94
* made compiler more robust for idetools supportAraq2012-07-3011-53/+94
* more improvements for idetoolsAraq2012-07-303-9/+15
* better error handling for better idetools supportAraq2012-07-2912-268/+377
* better overloading capabilities for 'shallowCopy'; little bugfix for 'system....Araq2012-07-292-12/+15
* improvements for idetools; system.compiles improvedAraq2012-07-296-32/+100
* added some support for HaikuAraq2012-07-292-2/+7
* more reliable NI/NU definitionsAraq2012-07-281-14/+3
* fixes #178Araq2012-07-285-13/+20
me the previous revision' href='/ahoang/Nim/blame/compiler/evaltempl.nim?h=devel&id=52d1a637b95f7cea95ad6e9591a941ef1be113cb'>^
48f73aaa1 ^





ed1d41c51 ^
48f73aaa1 ^



5e15dec17 ^
48f73aaa1 ^






5659a1662 ^
ed1d41c51 ^
af7c92c00 ^

48f73aaa1 ^
af7c92c00 ^
05724645f ^





5e15dec17 ^

02f97489b ^
bdcb72959 ^
02f97489b ^


ae60f4ae7 ^
fe124cead ^






5b27b263f ^
fe124cead ^
70ea45cdb ^
02f97489b ^
1d44fee39 ^
1be0022e7 ^
02f97489b ^
05724645f ^
d68181246 ^
1be0022e7 ^
05724645f ^
1be0022e7 ^

6fd4b8350 ^
02f97489b ^
a3f19c87f ^
02f97489b ^




221006096 ^
05724645f ^
2a7fc84c8 ^
0df8fa1dd ^
02f97489b ^
d68181246 ^
39290cf88 ^
02f97489b ^

5e15dec17 ^
7e15d5134 ^

5e15dec17 ^
ee67a67ac ^
91935fd91 ^


3e5e18bc5 ^
91935fd91 ^









ee67a67ac ^



91935fd91 ^
ee67a67ac ^
91935fd91 ^
05724645f ^
b07694cd9 ^


669a56449 ^

05724645f ^
5e15dec17 ^


05724645f ^
5e15dec17 ^


05724645f ^
b07694cd9 ^
5e15dec17 ^
d58212ccc ^
af7c92c00 ^
e87470597 ^
d58212ccc ^
af7c92c00 ^



05724645f ^
af7c92c00 ^


15c208cd2 ^
28394153a ^

f18b3af9d ^
af7c92c00 ^
8f4b37432 ^
ee67a67ac ^
e87470597 ^

669a56449 ^
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

 
                            
                                         







                                                   
                                                                    

    
                   
                            
                                                           
                       

                                                                         
                     
                  
 



                                                  
                                                                            





                                      
 


                     
                                                                    
                                                                                          
                                           

                                                                               
                                                                 
           
                                                                        
                                              
                    
                        



                                                                          
                                     




                                                                                
         
                                           
                                              
                                         





                                                                               
                                



                                                      
       






                                                                               
                                        
                              

                                                    
                                             
 





                                                                                 

                                                                       
                               
                           


           
                                                           






                                                                
                                 
                                                     
                                       
                                                    
                                                   
 
                                                         
                                                        
 
                                 
                                                                  

                             
                                      
                                   
                      




                                                         
                                                
                                                         
                                               
         
                                      
 
                              

                                                
 

                                                        
 
                                                                      


                      
                                                                      









                                                   



                                           
                  
                        
 
                                                     


                                          

                                                  
                                                                


                                                 
                                                     


                               
                   
             
                          
 
                         
                                                       
                  



                                               
                                                       


                                                         
                                            

                          
                                 
                                                      
                                  
                                                

                                                           
                               
#
#
#           The Nim Compiler
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Template evaluation engine. Now hygienic.

import
  strutils, options, ast, astalgo, msgs, renderer, lineinfos, idents

type
  TemplCtx = object
    owner, genSymOwner: PSym
    instLines: bool   # use the instantiation lines numbers
    isDeclarative: bool
    mapping: TIdTable # every gensym'ed symbol needs to be mapped to some
                      # new symbol
    config: ConfigRef
    ic: IdentCache

proc copyNode(ctx: TemplCtx, a, b: PNode): PNode =
  result = copyNode(a)
  if ctx.instLines: result.info = b.info

proc evalTemplateAux(templ, actual: PNode, c: var TemplCtx, result: PNode) =
  template handleParam(param) =
    let x = param
    if x.kind == nkArgList:
      for y in items(x): result.add(y)
    else:
      result.add copyTree(x)

  case templ.kind
  of nkSym:
    var s = templ.sym
    if (s.owner == nil and s.kind == skParam) or s.owner == c.owner:
      if s.kind == skParam and {sfGenSym, sfTemplateParam} * s.flags == {sfTemplateParam}:
        handleParam actual.sons[s.position]
      elif (s.owner != nil) and (s.kind == skGenericParam or
           s.kind == skType and s.typ != nil and s.typ.kind == tyGenericParam):
        handleParam actual.sons[s.owner.typ.len + s.position - 1]
      else:
        internalAssert c.config, sfGenSym in s.flags or s.kind == skType
        var x = PSym(idTableGet(c.mapping, s))
        if x == nil:
          x = copySym(s)
          # sem'check needs to set the owner properly later, see bug #9476
          x.owner = nil # c.genSymOwner
          #if x.kind == skParam and x.owner.kind == skModule:
          #  internalAssert c.config, false
          idTablePut(c.mapping, s, x)
        if sfGenSym in s.flags and optNimV019 notin c.config.globalOptions:
          result.add newIdentNode(getIdent(c.ic, x.name.s & "`gensym" & $x.id),
            if c.instLines: actual.info else: templ.info)
        else:
          result.add newSymNode(x, if c.instLines: actual.info else: templ.info)
    else:
      result.add copyNode(c, templ, actual)
  of nkNone..nkIdent, nkType..nkNilLit: # atom
    result.add copyNode(c, templ, actual)
  of nkCommentStmt:
    # for the documentation generator we don't keep documentation comments
    # in the AST that would confuse it (bug #9432), but only if we are not in a
    # "declarative" context (bug #9235).
    if c.isDeclarative:
      var res = copyNode(c, templ, actual)
      for i in 0 ..< len(templ):
        evalTemplateAux(templ.sons[i], actual, c, res)
      result.add res
    else:
      result.add newNodeI(nkEmpty, templ.info)
  else:
    var isDeclarative = false
    if templ.kind in {nkProcDef, nkFuncDef, nkMethodDef, nkIteratorDef,
                      nkMacroDef, nkTemplateDef, nkConverterDef, nkTypeSection,
                      nkVarSection, nkLetSection, nkConstSection} and
        not c.isDeclarative:
      c.isDeclarative = true
      isDeclarative = true
    var res = copyNode(c, templ, actual)
    for i in 0 ..< len(templ):
      evalTemplateAux(templ.sons[i], actual, c, res)
    result.add res
    if isDeclarative: c.isDeclarative = false

const
  errWrongNumberOfArguments = "wrong number of arguments"
  errMissingGenericParamsForTemplate = "'$1' has unspecified generic parameters"
  errTemplateInstantiationTooNested = "template instantiation too nested"

proc evalTemplateArgs(n: PNode, s: PSym; conf: ConfigRef; fromHlo: bool): PNode =
  # if the template has zero arguments, it can be called without ``()``
  # `n` is then a nkSym or something similar
  var totalParams = case n.kind
    of nkCallKinds: n.len-1
    else: 0

  var
    # XXX: Since immediate templates are not subject to the
    # standard sigmatching algorithm, they will have a number
    # of deficiencies when it comes to generic params:
    # Type dependencies between the parameters won't be honoured
    # and the bound generic symbols won't be resolvable within
    # their bodies. We could try to fix this, but it may be
    # wiser to just deprecate immediate templates and macros
    # now that we have working untyped parameters.
    genericParams = if fromHlo: 0
                    else: s.ast[genericParamsPos].len
    expectedRegularParams = s.typ.len-1
    givenRegularParams = totalParams - genericParams
  if givenRegularParams < 0: givenRegularParams = 0

  if totalParams > expectedRegularParams + genericParams:
    globalError(conf, n.info, errWrongNumberOfArguments)

  if totalParams < genericParams:
    globalError(conf, n.info, errMissingGenericParamsForTemplate %
                n.renderTree)

  result = newNodeI(nkArgList, n.info)
  for i in 1 .. givenRegularParams:
    result.addSon n[i]

  # handle parameters with default values, which were
  # not supplied by the user
  for i in givenRegularParams+1 .. expectedRegularParams:
    let default = s.typ.n.sons[i].sym.ast
    if default.isNil or default.kind == nkEmpty:
      localError(conf, n.info, errWrongNumberOfArguments)
      addSon(result, newNodeI(nkEmpty, n.info))
    else:
      addSon(result, default.copyTree)

  # add any generic parameters
  for i in 1 .. genericParams:
    result.addSon n.sons[givenRegularParams + i]

# to prevent endless recursion in template instantiation
const evalTemplateLimit* = 1000

proc wrapInComesFrom*(info: TLineInfo; sym: PSym; res: PNode): PNode =
  when true:
    result = res
    result.info = info
    if result.kind in {nkStmtList, nkStmtListExpr} and result.len > 0:
      result.lastSon.info = info
    when false:
      # this hack is required to
      var x = result
      while x.kind == nkStmtListExpr: x = x.lastSon
      if x.kind in nkCallKinds:
        for i in 1..<x.len:
          if x[i].kind in nkCallKinds:
            x.sons[i].info = info
  else:
    result = newNodeI(nkStmtListExpr, info)
    var d = newNodeI(nkComesFrom, info)
    d.add newSymNode(sym, info)
    result.add d
    result.add res
    result.typ = res.typ

proc evalTemplate*(n: PNode, tmpl, genSymOwner: PSym;
                   conf: ConfigRef;
                   ic: IdentCache;
                   fromHlo=false): PNode =
  inc(conf.evalTemplateCounter)
  if conf.evalTemplateCounter > evalTemplateLimit:
    globalError(conf, n.info, errTemplateInstantiationTooNested)
    result = n

  # replace each param by the corresponding node:
  var args = evalTemplateArgs(n, tmpl, conf, fromHlo)
  var ctx: TemplCtx
  ctx.owner = tmpl
  ctx.genSymOwner = genSymOwner
  ctx.config = conf
  ctx.ic = ic
  initIdTable(ctx.mapping)

  let body = tmpl.getBody
  #echo "instantion of ", renderTree(body, {renderIds})
  if isAtom(body):
    result = newNodeI(nkPar, body.info)
    evalTemplateAux(body, args, ctx, result)
    if result.len == 1: result = result.sons[0]
    else:
      localError(conf, result.info, "illformed AST: " &
                  renderTree(result, {renderNoComments}))
  else:
    result = copyNode(body)
    ctx.instLines = sfCallsite in tmpl.flags
    if ctx.instLines:
      result.info = n.info
    for i in 0 ..< safeLen(body):
      evalTemplateAux(body.sons[i], args, ctx, result)
  result.flags.incl nfFromTemplate
  result = wrapInComesFrom(n.info, tmpl, result)
  #if ctx.debugActive:
  #  echo "instantion of ", renderTree(result, {renderIds})
  dec(conf.evalTemplateCounter)