pgenutil.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. {
  2. Copyright (c) 2011
  3. Contains different functions that are used in the context of
  4. parsing generics.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pgenutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globtype,
  26. { symtable }
  27. symtype,symdef,symbase;
  28. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string);
  29. function parse_generic_parameters:TFPObjectList;
  30. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;out prettyname,specializename:ansistring;parsedtype:tdef):boolean;
  31. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  32. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  33. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  34. type
  35. tspecializationstate = record
  36. oldsymtablestack : tsymtablestack;
  37. oldextendeddefs : TFPHashObjectList;
  38. end;
  39. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  40. procedure specialization_done(var state:tspecializationstate);
  41. implementation
  42. uses
  43. { common }
  44. cutils,fpccrc,
  45. { global }
  46. globals,tokens,verbose,
  47. { symtable }
  48. symconst,symsym,symtable,
  49. { modules }
  50. fmodule,
  51. { pass 1 }
  52. htypechk,
  53. node,nobj,nmem,
  54. { parser }
  55. scanner,
  56. pbase,pexpr,pdecsub,ptype;
  57. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string);
  58. var
  59. st : TSymtable;
  60. srsym : tsym;
  61. pt2 : tnode;
  62. found,
  63. first,
  64. err : boolean;
  65. i,
  66. gencount : longint;
  67. crc : cardinal;
  68. genericdef,def : tstoreddef;
  69. generictype : ttypesym;
  70. genericdeflist : TFPObjectList;
  71. generictypelist : TFPObjectList;
  72. prettyname,specializename : ansistring;
  73. ufinalspecializename,
  74. countstr,genname,ugenname,finalspecializename : string;
  75. vmtbuilder : TVMTBuilder;
  76. specializest : tsymtable;
  77. item : tobject;
  78. old_current_structdef : tabstractrecorddef;
  79. old_current_genericdef,old_current_specializedef : tstoreddef;
  80. tempst : tglobalsymtable;
  81. old_block_type: tblock_type;
  82. hashedid: thashedidstring;
  83. state : tspecializationstate;
  84. hmodule : tmodule;
  85. oldcurrent_filepos : tfileposinfo;
  86. begin
  87. { retrieve generic def that we are going to replace }
  88. genericdef:=tstoreddef(tt);
  89. tt:=nil;
  90. { either symname must be given or genericdef needs to be valid }
  91. if (symname='') and
  92. (not assigned(genericdef) or
  93. not assigned(genericdef.typesym) or
  94. (genericdef.typesym.typ<>typesym)) then
  95. internalerror(2011042701);
  96. { Only parse the parameters for recovery or
  97. for recording in genericbuf }
  98. if parse_generic then
  99. begin
  100. first:=assigned(parsedtype);
  101. if not first and not try_to_consume(_LT) then
  102. consume(_LSHARPBRACKET);
  103. gencount:=0;
  104. repeat
  105. if not first then
  106. begin
  107. pt2:=factor(false,true);
  108. pt2.free;
  109. end;
  110. first:=false;
  111. inc(gencount);
  112. until not try_to_consume(_COMMA);
  113. if not try_to_consume(_GT) then
  114. consume(_RSHARPBRACKET);
  115. { we need to return a def that can later pass some checks like
  116. whether it's an interface or not }
  117. if not assigned(tt) or (tt.typ=undefineddef) then
  118. begin
  119. if (symname='') and (df_generic in genericdef.defoptions) then
  120. { this happens in non-Delphi modes }
  121. tt:=genericdef
  122. else
  123. begin
  124. { find the corresponding generic symbol so that any checks
  125. done on the returned def will be handled correctly }
  126. str(gencount,countstr);
  127. if symname='' then
  128. genname:=ttypesym(genericdef.typesym).realname
  129. else
  130. genname:=symname;
  131. genname:=genname+'$'+countstr;
  132. ugenname:=upper(genname);
  133. { first check whether the found name is the same as that of
  134. the current def or one of its (generic) surrounding defs;
  135. this is necessary as the symbol of the generic can not yet
  136. be used for lookup as it still contains a reference to an
  137. errordef) }
  138. def:=current_genericdef;
  139. repeat
  140. if def.typ in [objectdef,recorddef] then
  141. if tabstractrecorddef(def).objname^=ugenname then
  142. begin
  143. tt:=def;
  144. break;
  145. end;
  146. def:=tstoreddef(def.owner.defowner);
  147. until not assigned(def) or not (df_generic in def.defoptions);
  148. { it's not part of the current object hierarchy, so search
  149. for the symbol }
  150. if not assigned(tt) then
  151. begin
  152. if not searchsym(ugenname,srsym,st) or
  153. (srsym.typ<>typesym) then
  154. begin
  155. identifier_not_found(genname);
  156. exit;
  157. end;
  158. tt:=ttypesym(srsym).typedef;
  159. { this happens in non-Delphi modes if we encounter a
  160. specialization of the generic class or record we're
  161. currently parsing }
  162. if (tt.typ=errordef) and assigned(current_structdef) and
  163. (current_structdef.objname^=ugenname) then
  164. tt:=current_structdef;
  165. end;
  166. end;
  167. end;
  168. exit;
  169. end;
  170. if not assigned(parsedtype) and not try_to_consume(_LT) then
  171. consume(_LSHARPBRACKET);
  172. generictypelist:=TFPObjectList.create(false);
  173. genericdeflist:=TFPObjectList.Create(false);
  174. { Parse type parameters }
  175. err:=not parse_generic_specialization_types(genericdeflist,prettyname,specializename,parsedtype);
  176. if err then
  177. begin
  178. try_to_consume(_RSHARPBRACKET);
  179. exit;
  180. end;
  181. { search a generic with the given count of params }
  182. countstr:='';
  183. str(genericdeflist.Count,countstr);
  184. { use the name of the symbol as procvars return a user friendly version
  185. of the name }
  186. if symname='' then
  187. genname:=ttypesym(genericdef.typesym).realname
  188. else
  189. genname:=symname;
  190. { in case of non-Delphi mode the type name could already be a generic
  191. def (but maybe the wrong one) }
  192. if assigned(genericdef) and
  193. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  194. begin
  195. { remove the type count suffix from the generic's name }
  196. for i:=Length(genname) downto 1 do
  197. if genname[i]='$' then
  198. begin
  199. genname:=copy(genname,1,i-1);
  200. break;
  201. end;
  202. { in case of a specialization we've only reached the specialization
  203. checksum yet }
  204. if df_specialization in genericdef.defoptions then
  205. for i:=length(genname) downto 1 do
  206. if genname[i]='$' then
  207. begin
  208. genname:=copy(genname,1,i-1);
  209. break;
  210. end;
  211. end;
  212. genname:=genname+'$'+countstr;
  213. ugenname:=upper(genname);
  214. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  215. begin
  216. if genericdef.owner.symtabletype = objectsymtable then
  217. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,srsym,st,false)
  218. else
  219. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,srsym,st);
  220. end
  221. else
  222. found:=searchsym(ugenname,srsym,st);
  223. if not found or (srsym.typ<>typesym) then
  224. begin
  225. identifier_not_found(genname);
  226. genericdeflist.Free;
  227. generictypelist.Free;
  228. exit;
  229. end;
  230. { we've found the correct def }
  231. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  232. { build the new type's name }
  233. finalspecializename:=generate_generic_name(genname,specializename);
  234. ufinalspecializename:=upper(finalspecializename);
  235. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  236. { select the symtable containing the params }
  237. case genericdef.typ of
  238. procdef:
  239. st:=genericdef.GetSymtable(gs_para);
  240. objectdef,
  241. recorddef:
  242. st:=genericdef.GetSymtable(gs_record);
  243. arraydef:
  244. st:=tarraydef(genericdef).symtable;
  245. procvardef:
  246. st:=genericdef.GetSymtable(gs_para);
  247. else
  248. internalerror(200511182);
  249. end;
  250. { build the list containing the types for the generic params }
  251. gencount:=0;
  252. for i:=0 to st.SymList.Count-1 do
  253. begin
  254. srsym:=tsym(st.SymList[i]);
  255. if sp_generic_para in srsym.symoptions then
  256. begin
  257. if gencount=genericdeflist.Count then
  258. internalerror(2011042702);
  259. generictype:=ttypesym.create(srsym.realname,tdef(genericdeflist[gencount]));
  260. generictypelist.add(generictype);
  261. inc(gencount);
  262. end;
  263. end;
  264. { Special case if we are referencing the current defined object }
  265. if assigned(current_structdef) and
  266. (current_structdef.objname^=ufinalspecializename) then
  267. tt:=current_structdef;
  268. { decide in which symtable to put the specialization }
  269. if current_module.is_unit and current_module.in_interface then
  270. specializest:=current_module.globalsymtable
  271. else
  272. specializest:=current_module.localsymtable;
  273. { Can we reuse an already specialized type? }
  274. { for this first check whether we are currently specializing a nested
  275. type of the current (main) specialization (this is necessary, because
  276. during that time the symbol of the main specialization will still
  277. contain a reference to an errordef) }
  278. if not assigned(tt) and assigned(current_specializedef) then
  279. begin
  280. def:=current_specializedef;
  281. repeat
  282. if def.typ in [objectdef,recorddef] then
  283. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  284. tt:=def;
  285. break;
  286. end;
  287. def:=tstoreddef(def.owner.defowner);
  288. until not assigned(def) or not (df_specialization in def.defoptions);
  289. end;
  290. { now check whether there is a specialization somewhere else }
  291. if not assigned(tt) then
  292. begin
  293. hashedid.id:=ufinalspecializename;
  294. srsym:=tsym(specializest.findwithhash(hashedid));
  295. if assigned(srsym) then
  296. begin
  297. if srsym.typ<>typesym then
  298. internalerror(200710171);
  299. tt:=ttypesym(srsym).typedef;
  300. end
  301. else
  302. { the generic could have been specialized in the globalsymtable
  303. already, so search there as well }
  304. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  305. begin
  306. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  307. if assigned(srsym) then
  308. begin
  309. if srsym.typ<>typesym then
  310. internalerror(2011121101);
  311. tt:=ttypesym(srsym).typedef;
  312. end;
  313. end;
  314. end;
  315. if not assigned(tt) then
  316. begin
  317. specialization_init(genericdef,state);
  318. { push a temporary global symtable so that the specialization is
  319. added to the correct symtable; this symtable does not contain
  320. any other symbols, so that the type resolution can not be
  321. influenced by symbols in the current unit }
  322. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  323. symtablestack.push(tempst);
  324. { Reparse the original type definition }
  325. if not err then
  326. begin
  327. if parse_class_parent then
  328. begin
  329. old_current_structdef:=current_structdef;
  330. old_current_genericdef:=current_genericdef;
  331. old_current_specializedef:=current_specializedef;
  332. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  333. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  334. else
  335. current_structdef:=nil;
  336. current_genericdef:=nil;
  337. current_specializedef:=nil;
  338. end;
  339. { First a new typesym so we can reuse this specialization and
  340. references to this specialization can be handled }
  341. srsym:=ttypesym.create(finalspecializename,generrordef);
  342. specializest.insert(srsym);
  343. { specializations are declarations as such it is the wisest to
  344. declare set the blocktype to "type"; otherwise we'll
  345. experience unexpected side effects like the addition of
  346. classrefdefs if we have a generic that's derived from another
  347. generic }
  348. old_block_type:=block_type;
  349. block_type:=bt_type;
  350. if not assigned(genericdef.generictokenbuf) then
  351. internalerror(200511171);
  352. hmodule:=find_module_from_symtable(genericdef.owner);
  353. if hmodule=nil then
  354. internalerror(2012051202);
  355. oldcurrent_filepos:=current_filepos;
  356. { use the index the module got from the current compilation process }
  357. current_filepos.moduleindex:=hmodule.unit_index;
  358. current_tokenpos:=current_filepos;
  359. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  360. read_named_type(tt,srsym,genericdef,generictypelist,false);
  361. current_filepos:=oldcurrent_filepos;
  362. ttypesym(srsym).typedef:=tt;
  363. tt.typesym:=srsym;
  364. if _prettyname<>'' then
  365. ttypesym(tt.typesym).fprettyname:=_prettyname
  366. else
  367. ttypesym(tt.typesym).fprettyname:=prettyname;
  368. { Note regarding hint directives:
  369. There is no need to remove the flags for them from the
  370. specialized generic symbol, because hint directives that
  371. follow the specialization are handled by the code in
  372. pdecl.types_dec and added to the type symbol.
  373. E.g.: TFoo = TBar<Blubb> deprecated;
  374. Here the symbol TBar$1$Blubb will contain the
  375. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  376. case tt.typ of
  377. { Build VMT indexes for classes and read hint directives }
  378. objectdef:
  379. begin
  380. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  381. consume(_SEMICOLON);
  382. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  383. vmtbuilder.generate_vmt;
  384. vmtbuilder.free;
  385. end;
  386. { handle params, calling convention, etc }
  387. procvardef:
  388. begin
  389. if not check_proc_directive(true) then
  390. begin
  391. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  392. consume(_SEMICOLON);
  393. end;
  394. parse_var_proc_directives(ttypesym(srsym));
  395. handle_calling_convention(tprocvardef(tt));
  396. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  397. consume(_SEMICOLON);
  398. end;
  399. else
  400. { parse hint directives for records and arrays }
  401. begin
  402. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  403. consume(_SEMICOLON);
  404. end;
  405. end;
  406. { Consume the semicolon if it is also recorded }
  407. try_to_consume(_SEMICOLON);
  408. block_type:=old_block_type;
  409. if parse_class_parent then
  410. begin
  411. current_structdef:=old_current_structdef;
  412. current_genericdef:=old_current_genericdef;
  413. current_specializedef:=old_current_specializedef;
  414. end;
  415. end;
  416. { extract all created symbols and defs from the temporary symtable
  417. and add them to the specializest }
  418. for i:=tempst.SymList.Count-1 downto 0 do
  419. begin
  420. item:=tempst.SymList.Items[i];
  421. { using changeowner the symbol is automatically added to the
  422. new symtable }
  423. tsym(item).ChangeOwner(specializest);
  424. end;
  425. for i:=tempst.DefList.Count-1 downto 0 do
  426. begin
  427. item:=tempst.DefList.Items[i];
  428. { using changeowner the def is automatically added to the new
  429. symtable }
  430. tdef(item).ChangeOwner(specializest);
  431. end;
  432. tempst.free;
  433. specialization_done(state);
  434. end;
  435. if not (token in [_GT, _RSHARPBRACKET]) then
  436. begin
  437. consume(_RSHARPBRACKET);
  438. exit;
  439. end
  440. else
  441. consume(token);
  442. genericdeflist.free;
  443. generictypelist.free;
  444. if assigned(genericdef) then
  445. begin
  446. { check the hints of the found generic symbol }
  447. srsym:=genericdef.typesym;
  448. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  449. end;
  450. end;
  451. function parse_generic_parameters:TFPObjectList;
  452. var
  453. generictype : ttypesym;
  454. begin
  455. result:=TFPObjectList.Create(false);
  456. repeat
  457. if token=_ID then
  458. begin
  459. generictype:=ttypesym.create(orgpattern,cundefinedtype);
  460. include(generictype.symoptions,sp_generic_para);
  461. result.add(generictype);
  462. end;
  463. consume(_ID);
  464. until not try_to_consume(_COMMA) ;
  465. end;
  466. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;out prettyname,specializename:ansistring;parsedtype:tdef):boolean;
  467. var
  468. old_block_type : tblock_type;
  469. first : boolean;
  470. typeparam : tnode;
  471. begin
  472. result:=true;
  473. if genericdeflist=nil then
  474. internalerror(2012061401);
  475. { set the block type to type, so that the parsed type are returned as
  476. ttypenode (e.g. classes are in non type-compatible blocks returned as
  477. tloadvmtaddrnode) }
  478. old_block_type:=block_type;
  479. { if parsedtype is set, then the first type identifer was already parsed
  480. (happens in inline specializations) and thus we only need to parse
  481. the remaining types and do as if the first one was already given }
  482. first:=not assigned(parsedtype);
  483. if assigned(parsedtype) then
  484. begin
  485. genericdeflist.Add(parsedtype);
  486. specializename:='$'+parsedtype.typename;
  487. prettyname:=parsedtype.typesym.prettyname;
  488. end
  489. else
  490. begin
  491. specializename:='';
  492. prettyname:='';
  493. end;
  494. while not (token in [_GT,_RSHARPBRACKET]) do
  495. begin
  496. { "first" is set to false at the end of the loop! }
  497. if not first then
  498. consume(_COMMA);
  499. block_type:=bt_type;
  500. typeparam:=factor(false,true);
  501. if typeparam.nodetype=typen then
  502. begin
  503. if df_generic in typeparam.resultdef.defoptions then
  504. Message(parser_e_no_generics_as_params);
  505. genericdeflist.Add(typeparam.resultdef);
  506. if not assigned(typeparam.resultdef.typesym) then
  507. message(type_e_generics_cannot_reference_itself)
  508. else
  509. begin
  510. specializename:=specializename+'$'+typeparam.resultdef.typename;
  511. if first then
  512. prettyname:=prettyname+typeparam.resultdef.typesym.prettyname
  513. else
  514. prettyname:=prettyname+','+typeparam.resultdef.typesym.prettyname;
  515. end;
  516. end
  517. else
  518. begin
  519. Message(type_e_type_id_expected);
  520. result:=false;
  521. end;
  522. typeparam.free;
  523. first:=false;
  524. end;
  525. block_type:=old_block_type;
  526. end;
  527. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:TFPObjectList);
  528. var
  529. i: longint;
  530. generictype: ttypesym;
  531. st: tsymtable;
  532. begin
  533. def.genericdef:=genericdef;
  534. if not assigned(genericlist) then
  535. exit;
  536. case def.typ of
  537. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  538. arraydef: st:=tarraydef(def).symtable;
  539. procvardef,procdef: st:=tabstractprocdef(def).parast;
  540. else
  541. internalerror(201101020);
  542. end;
  543. for i:=0 to genericlist.count-1 do
  544. begin
  545. generictype:=ttypesym(genericlist[i]);
  546. if generictype.typedef.typ=undefineddef then
  547. include(def.defoptions,df_generic)
  548. else
  549. include(def.defoptions,df_specialization);
  550. st.insert(generictype);
  551. end;
  552. end;
  553. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfpobjectlist);
  554. var
  555. gensym : ttypesym;
  556. begin
  557. { for generics in non-Delphi modes we insert a private type symbol
  558. that has the same base name as the currently parsed generic and
  559. that references this defs }
  560. if not (m_delphi in current_settings.modeswitches) and
  561. (
  562. (
  563. parse_generic and
  564. assigned(genericlist) and
  565. (genericlist.count>0)
  566. ) or
  567. (
  568. assigned(current_specializedef) and
  569. assigned(current_structdef.genericdef) and
  570. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  571. (pos('$',name)>0)
  572. )
  573. ) then
  574. begin
  575. { we need to pass nil as def here, because the constructor wants
  576. to set the typesym of the def which is not what we want }
  577. gensym:=ttypesym.create(copy(name,1,pos('$',name)-1),nil);
  578. gensym.typedef:=current_structdef;
  579. include(gensym.symoptions,sp_internal);
  580. { the symbol should be only visible to the generic class
  581. itself }
  582. gensym.visibility:=vis_strictprivate;
  583. symtablestack.top.insert(gensym);
  584. end;
  585. end;
  586. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  587. var
  588. crc : cardinal;
  589. begin
  590. if specializename='' then
  591. internalerror(2012061901);
  592. { build the new type's name }
  593. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  594. result:=name+'$crc'+hexstr(crc,8);
  595. end;
  596. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  597. var
  598. pu : tused_unit;
  599. hmodule : tmodule;
  600. unitsyms : TFPHashObjectList;
  601. sym : tsym;
  602. i : Integer;
  603. begin
  604. if not assigned(genericdef) then
  605. internalerror(200705151);
  606. { Setup symtablestack at definition time
  607. to get types right, however this is not perfect, we should probably record
  608. the resolved symbols }
  609. state.oldsymtablestack:=symtablestack;
  610. state.oldextendeddefs:=current_module.extendeddefs;
  611. current_module.extendeddefs:=TFPHashObjectList.create(true);
  612. symtablestack:=tdefawaresymtablestack.create;
  613. hmodule:=find_module_from_symtable(genericdef.owner);
  614. if hmodule=nil then
  615. internalerror(200705152);
  616. { collect all unit syms in the generic's unit as we need to establish
  617. their unitsym.module link again so that unit identifiers can be used }
  618. unitsyms:=tfphashobjectlist.create(false);
  619. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  620. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  621. begin
  622. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  623. if sym.typ=unitsym then
  624. unitsyms.add(upper(sym.realname),sym);
  625. end;
  626. { add all units if we are specializing inside the current unit (as the
  627. generic could have been declared in the implementation part), but load
  628. only interface units, if we are in a different unit as then the generic
  629. needs to be in the interface section }
  630. pu:=tused_unit(hmodule.used_units.first);
  631. while assigned(pu) do
  632. begin
  633. if (hmodule<>current_module) and not pu.in_interface then
  634. begin
  635. pu:=tused_unit(pu.next);
  636. continue;
  637. end;
  638. if not assigned(pu.u.globalsymtable) then
  639. internalerror(200705153);
  640. symtablestack.push(pu.u.globalsymtable);
  641. sym:=tsym(unitsyms.find(pu.u.modulename^));
  642. if assigned(sym) and not assigned(tunitsym(sym).module) then
  643. tunitsym(sym).module:=pu.u;
  644. pu:=tused_unit(pu.next);
  645. end;
  646. unitsyms.free;
  647. if assigned(hmodule.globalsymtable) then
  648. symtablestack.push(hmodule.globalsymtable);
  649. { push the localsymtable if needed }
  650. if (hmodule<>current_module) or not current_module.in_interface then
  651. symtablestack.push(hmodule.localsymtable);
  652. end;
  653. procedure specialization_done(var state: tspecializationstate);
  654. begin
  655. { Restore symtablestack }
  656. current_module.extendeddefs.free;
  657. current_module.extendeddefs:=state.oldextendeddefs;
  658. symtablestack.free;
  659. symtablestack:=state.oldsymtablestack;
  660. { clear the state record to be on the safe side }
  661. fillchar(state, sizeof(state), 0);
  662. end;
  663. end.