pgenutil.pas 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. end
  555. else
  556. found:=searchsym(ugenname,srsym,st);
  557. if not found or (srsym.typ<>typesym) then
  558. begin
  559. identifier_not_found(genname);
  560. if not try_to_consume(_GT) then
  561. try_to_consume(_RSHARPBRACKET);
  562. for i:=0 to poslist.count-1 do
  563. dispose(pfileposinfo(poslist[i]));
  564. poslist.free;
  565. genericdeflist.Free;
  566. tt:=generrordef;
  567. exit;
  568. end;
  569. { we've found the correct def }
  570. genericdef:=tstoreddef(ttypesym(srsym).typedef);
  571. if not check_generic_constraints(genericdef,genericdeflist,poslist) then
  572. begin
  573. { the parameters didn't fit the constraints, so don't continue with the
  574. specialization }
  575. genericdeflist.free;
  576. for i:=0 to poslist.count-1 do
  577. dispose(pfileposinfo(poslist[i]));
  578. poslist.free;
  579. tt:=generrordef;
  580. if not try_to_consume(_GT) then
  581. try_to_consume(_RSHARPBRACKET);
  582. exit;
  583. end;
  584. { build the new type's name }
  585. finalspecializename:=generate_generic_name(genname,specializename);
  586. ufinalspecializename:=upper(finalspecializename);
  587. prettyname:=genericdef.typesym.prettyname+'<'+prettyname+'>';
  588. { select the symtable containing the params }
  589. case genericdef.typ of
  590. procdef:
  591. st:=genericdef.GetSymtable(gs_para);
  592. objectdef,
  593. recorddef:
  594. st:=genericdef.GetSymtable(gs_record);
  595. arraydef:
  596. st:=tarraydef(genericdef).symtable;
  597. procvardef:
  598. st:=genericdef.GetSymtable(gs_para);
  599. else
  600. internalerror(200511182);
  601. end;
  602. generictypelist:=tfphashobjectlist.create(false);
  603. { build the list containing the types for the generic params }
  604. if not assigned(genericdef.genericparas) then
  605. internalerror(2013092601);
  606. if genericdeflist.count<>genericdef.genericparas.count then
  607. internalerror(2013092603);
  608. for i:=0 to genericdef.genericparas.Count-1 do
  609. begin
  610. srsym:=tsym(genericdef.genericparas[i]);
  611. if not (sp_generic_para in srsym.symoptions) then
  612. internalerror(2013092602);
  613. generictypelist.add(srsym.realname,tdef(genericdeflist[i]).typesym);
  614. end;
  615. { Special case if we are referencing the current defined object }
  616. if assigned(current_structdef) and
  617. (current_structdef.objname^=ufinalspecializename) then
  618. tt:=current_structdef;
  619. { decide in which symtable to put the specialization }
  620. if parse_generic then
  621. begin
  622. if not assigned(current_genericdef) then
  623. internalerror(2014050901);
  624. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  625. { if we are parsing the definition of a method we specialize into
  626. the local symtable of it }
  627. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  628. else
  629. { we specialize the partial specialization into the symtable of the currently parsed
  630. generic }
  631. case current_genericdef.typ of
  632. procvardef,
  633. procdef:
  634. specializest:=current_genericdef.getsymtable(gs_local);
  635. objectdef,
  636. recorddef:
  637. specializest:=current_genericdef.getsymtable(gs_record);
  638. arraydef:
  639. specializest:=tarraydef(current_genericdef).symtable;
  640. else
  641. internalerror(2014050902);
  642. end;
  643. end
  644. else
  645. if current_module.is_unit and current_module.in_interface then
  646. specializest:=current_module.globalsymtable
  647. else
  648. specializest:=current_module.localsymtable;
  649. if not assigned(specializest) then
  650. internalerror(2014050910);
  651. { Can we reuse an already specialized type? }
  652. { for this first check whether we are currently specializing a nested
  653. type of the current (main) specialization (this is necessary, because
  654. during that time the symbol of the main specialization will still
  655. contain a reference to an errordef) }
  656. if not assigned(tt) and assigned(current_specializedef) then
  657. begin
  658. def:=current_specializedef;
  659. repeat
  660. if def.typ in [objectdef,recorddef] then
  661. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  662. tt:=def;
  663. break;
  664. end;
  665. def:=tstoreddef(def.owner.defowner);
  666. until not assigned(def) or not (df_specialization in def.defoptions);
  667. end;
  668. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  669. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  670. parsing anyway), so for now we treat those still as generic defs without doing a partial
  671. specialization }
  672. if not assigned(tt) then
  673. begin
  674. def:=current_genericdef;
  675. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  676. begin
  677. if def=genericdef then
  678. begin
  679. tt:=def;
  680. break;
  681. end;
  682. def:=tstoreddef(def.owner.defowner);
  683. end;
  684. end;
  685. { now check whether there is a specialization somewhere else }
  686. if not assigned(tt) then
  687. begin
  688. hashedid.id:=ufinalspecializename;
  689. srsym:=tsym(specializest.findwithhash(hashedid));
  690. if assigned(srsym) then
  691. begin
  692. if srsym.typ<>typesym then
  693. internalerror(200710171);
  694. tt:=ttypesym(srsym).typedef;
  695. end
  696. else
  697. { the generic could have been specialized in the globalsymtable
  698. already, so search there as well }
  699. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  700. begin
  701. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  702. if assigned(srsym) then
  703. begin
  704. if srsym.typ<>typesym then
  705. internalerror(2011121101);
  706. tt:=ttypesym(srsym).typedef;
  707. end;
  708. end;
  709. end;
  710. if not assigned(tt) then
  711. begin
  712. specialization_init(genericdef,state);
  713. { push a temporary global symtable so that the specialization is
  714. added to the correct symtable; this symtable does not contain
  715. any other symbols, so that the type resolution can not be
  716. influenced by symbols in the current unit }
  717. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  718. symtablestack.push(tempst);
  719. { Reparse the original type definition }
  720. if not err then
  721. begin
  722. old_current_specializedef:=nil;
  723. old_current_genericdef:=nil;
  724. old_current_structdef:=nil;
  725. if parse_class_parent then
  726. begin
  727. old_current_structdef:=current_structdef;
  728. old_current_genericdef:=current_genericdef;
  729. old_current_specializedef:=current_specializedef;
  730. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  731. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  732. else
  733. current_structdef:=nil;
  734. current_genericdef:=nil;
  735. current_specializedef:=nil;
  736. end;
  737. maybe_add_waiting_unit(genericdef);
  738. { First a new typesym so we can reuse this specialization and
  739. references to this specialization can be handled }
  740. srsym:=ctypesym.create(finalspecializename,generrordef);
  741. specializest.insert(srsym);
  742. { specializations are declarations as such it is the wisest to
  743. declare set the blocktype to "type"; otherwise we'll
  744. experience unexpected side effects like the addition of
  745. classrefdefs if we have a generic that's derived from another
  746. generic }
  747. old_block_type:=block_type;
  748. block_type:=bt_type;
  749. if not assigned(genericdef.generictokenbuf) then
  750. internalerror(200511171);
  751. hmodule:=find_module_from_symtable(genericdef.owner);
  752. if hmodule=nil then
  753. internalerror(2012051202);
  754. oldcurrent_filepos:=current_filepos;
  755. { use the index the module got from the current compilation process }
  756. current_filepos.moduleindex:=hmodule.unit_index;
  757. current_tokenpos:=current_filepos;
  758. if parse_generic then
  759. begin
  760. recordbuf:=current_scanner.recordtokenbuf;
  761. current_scanner.recordtokenbuf:=nil;
  762. end
  763. else
  764. recordbuf:=nil;
  765. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  766. hadtypetoken:=false;
  767. read_named_type(tt,srsym,genericdef,generictypelist,false,hadtypetoken);
  768. current_filepos:=oldcurrent_filepos;
  769. ttypesym(srsym).typedef:=tt;
  770. tt.typesym:=srsym;
  771. if _prettyname<>'' then
  772. ttypesym(tt.typesym).fprettyname:=_prettyname
  773. else
  774. ttypesym(tt.typesym).fprettyname:=prettyname;
  775. { Note regarding hint directives:
  776. There is no need to remove the flags for them from the
  777. specialized generic symbol, because hint directives that
  778. follow the specialization are handled by the code in
  779. pdecl.types_dec and added to the type symbol.
  780. E.g.: TFoo = TBar<Blubb> deprecated;
  781. Here the symbol TBar$1$Blubb will contain the
  782. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  783. case tt.typ of
  784. { Build VMT indexes for classes and read hint directives }
  785. objectdef:
  786. begin
  787. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  788. consume(_SEMICOLON);
  789. vmtbuilder:=TVMTBuilder.Create(tobjectdef(tt));
  790. vmtbuilder.generate_vmt;
  791. vmtbuilder.free;
  792. end;
  793. { handle params, calling convention, etc }
  794. procvardef:
  795. begin
  796. if not check_proc_directive(true) then
  797. begin
  798. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  799. consume(_SEMICOLON);
  800. end;
  801. parse_var_proc_directives(ttypesym(srsym));
  802. handle_calling_convention(tprocvardef(tt));
  803. if try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg) then
  804. consume(_SEMICOLON);
  805. end;
  806. else
  807. { parse hint directives for records and arrays }
  808. begin
  809. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  810. consume(_SEMICOLON);
  811. end;
  812. end;
  813. { Consume the semicolon if it is also recorded }
  814. try_to_consume(_SEMICOLON);
  815. if assigned(recordbuf) then
  816. begin
  817. if assigned(current_scanner.recordtokenbuf) then
  818. internalerror(2014050909);
  819. current_scanner.recordtokenbuf:=recordbuf;
  820. end;
  821. block_type:=old_block_type;
  822. if parse_class_parent then
  823. begin
  824. current_structdef:=old_current_structdef;
  825. current_genericdef:=old_current_genericdef;
  826. current_specializedef:=old_current_specializedef;
  827. end;
  828. end;
  829. { extract all created symbols and defs from the temporary symtable
  830. and add them to the specializest }
  831. for i:=tempst.SymList.Count-1 downto 0 do
  832. begin
  833. item:=tempst.SymList.Items[i];
  834. { using changeowner the symbol is automatically added to the
  835. new symtable }
  836. tsym(item).ChangeOwner(specializest);
  837. end;
  838. for i:=tempst.DefList.Count-1 downto 0 do
  839. begin
  840. item:=tempst.DefList.Items[i];
  841. { using changeowner the def is automatically added to the new
  842. symtable }
  843. tdef(item).ChangeOwner(specializest);
  844. { for partial specializations we implicitely declare any methods as having their
  845. implementations although we'll not specialize them in reality }
  846. if parse_generic then
  847. unset_forwarddef(tdef(item));
  848. end;
  849. { if a generic was declared during the specialization we need to
  850. flag the specialize symtable accordingly }
  851. if sto_has_generic in tempst.tableoptions then
  852. specializest.includeoption(sto_has_generic);
  853. tempst.free;
  854. specialization_done(state);
  855. end;
  856. if not (token in [_GT, _RSHARPBRACKET]) then
  857. begin
  858. consume(_RSHARPBRACKET);
  859. exit;
  860. end
  861. else
  862. consume(token);
  863. genericdeflist.free;
  864. generictypelist.free;
  865. if assigned(genericdef) then
  866. begin
  867. { check the hints of the found generic symbol }
  868. srsym:=genericdef.typesym;
  869. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  870. end;
  871. end;
  872. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  873. var
  874. generictype : ttypesym;
  875. i,firstidx : longint;
  876. srsymtable : tsymtable;
  877. basedef,def : tdef;
  878. defname : tidstring;
  879. allowconstructor,
  880. doconsume : boolean;
  881. constraintdata : tgenericconstraintdata;
  882. old_block_type : tblock_type;
  883. begin
  884. result:=tfphashobjectlist.create(false);
  885. firstidx:=0;
  886. old_block_type:=block_type;
  887. block_type:=bt_type;
  888. repeat
  889. if token=_ID then
  890. begin
  891. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  892. include(generictype.symoptions,sp_generic_para);
  893. result.add(orgpattern,generictype);
  894. end;
  895. consume(_ID);
  896. if try_to_consume(_COLON) then
  897. begin
  898. if not allowconstraints then
  899. { TODO }
  900. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  901. { construct a name which can be used for a type specification }
  902. constraintdata:=tgenericconstraintdata.create;
  903. defname:='';
  904. str(current_module.deflist.count,defname);
  905. defname:='$gendef'+defname;
  906. allowconstructor:=m_delphi in current_settings.modeswitches;
  907. basedef:=generrordef;
  908. repeat
  909. doconsume:=true;
  910. case token of
  911. _CONSTRUCTOR:
  912. begin
  913. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  914. Message(parser_e_illegal_expression);
  915. include(constraintdata.flags,gcf_constructor);
  916. allowconstructor:=false;
  917. end;
  918. _CLASS:
  919. begin
  920. if gcf_class in constraintdata.flags then
  921. Message(parser_e_illegal_expression);
  922. if basedef=generrordef then
  923. include(constraintdata.flags,gcf_class)
  924. else
  925. Message(parser_e_illegal_expression);
  926. end;
  927. _RECORD:
  928. begin
  929. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  930. or (constraintdata.interfaces.count>0) then
  931. Message(parser_e_illegal_expression)
  932. else
  933. begin
  934. srsymtable:=trecordsymtable.create(defname,0);
  935. basedef:=crecorddef.create(defname,srsymtable);
  936. include(constraintdata.flags,gcf_record);
  937. allowconstructor:=false;
  938. end;
  939. end;
  940. else
  941. begin
  942. { after single_type "token" is the trailing ",", ";" or
  943. ">"! }
  944. doconsume:=false;
  945. { def is already set to a class or record }
  946. if gcf_record in constraintdata.flags then
  947. Message(parser_e_illegal_expression);
  948. single_type(def, [stoAllowSpecialization]);
  949. { only types that are inheritable are allowed }
  950. if (def.typ<>objectdef) or
  951. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  952. Message1(type_e_class_or_interface_type_expected,def.typename)
  953. else
  954. case tobjectdef(def).objecttype of
  955. odt_class,
  956. odt_javaclass:
  957. begin
  958. if gcf_class in constraintdata.flags then
  959. { "class" + concrete class is not allowed }
  960. Message(parser_e_illegal_expression)
  961. else
  962. { do we already have a concrete class? }
  963. if basedef<>generrordef then
  964. Message(parser_e_illegal_expression)
  965. else
  966. basedef:=def;
  967. end;
  968. odt_interfacecom,
  969. odt_interfacecorba,
  970. odt_interfacejava,
  971. odt_dispinterface:
  972. constraintdata.interfaces.add(def);
  973. end;
  974. end;
  975. end;
  976. if doconsume then
  977. consume(token);
  978. until not try_to_consume(_COMMA);
  979. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  980. (constraintdata.interfaces.count>1) or
  981. (
  982. (basedef.typ=objectdef) and
  983. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  984. ) then
  985. begin
  986. if basedef.typ=errordef then
  987. { don't pass an errordef as a parent to a tobjectdef }
  988. basedef:=class_tobject
  989. else
  990. if (basedef.typ<>objectdef) or
  991. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  992. internalerror(2012101101);
  993. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef));
  994. for i:=0 to constraintdata.interfaces.count-1 do
  995. tobjectdef(basedef).implementedinterfaces.add(
  996. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  997. end
  998. else
  999. if constraintdata.interfaces.count=1 then
  1000. begin
  1001. if basedef.typ<>errordef then
  1002. internalerror(2013021601);
  1003. def:=tdef(constraintdata.interfaces[0]);
  1004. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def));
  1005. constraintdata.interfaces.delete(0);
  1006. end;
  1007. if basedef.typ<>errordef then
  1008. with tstoreddef(basedef) do
  1009. begin
  1010. genconstraintdata:=tgenericconstraintdata.create;
  1011. genconstraintdata.flags:=constraintdata.flags;
  1012. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1013. include(defoptions,df_genconstraint);
  1014. end;
  1015. for i:=firstidx to result.count-1 do
  1016. ttypesym(result[i]).typedef:=basedef;
  1017. { we need a typesym in case we do a Delphi-mode inline
  1018. specialization with this parameter; so just use the first sym }
  1019. if not assigned(basedef.typesym) then
  1020. basedef.typesym:=ttypesym(result[firstidx]);
  1021. firstidx:=result.count;
  1022. constraintdata.free;
  1023. end;
  1024. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1025. block_type:=old_block_type;
  1026. end;
  1027. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1028. var
  1029. i : longint;
  1030. generictype,sym : ttypesym;
  1031. st : tsymtable;
  1032. begin
  1033. def.genericdef:=genericdef;
  1034. if not assigned(genericlist) then
  1035. exit;
  1036. if assigned(genericdef) then
  1037. include(def.defoptions,df_specialization)
  1038. else
  1039. if genericlist.count>0 then
  1040. include(def.defoptions,df_generic);
  1041. case def.typ of
  1042. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1043. arraydef: st:=tarraydef(def).symtable;
  1044. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1045. else
  1046. internalerror(201101020);
  1047. end;
  1048. if (genericlist.count>0) and not assigned(def.genericparas) then
  1049. def.genericparas:=tfphashobjectlist.create(false);
  1050. for i:=0 to genericlist.count-1 do
  1051. begin
  1052. generictype:=ttypesym(genericlist[i]);
  1053. if assigned(generictype.owner) then
  1054. begin
  1055. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef);
  1056. st.insert(sym);
  1057. include(sym.symoptions,sp_generic_para);
  1058. end
  1059. else
  1060. begin
  1061. st.insert(generictype);
  1062. include(generictype.symoptions,sp_generic_para);
  1063. end;
  1064. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1065. end;
  1066. end;
  1067. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1068. var
  1069. gensym : ttypesym;
  1070. begin
  1071. { for generics in non-Delphi modes we insert a private type symbol
  1072. that has the same base name as the currently parsed generic and
  1073. that references this defs }
  1074. if not (m_delphi in current_settings.modeswitches) and
  1075. (
  1076. (
  1077. parse_generic and
  1078. assigned(genericlist) and
  1079. (genericlist.count>0)
  1080. ) or
  1081. (
  1082. assigned(current_specializedef) and
  1083. assigned(current_structdef.genericdef) and
  1084. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1085. (pos('$',name)>0)
  1086. )
  1087. ) then
  1088. begin
  1089. { we need to pass nil as def here, because the constructor wants
  1090. to set the typesym of the def which is not what we want }
  1091. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  1092. gensym.typedef:=current_structdef;
  1093. include(gensym.symoptions,sp_internal);
  1094. { the symbol should be only visible to the generic class
  1095. itself }
  1096. gensym.visibility:=vis_strictprivate;
  1097. symtablestack.top.insert(gensym);
  1098. end;
  1099. end;
  1100. function generate_generic_name(const name:tidstring;specializename:ansistring):tidstring;
  1101. var
  1102. crc : cardinal;
  1103. begin
  1104. if specializename='' then
  1105. internalerror(2012061901);
  1106. { build the new type's name }
  1107. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1108. result:=name+'$crc'+hexstr(crc,8);
  1109. end;
  1110. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1111. var
  1112. i,code : longint;
  1113. countstr : string;
  1114. begin
  1115. for i:=length(name) downto 1 do
  1116. if name[i]='$' then
  1117. begin
  1118. nongeneric:=copy(name,1,i-1);
  1119. countstr:=copy(name,i+1,length(name)-i);
  1120. val(countstr,count,code);
  1121. if code<>0 then
  1122. break;
  1123. exit;
  1124. end;
  1125. nongeneric:=name;
  1126. count:=0;
  1127. end;
  1128. function resolve_generic_dummysym(const name:tidstring):tsym;
  1129. var
  1130. list : tfpobjectlist;
  1131. begin
  1132. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1133. if assigned(list) and (list.count>0) then
  1134. result:=tgenericdummyentry(list.last).resolvedsym
  1135. else
  1136. result:=nil;
  1137. end;
  1138. function could_be_generic(const name:tidstring):boolean;
  1139. begin
  1140. result:=(name<>'') and
  1141. (current_module.genericdummysyms.findindexof(name)>=0);
  1142. end;
  1143. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1144. var
  1145. pu : tused_unit;
  1146. hmodule : tmodule;
  1147. unitsyms : TFPHashObjectList;
  1148. sym : tsym;
  1149. i : Integer;
  1150. begin
  1151. if not assigned(genericdef) then
  1152. internalerror(200705151);
  1153. { Setup symtablestack at definition time
  1154. to get types right, however this is not perfect, we should probably record
  1155. the resolved symbols }
  1156. state.oldsymtablestack:=symtablestack;
  1157. state.oldextendeddefs:=current_module.extendeddefs;
  1158. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1159. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1160. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1161. symtablestack:=tdefawaresymtablestack.create;
  1162. hmodule:=find_module_from_symtable(genericdef.owner);
  1163. if hmodule=nil then
  1164. internalerror(200705152);
  1165. { collect all unit syms in the generic's unit as we need to establish
  1166. their unitsym.module link again so that unit identifiers can be used }
  1167. unitsyms:=tfphashobjectlist.create(false);
  1168. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1169. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1170. begin
  1171. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1172. if sym.typ=unitsym then
  1173. unitsyms.add(upper(sym.realname),sym);
  1174. end;
  1175. { add all units if we are specializing inside the current unit (as the
  1176. generic could have been declared in the implementation part), but load
  1177. only interface units, if we are in a different unit as then the generic
  1178. needs to be in the interface section }
  1179. pu:=tused_unit(hmodule.used_units.first);
  1180. while assigned(pu) do
  1181. begin
  1182. if not assigned(pu.u.globalsymtable) then
  1183. { in certain circular, but valid unit constellations it can happen
  1184. that we specialize a generic in a different unit that was used
  1185. in the implementation section of the generic's unit and were the
  1186. interface is still being parsed and thus the localsymtable is in
  1187. reality the global symtable }
  1188. if pu.u.in_interface then
  1189. symtablestack.push(pu.u.localsymtable)
  1190. else
  1191. internalerror(200705153)
  1192. else
  1193. symtablestack.push(pu.u.globalsymtable);
  1194. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1195. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1196. tunitsym(sym).module:=pu.u;
  1197. pu:=tused_unit(pu.next);
  1198. end;
  1199. unitsyms.free;
  1200. if assigned(hmodule.globalsymtable) then
  1201. symtablestack.push(hmodule.globalsymtable);
  1202. { push the localsymtable if needed }
  1203. if (hmodule<>current_module) or not current_module.in_interface then
  1204. symtablestack.push(hmodule.localsymtable);
  1205. end;
  1206. procedure specialization_done(var state: tspecializationstate);
  1207. begin
  1208. { Restore symtablestack }
  1209. current_module.extendeddefs.free;
  1210. current_module.extendeddefs:=state.oldextendeddefs;
  1211. current_module.genericdummysyms.free;
  1212. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1213. symtablestack.free;
  1214. symtablestack:=state.oldsymtablestack;
  1215. { clear the state record to be on the safe side }
  1216. fillchar(state, sizeof(state), 0);
  1217. end;
  1218. end.