summary refs log tree commit diff stats
path: root/tests/lookups
Commit message (Expand)AuthorAgeFilesLines
* new .redefine pragma for templates, warn on redefinition without it (#20211)metagn2022-08-231-1/+1
* make megatest consistent with unjoined tests wrt newlines, honor newlines in ...Timothee Cour2020-11-281-1/+1
* implement the 'bind' statement for generics, it was an oversight that this wa...Andreas Rumpf2020-06-071-0/+17
* Allow void macro result (#11286)Arne Döring2019-05-211-1/+1
* updated tests to be executedArne Döring2018-11-231-1/+7
* delete old cruftArne Döring2018-11-231-4/+0
* fixes #4353Andreas Rumpf2016-08-041-0/+4
* fixes #4555Andreas Rumpf2016-08-021-0/+17
* tests: Trim .nim files trailing whitespaceAdam Strzelecki2015-09-042-3/+3
* make tests greenAraq2014-08-311-1/+1
* Changed tests and tools to use 'discard' statements instead of 'nil' for empt...Clay Sweetser2014-02-151-7/+7
* tester support html generationAraq2014-01-141-0/+29
* new tester; all tests categorizedAraq2014-01-131-0/+17
raq <rumpf_a@web.de> 2018-04-03 23:41:20 +0200 committer Araq <rumpf_a@web.de> 2018-04-03 23:41:20 +0200 implemented v2 string literals' href='/ahoang/Nim/commit/compiler/ccgliterals.nim?h=devel&id=f108e89670d5f090819cfffbce6ba0b0539ea68a'>f108e8967 ^
32afdc09c ^

f108e8967 ^


32afdc09c ^
f485ebe16 ^
f108e8967 ^

d83736221 ^
f485ebe16 ^











d83736221 ^






1284827df ^
d83736221 ^












1284827df ^
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
















                                                                       
                                                   



































                                                                             
                         
                                                
                                                          
                                       
                                              

                                                     

                                                       

                                     


                                                         
                                                                                    
                                                 

                                 
 











                                                                       






                                                                             
                                                                                         












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

## This include file contains the logic to produce constant string
## and seq literals. The code here is responsible that
## ``const x = ["a", "b"]`` works without hidden runtime creation code.
## The price is that seqs and strings are not purely a library
## implementation.

template detectVersion(field, corename) =
  if m.g.field == 0:
    let core = getCompilerProc(m.g.graph, corename)
    if core == nil or core.kind != skConst:
      m.g.field = 1
    else:
      m.g.field = int ast.getInt(core.ast)
  result = m.g.field

proc detectStrVersion(m: BModule): int =
  detectVersion(strVersion, "nimStrVersion")

proc detectSeqVersion(m: BModule): int =
  detectVersion(seqVersion, "nimSeqVersion")

# ----- Version 1: GC'ed strings and seqs --------------------------------

proc genStringLiteralDataOnlyV1(m: BModule, s: string): Rope =
  discard cgsym(m, "TGenericSeq")
  result = getTempName(m)
  addf(m.s[cfsData], "STRING_LITERAL($1, $2, $3);$n",
       [result, makeCString(s), rope(len(s))])

proc genStringLiteralV1(m: BModule; n: PNode): Rope =
  if s.isNil:
    result = ropecg(m, "((#NimStringDesc*) NIM_NIL)", [])
  else:
    let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
    if id == m.labels:
      # string literal not found in the cache:
      result = ropecg(m, "((#NimStringDesc*) &$1)",
                      [genStringLiteralDataOnlyV1(m, n.strVal)])
    else:
      result = ropecg(m, "((#NimStringDesc*) &$1$2)",
                      [m.tmpBase, rope(id)])

# ------ Version 2: destructor based strings and seqs -----------------------

proc genStringLiteralDataOnlyV2(m: BModule, s: string): Rope =
  result = getTempName(m)
  addf(m.s[cfsData], "static const struct {$n" &
       "  NI cap; void* allocator; NIM_CHAR data[$2];$n" &
       "} $1 = { $2, NIM_NIL, $3 };$n",
       [result, rope(len(s)), makeCString(s)])

proc genStringLiteralV2(m: BModule; n: PNode): Rope =
  let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
  if id == m.labels:
    discard cgsym(m, "NimStrPayload")
    discard cgsym(m, "NimStringV2")
    # string literal not found in the cache:
    let pureLit = genStringLiteralDataOnlyV2(m, n.strVal)
    result = getTempName(m)
    addf(m.s[cfsData], "static const NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n",
          [result, rope(len(n.strVal)), pureLit])
  else:
    result = m.tmpBase & rope(id)

proc genStringLiteralV2Const(m: BModule; n: PNode): Rope =
  let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
  var pureLit: Rope
  if id == m.labels:
    discard cgsym(m, "NimStrPayload")
    discard cgsym(m, "NimStringV2")
    # string literal not found in the cache:
    pureLit = genStringLiteralDataOnlyV2(m, n.strVal)
  else:
    pureLit = m.tmpBase & rope(id)
  result = "{$1, (NimStrPayload*)&$2}" % [rope(len(n.strVal)), pureLit]

# ------ Version selector ---------------------------------------------------

proc genStringLiteralDataOnly(m: BModule; s: string; info: TLineInfo): Rope =
  case detectStrVersion(m)
  of 0, 1: result = genStringLiteralDataOnlyV1(m, s)
  of 2: result = genStringLiteralDataOnlyV2(m, s)
  else:
    localError(m.config, info, "cannot determine how to produce code for string literal")

proc genStringLiteralFromData(m: BModule; data: Rope; info: TLineInfo): Rope =
  result = ropecg(m, "((#NimStringDesc*) &$1)",
                [data])

proc genNilStringLiteral(m: BModule; info: TLineInfo): Rope =
  result = ropecg(m, "((#NimStringDesc*) NIM_NIL)", [])

proc genStringLiteral(m: BModule; n: PNode): Rope =
  case detectStrVersion(m)
  of 0, 1: result = genStringLiteralV1(m, n)
  of 2: result = genStringLiteralV2(m, n)
  else:
    localError(m.config, n.info, "cannot determine how to produce code for string literal")