pgenutil.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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;parsedpos:tfileposinfo);
  29. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  30. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  31. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  32. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  33. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  34. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  35. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  36. function resolve_generic_dummysym(const name:tidstring):tsym;
  37. function could_be_generic(const name:tidstring):boolean;inline;
  38. type
  39. tspecializationstate = record
  40. oldsymtablestack : tsymtablestack;
  41. oldextendeddefs : TFPHashObjectList;
  42. oldgenericdummysyms: tfphashobjectlist;
  43. end;
  44. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  45. procedure specialization_done(var state:tspecializationstate);
  46. implementation
  47. uses
  48. { common }
  49. cutils,fpccrc,
  50. { global }
  51. globals,tokens,verbose,finput,
  52. { symtable }
  53. symconst,symsym,symtable,defcmp,procinfo,
  54. { modules }
  55. fmodule,
  56. { pass 1 }
  57. htypechk,
  58. node,nobj,nmem,
  59. { parser }
  60. scanner,
  61. pbase,pexpr,pdecsub,ptype;
  62. procedure maybe_add_waiting_unit(tt:tdef);
  63. var
  64. hmodule : tmodule;
  65. begin
  66. if not assigned(tt) or
  67. not (df_generic in tt.defoptions) then
  68. exit;
  69. hmodule:=find_module_from_symtable(tt.owner);
  70. if not assigned(hmodule) then
  71. internalerror(2012092401);
  72. if hmodule=current_module then
  73. exit;
  74. if hmodule.state<>ms_compiled then
  75. begin
  76. {$ifdef DEBUG_UNITWAITING}
  77. Writeln('Unit ', current_module.modulename^,
  78. ' waiting for ', hmodule.modulename^);
  79. {$endif DEBUG_UNITWAITING}
  80. if current_module.waitingforunit.indexof(hmodule)<0 then
  81. current_module.waitingforunit.add(hmodule);
  82. if hmodule.waitingunits.indexof(current_module)<0 then
  83. hmodule.waitingunits.add(current_module);
  84. end;
  85. end;
  86. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  87. var
  88. i,j,
  89. intfcount : longint;
  90. formaldef,
  91. paradef : tstoreddef;
  92. objdef,
  93. paraobjdef,
  94. formalobjdef : tobjectdef;
  95. intffound : boolean;
  96. filepos : tfileposinfo;
  97. begin
  98. { check whether the given specialization parameters fit to the eventual
  99. constraints of the generic }
  100. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  101. internalerror(2012101001);
  102. if genericdef.genericparas.count<>paradeflist.count then
  103. internalerror(2012101002);
  104. if paradeflist.count<>poslist.count then
  105. internalerror(2012120801);
  106. result:=true;
  107. for i:=0 to genericdef.genericparas.count-1 do
  108. begin
  109. filepos:=pfileposinfo(poslist[i])^;
  110. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  111. if formaldef.typ=undefineddef then
  112. { the parameter is of unspecified type, so no need to check }
  113. continue;
  114. if not (df_genconstraint in formaldef.defoptions) or
  115. not assigned(formaldef.genconstraintdata) then
  116. internalerror(2013021602);
  117. paradef:=tstoreddef(paradeflist[i]);
  118. { undefineddef is compatible with anything }
  119. if formaldef.typ=undefineddef then
  120. continue;
  121. if paradef.typ<>formaldef.typ then
  122. begin
  123. case formaldef.typ of
  124. recorddef:
  125. MessagePos(filepos,type_e_record_type_expected);
  126. objectdef:
  127. case tobjectdef(formaldef).objecttype of
  128. odt_class,
  129. odt_javaclass:
  130. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  131. odt_interfacecom,
  132. odt_interfacecorba,
  133. odt_dispinterface,
  134. odt_interfacejava:
  135. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  136. else
  137. internalerror(2012101003);
  138. end;
  139. errordef:
  140. { ignore }
  141. ;
  142. else
  143. internalerror(2012101004);
  144. end;
  145. result:=false;
  146. end
  147. else
  148. begin
  149. { the paradef types are the same, so do special checks for the
  150. cases in which they are needed }
  151. if formaldef.typ=objectdef then
  152. begin
  153. paraobjdef:=tobjectdef(paradef);
  154. formalobjdef:=tobjectdef(formaldef);
  155. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  156. internalerror(2012101102);
  157. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  158. begin
  159. { this is either a concerete interface or class type (the
  160. latter without specific implemented interfaces) }
  161. case paraobjdef.objecttype of
  162. odt_interfacecom,
  163. odt_interfacecorba,
  164. odt_interfacejava,
  165. odt_dispinterface:
  166. if not def_is_related(paraobjdef,formalobjdef.childof) then
  167. begin
  168. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  169. result:=false;
  170. end;
  171. odt_class,
  172. odt_javaclass:
  173. begin
  174. objdef:=paraobjdef;
  175. intffound:=false;
  176. while assigned(objdef) do
  177. begin
  178. for j:=0 to objdef.implementedinterfaces.count-1 do
  179. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  180. begin
  181. intffound:=true;
  182. break;
  183. end;
  184. if intffound then
  185. break;
  186. objdef:=objdef.childof;
  187. end;
  188. result:=intffound;
  189. if not result then
  190. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  191. end;
  192. else
  193. begin
  194. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  195. result:=false;
  196. end;
  197. end;
  198. end
  199. else
  200. begin
  201. { this is either a "class" or a concrete instance with
  202. or without implemented interfaces }
  203. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  204. begin
  205. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  206. result:=false;
  207. continue;
  208. end;
  209. if assigned(formalobjdef.childof) and
  210. not def_is_related(paradef,formalobjdef.childof) then
  211. begin
  212. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  213. result:=false;
  214. end;
  215. intfcount:=0;
  216. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  217. begin
  218. objdef:=paraobjdef;
  219. while assigned(objdef) do
  220. begin
  221. intffound:=assigned(
  222. find_implemented_interface(objdef,
  223. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  224. )
  225. );
  226. if intffound then
  227. break;
  228. objdef:=objdef.childof;
  229. end;
  230. if intffound then
  231. inc(intfcount)
  232. else
  233. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  234. end;
  235. if intfcount<>formalobjdef.implementedinterfaces.count then
  236. result:=false;
  237. end;
  238. end;
  239. end;
  240. end;
  241. end;
  242. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  243. var
  244. old_block_type : tblock_type;
  245. first : boolean;
  246. typeparam : tnode;
  247. parampos : pfileposinfo;
  248. tmpparampos : tfileposinfo;
  249. begin
  250. result:=true;
  251. if genericdeflist=nil then
  252. internalerror(2012061401);
  253. { set the block type to type, so that the parsed type are returned as
  254. ttypenode (e.g. classes are in non type-compatible blocks returned as
  255. tloadvmtaddrnode) }
  256. old_block_type:=block_type;
  257. { if parsedtype is set, then the first type identifer was already parsed
  258. (happens in inline specializations) and thus we only need to parse
  259. the remaining types and do as if the first one was already given }
  260. first:=not assigned(parsedtype);
  261. if assigned(parsedtype) then
  262. begin
  263. genericdeflist.Add(parsedtype);
  264. specializename:='$'+parsedtype.fulltypename;
  265. prettyname:=parsedtype.typesym.prettyname;
  266. if assigned(poslist) then
  267. begin
  268. New(parampos);
  269. parampos^:=parsedpos;
  270. poslist.add(parampos);
  271. end;
  272. end
  273. else
  274. begin
  275. specializename:='';
  276. prettyname:='';
  277. end;
  278. while not (token in [_GT,_RSHARPBRACKET]) do
  279. begin
  280. { "first" is set to false at the end of the loop! }
  281. if not first then
  282. consume(_COMMA);
  283. block_type:=bt_type;
  284. tmpparampos:=current_filepos;
  285. typeparam:=factor(false,true);
  286. if typeparam.nodetype=typen then
  287. begin
  288. if tstoreddef(typeparam.resultdef).is_generic and
  289. (
  290. not parse_generic or
  291. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  292. ) then
  293. Message(parser_e_no_generics_as_params);
  294. if assigned(poslist) then
  295. begin
  296. New(parampos);
  297. parampos^:=tmpparampos;
  298. poslist.add(parampos);
  299. end;
  300. genericdeflist.Add(typeparam.resultdef);
  301. if not assigned(typeparam.resultdef.typesym) then
  302. message(type_e_generics_cannot_reference_itself)
  303. else
  304. begin
  305. { we use the full name of the type to uniquely identify it }
  306. specializename:=specializename+'$'+typeparam.resultdef.fulltypename;
  307. if not first then
  308. prettyname:=prettyname+',';
  309. prettyname:=prettyname+typeparam.resultdef.fullownerhierarchyname+typeparam.resultdef.typesym.prettyname;
  310. end;
  311. end
  312. else
  313. begin
  314. Message(type_e_type_id_expected);
  315. result:=false;
  316. end;
  317. typeparam.free;
  318. first:=false;
  319. end;
  320. block_type:=old_block_type;
  321. end;
  322. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  323. var
  324. dummypos : tfileposinfo;
  325. begin
  326. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  327. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  328. end;
  329. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  330. var
  331. dummypos : tfileposinfo;
  332. begin
  333. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  334. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  335. end;
  336. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  337. procedure unset_forwarddef(def: tdef);
  338. var
  339. st : TSymtable;
  340. i : longint;
  341. begin
  342. case def.typ of
  343. procdef:
  344. tprocdef(def).forwarddef:=false;
  345. objectdef,
  346. recorddef:
  347. begin
  348. st:=def.getsymtable(gs_record);
  349. for i:=0 to st.deflist.count-1 do
  350. unset_forwarddef(tdef(st.deflist[i]));
  351. end;
  352. end;
  353. end;
  354. var
  355. st : TSymtable;
  356. srsym : tsym;
  357. pt2 : tnode;
  358. hadtypetoken,
  359. errorrecovery,
  360. found,
  361. first,
  362. err : boolean;
  363. errval,
  364. i,
  365. gencount : longint;
  366. genericdef,def : tstoreddef;
  367. generictype : ttypesym;
  368. genericdeflist : TFPObjectList;
  369. generictypelist : tfphashobjectlist;
  370. prettyname,specializename : ansistring;
  371. ufinalspecializename,
  372. countstr,genname,ugenname,finalspecializename : string;
  373. vmtbuilder : TVMTBuilder;
  374. specializest : tsymtable;
  375. item : tobject;
  376. old_current_structdef : tabstractrecorddef;
  377. old_current_genericdef,old_current_specializedef : tstoreddef;
  378. tempst : tglobalsymtable;
  379. old_block_type: tblock_type;
  380. hashedid: thashedidstring;
  381. state : tspecializationstate;
  382. hmodule : tmodule;
  383. oldcurrent_filepos : tfileposinfo;
  384. poslist : tfplist;
  385. recordbuf: tdynamicarray;
  386. begin
  387. { retrieve generic def that we are going to replace }
  388. genericdef:=tstoreddef(tt);
  389. tt:=nil;
  390. { either symname must be given or genericdef needs to be valid }
  391. errorrecovery:=false;
  392. if (symname='') and
  393. (not assigned(genericdef) or
  394. not assigned(genericdef.typesym) or
  395. (genericdef.typesym.typ<>typesym)) then
  396. begin
  397. errorrecovery:=true;
  398. tt:=generrordef;
  399. end;
  400. { Only parse the parameters for recovery or
  401. for recording in genericbuf }
  402. if errorrecovery then
  403. begin
  404. first:=assigned(parsedtype);
  405. if not first and not try_to_consume(_LT) then
  406. consume(_LSHARPBRACKET);
  407. gencount:=0;
  408. { handle "<>" }
  409. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  410. Message(type_e_type_id_expected)
  411. else
  412. repeat
  413. if not first then
  414. begin
  415. pt2:=factor(false,true);
  416. pt2.free;
  417. end;
  418. first:=false;
  419. inc(gencount);
  420. until not try_to_consume(_COMMA);
  421. if not try_to_consume(_GT) then
  422. consume(_RSHARPBRACKET);
  423. { we need to return a def that can later pass some checks like
  424. whether it's an interface or not }
  425. if not errorrecovery and
  426. (not assigned(tt) or (tt.typ=undefineddef)) then
  427. begin
  428. if (symname='') and genericdef.is_generic then
  429. { this happens in non-Delphi modes }
  430. tt:=genericdef
  431. else
  432. begin
  433. { find the corresponding generic symbol so that any checks
  434. done on the returned def will be handled correctly }
  435. str(gencount,countstr);
  436. if symname='' then
  437. genname:=ttypesym(genericdef.typesym).realname
  438. else
  439. genname:=symname;
  440. genname:=genname+'$'+countstr;
  441. ugenname:=upper(genname);
  442. { first check whether the found name is the same as that of
  443. the current def or one of its (generic) surrounding defs;
  444. this is necessary as the symbol of the generic can not yet
  445. be used for lookup as it still contains a reference to an
  446. errordef) }
  447. def:=current_genericdef;
  448. repeat
  449. if def.typ in [objectdef,recorddef] then
  450. if tabstractrecorddef(def).objname^=ugenname then
  451. begin
  452. tt:=def;
  453. break;
  454. end;
  455. def:=tstoreddef(def.owner.defowner);
  456. until not assigned(def) or not (df_generic in def.defoptions);
  457. { it's not part of the current object hierarchy, so search
  458. for the symbol }
  459. if not assigned(tt) then
  460. begin
  461. srsym:=nil;
  462. if not searchsym(ugenname,srsym,st) or
  463. (srsym.typ<>typesym) then
  464. begin
  465. identifier_not_found(genname);
  466. tt:=generrordef;
  467. exit;
  468. end;
  469. tt:=ttypesym(srsym).typedef;
  470. { this happens in non-Delphi modes if we encounter a
  471. specialization of the generic class or record we're
  472. currently parsing }
  473. if (tt.typ=errordef) and assigned(current_structdef) and
  474. (current_structdef.objname^=ugenname) then
  475. tt:=current_structdef;
  476. end;
  477. end;
  478. end;
  479. exit;
  480. end;
  481. if not assigned(parsedtype) and not try_to_consume(_LT) then
  482. begin
  483. consume(_LSHARPBRACKET);
  484. { handle "<>" }
  485. if (token=_GT) or (token=_RSHARPBRACKET) then
  486. begin
  487. Message(type_e_type_id_expected);
  488. if not try_to_consume(_GT) then
  489. try_to_consume(_RSHARPBRACKET);
  490. tt:=generrordef;
  491. exit;
  492. end;
  493. end;
  494. genericdeflist:=TFPObjectList.Create(false);
  495. poslist:=tfplist.create;
  496. { Parse type parameters }
  497. err:=not parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,parsedtype,parsedpos);
  498. if err then
  499. begin
  500. if not try_to_consume(_GT) then
  501. try_to_consume(_RSHARPBRACKET);
  502. genericdeflist.free;
  503. for i:=0 to poslist.count-1 do
  504. dispose(pfileposinfo(poslist[i]));
  505. poslist.free;
  506. tt:=generrordef;
  507. exit;
  508. end;
  509. { use the name of the symbol as procvars return a user friendly version
  510. of the name }
  511. if symname='' then
  512. genname:=ttypesym(genericdef.typesym).realname
  513. else
  514. genname:=symname;
  515. { in case of non-Delphi mode the type name could already be a generic
  516. def (but maybe the wrong one) }
  517. if assigned(genericdef) and
  518. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  519. begin
  520. { remove the type count suffix from the generic's name }
  521. for i:=Length(genname) downto 1 do
  522. if genname[i]='$' then
  523. begin
  524. genname:=copy(genname,1,i-1);
  525. break;
  526. end;
  527. { in case of a specialization we've only reached the specialization
  528. checksum yet }
  529. if df_specialization in genericdef.defoptions then
  530. for i:=length(genname) downto 1 do
  531. if genname[i]='$' then
  532. begin
  533. genname:=copy(genname,1,i-1);
  534. break;
  535. end;
  536. end
  537. else
  538. begin
  539. split_generic_name(genname,ugenname,gencount);
  540. if genname<>ugenname then
  541. genname:=ugenname;
  542. end;
  543. { search a generic with the given count of params }
  544. countstr:='';
  545. str(genericdeflist.Count,countstr);
  546. genname:=genname+'$'+countstr;
  547. ugenname:=upper(genname);
  548. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  549. begin
  550. if genericdef.owner.symtabletype = objectsymtable then
  551. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,srsym,st,[])
  552. else
  553. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,srsym,st);
  554. if not found then
  555. found:=searchsym(ugenname,srsym,st);
  556. end
  557. else
  558. found:=searchsym(ugenname,srsym,st);
  559. if not found or (srsym.typ<>typesym) then
  560. begin
  561. identifier_not_found(genname);
  562. if not try_to_consume(_GT) then
  563. try_to_consume(_RSHARPBRACKET);
  564. for i:=0 to poslist.count-1 do
  565. dispose(pfileposinfo(poslist[i]));
  566. poslist.free;
  567. genericdeflist.Free;
  568. tt:=generrordef;
  569. exit;
  570. end;
  571. { we've found the correct def }
  572. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  573. if not check_generic_constraints(genericdef,genericdeflist,poslist) then
  574. begin
  575. { the parameters didn't fit the constraints, so don't continue with the
  576. specialization }
  577. genericdeflist.free;
  578. for i:=0 to poslist.count-1 do
  579. dispose(pfileposinfo(poslist[i]));
  580. poslist.free;
  581. tt:=generrordef;
  582. if not try_to_consume(_GT) then
  583. try_to_consume(_RSHARPBRACKET);
  584. exit;
  585. end;
  586. { build the new type's name }
  587. finalspecializename:=generate_generic_name(genname,specializename);
  588. ufinalspecializename:=upper(finalspecializename);
  589. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  590. { select the symtable containing the params }
  591. case genericdef.typ of
  592. procdef:
  593. st:=genericdef.GetSymtable(gs_para);
  594. objectdef,
  595. recorddef:
  596. st:=genericdef.GetSymtable(gs_record);
  597. arraydef:
  598. st:=tarraydef(genericdef).symtable;
  599. procvardef:
  600. st:=genericdef.GetSymtable(gs_para);
  601. else
  602. internalerror(200511182);
  603. end;
  604. generictypelist:=tfphashobjectlist.create(false);
  605. { build the list containing the types for the generic params }
  606. if not assigned(genericdef.genericparas) then
  607. internalerror(2013092601);
  608. if genericdeflist.count<>genericdef.genericparas.count then
  609. internalerror(2013092603);
  610. for i:=0 to genericdef.genericparas.Count-1 do
  611. begin
  612. srsym:=tsym(genericdef.genericparas[i]);
  613. if not (sp_generic_para in srsym.symoptions) then
  614. internalerror(2013092602);
  615. generictypelist.add(srsym.realname,tdef(genericdeflist[i]).typesym);
  616. end;
  617. { Special case if we are referencing the current defined object }
  618. if assigned(current_structdef) and
  619. (current_structdef.objname^=ufinalspecializename) then
  620. tt:=current_structdef;
  621. { decide in which symtable to put the specialization }
  622. if parse_generic then
  623. begin
  624. if not assigned(current_genericdef) then
  625. internalerror(2014050901);
  626. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  627. { if we are parsing the definition of a method we specialize into
  628. the local symtable of it }
  629. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  630. else
  631. { we specialize the partial specialization into the symtable of the currently parsed
  632. generic }
  633. case current_genericdef.typ of
  634. procvardef,
  635. procdef:
  636. specializest:=current_genericdef.getsymtable(gs_local);
  637. objectdef,
  638. recorddef:
  639. specializest:=current_genericdef.getsymtable(gs_record);
  640. arraydef:
  641. specializest:=tarraydef(current_genericdef).symtable;
  642. else
  643. internalerror(2014050902);
  644. end;
  645. end
  646. else
  647. if current_module.is_unit and current_module.in_interface then
  648. specializest:=current_module.globalsymtable
  649. else
  650. specializest:=current_module.localsymtable;
  651. if not assigned(specializest) then
  652. internalerror(2014050910);
  653. { Can we reuse an already specialized type? }
  654. { for this first check whether we are currently specializing a nested
  655. type of the current (main) specialization (this is necessary, because
  656. during that time the symbol of the main specialization will still
  657. contain a reference to an errordef) }
  658. if not assigned(tt) and assigned(current_specializedef) then
  659. begin
  660. def:=current_specializedef;
  661. repeat
  662. if def.typ in [objectdef,recorddef] then
  663. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  664. tt:=def;
  665. break;
  666. end;
  667. def:=tstoreddef(def.owner.defowner);
  668. until not assigned(def) or not (df_specialization in def.defoptions);
  669. end;
  670. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  671. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  672. parsing anyway), so for now we treat those still as generic defs without doing a partial
  673. specialization }
  674. if not assigned(tt) then
  675. begin
  676. def:=current_genericdef;
  677. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  678. begin
  679. if def=genericdef then
  680. begin
  681. tt:=def;
  682. break;
  683. end;
  684. def:=tstoreddef(def.owner.defowner);
  685. end;
  686. end;
  687. { now check whether there is a specialization somewhere else }
  688. if not assigned(tt) then
  689. begin
  690. hashedid.id:=ufinalspecializename;
  691. srsym:=tsym(specializest.findwithhash(hashedid));
  692. if assigned(srsym) then
  693. begin
  694. if srsym.typ<>typesym then
  695. internalerror(200710171);
  696. tt:=ttypesym(srsym).typedef;
  697. end
  698. else
  699. { the generic could have been specialized in the globalsymtable
  700. already, so search there as well }
  701. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  702. begin
  703. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  704. if assigned(srsym) then
  705. begin
  706. if srsym.typ<>typesym then
  707. internalerror(2011121101);
  708. tt:=ttypesym(srsym).typedef;
  709. end;
  710. end;
  711. end;
  712. if not assigned(tt) then
  713. begin
  714. specialization_init(genericdef,state);
  715. { push a temporary global symtable so that the specialization is
  716. added to the correct symtable; this symtable does not contain
  717. any other symbols, so that the type resolution can not be
  718. influenced by symbols in the current unit }
  719. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  720. symtablestack.push(tempst);
  721. { Reparse the original type definition }
  722. if not err then
  723. begin
  724. old_current_specializedef:=nil;
  725. old_current_genericdef:=nil;
  726. old_current_structdef:=nil;
  727. if parse_class_parent then
  728. begin
  729. old_current_structdef:=current_structdef;
  730. old_current_genericdef:=current_genericdef;
  731. old_current_specializedef:=current_specializedef;
  732. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  733. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  734. else
  735. current_structdef:=nil;
  736. current_genericdef:=nil;
  737. current_specializedef:=nil;
  738. end;
  739. maybe_add_waiting_unit(genericdef);
  740. { First a new typesym so we can reuse this specialization and
  741. references to this specialization can be handled }
  742. srsym:=ctypesym.create(finalspecializename,generrordef);
  743. specializest.insert(srsym);
  744. { specializations are declarations as such it is the wisest to
  745. declare set the blocktype to "type"; otherwise we'll
  746. experience unexpected side effects like the addition of
  747. classrefdefs if we have a generic that's derived from another
  748. generic }
  749. old_block_type:=block_type;
  750. block_type:=bt_type;
  751. if not assigned(genericdef.generictokenbuf) then
  752. internalerror(200511171);
  753. hmodule:=find_module_from_symtable(genericdef.owner);
  754. if hmodule=nil then
  755. internalerror(2012051202);
  756. oldcurrent_filepos:=current_filepos;
  757. { use the index the module got from the current compilation process }
  758. current_filepos.moduleindex:=hmodule.unit_index;
  759. current_tokenpos:=current_filepos;
  760. if parse_generic then
  761. begin
  762. recordbuf:=current_scanner.recordtokenbuf;
  763. current_scanner.recordtokenbuf:=nil;
  764. end
  765. else
  766. recordbuf:=nil;
  767. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  768. hadtypetoken:=false;
  769. read_named_type(tt,srsym,genericdef,generictypelist,false,hadtypetoken);
  770. current_filepos:=oldcurrent_filepos;
  771. ttypesym(srsym).typedef:=tt;
  772. tt.typesym:=srsym;
  773. if _prettyname<>'' then
  774. ttypesym(tt.typesym).fprettyname:=_prettyname
  775. else
  776. ttypesym(tt.typesym).fprettyname:=prettyname;
  777. { Note regarding hint directives:
  778. There is no need to remove the flags for them from the
  779. specialized generic symbol, because hint directives that
  780. follow the specialization are handled by the code in
  781. pdecl.types_dec and added to the type symbol.
  782. E.g.: TFoo = TBar<Blubb> deprecated;
  783. Here the symbol TBar$1$Blubb will contain the
  784. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  785. case tt.typ of
  786. { Build VMT indexes for classes and read hint directives }
  787. objectdef:
  788. begin
  789. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  790. consume(_SEMICOLON);
  791. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  792. vmtbuilder.generate_vmt;
  793. vmtbuilder.free;
  794. end;
  795. { handle params, calling convention, etc }
  796. procvardef:
  797. begin
  798. if not check_proc_directive(true) then
  799. begin
  800. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  801. consume(_SEMICOLON);
  802. end;
  803. parse_var_proc_directives(ttypesym(srsym));
  804. handle_calling_convention(tprocvardef(tt));
  805. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  806. consume(_SEMICOLON);
  807. end;
  808. else
  809. { parse hint directives for records and arrays }
  810. begin
  811. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  812. consume(_SEMICOLON);
  813. end;
  814. end;
  815. { Consume the semicolon if it is also recorded }
  816. try_to_consume(_SEMICOLON);
  817. if assigned(recordbuf) then
  818. begin
  819. if assigned(current_scanner.recordtokenbuf) then
  820. internalerror(2014050909);
  821. current_scanner.recordtokenbuf:=recordbuf;
  822. end;
  823. block_type:=old_block_type;
  824. if parse_class_parent then
  825. begin
  826. current_structdef:=old_current_structdef;
  827. current_genericdef:=old_current_genericdef;
  828. current_specializedef:=old_current_specializedef;
  829. end;
  830. end;
  831. { extract all created symbols and defs from the temporary symtable
  832. and add them to the specializest }
  833. for i:=tempst.SymList.Count-1 downto 0 do
  834. begin
  835. item:=tempst.SymList.Items[i];
  836. { using changeowner the symbol is automatically added to the
  837. new symtable }
  838. tsym(item).ChangeOwner(specializest);
  839. end;
  840. for i:=tempst.DefList.Count-1 downto 0 do
  841. begin
  842. item:=tempst.DefList.Items[i];
  843. { using changeowner the def is automatically added to the new
  844. symtable }
  845. tdef(item).ChangeOwner(specializest);
  846. { for partial specializations we implicitely declare any methods as having their
  847. implementations although we'll not specialize them in reality }
  848. if parse_generic then
  849. unset_forwarddef(tdef(item));
  850. end;
  851. { if a generic was declared during the specialization we need to
  852. flag the specialize symtable accordingly }
  853. if sto_has_generic in tempst.tableoptions then
  854. specializest.includeoption(sto_has_generic);
  855. tempst.free;
  856. specialization_done(state);
  857. end;
  858. if not (token in [_GT, _RSHARPBRACKET]) then
  859. begin
  860. consume(_RSHARPBRACKET);
  861. exit;
  862. end
  863. else
  864. consume(token);
  865. genericdeflist.free;
  866. generictypelist.free;
  867. if assigned(genericdef) then
  868. begin
  869. { check the hints of the found generic symbol }
  870. srsym:=genericdef.typesym;
  871. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  872. end;
  873. end;
  874. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  875. var
  876. generictype : ttypesym;
  877. i,firstidx : longint;
  878. srsymtable : tsymtable;
  879. basedef,def : tdef;
  880. defname : tidstring;
  881. allowconstructor,
  882. doconsume : boolean;
  883. constraintdata : tgenericconstraintdata;
  884. old_block_type : tblock_type;
  885. begin
  886. result:=tfphashobjectlist.create(false);
  887. firstidx:=0;
  888. old_block_type:=block_type;
  889. block_type:=bt_type;
  890. repeat
  891. if token=_ID then
  892. begin
  893. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  894. include(generictype.symoptions,sp_generic_para);
  895. result.add(orgpattern,generictype);
  896. end;
  897. consume(_ID);
  898. if try_to_consume(_COLON) then
  899. begin
  900. if not allowconstraints then
  901. { TODO }
  902. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  903. { construct a name which can be used for a type specification }
  904. constraintdata:=tgenericconstraintdata.create;
  905. defname:='';
  906. str(current_module.deflist.count,defname);
  907. defname:='$gendef'+defname;
  908. allowconstructor:=m_delphi in current_settings.modeswitches;
  909. basedef:=generrordef;
  910. repeat
  911. doconsume:=true;
  912. case token of
  913. _CONSTRUCTOR:
  914. begin
  915. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  916. Message(parser_e_illegal_expression);
  917. include(constraintdata.flags,gcf_constructor);
  918. allowconstructor:=false;
  919. end;
  920. _CLASS:
  921. begin
  922. if gcf_class in constraintdata.flags then
  923. Message(parser_e_illegal_expression);
  924. if basedef=generrordef then
  925. include(constraintdata.flags,gcf_class)
  926. else
  927. Message(parser_e_illegal_expression);
  928. end;
  929. _RECORD:
  930. begin
  931. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  932. or (constraintdata.interfaces.count>0) then
  933. Message(parser_e_illegal_expression)
  934. else
  935. begin
  936. srsymtable:=trecordsymtable.create(defname,0);
  937. basedef:=crecorddef.create(defname,srsymtable);
  938. include(constraintdata.flags,gcf_record);
  939. allowconstructor:=false;
  940. end;
  941. end;
  942. else
  943. begin
  944. { after single_type "token" is the trailing ",", ";" or
  945. ">"! }
  946. doconsume:=false;
  947. { def is already set to a class or record }
  948. if gcf_record in constraintdata.flags then
  949. Message(parser_e_illegal_expression);
  950. single_type(def, [stoAllowSpecialization]);
  951. { only types that are inheritable are allowed }
  952. if (def.typ<>objectdef) or
  953. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  954. Message1(type_e_class_or_interface_type_expected,def.typename)
  955. else
  956. case tobjectdef(def).objecttype of
  957. odt_class,
  958. odt_javaclass:
  959. begin
  960. if gcf_class in constraintdata.flags then
  961. { "class" + concrete class is not allowed }
  962. Message(parser_e_illegal_expression)
  963. else
  964. { do we already have a concrete class? }
  965. if basedef<>generrordef then
  966. Message(parser_e_illegal_expression)
  967. else
  968. basedef:=def;
  969. end;
  970. odt_interfacecom,
  971. odt_interfacecorba,
  972. odt_interfacejava,
  973. odt_dispinterface:
  974. constraintdata.interfaces.add(def);
  975. end;
  976. end;
  977. end;
  978. if doconsume then
  979. consume(token);
  980. until not try_to_consume(_COMMA);
  981. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  982. (constraintdata.interfaces.count>1) or
  983. (
  984. (basedef.typ=objectdef) and
  985. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  986. ) then
  987. begin
  988. if basedef.typ=errordef then
  989. { don't pass an errordef as a parent to a tobjectdef }
  990. basedef:=class_tobject
  991. else
  992. if (basedef.typ<>objectdef) or
  993. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  994. internalerror(2012101101);
  995. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef));
  996. for i:=0 to constraintdata.interfaces.count-1 do
  997. tobjectdef(basedef).implementedinterfaces.add(
  998. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  999. end
  1000. else
  1001. if constraintdata.interfaces.count=1 then
  1002. begin
  1003. if basedef.typ<>errordef then
  1004. internalerror(2013021601);
  1005. def:=tdef(constraintdata.interfaces[0]);
  1006. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def));
  1007. constraintdata.interfaces.delete(0);
  1008. end;
  1009. if basedef.typ<>errordef then
  1010. with tstoreddef(basedef) do
  1011. begin
  1012. genconstraintdata:=tgenericconstraintdata.create;
  1013. genconstraintdata.flags:=constraintdata.flags;
  1014. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1015. include(defoptions,df_genconstraint);
  1016. end;
  1017. for i:=firstidx to result.count-1 do
  1018. ttypesym(result[i]).typedef:=basedef;
  1019. { we need a typesym in case we do a Delphi-mode inline
  1020. specialization with this parameter; so just use the first sym }
  1021. if not assigned(basedef.typesym) then
  1022. basedef.typesym:=ttypesym(result[firstidx]);
  1023. firstidx:=result.count;
  1024. constraintdata.free;
  1025. end;
  1026. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1027. block_type:=old_block_type;
  1028. end;
  1029. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1030. var
  1031. i : longint;
  1032. generictype,sym : ttypesym;
  1033. st : tsymtable;
  1034. begin
  1035. def.genericdef:=genericdef;
  1036. if not assigned(genericlist) then
  1037. exit;
  1038. if assigned(genericdef) then
  1039. include(def.defoptions,df_specialization)
  1040. else
  1041. if genericlist.count>0 then
  1042. include(def.defoptions,df_generic);
  1043. case def.typ of
  1044. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1045. arraydef: st:=tarraydef(def).symtable;
  1046. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1047. else
  1048. internalerror(201101020);
  1049. end;
  1050. if (genericlist.count>0) and not assigned(def.genericparas) then
  1051. def.genericparas:=tfphashobjectlist.create(false);
  1052. for i:=0 to genericlist.count-1 do
  1053. begin
  1054. generictype:=ttypesym(genericlist[i]);
  1055. if assigned(generictype.owner) then
  1056. begin
  1057. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef);
  1058. st.insert(sym);
  1059. include(sym.symoptions,sp_generic_para);
  1060. end
  1061. else
  1062. begin
  1063. st.insert(generictype);
  1064. include(generictype.symoptions,sp_generic_para);
  1065. end;
  1066. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1067. end;
  1068. end;
  1069. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1070. var
  1071. gensym : ttypesym;
  1072. begin
  1073. { for generics in non-Delphi modes we insert a private type symbol
  1074. that has the same base name as the currently parsed generic and
  1075. that references this defs }
  1076. if not (m_delphi in current_settings.modeswitches) and
  1077. (
  1078. (
  1079. parse_generic and
  1080. assigned(genericlist) and
  1081. (genericlist.count>0)
  1082. ) or
  1083. (
  1084. assigned(current_specializedef) and
  1085. assigned(current_structdef.genericdef) and
  1086. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1087. (pos('$',name)>0)
  1088. )
  1089. ) then
  1090. begin
  1091. { we need to pass nil as def here, because the constructor wants
  1092. to set the typesym of the def which is not what we want }
  1093. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  1094. gensym.typedef:=current_structdef;
  1095. include(gensym.symoptions,sp_internal);
  1096. { the symbol should be only visible to the generic class
  1097. itself }
  1098. gensym.visibility:=vis_strictprivate;
  1099. symtablestack.top.insert(gensym);
  1100. end;
  1101. end;
  1102. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  1103. var
  1104. crc : cardinal;
  1105. begin
  1106. if specializename='' then
  1107. internalerror(2012061901);
  1108. { build the new type's name }
  1109. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1110. result:=name+'$crc'+hexstr(crc,8);
  1111. end;
  1112. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1113. var
  1114. i,code : longint;
  1115. countstr : string;
  1116. begin
  1117. for i:=length(name) downto 1 do
  1118. if name[i]='$' then
  1119. begin
  1120. nongeneric:=copy(name,1,i-1);
  1121. countstr:=copy(name,i+1,length(name)-i);
  1122. val(countstr,count,code);
  1123. if code<>0 then
  1124. break;
  1125. exit;
  1126. end;
  1127. nongeneric:=name;
  1128. count:=0;
  1129. end;
  1130. function resolve_generic_dummysym(const name:tidstring):tsym;
  1131. var
  1132. list : tfpobjectlist;
  1133. begin
  1134. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1135. if assigned(list) and (list.count>0) then
  1136. result:=tgenericdummyentry(list.last).resolvedsym
  1137. else
  1138. result:=nil;
  1139. end;
  1140. function could_be_generic(const name:tidstring):boolean;
  1141. begin
  1142. result:=(name<>'') and
  1143. (current_module.genericdummysyms.findindexof(name)>=0);
  1144. end;
  1145. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1146. var
  1147. pu : tused_unit;
  1148. hmodule : tmodule;
  1149. unitsyms : TFPHashObjectList;
  1150. sym : tsym;
  1151. i : Integer;
  1152. begin
  1153. if not assigned(genericdef) then
  1154. internalerror(200705151);
  1155. { Setup symtablestack at definition time
  1156. to get types right, however this is not perfect, we should probably record
  1157. the resolved symbols }
  1158. state.oldsymtablestack:=symtablestack;
  1159. state.oldextendeddefs:=current_module.extendeddefs;
  1160. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1161. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1162. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1163. symtablestack:=tdefawaresymtablestack.create;
  1164. hmodule:=find_module_from_symtable(genericdef.owner);
  1165. if hmodule=nil then
  1166. internalerror(200705152);
  1167. { collect all unit syms in the generic's unit as we need to establish
  1168. their unitsym.module link again so that unit identifiers can be used }
  1169. unitsyms:=tfphashobjectlist.create(false);
  1170. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1171. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1172. begin
  1173. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1174. if sym.typ=unitsym then
  1175. unitsyms.add(upper(sym.realname),sym);
  1176. end;
  1177. { add all units if we are specializing inside the current unit (as the
  1178. generic could have been declared in the implementation part), but load
  1179. only interface units, if we are in a different unit as then the generic
  1180. needs to be in the interface section }
  1181. pu:=tused_unit(hmodule.used_units.first);
  1182. while assigned(pu) do
  1183. begin
  1184. if not assigned(pu.u.globalsymtable) then
  1185. { in certain circular, but valid unit constellations it can happen
  1186. that we specialize a generic in a different unit that was used
  1187. in the implementation section of the generic's unit and were the
  1188. interface is still being parsed and thus the localsymtable is in
  1189. reality the global symtable }
  1190. if pu.u.in_interface then
  1191. symtablestack.push(pu.u.localsymtable)
  1192. else
  1193. internalerror(200705153)
  1194. else
  1195. symtablestack.push(pu.u.globalsymtable);
  1196. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1197. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1198. tunitsym(sym).module:=pu.u;
  1199. pu:=tused_unit(pu.next);
  1200. end;
  1201. unitsyms.free;
  1202. if assigned(hmodule.globalsymtable) then
  1203. symtablestack.push(hmodule.globalsymtable);
  1204. { push the localsymtable if needed }
  1205. if (hmodule<>current_module) or not current_module.in_interface then
  1206. symtablestack.push(hmodule.localsymtable);
  1207. end;
  1208. procedure specialization_done(var state: tspecializationstate);
  1209. begin
  1210. { Restore symtablestack }
  1211. current_module.extendeddefs.free;
  1212. current_module.extendeddefs:=state.oldextendeddefs;
  1213. current_module.genericdummysyms.free;
  1214. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1215. symtablestack.free;
  1216. symtablestack:=state.oldsymtablestack;
  1217. { clear the state record to be on the safe side }
  1218. fillchar(state, sizeof(state), 0);
  1219. end;
  1220. end.