summary refs log blame commit diff stats
path: root/tests/accept/compile/tforwardgeneric.nim
blob: 84bef15cc8d27f34af4aa08f957b6c1af83e2654 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                     
discard """
  output: "1.0000000000000000e+00 10"
"""

proc p[T](a, b: T): T

echo p(0.9, 0.1), " ", p(9, 1)

proc p[T](a, b: T): T =
  result  = a + b
>66a7e3d37 ^
ddaedab83 ^
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266


                                
                                          







































                                                                            










                                                                               
                             

                                                                     

                                                          

                                                                           

      


                                             




































































                                                                     
                                                    

























                                                                   
                                      





                                               
                             
                                   


























































































                                                                               

                          
    
//
//
//           The Nimrod Compiler
//        (c) Copyright 2009 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit semdata;

// This module contains the data structures for the semantic checking phase.

interface

{$include 'config.inc'}

uses
  sysutils, nsystem, charsets, strutils,
  lists, options, scanner, ast, astalgo, trees, treetab, wordrecg,
  ropes, msgs, platform, nos, condsyms, idents, rnimsyn, types,
  extccomp, nmath, magicsys, nversion, nimsets, pnimsyn, ntime, passes,
  rodread;

type
  TOptionEntry = object(lists.TListEntry)
    // entries to put on a stack for pragma parsing
    options: TOptions;
    defaultCC: TCallingConvention;
    dynlib: PLib;
    Notes: TNoteKinds;
  end;
  POptionEntry = ^TOptionEntry;

  TProcCon = record          // procedure context; also used for top-level
                             // statements
    owner: PSym;             // the symbol this context belongs to
    resultSym: PSym;         // the result symbol (if we are in a proc)
    nestedLoopCounter: int;  // whether we are in a loop or not
    nestedBlockCounter: int; // whether we are in a block or not
  end;
  PProcCon = ^TProcCon;

  PContext = ^TContext;
  TContext = object(TPassContext) // a context represents a module
    module: PSym;                 // the module sym belonging to the context
    p: PProcCon;                  // procedure context
    InstCounter: int;             // to prevent endless instantiations
    generics: PNode;              // a list of the things to compile; list of
                                  // nkExprEqExpr nodes which contain the
                                  // generic symbol and the instantiated symbol
    lastGenericIdx: int;          // used for the generics stack
    tab: TSymTab;                 // each module has its own symbol table
    AmbiguousSymbols: TIntSet;    // ids of all ambiguous symbols (cannot
                                  // store this info in the syms themselves!)
    converters: TSymSeq;          // sequence of converters
    optionStack: TLinkedList;
    libs: TLinkedList;            // all libs used by this module
    fromCache: bool;              // is the module read from a cache?
    semConstExpr: function (c: PContext; n: PNode): PNode;
      // for the pragmas module
    includedFiles: TIntSet;       // used to detect recursive include files
    filename: string;             // the module's filename
  end;

var
  gInstTypes: TIdTable; // map PType to PType

function newContext(module: PSym; const nimfile: string): PContext;
function newProcCon(owner: PSym): PProcCon;

function lastOptionEntry(c: PContext): POptionEntry;
function newOptionEntry(): POptionEntry;

procedure addConverter(c: PContext; conv: PSym);

function newLib(kind: TLibKind): PLib;
procedure addToLib(lib: PLib; sym: PSym);

function makePtrType(c: PContext; baseType: PType): PType;
function makeVarType(c: PContext; baseType: PType): PType;

function newTypeS(const kind: TTypeKind; c: PContext): PType;
procedure fillTypeS(dest: PType; const kind: TTypeKind; c: PContext);
function makeRangeType(c: PContext; first, last: biggestInt;
                       const info: TLineInfo): PType;

procedure illFormedAst(n: PNode);
function getSon(n: PNode; indx: int): PNode;
procedure checkSonsLen(n: PNode; len: int);
procedure checkMinSonsLen(n: PNode; len: int);

// owner handling:
function getCurrOwner(): PSym;
procedure PushOwner(owner: PSym);
procedure PopOwner;

implementation

var
  gOwners: array of PSym; // owner stack (used for initializing the
                          // owner field of syms)
                          // the documentation comment always gets
                          // assigned to the current owner
                          // BUGFIX: global array is needed!
{@emit gOwners := @[]; }

function getCurrOwner(): PSym;
begin
  result := gOwners[high(gOwners)];
end;

procedure PushOwner(owner: PSym);
var
  len: int;
begin
  len := length(gOwners);
  setLength(gOwners, len+1);
  gOwners[len] := owner;
end;

procedure PopOwner;
var
  len: int;
begin
  len := length(gOwners);
  if (len <= 0) then InternalError('popOwner');
  setLength(gOwners, len - 1);
end;

function lastOptionEntry(c: PContext): POptionEntry;
begin
  result := POptionEntry(c.optionStack.tail);
end;

function newProcCon(owner: PSym): PProcCon;
begin
  if owner = nil then InternalError('owner is nil');
  new(result);
{@ignore}
  fillChar(result^, sizeof(result^), 0);
{@emit}
  result.owner := owner;
end;

function newOptionEntry(): POptionEntry;
begin
  new(result);
{@ignore}
  fillChar(result^, sizeof(result^), 0);
{@emit}
  result.options := gOptions;
  result.defaultCC := ccDefault;
  result.dynlib := nil;
  result.notes := gNotes;
end;

function newContext(module: PSym; const nimfile: string): PContext;
begin
  new(result);
{@ignore}
  fillChar(result^, sizeof(result^), 0);
{@emit}
  InitSymTab(result.tab);
  IntSetInit(result.AmbiguousSymbols);
  initLinkedList(result.optionStack);
  initLinkedList(result.libs);
  append(result.optionStack, newOptionEntry());
  result.module := module;
  result.generics := newNode(nkStmtList);
{@emit result.converters := @[];}
  result.filename := nimfile;
  IntSetInit(result.includedFiles);
end;

procedure addConverter(c: PContext; conv: PSym);
var
  i, L: int;
begin
  L := length(c.converters);
  for i := 0 to L-1 do
    if c.converters[i].id = conv.id then exit;
  setLength(c.converters, L+1);
  c.converters[L] := conv;
end;


function newLib(kind: TLibKind): PLib;
begin
  new(result);
{@ignore}
  fillChar(result^, sizeof(result^), 0);
{@emit}
  result.kind := kind;
  //initObjectSet(result.syms)
end;

procedure addToLib(lib: PLib; sym: PSym);
begin
  //ObjectSetIncl(lib.syms, sym);
  if sym.annex <> nil then liMessage(sym.info, errInvalidPragma);
  sym.annex := lib
end;

function makePtrType(c: PContext; baseType: PType): PType;
begin
  if (baseType = nil) then InternalError('makePtrType');
  result := newTypeS(tyPtr, c);
  addSon(result, baseType);
end;

function makeVarType(c: PContext; baseType: PType): PType;
begin
  if (baseType = nil) then InternalError('makeVarType');
  result := newTypeS(tyVar, c);
  addSon(result, baseType);
end;

function newTypeS(const kind: TTypeKind; c: PContext): PType;
begin
  result := newType(kind, getCurrOwner())
end;

procedure fillTypeS(dest: PType; const kind: TTypeKind; c: PContext);
begin
  dest.kind := kind;
  dest.owner := getCurrOwner();
  dest.size := -1;
end;

function makeRangeType(c: PContext; first, last: biggestInt;
                       const info: TLineInfo): PType;
var
  n: PNode;
begin
  n := newNodeI(nkRange, info);
  addSon(n, newIntNode(nkIntLit, first));
  addSon(n, newIntNode(nkIntLit, last));
  result := newTypeS(tyRange, c);
  result.n := n;
  addSon(result, getSysType(tyInt)); // basetype of range
end;

procedure illFormedAst(n: PNode);
begin
  liMessage(n.info, errIllFormedAstX, renderTree(n, {@set}[renderNoComments]));
end;

function getSon(n: PNode; indx: int): PNode;
begin
  if (n <> nil) and (indx < sonsLen(n)) then result := n.sons[indx]
  else begin illFormedAst(n); result := nil end;
end;

procedure checkSonsLen(n: PNode; len: int);
begin
  if (n = nil) or (sonsLen(n) <> len) then illFormedAst(n);
end;

procedure checkMinSonsLen(n: PNode; len: int);
begin
  if (n = nil) or (sonsLen(n) < len) then illFormedAst(n);
end;

initialization
  initIdTable(gInstTypes);
end.