pgenutil.pas 59 KB

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