pgenutil.pas 72 KB

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