pgenutil.pas 73 KB

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