pgenutil.pas 60 KB

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