summary refs log tree commit diff stats
path: root/doc/manual/stmts.txt
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2015-09-10 13:18:11 +0200
committerAraq <rumpf_a@web.de>2015-09-12 11:07:46 +0200
commit3959a59e57d43a41b5ff6929875d53c46fd8f78a (patch)
tree4d8efa8f6eaa09dc3252e13f679277c9e0b0a330 /doc/manual/stmts.txt
parentac6b21a018cc5873a8ec5011bbc0068989e12259 (diff)
downloadNim-3959a59e57d43a41b5ff6929875d53c46fd8f78a.tar.gz
documented void context
Diffstat (limited to 'doc/manual/stmts.txt')
-rw-r--r--doc/manual/stmts.txt18
1 files changed, 18 insertions, 0 deletions
diff --git a/doc/manual/stmts.txt b/doc/manual/stmts.txt
index 062c08d7c..a833d7b7d 100644
--- a/doc/manual/stmts.txt
+++ b/doc/manual/stmts.txt
@@ -60,6 +60,24 @@ An empty ``discard`` statement is often used as a null statement:
     else: discard
 
 
+Void context
+------------
+
+In a list of statements every expression except the last one needs to have the
+type ``void``. In addition to this rule an assignment to the builtin ``result``
+symbol also triggers a ``void`` context:
+
+.. code-block:: nim
+  proc invalid*(): string =
+    result = "foo"
+    "invalid"  # Error: value of type 'string' has to be discarded
+
+.. code-block:: nim
+  proc valid*(): string =
+    let x = 317
+    "valid"
+
+
 Var statement
 -------------
 
previous revision' href='/ahoang/Nim/blame/compiler/procfind.nim?h=devel&id=9e92455a534956bfbb0a7ec5e6f2bdffd7268818'>^
b0c11d3ef ^
0761b449e ^


a27eb5153 ^
0761b449e ^
2df9b442c ^
e25474154 ^

e3f53409f ^
f44a4362b ^

ce23b814a ^
e25474154 ^
b731e6ef1 ^
0761b449e ^




b731e6ef1 ^
0761b449e ^

92b8fac94 ^

0761b449e ^


92b8fac94 ^
0761b449e ^

b731e6ef1 ^
e25474154 ^
a1f677980 ^
0761b449e ^

92b8fac94 ^
0761b449e ^

73c6efdf6 ^
92b8fac94 ^
e25474154 ^
e3f53409f ^






55c78af9c ^








e3f53409f ^














ce23b814a ^












5cd933a44 ^
2df9b442c ^
ce23b814a ^


f317807a8 ^

ce23b814a ^





f317807a8 ^
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

 
                            
                                         







                                                               
      

                                           



                                                    
                                                     
            
                                   
                                                     
            


                                
                                                                      
                                     
                                                           

               
                                                                    

                                                              
                                                               
                    
                                                    




                                                                          
                                                             

                                                    

                                                               


                                                               
                                               

                        
                                                                 
                                                
                       

                              
                                                            

                          
                 
                                               
 






                                                                   








                                                                          














                                                                 












                                                                    
 
                                                                              


                                                                                 

                                                        





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

# This module implements the searching for procs and iterators.
# This is needed for proper handling of forward declarations.

import
  ast, astalgo, msgs, semdata, types, trees

proc equalGenericParams(procA, procB: PNode): bool =
  if sonsLen(procA) != sonsLen(procB): return
  for i in countup(0, sonsLen(procA) - 1):
    if procA.sons[i].kind != nkSym:
      internalError(procA.info, "equalGenericParams")
      return
    if procB.sons[i].kind != nkSym:
      internalError(procB.info, "equalGenericParams")
      return
    let a = procA.sons[i].sym
    let b = procB.sons[i].sym
    if a.name.id != b.name.id or
        not sameTypeOrNil(a.typ, b.typ, {ExactTypeDescValues}): return
    if a.ast != nil and b.ast != nil:
      if not exprStructuralEquivalent(a.ast, b.ast): return
  result = true

proc searchForProcOld*(c: PContext, scope: PScope, fn: PSym): PSym =
  # Searchs for a forward declaration or a "twin" symbol of fn
  # in the symbol table. If the parameter lists are exactly
  # the same the sym in the symbol table is returned, else nil.
  var it: TIdentIter
  result = initIdentIter(it, scope.symbols, fn.name)
  if isGenericRoutine(fn):
    # we simply check the AST; this is imprecise but nearly the best what
    # can be done; this doesn't work either though as type constraints are
    # not kept in the AST ..
    while result != nil:
      if result.kind == fn.kind and isGenericRoutine(result):
        let genR = result.ast.sons[genericParamsPos]
        let genF = fn.ast.sons[genericParamsPos]
        if exprStructuralEquivalent(genR, genF) and
           exprStructuralEquivalent(result.ast.sons[paramsPos],
                                    fn.ast.sons[paramsPos]) and
           equalGenericParams(genR, genF):
            return
      result = nextIdentIter(it, scope.symbols)
  else:
    while result != nil:
      if result.kind == fn.kind and not isGenericRoutine(result):
        case equalParams(result.typ.n, fn.typ.n)
        of paramsEqual:
          return
        of paramsIncompatible:
          localError(fn.info, errNotOverloadable, fn.name.s)
          return
        of paramsNotEqual:
          discard
      result = nextIdentIter(it, scope.symbols)

proc searchForProcNew(c: PContext, scope: PScope, fn: PSym): PSym =
  const flags = {ExactGenericParams, ExactTypeDescValues,
                 ExactConstraints, IgnoreCC}

  var it: TIdentIter
  result = initIdentIter(it, scope.symbols, fn.name)
  while result != nil:
    if result.kind in skProcKinds and sameType(result.typ, fn.typ, flags):
      case equalParams(result.typ.n, fn.typ.n)
      of paramsEqual:
        return
      of paramsIncompatible:
        localError(fn.info, errNotOverloadable, fn.name.s)
        return
      of paramsNotEqual:
        discard

    result = nextIdentIter(it, scope.symbols)
  
  return nil

proc searchForProc*(c: PContext, scope: PScope, fn: PSym): PSym =
  result = searchForProcNew(c, scope, fn)
  when false:
    let old = searchForProcOld(c, scope, fn)
    if old != result:
      echo "Mismatch in searchForProc: ", fn.info
      debug fn.typ
      debug if result != nil: result.typ else: nil
      debug if old != nil: old.typ else: nil
 
when false:
  proc paramsFitBorrow(child, parent: PNode): bool = 
    var length = sonsLen(child)
    result = false
    if length == sonsLen(parent): 
      for i in countup(1, length - 1): 
        var m = child.sons[i].sym
        var n = parent.sons[i].sym
        assert((m.kind == skParam) and (n.kind == skParam))
        if not compareTypes(m.typ, n.typ, dcEqOrDistinctOf): return 
      if not compareTypes(child.sons[0].typ, parent.sons[0].typ,
                          dcEqOrDistinctOf): return
      result = true

  proc searchForBorrowProc*(c: PContext, startScope: PScope, fn: PSym): PSym =
    # Searchs for the fn in the symbol table. If the parameter lists are suitable
    # for borrowing the sym in the symbol table is returned, else nil.
    var it: TIdentIter
    for scope in walkScopes(startScope):
      result = initIdentIter(it, scope.symbols, fn.Name)
      while result != nil: 
        # watchout! result must not be the same as fn!
        if (result.Kind == fn.kind) and (result.id != fn.id): 
          if equalGenericParams(result.ast.sons[genericParamsPos], 
                                fn.ast.sons[genericParamsPos]): 
            if paramsFitBorrow(fn.typ.n, result.typ.n): return 
        result = NextIdentIter(it, scope.symbols)