| | @@ -48,6 +48,15 @@ |
| 48 | 48 | base: longint; { the deferred statements in force outside it } |
| 49 | 49 | end; |
| 50 | 50 | |
| 51 | + { A generic module's import whose instance isn't made yet } |
| 52 | + TDeferred = record |
| 53 | + o: PObj; { the name it imports as } |
| 54 | + m: PModule; { the module that imports it } |
| 55 | + mname: longint; { the generic module } |
| 56 | + args: TScanState; { at the "(" of its arguments } |
| 57 | + at: TPos; { the import } |
| 58 | + end; |
| 59 | + |
| 51 | 60 | var |
| 52 | 61 | checkMode: boolean; { reading a file only for its interface } |
| 53 | 62 | inTypes: boolean; { in a type section: ^T may name a later type } |
| | @@ -71,6 +80,10 @@ |
| 71 | 80 | declaring: array[0..63] of longint; { the names the declaration being read declares } |
| 72 | 81 | numDeclaring: longint; |
| 73 | 82 | skipped: boolean; { the last statement had an error and was skipped } |
| 83 | + deferred: array[0..63] of TDeferred; |
| 84 | + numDeferred: longint; |
| 85 | + making: boolean; { reading an instance's arguments, at its first use } |
| 86 | + useAt: TPos; { ... which is here } |
| 74 | 87 | |
| 75 | 88 | procedure Expression(var x: TItem; want: PType); forward; |
| 76 | 89 | function ParseType: PType; forward; |
| | @@ -194,7 +207,7 @@ |
| 194 | 207 | CutScopes(r.scopes); depth := r.depth; numDefers := r.numDefers; |
| 195 | 208 | curLoop := r.loop; curResult := r.result; |
| 196 | 209 | deferring := r.deferring; directDefer := r.directDefer; |
| 197 | | - inTypes := false; declExported := false; inExtern := false; |
| 210 | + inTypes := false; declExported := false; inExtern := false; making := false; |
| 198 | 211 | end; |
| 199 | 212 | |
| 200 | 213 | { Gives up the construct r is for, and the one around it } |
| | @@ -290,22 +303,29 @@ |
| 290 | 303 | else if (NameOf(n) = 'elif') or (NameOf(n) = 'elseif') then Help('raz writes elsif') |
| 291 | 304 | else DidYouMean(Closest(n, -1)); |
| 292 | 305 | end; |
| 306 | + |
| 307 | +procedure FirstUse(o: PObj; const at: TPos); forward; |
| 308 | +procedure InstancesFor(wholeRest: boolean); forward; |
| 309 | +procedure AddImport(m, im: PModule); forward; |
| 293 | 310 | |
| 294 | 311 | function QualIdent: PObj; |
| 295 | | -var o: PObj; m: PModule; |
| 312 | +var o: PObj; m: PModule; at: TPos; |
| 296 | 313 | begin |
| 297 | 314 | if tok <> sIdent then Expected('a name'); |
| 315 | + at := TokPos; |
| 298 | 316 | o := Lookup(id); |
| 299 | 317 | if o = nil then begin |
| 300 | | - Poison(id); |
| 318 | + if not making then Poison(id); { it may yet be declared } |
| 301 | 319 | quiet := false; |
| 302 | | - NotDeclared(TokPos, id); |
| 320 | + NotDeclared(at, id); |
| 321 | + if making then Note(useAt, 'the instance is made here, where it is first used'); |
| 303 | 322 | Fail; |
| 304 | 323 | end; |
| 305 | 324 | if o^.typ = tPoison then quiet := true; |
| 306 | 325 | Named(o); |
| 307 | 326 | Next; |
| 308 | 327 | if o^.cls = oModule then begin |
| 328 | + if o^.module = nil then FirstUse(o, at); |
| 309 | 329 | Expect(sPeriod); |
| 310 | 330 | if tok <> sIdent then Expected('a name'); |
| 311 | 331 | m := o^.module; |
| | @@ -1643,6 +1663,7 @@ |
| 1643 | 1663 | procedure ProcDecl; |
| 1644 | 1664 | var n, line: longint; exp: boolean; sig: PType; p: PObj; at: TPos; |
| 1645 | 1665 | begin |
| 1666 | + InstancesFor(false); |
| 1646 | 1667 | line := CurLine; |
| 1647 | 1668 | Next; |
| 1648 | 1669 | IdentDef(n, exp, at); |
| | @@ -1932,6 +1953,19 @@ |
| 1932 | 1953 | Expect(sRparen); |
| 1933 | 1954 | end; |
| 1934 | 1955 | |
| 1956 | +{ Past an instance's arguments, to be read when it is made } |
| 1957 | +procedure SkipArgs; |
| 1958 | +var p: longint; |
| 1959 | +begin |
| 1960 | + p := 0; |
| 1961 | + repeat |
| 1962 | + if tok = sLparen then inc(p) |
| 1963 | + else if tok = sRparen then dec(p) |
| 1964 | + else if tok in [sSemicolon, sEof] then Expected('")"'); |
| 1965 | + Next; |
| 1966 | + until p = 0; |
| 1967 | +end; |
| 1968 | + |
| 1935 | 1969 | { A generic module's parameters, bound to the instance's arguments: a |
| 1936 | 1970 | type parameter names its type, a procedure parameter its procedure. } |
| 1937 | 1971 | procedure Params(m: PModule); |
| | @@ -2043,8 +2077,9 @@ |
| 2043 | 2077 | function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule; |
| 2044 | 2078 | var |
| 2045 | 2079 | st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; |
| 2046 | | - oldMod: PModule; oldCheck: boolean; m, im: PModule; |
| 2047 | | - imp: PImport; lib: PLib; start: TPos; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs; |
| 2080 | + oldMod: PModule; oldCheck: boolean; m: PModule; |
| 2081 | + lib: PLib; start: TPos; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs; |
| 2082 | + generic: boolean; argsAt: TScanState; |
| 2048 | 2083 | begin |
| 2049 | 2084 | SaveScan(st); |
| 2050 | 2085 | SaveScopes(d, saved); |
| | @@ -2083,24 +2118,36 @@ |
| 2083 | 2118 | alias := id; |
| 2084 | 2119 | mname := id; |
| 2085 | 2120 | iargs.n := 0; |
| 2121 | + generic := false; |
| 2086 | 2122 | Next; |
| 2087 | 2123 | if tok = sBecomes then begin |
| 2088 | 2124 | Next; |
| 2089 | 2125 | if tok <> sIdent then Expected('a module name'); |
| 2090 | 2126 | mname := id; |
| 2091 | 2127 | Next; |
| 2092 | | - if tok = sLparen then ParseArgs(iargs, NameOf(mname)); |
| 2128 | + if tok = sLparen then begin |
| 2129 | + generic := true; |
| 2130 | + SaveScan(argsAt); |
| 2131 | + SkipArgs; |
| 2132 | + end; |
| 2093 | 2133 | end; |
| 2094 | 2134 | iargs.at := SpanFrom(start); |
| 2095 | | - im := ImportModule(mname, iargs); |
| 2096 | 2135 | o := NewObj(oModule, alias, nil); |
| 2097 | 2136 | o^.pos := start; |
| 2098 | | - o^.module := im; |
| 2099 | 2137 | Declare(o); |
| 2100 | | - New(imp); |
| 2101 | | - imp^.module := im; |
| 2102 | | - imp^.next := m^.imports; |
| 2103 | | - m^.imports := imp; |
| 2138 | + if not generic then begin |
| 2139 | + o^.module := ImportModule(mname, iargs); |
| 2140 | + AddImport(m, o^.module); |
| 2141 | + end else begin |
| 2142 | + o^.module := nil; { until the instance is made, at its first use } |
| 2143 | + if numDeferred > High(deferred) then Mark('more than ' + IntStr(High(deferred) + 1) + ' generic imports'); |
| 2144 | + deferred[numDeferred].o := o; |
| 2145 | + deferred[numDeferred].m := m; |
| 2146 | + deferred[numDeferred].mname := mname; |
| 2147 | + deferred[numDeferred].args := argsAt; |
| 2148 | + deferred[numDeferred].at := iargs.at; |
| 2149 | + inc(numDeferred); |
| 2150 | + end; |
| 2104 | 2151 | if tok <> sComma then break; |
| 2105 | 2152 | Next; |
| 2106 | 2153 | until false; |
| | @@ -2123,6 +2170,7 @@ |
| 2123 | 2170 | if tok = sBegin then begin |
| 2124 | 2171 | if checkMode then SkipBody |
| 2125 | 2172 | else begin |
| 2173 | + InstancesFor(true); |
| 2126 | 2174 | line := CurLine; |
| 2127 | 2175 | Next; |
| 2128 | 2176 | body := NewChunk(secText, m^.sname, nameBody); |
| | @@ -2283,6 +2331,78 @@ |
| 2283 | 2331 | m := CompileFile(dir + '/' + f, name, args, false); |
| 2284 | 2332 | CheckSiblings(dir, f, name, m, args); |
| 2285 | 2333 | ImportModule := m; |
| 2334 | +end; |
| 2335 | + |
| 2336 | +procedure AddImport(m, im: PModule); |
| 2337 | +var imp: PImport; |
| 2338 | +begin |
| 2339 | + New(imp); |
| 2340 | + imp^.module := im; |
| 2341 | + imp^.next := m^.imports; |
| 2342 | + m^.imports := imp; |
| 2343 | +end; |
| 2344 | + |
| 2345 | +{ Makes the instance that deferred[i] imports: its arguments are read |
| 2346 | + where the import wrote them, and must be declared by now. Compiling |
| 2347 | + the instance comes in the middle of a declaration, so what that |
| 2348 | + declaration has under way is kept aside meanwhile. } |
| 2349 | +procedure MakeInstance(i: longint; const at: TPos); |
| 2350 | +var |
| 2351 | + d: TDeferred; st: TScanState; args: TArgs; types, exported, ext, wasQuiet: boolean; nf, nd: longint; |
| 2352 | + ft: array[0..255] of PType; fp: array[0..255] of TPos; decl: array[0..63] of longint; |
| 2353 | +begin |
| 2354 | + d := deferred[i]; |
| 2355 | + dec(numDeferred); |
| 2356 | + deferred[i] := deferred[numDeferred]; |
| 2357 | + SaveScan(st); |
| 2358 | + RestoreScan(d.args); |
| 2359 | + args.n := 0; |
| 2360 | + making := true; useAt := at; |
| 2361 | + ParseArgs(args, NameOf(d.mname)); |
| 2362 | + making := false; |
| 2363 | + RestoreScan(st); |
| 2364 | + args.at := d.at; |
| 2365 | + types := inTypes; exported := declExported; ext := inExtern; wasQuiet := quiet; |
| 2366 | + nf := numFwd; ft := fwdTypes; fp := fwdPos; nd := numDeclaring; decl := declaring; |
| 2367 | + d.o^.module := ImportModule(d.mname, args); |
| 2368 | + inTypes := types; declExported := exported; inExtern := ext; quiet := wasQuiet; |
| 2369 | + numFwd := nf; fwdTypes := ft; fwdPos := fp; numDeclaring := nd; declaring := decl; |
| 2370 | + AddImport(curModule, d.o^.module); |
| 2371 | +end; |
| 2372 | + |
| 2373 | +procedure FirstUse(o: PObj; const at: TPos); |
| 2374 | +var i: longint; |
| 2375 | +begin |
| 2376 | + for i := 0 to numDeferred - 1 do |
| 2377 | + if deferred[i].o = o then begin MakeInstance(i, at); exit end; |
| 2378 | + Fail; { its arguments had an error, already reported } |
| 2379 | +end; |
| 2380 | + |
| 2381 | +{ An instance can't be compiled in the middle of another procedure's |
| 2382 | + code, so before a procedure's code begins, from its "proc" on, or |
| 2383 | + the module body's, from its "begin" to the end of the file, this |
| 2384 | + reads ahead for the names of imports not yet made, and makes those. } |
| 2385 | +procedure InstancesFor(wholeRest: boolean); |
| 2386 | +var st: TScanState; d, i: longint; begun: boolean; |
| 2387 | +begin |
| 2388 | + if checkMode then exit; |
| 2389 | + i := numDeferred - 1; |
| 2390 | + while (i >= 0) and (deferred[i].m <> curModule) do dec(i); |
| 2391 | + if i < 0 then exit; |
| 2392 | + SaveScan(st); |
| 2393 | + d := 0; begun := false; |
| 2394 | + repeat |
| 2395 | + case tok of |
| 2396 | + sIdent: for i := numDeferred - 1 downto 0 do |
| 2397 | + if (deferred[i].m = curModule) and (deferred[i].o^.name = id) then MakeInstance(i, TokPos); |
| 2398 | + sBegin: begin begun := begun or (d = 0); inc(d) end; |
| 2399 | + sIf, sWhile, sFor, sMatch, sRepeat, sRecord, sVariant: inc(d); |
| 2400 | + sEnd, sUntil: dec(d); |
| 2401 | + end; |
| 2402 | + if not wholeRest and (d = 0) and (begun or (tok in [sForward, sCode])) then break; |
| 2403 | + Next; |
| 2404 | + until tok = sEof; |
| 2405 | + RestoreScan(st); |
| 2286 | 2406 | end; |
| 2287 | 2407 | |
| 2288 | 2408 | function CompileMain(const path: shortstring): PModule; |