pgenutil.pas 86 KB

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