summary refs log tree commit diff stats
path: root/compiler/platform.nim
Commit message (Expand)AuthorAgeFilesLines
* move assertions out of system (#19599)flywind2022-03-231-0/+4
* Add support for LoongArch (#19223)wenghongquan2021-12-091-2/+3
* Add Zephyr Support (#19003)Jaremy Creechley2021-10-241-1/+5
* Add Elbrus 2000 architecture (#19024)Danil Yarantsev2021-10-211-2/+4
* ported Nim to proprietary CrossOS [backport] (#18889)Andreas Rumpf2021-09-241-1/+5
* closes #18433 (#18484)Andreas Rumpf2021-07-131-4/+2
* let's assume littleEndian in JS backend (#16886)flywind2021-02-011-1/+1
* some comments for amd64 and nimvm CPU (#16756)Timothee Cour2021-01-201-1/+2
* Add 32-bit RISC-V support (#16231)Alf-André Walla2020-12-031-1/+2
* Fixing issue #15302 -- lwip doesn't support signals (#15303)Jaremy Creechley2020-09-141-1/+1
* Changes for FreeRTOS/LwIP Port for the ESP32 (ESP-IDF) (#15250)Jaremy Creechley2020-08-311-2/+7
* Added 'ansic' os support for minimal (embedded) targets (#13088)Ico Doornekamp2020-01-151-1/+5
* Cosmetic compiler cleanup (#12718)Clyybber2019-11-281-4/+4
* Add build support for Linux/hppa (#12271)John Paul Adrian Glaubitz2019-09-261-3/+4
* Add --os:ios switch and docs. (#12204)treeform2019-09-181-1/+5
* styleCheck: make the compiler and large parts of the stdlib compatible with -...Araq2019-07-101-1/+1
* list available options for --cpu, --os and --cc if the passed option is not f...Ico Doornekamp2019-05-251-0/+8
* Replace countup(x, y) with x .. yClyybber2019-05-071-2/+2
* add wasm32 platformJacek Sieka2019-03-031-2/+3
* fixes #8081Araq2018-09-221-4/+3
* Nintendo switch support (#8069)Joey2018-06-271-2/+7
* rename 'nimrodVM' to 'nimVM'Andreas Rumpf2018-05-281-4/+4
* platform.nim doesn't use globals anymore; prepare msgs.nim for not using globalsAndreas Rumpf2018-05-181-28/+24
* Add RISC-V (riscv64) support (#7417)Federico Ceratto2018-04-071-2/+3
* Introduce first class support for Android (#5772)Fredrik Høisæther Rasch2017-08-061-2/+6
* Add mips64 and mips64el CPU platforms (#5866)James Cowgill2017-08-031-2/+5
* support for the Genode OS framework (#5560)Emery Hemingway2017-03-311-1/+6
* Make DragonFlyBSD a first class BSD-like OS (#5548)Eugene Kabanov2017-03-161-2/+9
* define sparc64 platformSergey Avseyev2016-07-211-2/+3
* nimrod -> nimErik Johansson Andersson2016-02-051-1/+1
* add msp430 cpu supportKeerthan Jaic2015-08-261-2/+3
* Add arm64 support (untested)def2015-07-311-1/+2
* Add powerpc64el support (untested)def2015-07-311-2/+3
* Add Mipsel CPU support (untested)def2015-07-311-1/+2
* codegen doesn't produce line tracing commands anymore; fixes #1344Araq2015-03-211-142/+142
* merged #2083 manuallyAraq2015-02-081-1/+1
* the compiler knows vxWorks is an OSAraq2015-01-281-3/+7
* minor improvement to the barrier implementationAraq2014-12-091-1/+1
* case consistency part 4Araq2013-12-271-4/+4
* case consistency part 1Araq2013-12-271-6/+6
* --os:standalone works againAraq2013-06-301-1/+1
* Removes executable bit for text files.Grzegorz Adam Hankiewicz2013-03-161-0/+0
* EcmaScript => JS. Fixes #330Simon Hafner2013-02-151-4/+4
* added some support for HaikuAraq2012-07-291-1/+5
* implemented support for the AVR CPU and standalone OSAraq2012-03-311-4/+10
* year 2012 for most copyright headersAraq2012-01-021-1/+1
* new compiler option tlsEmulationAraq2011-10-271-5/+5
* first steps to thread local heapsAraq2011-06-021-2/+3
* big repo cleanupAraq2011-04-121-0/+213
rtial bootstraps on windows' href='/ahoang/Nim/commit/compiler/importer.nim?h=devel&id=b731e6ef1c4f10b9ba544c0a66ea1066b3c471a8'>b731e6ef1 ^
c98e3d2c2 ^

92b8fac94 ^
c98e3d2c2 ^
3b7ef2288 ^
c98e3d2c2 ^

3b7ef2288 ^
a541be893 ^

e25474154 ^


a541be893 ^
92b8fac94 ^
c98e3d2c2 ^

e25474154 ^
c98e3d2c2 ^
92b8fac94 ^
c98e3d2c2 ^
e25474154 ^
1786e3099 ^



c98e3d2c2 ^
a541be893 ^
10e23e915 ^
92b8fac94 ^
b595fc834 ^
92b8fac94 ^
b595fc834 ^

b731e6ef1 ^
92b8fac94 ^
b595fc834 ^
b731e6ef1 ^
862c0ef83 ^
b595fc834 ^

92b8fac94 ^
b595fc834 ^
b731e6ef1 ^
b595fc834 ^
92b8fac94 ^
b595fc834 ^
e25474154 ^
594674797 ^
e25474154 ^
92b8fac94 ^
92e10e4b1 ^


b731e6ef1 ^
92b8fac94 ^
c98e3d2c2 ^
92e10e4b1 ^
92b8fac94 ^
e25474154 ^
c98e3d2c2 ^
594674797 ^
c98e3d2c2 ^

594674797 ^
c98e3d2c2 ^












a541be893 ^
c98e3d2c2 ^

83584bf88 ^






fa7754731 ^
83584bf88 ^
a541be893 ^
83584bf88 ^


27585ee6e ^
849484f92 ^
83584bf88 ^
92b8fac94 ^
83584bf88 ^
a541be893 ^
e25474154 ^
594674797 ^
a541be893 ^
83584bf88 ^

7d6500f1d ^

c98e3d2c2 ^
2c5743db3 ^
e25474154 ^
a541be893 ^
e25474154 ^

83584bf88 ^

7d6500f1d ^

83584bf88 ^
1c9b4e5d3 ^

92e10e4b1 ^
a541be893 ^
92e10e4b1 ^

83584bf88 ^

92e10e4b1 ^


a541be893 ^
10e23e915 ^
92e10e4b1 ^

2c5743db3 ^
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

 
                            
                                         






                                                        
      
                                                                               
                           


                                              
 





                                                                       
                                       



                         
                       






                                                                             

                                                           
               
                                                                


                                     
                                                                                
               
 
                                        
                                                                  
                                  
                                                          
                       
                                                     


                                 
 
                                            

                                                                            
                                                       
                                                        

                                       
                                         

                                        

                                                                          
                                       
                      
                    

                                                              
                                  

                                                  


                                                                       
                          
                                                                

                              
                   
                 
                                                          
                    
                               



                                                              
 
                                                         
                                            
                                         
              
                                                        

                                    
                                       
                                              
                                                       
               
                   

                                                           
                                                    
                     
                                                                           
                             
                                          
                               
 
                                                                            
                 
                                     


                               
                                           
                                                               
                                                        
                               
                                
 
                                                    
                       

                                               
                                                                












                                                                       
                             

                                              






                                                                          
                                                                            
 
                                                  


                                                          
                                                 
                                                                       
                                    
                                                    
 
                                               
            
                      
                                      

                                        

                                                                      
                                            
                                          
 
                                             

                       

                                      

                                                                      
                                        

                                     
 
                                                      

                       

                                      


                                                                      
                                        
                                                        

                                           
                                         
#
#
#           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 symbol importing mechanism.

import
  intsets, strutils, os, ast, astalgo, msgs, options, idents, rodread, lookups,
  semdata, passes, renderer

proc evalImport*(c: PContext, n: PNode): PNode
proc evalFrom*(c: PContext, n: PNode): PNode

proc getModuleName*(n: PNode): string =
  # This returns a short relative module name without the nim extension
  # e.g. like "system", "importer" or "somepath/module"
  # The proc won't perform any checks that the path is actually valid
  case n.kind
  of nkStrLit, nkRStrLit, nkTripleStrLit:
    result = unixToNativePath(n.strVal)
  of nkIdent:
    result = n.ident.s
  of nkSym:
    result = n.sym.name.s
  of nkInfix, nkPrefix:
    if n.sons[0].kind == nkIdent and n.sons[0].ident.id == getIdent("as").id:
      # XXX hack ahead:
      n.kind = nkImportAs
      n.sons[0] = n.sons[1]
      n.sons[1] = n.sons[2]
      n.sons.setLen(2)
      return getModuleName(n.sons[0])
    # hacky way to implement 'x / y /../ z':
    result = renderTree(n, {renderNoComments}).replace(" ")
  of nkDotExpr:
    result = renderTree(n, {renderNoComments}).replace(".", "/")
  of nkImportAs:
    result = getModuleName(n.sons[0])
  else:
    localError(n.info, errGenerated, "invalid module name: '$1'" % n.renderTree)
    result = ""

proc checkModuleName*(n: PNode): int32 =
  # This returns the full canonical path for a given module import
  let modulename = n.getModuleName
  let fullPath = findModule(modulename, n.info.toFullPath)
  if fullPath.len == 0:
    localError(n.info, errCannotOpenFile, modulename)
    result = InvalidFileIDX
  else:
    result = fullPath.fileInfoIdx

proc rawImportSymbol(c: PContext, s: PSym) =
  # This does not handle stubs, because otherwise loading on demand would be
  # pointless in practice. So importing stubs is fine here!
  # check if we have already a symbol of the same name:
  var check = strTableGet(c.importTable.symbols, s.name)
  if check != nil and check.id != s.id:
    if s.kind notin OverloadableSyms:
      # s and check need to be qualified:
      incl(c.ambiguousSymbols, s.id)
      incl(c.ambiguousSymbols, check.id)
  # thanks to 'export' feature, it could be we import the same symbol from
  # multiple sources, so we need to call 'StrTableAdd' here:
  strTableAdd(c.importTable.symbols, s)
  if s.kind == skType:
    var etyp = s.typ
    if etyp.kind in {tyBool, tyEnum} and sfPure notin s.flags:
      for j in countup(0, sonsLen(etyp.n) - 1):
        var e = etyp.n.sons[j].sym
        if e.kind != skEnumField:
          internalError(s.info, "rawImportSymbol")
          # BUGFIX: because of aliases for enums the symbol may already
          # have been put into the symbol table
          # BUGFIX: but only iff they are the same symbols!
        var it: TIdentIter
        check = initIdentIter(it, c.importTable.symbols, e.name)
        while check != nil:
          if check.id == e.id:
            e = nil
            break
          check = nextIdentIter(it, c.importTable.symbols)
        if e != nil:
          rawImportSymbol(c, e)
  else:
    # rodgen assures that converters and patterns are no stubs
    if s.kind == skConverter: addConverter(c, s)
    if hasPattern(s): addPattern(c, s)

proc importSymbol(c: PContext, n: PNode, fromMod: PSym) =
  let ident = lookups.considerQuotedIdent(n)
  let s = strTableGet(fromMod.tab, ident)
  if s == nil:
    localError(n.info, errUndeclaredIdentifier, ident.s)
  else:
    if s.kind == skStub: loadStub(s)
    if s.kind notin ExportableSymKinds:
      internalError(n.info, "importSymbol: 2")
    # for an enumeration we have to add all identifiers
    case s.kind
    of skProcKinds:
      # for a overloadable syms add all overloaded routines
      var it: TIdentIter
      var e = initIdentIter(it, fromMod.tab, s.name)
      while e != nil:
        if e.name.id != s.name.id: internalError(n.info, "importSymbol: 3")
        rawImportSymbol(c, e)
        e = nextIdentIter(it, fromMod.tab)
    else: rawImportSymbol(c, s)

proc importAllSymbolsExcept(c: PContext, fromMod: PSym, exceptSet: IntSet) =
  var i: TTabIter
  var s = initTabIter(i, fromMod.tab)
  while s != nil:
    if s.kind != skModule:
      if s.kind != skEnumField:
        if s.kind notin ExportableSymKinds:
          internalError(s.info, "importAllSymbols: " & $s.kind)
        if exceptSet.empty or s.name.id notin exceptSet:
          rawImportSymbol(c, s)
    s = nextIter(i, fromMod.tab)

proc importAllSymbols*(c: PContext, fromMod: PSym) =
  var exceptSet: IntSet
  importAllSymbolsExcept(c, fromMod, exceptSet)

proc importForwarded(c: PContext, n: PNode, exceptSet: IntSet) =
  if n.isNil: return
  case n.kind
  of nkExportStmt:
    for a in n:
      assert a.kind == nkSym
      let s = a.sym
      if s.kind == skModule:
        importAllSymbolsExcept(c, s, exceptSet)
      elif exceptSet.empty or s.name.id notin exceptSet:
        rawImportSymbol(c, s)
  of nkExportExceptStmt:
    localError(n.info, errGenerated, "'export except' not implemented")
  else:
    for i in 0..safeLen(n)-1:
      importForwarded(c, n.sons[i], exceptSet)

proc importModuleAs(n: PNode, realModule: PSym): PSym =
  result = realModule
  if n.kind != nkImportAs: discard
  elif n.len != 2 or n.sons[1].kind != nkIdent:
    localError(n.info, errGenerated, "module alias must be an identifier")
  elif n.sons[1].ident.id != realModule.name.id:
    # some misguided guy will write 'import abc.foo as foo' ...
    result = createModuleAlias(realModule, n.sons[1].ident, realModule.info)

proc myImportModule(c: PContext, n: PNode): PSym =
  var f = checkModuleName(n)
  if f != InvalidFileIDX:
    result = importModuleAs(n, gImportModule(c.module, f))
    if result.info.fileIndex == n.info.fileIndex:
      localError(n.info, errGenerated, "A module cannot import itself")
    if sfDeprecated in result.flags:
      message(n.info, warnDeprecated, result.name.s)

proc evalImport(c: PContext, n: PNode): PNode =
  result = n
  var emptySet: IntSet
  for i in countup(0, sonsLen(n) - 1):
    var m = myImportModule(c, n.sons[i])
    if m != nil:
      # ``addDecl`` needs to be done before ``importAllSymbols``!
      addDecl(c, m)             # add symbol to symbol table of module
      importAllSymbolsExcept(c, m, emptySet)
      #importForwarded(c, m.ast, emptySet)

proc evalFrom(c: PContext, n: PNode): PNode =
  result = n
  checkMinSonsLen(n, 2)
  var m = myImportModule(c, n.sons[0])
  if m != nil:
    n.sons[0] = newSymNode(m)
    addDecl(c, m)               # add symbol to symbol table of module
    for i in countup(1, sonsLen(n) - 1):
      if n.sons[i].kind != nkNilLit:
        importSymbol(c, n.sons[i], m)

proc evalImportExcept*(c: PContext, n: PNode): PNode =
  result = n
  checkMinSonsLen(n, 2)
  var m = myImportModule(c, n.sons[0])
  if m != nil:
    n.sons[0] = newSymNode(m)
    addDecl(c, m)               # add symbol to symbol table of module
    var exceptSet = initIntSet()
    for i in countup(1, sonsLen(n) - 1):
      let ident = lookups.considerQuotedIdent(n.sons[i])
      exceptSet.incl(ident.id)
    importAllSymbolsExcept(c, m, exceptSet)
    #importForwarded(c, m.ast, exceptSet)