pgenutil.pas 62 KB

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