pgenutil.pas 55 KB

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