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;symtable:tsymtable):tdef;inline;
  34. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;symtable:tsymtable;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,'',nil,dummypos);
  587. end;
  588. {$pop}
  589. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string;symtable:tsymtable):tdef;
  590. var
  591. dummypos : tfileposinfo;
  592. {$push}
  593. {$warn 5036 off}
  594. begin
  595. result:=generate_specialization_phase1(context,genericdef,nil,symname,symtable,dummypos);
  596. end;
  597. {$pop}
  598. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;symtable:tsymtable;parsedpos:tfileposinfo):tdef;
  599. var
  600. found,
  601. err : boolean;
  602. i,
  603. gencount : longint;
  604. countstr,genname,ugenname : string;
  605. tmpstack : tfpobjectlist;
  606. symowner : tsymtable;
  607. begin
  608. context:=nil;
  609. result:=nil;
  610. { either symname must be given or genericdef needs to be valid }
  611. if (symname='') and
  612. (not assigned(genericdef) or
  613. (
  614. (genericdef.typ<>procdef) and
  615. (
  616. not assigned(genericdef.typesym) or
  617. (genericdef.typesym.typ<>typesym)
  618. )
  619. ) or
  620. (
  621. (genericdef.typ=procdef) and
  622. (
  623. not assigned(tprocdef(genericdef).procsym) or
  624. (tprocdef(genericdef).procsym.typ<>procsym)
  625. )
  626. )
  627. ) then
  628. begin
  629. internalerror(2019112401);
  630. end;
  631. if not assigned(parsedtype) and not try_to_consume(_LT) then
  632. begin
  633. consume(_LSHARPBRACKET);
  634. { handle "<>" }
  635. if (token=_GT) or (token=_RSHARPBRACKET) then
  636. begin
  637. Message(type_e_type_id_expected);
  638. if not try_to_consume(_GT) then
  639. try_to_consume(_RSHARPBRACKET);
  640. result:=generrordef;
  641. exit;
  642. end;
  643. end;
  644. context:=tspecializationcontext.create;
  645. { Parse type parameters }
  646. err:=not parse_generic_specialization_types_internal(context.paramlist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  647. if err then
  648. begin
  649. if not try_to_consume(_GT) then
  650. try_to_consume(_RSHARPBRACKET);
  651. context.free;
  652. context:=nil;
  653. result:=generrordef;
  654. exit;
  655. end;
  656. { use the name of the symbol as procvars return a user friendly version
  657. of the name }
  658. if symname='' then
  659. begin
  660. if genericdef.typ=procdef then
  661. genname:=tprocdef(genericdef).procsym.realname
  662. else
  663. genname:=ttypesym(genericdef.typesym).realname;
  664. end
  665. else
  666. genname:=symname;
  667. { in case of non-Delphi mode the type name could already be a generic
  668. def (but maybe the wrong one) }
  669. if assigned(genericdef) and
  670. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  671. begin
  672. { remove the type count suffix from the generic's name }
  673. for i:=Length(genname) downto 1 do
  674. if genname[i]='$' then
  675. begin
  676. genname:=copy(genname,1,i-1);
  677. break;
  678. end;
  679. { in case of a specialization we've only reached the specialization
  680. checksum yet }
  681. if df_specialization in genericdef.defoptions then
  682. for i:=length(genname) downto 1 do
  683. if genname[i]='$' then
  684. begin
  685. genname:=copy(genname,1,i-1);
  686. break;
  687. end;
  688. end
  689. else
  690. begin
  691. split_generic_name(genname,ugenname,gencount);
  692. if genname<>ugenname then
  693. genname:=ugenname;
  694. end;
  695. { search a generic with the given count of params }
  696. countstr:='';
  697. str(context.paramlist.Count,countstr);
  698. genname:=genname+'$'+countstr;
  699. ugenname:=upper(genname);
  700. context.genname:=genname;
  701. if assigned(genericdef) then
  702. symowner:=genericdef.owner
  703. else
  704. symowner:=symtable;
  705. if assigned(symowner) and (symowner.symtabletype in [objectsymtable,recordsymtable]) then
  706. begin
  707. if symowner.symtabletype = objectsymtable then
  708. found:=searchsym_in_class(tobjectdef(symowner.defowner),tobjectdef(symowner.defowner),ugenname,context.sym,context.symtable,[])
  709. else
  710. found:=searchsym_in_record(tabstractrecorddef(symowner.defowner),ugenname,context.sym,context.symtable);
  711. if not found then
  712. found:=searchsym(ugenname,context.sym,context.symtable);
  713. end
  714. else
  715. found:=searchsym(ugenname,context.sym,context.symtable);
  716. if found and (context.sym.typ=absolutevarsym) and
  717. (vo_is_funcret in tabstractvarsym(context.sym).varoptions) then
  718. begin
  719. { we found the function result alias of a generic function; go up the
  720. symbol stack *before* this alias was inserted, so that we can
  721. (hopefully) find the correct generic symbol }
  722. tmpstack:=tfpobjectlist.create(false);
  723. while assigned(symtablestack.top) do
  724. begin
  725. tmpstack.Add(symtablestack.top);
  726. symtablestack.pop(symtablestack.top);
  727. if tmpstack.Last=context.symtable then
  728. break;
  729. end;
  730. if not assigned(symtablestack.top) then
  731. internalerror(2019123001);
  732. found:=searchsym(ugenname,context.sym,context.symtable);
  733. for i:=tmpstack.count-1 downto 0 do
  734. symtablestack.push(tsymtable(tmpstack[i]));
  735. tmpstack.free;
  736. end;
  737. if not found or not (context.sym.typ in [typesym,procsym]) then
  738. begin
  739. identifier_not_found(genname);
  740. if not try_to_consume(_GT) then
  741. try_to_consume(_RSHARPBRACKET);
  742. context.free;
  743. context:=nil;
  744. result:=generrordef;
  745. exit;
  746. end;
  747. { we've found the correct def }
  748. if context.sym.typ=typesym then
  749. result:=tstoreddef(ttypesym(context.sym).typedef)
  750. else
  751. begin
  752. if tprocsym(context.sym).procdeflist.count=0 then
  753. internalerror(2015061203);
  754. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  755. end;
  756. if not try_to_consume(_GT) then
  757. consume(_RSHARPBRACKET);
  758. end;
  759. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  760. procedure unset_forwarddef(def: tdef);
  761. var
  762. st : TSymtable;
  763. i : longint;
  764. begin
  765. case def.typ of
  766. procdef:
  767. tprocdef(def).forwarddef:=false;
  768. objectdef,
  769. recorddef:
  770. begin
  771. st:=def.getsymtable(gs_record);
  772. for i:=0 to st.deflist.count-1 do
  773. unset_forwarddef(tdef(st.deflist[i]));
  774. end;
  775. else
  776. ;
  777. end;
  778. end;
  779. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  780. var
  781. i : longint;
  782. begin
  783. gendef:=nil;
  784. psym:=nil;
  785. case sym.typ of
  786. typesym:
  787. begin
  788. gendef:=ttypesym(sym).typedef
  789. end;
  790. procsym:
  791. begin
  792. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  793. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  794. begin
  795. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  796. break;
  797. end;
  798. psym:=sym;
  799. end
  800. else
  801. internalerror(200710171);
  802. end;
  803. end;
  804. var
  805. finalspecializename,
  806. ufinalspecializename : tidstring;
  807. prettyname : ansistring;
  808. generictypelist : tfphashobjectlist;
  809. srsymtable,
  810. specializest : tsymtable;
  811. hashedid : thashedidstring;
  812. tempst : tglobalsymtable;
  813. psym,
  814. srsym : tsym;
  815. paramdef1,
  816. paramdef2,
  817. def : tdef;
  818. old_block_type : tblock_type;
  819. state : tspecializationstate;
  820. old_current_structdef : tabstractrecorddef;
  821. old_current_specializedef,
  822. old_current_genericdef : tstoreddef;
  823. old_current_procinfo : tprocinfo;
  824. old_module_procinfo : tobject;
  825. hmodule : tmodule;
  826. oldcurrent_filepos : tfileposinfo;
  827. recordbuf : tdynamicarray;
  828. hadtypetoken : boolean;
  829. i,
  830. replaydepth : longint;
  831. item : tobject;
  832. allequal,
  833. hintsprocessed : boolean;
  834. pd : tprocdef;
  835. pdflags : tpdflags;
  836. begin
  837. if not assigned(context) then
  838. internalerror(2015052203);
  839. result:=nil;
  840. pd:=nil;
  841. if not check_generic_constraints(genericdef,context.paramlist,context.poslist) then
  842. begin
  843. { the parameters didn't fit the constraints, so don't continue with the
  844. specialization }
  845. result:=generrordef;
  846. exit;
  847. end;
  848. { build the new type's name }
  849. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  850. ufinalspecializename:=upper(finalspecializename);
  851. if genericdef.typ=procdef then
  852. prettyname:=tprocdef(genericdef).procsym.prettyname
  853. else
  854. prettyname:=genericdef.typesym.prettyname;
  855. prettyname:=prettyname+'<'+context.prettyname+'>';
  856. generictypelist:=tfphashobjectlist.create(false);
  857. { build the list containing the types for the generic params }
  858. if not assigned(genericdef.genericparas) then
  859. internalerror(2013092601);
  860. if context.paramlist.count<>genericdef.genericparas.count then
  861. internalerror(2013092603);
  862. for i:=0 to genericdef.genericparas.Count-1 do
  863. begin
  864. srsym:=tsym(genericdef.genericparas[i]);
  865. if not (sp_generic_para in srsym.symoptions) then
  866. internalerror(2013092602);
  867. generictypelist.add(srsym.realname,context.paramlist[i]);
  868. end;
  869. { Special case if we are referencing the current defined object }
  870. if assigned(current_structdef) and
  871. (current_structdef.objname^=ufinalspecializename) then
  872. result:=current_structdef;
  873. { Can we reuse an already specialized type? }
  874. { for this first check whether we are currently specializing a nested
  875. type of the current (main) specialization (this is necessary, because
  876. during that time the symbol of the main specialization will still
  877. contain a reference to an errordef) }
  878. if not assigned(result) and assigned(current_specializedef) then
  879. begin
  880. def:=current_specializedef;
  881. repeat
  882. if def.typ in [objectdef,recorddef] then
  883. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  884. result:=def;
  885. break;
  886. end;
  887. if assigned(def.owner) then
  888. def:=tstoreddef(def.owner.defowner)
  889. else
  890. { this can happen when specializing a generic function }
  891. def:=nil;
  892. until not assigned(def) or not (df_specialization in def.defoptions);
  893. end;
  894. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  895. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  896. parsing anyway), so for now we treat those still as generic defs without doing a partial
  897. specialization }
  898. if not assigned(result) then
  899. begin
  900. def:=current_genericdef;
  901. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  902. begin
  903. if (df_generic in def.defoptions) and (def=genericdef) then
  904. begin
  905. result:=def;
  906. break;
  907. end;
  908. { the following happens when a routine with its parent struct
  909. as parameter is specialized as a parameter or result of a
  910. generic function }
  911. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  912. begin
  913. if tstoreddef(def).genericparas.count=generictypelist.count then
  914. begin
  915. allequal:=true;
  916. for i:=0 to generictypelist.count-1 do
  917. begin
  918. if tsym(generictypelist[i]).typ<>tsym(tstoreddef(def).genericparas[i]).typ then
  919. begin
  920. allequal:=false;
  921. break;
  922. end;
  923. if tsym(generictypelist[i]).typ=constsym then
  924. paramdef1:=tconstsym(generictypelist[i]).constdef
  925. else
  926. paramdef1:=ttypesym(generictypelist[i]).typedef;
  927. if tsym(tstoreddef(def).genericparas[i]).typ=constsym then
  928. paramdef2:=tconstsym(tstoreddef(def).genericparas[i]).constdef
  929. else
  930. paramdef2:=ttypesym(tstoreddef(def).genericparas[i]).typedef;
  931. if not equal_defs(paramdef1,paramdef2) then
  932. begin
  933. allequal:=false;
  934. break;
  935. end;
  936. if (tsym(generictypelist[i]).typ=constsym) and
  937. (
  938. (tconstsym(generictypelist[i]).consttyp<>tconstsym(tstoreddef(def).genericparas[i]).consttyp) or
  939. not same_constvalue(tconstsym(generictypelist[i]).consttyp,tconstsym(generictypelist[i]).value,tconstsym(tstoreddef(def).genericparas[i]).value)
  940. ) then
  941. begin
  942. allequal:=false;
  943. break;
  944. end;
  945. end;
  946. if allequal then
  947. begin
  948. result:=def;
  949. break;
  950. end;
  951. end;
  952. end;
  953. def:=tstoreddef(def.owner.defowner);
  954. end;
  955. end;
  956. { decide in which symtable to put the specialization }
  957. if parse_generic and not assigned(result) then
  958. begin
  959. srsymtable:=symtablestack.top;
  960. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  961. { if we are currently specializing a routine we need to specialize into
  962. the routine's local- or parasymtable so that they are correctly
  963. registered should the specialization be finalized }
  964. specializest:=srsymtable
  965. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  966. { if we are parsing the definition of a method we specialize into
  967. the local symtable of it }
  968. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  969. else
  970. begin
  971. if not assigned(current_genericdef) then
  972. internalerror(2014050901);
  973. { we specialize the partial specialization into the symtable of the currently parsed
  974. generic }
  975. case current_genericdef.typ of
  976. procvardef:
  977. specializest:=current_genericdef.getsymtable(gs_para);
  978. procdef:
  979. specializest:=current_genericdef.getsymtable(gs_local);
  980. objectdef,
  981. recorddef:
  982. specializest:=current_genericdef.getsymtable(gs_record);
  983. arraydef:
  984. specializest:=tarraydef(current_genericdef).symtable;
  985. else
  986. internalerror(2014050902);
  987. end;
  988. end;
  989. end
  990. else
  991. if current_module.is_unit and current_module.in_interface then
  992. specializest:=current_module.globalsymtable
  993. else
  994. specializest:=current_module.localsymtable;
  995. if not assigned(specializest) then
  996. internalerror(2014050910);
  997. { now check whether there is a specialization somewhere else }
  998. psym:=nil;
  999. if not assigned(result) then
  1000. begin
  1001. hashedid.id:=ufinalspecializename;
  1002. if specializest.symtabletype=objectsymtable then
  1003. begin
  1004. { search also in parent classes }
  1005. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  1006. internalerror(2016112901);
  1007. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  1008. srsym:=nil;
  1009. end
  1010. else
  1011. srsym:=tsym(specializest.findwithhash(hashedid));
  1012. if assigned(srsym) then
  1013. begin
  1014. retrieve_genericdef_or_procsym(srsym,result,psym);
  1015. end
  1016. else
  1017. { the generic could have been specialized in the globalsymtable
  1018. already, so search there as well }
  1019. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  1020. begin
  1021. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  1022. if assigned(srsym) then
  1023. begin
  1024. retrieve_genericdef_or_procsym(srsym,result,psym);
  1025. end;
  1026. end;
  1027. end;
  1028. if not assigned(result) then
  1029. begin
  1030. specialization_init(genericdef,state);
  1031. { push a temporary global symtable so that the specialization is
  1032. added to the correct symtable; this symtable does not contain
  1033. any other symbols, so that the type resolution can not be
  1034. influenced by symbols in the current unit }
  1035. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  1036. symtablestack.push(tempst);
  1037. { Reparse the original type definition }
  1038. begin
  1039. old_current_specializedef:=nil;
  1040. old_current_genericdef:=nil;
  1041. old_current_structdef:=nil;
  1042. old_current_procinfo:=current_procinfo;
  1043. old_module_procinfo:=current_module.procinfo;
  1044. current_procinfo:=nil;
  1045. current_module.procinfo:=nil;
  1046. if parse_class_parent then
  1047. begin
  1048. old_current_structdef:=current_structdef;
  1049. old_current_genericdef:=current_genericdef;
  1050. old_current_specializedef:=current_specializedef;
  1051. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  1052. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  1053. else
  1054. current_structdef:=nil;
  1055. current_genericdef:=nil;
  1056. current_specializedef:=nil;
  1057. end;
  1058. maybe_add_waiting_unit(genericdef);
  1059. { First a new sym so we can reuse this specialization and
  1060. references to this specialization can be handled }
  1061. if genericdef.typ=procdef then
  1062. if assigned(psym) then
  1063. srsym:=psym
  1064. else
  1065. srsym:=cprocsym.create(finalspecializename)
  1066. else
  1067. srsym:=ctypesym.create(finalspecializename,generrordef);
  1068. { insert the symbol only if we don't know already that we have
  1069. a procsym to add it to }
  1070. if not assigned(psym) then
  1071. specializest.insert(srsym);
  1072. { specializations are declarations as such it is the wisest to
  1073. declare set the blocktype to "type"; otherwise we'll
  1074. experience unexpected side effects like the addition of
  1075. classrefdefs if we have a generic that's derived from another
  1076. generic }
  1077. old_block_type:=block_type;
  1078. block_type:=bt_type;
  1079. if (
  1080. (genericdef.typ=procdef) and
  1081. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  1082. ) or (
  1083. (genericdef.typ<>procdef) and
  1084. not assigned(genericdef.generictokenbuf)
  1085. ) then
  1086. internalerror(200511171);
  1087. hmodule:=find_module_from_symtable(genericdef.owner);
  1088. if hmodule=nil then
  1089. internalerror(2012051202);
  1090. oldcurrent_filepos:=current_filepos;
  1091. { use the index the module got from the current compilation process }
  1092. current_filepos.moduleindex:=hmodule.unit_index;
  1093. current_tokenpos:=current_filepos;
  1094. if parse_generic then
  1095. begin
  1096. recordbuf:=current_scanner.recordtokenbuf;
  1097. current_scanner.recordtokenbuf:=nil;
  1098. end
  1099. else
  1100. recordbuf:=nil;
  1101. replaydepth:=current_scanner.replay_stack_depth;
  1102. if genericdef.typ=procdef then
  1103. begin
  1104. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf,hmodule.change_endian);
  1105. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  1106. if assigned(pd) then
  1107. begin
  1108. if assigned(psym) then
  1109. pd.procsym:=psym
  1110. else
  1111. pd.procsym:=srsym;
  1112. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions,tprocdef(genericdef).struct);
  1113. end;
  1114. result:=pd;
  1115. end
  1116. else
  1117. begin
  1118. current_scanner.startreplaytokens(genericdef.generictokenbuf,hmodule.change_endian);
  1119. hadtypetoken:=false;
  1120. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  1121. ttypesym(srsym).typedef:=result;
  1122. result.typesym:=srsym;
  1123. if _prettyname<>'' then
  1124. ttypesym(result.typesym).fprettyname:=_prettyname
  1125. else
  1126. ttypesym(result.typesym).fprettyname:=prettyname;
  1127. end;
  1128. current_filepos:=oldcurrent_filepos;
  1129. { Note regarding hint directives:
  1130. There is no need to remove the flags for them from the
  1131. specialized generic symbol, because hint directives that
  1132. follow the specialization are handled by the code in
  1133. pdecl.types_dec and added to the type symbol.
  1134. E.g.: TFoo = TBar<Blubb> deprecated;
  1135. Here the symbol TBar$1$Blubb will contain the
  1136. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1137. case result.typ of
  1138. { Build VMT indexes for classes and read hint directives }
  1139. objectdef:
  1140. begin
  1141. if replaydepth>current_scanner.replay_stack_depth then
  1142. begin
  1143. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1144. if replaydepth>current_scanner.replay_stack_depth then
  1145. consume(_SEMICOLON);
  1146. end;
  1147. build_vmt(tobjectdef(result));
  1148. end;
  1149. { handle params, calling convention, etc }
  1150. procvardef:
  1151. begin
  1152. hintsprocessed:=false;
  1153. if replaydepth>current_scanner.replay_stack_depth then
  1154. begin
  1155. if not check_proc_directive(true) then
  1156. begin
  1157. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1158. if replaydepth>current_scanner.replay_stack_depth then
  1159. consume(_SEMICOLON);
  1160. end
  1161. else
  1162. hintsprocessed:=true;
  1163. end;
  1164. if replaydepth>current_scanner.replay_stack_depth then
  1165. parse_var_proc_directives(ttypesym(srsym));
  1166. handle_calling_convention(tprocvardef(result),hcc_default_actions_intf);
  1167. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  1168. begin
  1169. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1170. if replaydepth>current_scanner.replay_stack_depth then
  1171. consume(_SEMICOLON);
  1172. end;
  1173. end;
  1174. procdef:
  1175. begin
  1176. pdflags:=[];
  1177. if genericdef.owner.symtabletype=objectsymtable then
  1178. include(pdflags,pd_object)
  1179. else if genericdef.owner.symtabletype=recordsymtable then
  1180. include(pdflags,pd_record);
  1181. parse_proc_directives(pd,pdflags);
  1182. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1183. consume(_SEMICOLON);
  1184. if parse_generic then
  1185. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1186. else
  1187. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1188. pdflags:=pdflags+[pd_body,pd_implemen];
  1189. proc_add_definition(tprocdef(result));
  1190. { for partial specializations we implicitely declare the routine as
  1191. having its implementation although we'll not specialize it in reality }
  1192. if parse_generic then
  1193. unset_forwarddef(result);
  1194. end;
  1195. else
  1196. { parse hint directives for records and arrays }
  1197. if replaydepth>current_scanner.replay_stack_depth then begin
  1198. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1199. if replaydepth>current_scanner.replay_stack_depth then
  1200. consume(_SEMICOLON);
  1201. end;
  1202. end;
  1203. { Consume the remainder of the buffer }
  1204. while current_scanner.replay_stack_depth>replaydepth do
  1205. consume(token);
  1206. if assigned(recordbuf) then
  1207. begin
  1208. if assigned(current_scanner.recordtokenbuf) then
  1209. internalerror(2014050909);
  1210. current_scanner.recordtokenbuf:=recordbuf;
  1211. end;
  1212. block_type:=old_block_type;
  1213. current_procinfo:=old_current_procinfo;
  1214. current_module.procinfo:=old_module_procinfo;
  1215. if parse_class_parent then
  1216. begin
  1217. current_structdef:=old_current_structdef;
  1218. current_genericdef:=old_current_genericdef;
  1219. current_specializedef:=old_current_specializedef;
  1220. end;
  1221. end;
  1222. { extract all created symbols and defs from the temporary symtable
  1223. and add them to the specializest }
  1224. for i:=tempst.SymList.Count-1 downto 0 do
  1225. begin
  1226. item:=tempst.SymList.Items[i];
  1227. { using changeowner the symbol is automatically added to the
  1228. new symtable }
  1229. tsym(item).ChangeOwner(specializest);
  1230. end;
  1231. for i:=tempst.DefList.Count-1 downto 0 do
  1232. begin
  1233. item:=tempst.DefList.Items[i];
  1234. { using changeowner the def is automatically added to the new
  1235. symtable }
  1236. tdef(item).ChangeOwner(specializest);
  1237. { for partial specializations we implicitely declare any methods as having their
  1238. implementations although we'll not specialize them in reality }
  1239. if parse_generic then
  1240. unset_forwarddef(tdef(item));
  1241. end;
  1242. { if a generic was declared during the specialization we need to
  1243. flag the specialize symtable accordingly }
  1244. if sto_has_generic in tempst.tableoptions then
  1245. specializest.includeoption(sto_has_generic);
  1246. tempst.free;
  1247. specialization_done(state);
  1248. { procdefs are only added once we know which overload we use }
  1249. if not parse_generic and (result.typ<>procdef) then
  1250. current_module.pendingspecializations.add(result.typename,result);
  1251. end;
  1252. generictypelist.free;
  1253. if assigned(genericdef) then
  1254. begin
  1255. { check the hints of the found generic symbol }
  1256. if genericdef.typ=procdef then
  1257. srsym:=tprocdef(genericdef).procsym
  1258. else
  1259. srsym:=genericdef.typesym;
  1260. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1261. end;
  1262. end;
  1263. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  1264. var
  1265. context : tspecializationcontext;
  1266. genericdef : tstoreddef;
  1267. begin
  1268. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,nil,parsedpos));
  1269. if genericdef<>generrordef then
  1270. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1271. tt:=genericdef;
  1272. if assigned(context) then
  1273. context.free;
  1274. end;
  1275. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1276. var
  1277. generictype : tstoredsym;
  1278. i,firstidx,const_list_index : longint;
  1279. srsymtable : tsymtable;
  1280. basedef,def : tdef;
  1281. defname : tidstring;
  1282. allowconst,
  1283. allowconstructor,
  1284. is_const,
  1285. doconsume : boolean;
  1286. constraintdata : tgenericconstraintdata;
  1287. old_block_type : tblock_type;
  1288. fileinfo : tfileposinfo;
  1289. begin
  1290. result:=tfphashobjectlist.create(false);
  1291. firstidx:=0;
  1292. const_list_index:=0;
  1293. old_block_type:=block_type;
  1294. block_type:=bt_type;
  1295. allowconst:=true;
  1296. is_const:=false;
  1297. repeat
  1298. if allowconst and try_to_consume(_CONST) then
  1299. begin
  1300. allowconst:=false;
  1301. is_const:=true;
  1302. const_list_index:=result.count;
  1303. end;
  1304. if token=_ID then
  1305. begin
  1306. if is_const then
  1307. generictype:=cconstsym.create_undefined(orgpattern,cundefinedtype)
  1308. else
  1309. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  1310. { type parameters need to be added as strict private }
  1311. generictype.visibility:=vis_strictprivate;
  1312. include(generictype.symoptions,sp_generic_para);
  1313. result.add(orgpattern,generictype);
  1314. end;
  1315. consume(_ID);
  1316. fileinfo:=current_tokenpos;
  1317. { const restriction }
  1318. if is_const and try_to_consume(_COLON) then
  1319. begin
  1320. def:=nil;
  1321. { parse the type and assign the const type to generictype }
  1322. single_type(def,[]);
  1323. for i:=const_list_index to result.count-1 do
  1324. begin
  1325. { finalize constant information once type is known }
  1326. if assigned(def) and (def.typ in tgeneric_param_const_types) then
  1327. begin
  1328. case def.typ of
  1329. orddef,
  1330. enumdef:
  1331. tconstsym(result[i]).consttyp:=constord;
  1332. stringdef:
  1333. tconstsym(result[i]).consttyp:=conststring;
  1334. floatdef:
  1335. tconstsym(result[i]).consttyp:=constreal;
  1336. setdef:
  1337. tconstsym(result[i]).consttyp:=constset;
  1338. { pointer always refers to nil with constants }
  1339. pointerdef:
  1340. tconstsym(result[i]).consttyp:=constnil;
  1341. else
  1342. internalerror(2020011402);
  1343. end;
  1344. tconstsym(result[i]).constdef:=def;
  1345. end
  1346. else
  1347. Message1(type_e_generic_const_type_not_allowed,def.fulltypename);
  1348. end;
  1349. { after type restriction const list terminates }
  1350. is_const:=false;
  1351. end
  1352. { type restriction }
  1353. else if try_to_consume(_COLON) then
  1354. begin
  1355. if not allowconstraints then
  1356. Message(parser_e_generic_constraints_not_allowed_here);
  1357. { construct a name which can be used for a type specification }
  1358. constraintdata:=tgenericconstraintdata.create;
  1359. constraintdata.fileinfo:=fileinfo;
  1360. defname:='';
  1361. str(current_module.deflist.count,defname);
  1362. defname:='$gendef'+defname;
  1363. allowconstructor:=m_delphi in current_settings.modeswitches;
  1364. basedef:=generrordef;
  1365. repeat
  1366. doconsume:=true;
  1367. case token of
  1368. _CONSTRUCTOR:
  1369. begin
  1370. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1371. Message(parser_e_illegal_expression);
  1372. include(constraintdata.flags,gcf_constructor);
  1373. allowconstructor:=false;
  1374. end;
  1375. _CLASS:
  1376. begin
  1377. if gcf_class in constraintdata.flags then
  1378. Message(parser_e_illegal_expression);
  1379. if basedef=generrordef then
  1380. include(constraintdata.flags,gcf_class)
  1381. else
  1382. Message(parser_e_illegal_expression);
  1383. end;
  1384. _RECORD:
  1385. begin
  1386. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1387. or (constraintdata.interfaces.count>0) then
  1388. Message(parser_e_illegal_expression)
  1389. else
  1390. begin
  1391. srsymtable:=trecordsymtable.create(defname,0,1);
  1392. basedef:=crecorddef.create(defname,srsymtable);
  1393. include(constraintdata.flags,gcf_record);
  1394. allowconstructor:=false;
  1395. end;
  1396. end;
  1397. else
  1398. begin
  1399. { after single_type "token" is the trailing ",", ";" or
  1400. ">"! }
  1401. doconsume:=false;
  1402. { def is already set to a class or record }
  1403. if gcf_record in constraintdata.flags then
  1404. Message(parser_e_illegal_expression);
  1405. single_type(def, [stoAllowSpecialization]);
  1406. { only types that are inheritable are allowed }
  1407. if (def.typ<>objectdef) or
  1408. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1409. Message1(type_e_class_or_interface_type_expected,def.typename)
  1410. else
  1411. case tobjectdef(def).objecttype of
  1412. odt_class,
  1413. odt_javaclass:
  1414. begin
  1415. if gcf_class in constraintdata.flags then
  1416. { "class" + concrete class is not allowed }
  1417. Message(parser_e_illegal_expression)
  1418. else
  1419. { do we already have a concrete class? }
  1420. if basedef<>generrordef then
  1421. Message(parser_e_illegal_expression)
  1422. else
  1423. basedef:=def;
  1424. end;
  1425. odt_interfacecom,
  1426. odt_interfacecorba,
  1427. odt_interfacejava,
  1428. odt_dispinterface:
  1429. constraintdata.interfaces.add(def);
  1430. else
  1431. ;
  1432. end;
  1433. end;
  1434. end;
  1435. if doconsume then
  1436. consume(token);
  1437. until not try_to_consume(_COMMA);
  1438. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1439. (constraintdata.interfaces.count>1) or
  1440. (
  1441. (basedef.typ=objectdef) and
  1442. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1443. ) then
  1444. begin
  1445. if basedef.typ=errordef then
  1446. { don't pass an errordef as a parent to a tobjectdef }
  1447. basedef:=class_tobject
  1448. else
  1449. if (basedef.typ<>objectdef) or
  1450. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1451. internalerror(2012101101);
  1452. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1453. for i:=0 to constraintdata.interfaces.count-1 do
  1454. tobjectdef(basedef).implementedinterfaces.add(
  1455. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1456. end
  1457. else
  1458. if constraintdata.interfaces.count=1 then
  1459. begin
  1460. if basedef.typ<>errordef then
  1461. internalerror(2013021601);
  1462. def:=tdef(constraintdata.interfaces[0]);
  1463. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1464. constraintdata.interfaces.delete(0);
  1465. end;
  1466. if basedef.typ<>errordef then
  1467. with tstoreddef(basedef) do
  1468. begin
  1469. genconstraintdata:=tgenericconstraintdata.create;
  1470. genconstraintdata.flags:=constraintdata.flags;
  1471. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1472. genconstraintdata.fileinfo:=constraintdata.fileinfo;
  1473. include(defoptions,df_genconstraint);
  1474. end;
  1475. for i:=firstidx to result.count-1 do
  1476. ttypesym(result[i]).typedef:=basedef;
  1477. { we need a typesym in case we do a Delphi-mode inline
  1478. specialization with this parameter; so just use the first sym }
  1479. if not assigned(basedef.typesym) then
  1480. basedef.typesym:=ttypesym(result[firstidx]);
  1481. firstidx:=result.count;
  1482. constraintdata.free;
  1483. end
  1484. else
  1485. begin
  1486. if token=_SEMICOLON then
  1487. begin
  1488. { two different typeless parameters are considered as incompatible }
  1489. for i:=firstidx to result.count-1 do
  1490. if tsym(result[i]).typ<>constsym then
  1491. begin
  1492. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1493. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1494. end;
  1495. { a semicolon terminates a type parameter group }
  1496. firstidx:=result.count;
  1497. end;
  1498. end;
  1499. if token=_SEMICOLON then
  1500. begin
  1501. is_const:=false;
  1502. allowconst:=true;
  1503. end;
  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 then
  1684. srsym:=sym
  1685. else
  1686. { dummy symbol is already not so dummy anymore }
  1687. srsym:=nil;
  1688. if assigned(srsym) then
  1689. begin
  1690. entry:=tgenericdummyentry.create;
  1691. entry.resolvedsym:=srsym;
  1692. entry.dummysym:=sym;
  1693. list.add(entry);
  1694. end;
  1695. end;
  1696. end;
  1697. function resolve_generic_dummysym(const name:tidstring):tsym;
  1698. var
  1699. list : tfpobjectlist;
  1700. begin
  1701. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1702. if assigned(list) and (list.count>0) then
  1703. result:=tgenericdummyentry(list.last).resolvedsym
  1704. else
  1705. result:=nil;
  1706. end;
  1707. function could_be_generic(const name:tidstring):boolean;
  1708. begin
  1709. result:=(name<>'') and
  1710. (current_module.genericdummysyms.findindexof(name)>=0);
  1711. end;
  1712. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1713. var
  1714. pu : tused_unit;
  1715. hmodule : tmodule;
  1716. unitsyms : TFPHashObjectList;
  1717. sym : tsym;
  1718. i : Integer;
  1719. begin
  1720. if not assigned(genericdef) then
  1721. internalerror(200705151);
  1722. { Setup symtablestack at definition time
  1723. to get types right, however this is not perfect, we should probably record
  1724. the resolved symbols }
  1725. state.oldsymtablestack:=symtablestack;
  1726. state.oldextendeddefs:=current_module.extendeddefs;
  1727. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1728. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1729. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1730. symtablestack:=tdefawaresymtablestack.create;
  1731. hmodule:=find_module_from_symtable(genericdef.owner);
  1732. if hmodule=nil then
  1733. internalerror(200705152);
  1734. { collect all unit syms in the generic's unit as we need to establish
  1735. their unitsym.module link again so that unit identifiers can be used }
  1736. unitsyms:=tfphashobjectlist.create(false);
  1737. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1738. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1739. begin
  1740. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1741. if sym.typ=unitsym then
  1742. unitsyms.add(upper(sym.realname),sym);
  1743. end;
  1744. { add all units if we are specializing inside the current unit (as the
  1745. generic could have been declared in the implementation part), but load
  1746. only interface units, if we are in a different unit as then the generic
  1747. needs to be in the interface section }
  1748. pu:=tused_unit(hmodule.used_units.first);
  1749. while assigned(pu) do
  1750. begin
  1751. if not assigned(pu.u.globalsymtable) then
  1752. { in certain circular, but valid unit constellations it can happen
  1753. that we specialize a generic in a different unit that was used
  1754. in the implementation section of the generic's unit and were the
  1755. interface is still being parsed and thus the localsymtable is in
  1756. reality the global symtable }
  1757. if pu.u.in_interface then
  1758. symtablestack.push(pu.u.localsymtable)
  1759. else
  1760. internalerror(200705153)
  1761. else
  1762. symtablestack.push(pu.u.globalsymtable);
  1763. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1764. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1765. tunitsym(sym).module:=pu.u;
  1766. pu:=tused_unit(pu.next);
  1767. end;
  1768. unitsyms.free;
  1769. if assigned(hmodule.globalsymtable) then
  1770. symtablestack.push(hmodule.globalsymtable);
  1771. { push the localsymtable if needed }
  1772. if ((hmodule<>current_module) or not current_module.in_interface)
  1773. and assigned(hmodule.localsymtable) then
  1774. symtablestack.push(hmodule.localsymtable);
  1775. end;
  1776. procedure specialization_done(var state: tspecializationstate);
  1777. begin
  1778. { Restore symtablestack }
  1779. current_module.extendeddefs.free;
  1780. current_module.extendeddefs:=state.oldextendeddefs;
  1781. current_module.genericdummysyms.free;
  1782. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1783. symtablestack.free;
  1784. symtablestack:=state.oldsymtablestack;
  1785. { clear the state record to be on the safe side }
  1786. fillchar(state, sizeof(state), 0);
  1787. end;
  1788. {****************************************************************************
  1789. SPECIALIZATION BODY GENERATION
  1790. ****************************************************************************}
  1791. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  1792. var
  1793. oldcurrent_filepos : tfileposinfo;
  1794. begin
  1795. if assigned(def.genericdef) and
  1796. (def.genericdef.typ=procdef) and
  1797. assigned(tprocdef(def.genericdef).generictokenbuf) then
  1798. begin
  1799. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  1800. internalerror(2015061902);
  1801. oldcurrent_filepos:=current_filepos;
  1802. current_filepos:=tprocdef(def.genericdef).fileinfo;
  1803. { use the index the module got from the current compilation process }
  1804. current_filepos.moduleindex:=hmodule.unit_index;
  1805. current_tokenpos:=current_filepos;
  1806. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf,hmodule.change_endian);
  1807. read_proc_body(def);
  1808. current_filepos:=oldcurrent_filepos;
  1809. end
  1810. { synthetic routines will be implemented afterwards }
  1811. else if def.synthetickind=tsk_none then
  1812. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  1813. end;
  1814. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  1815. var
  1816. i : longint;
  1817. hp : tdef;
  1818. hmodule : tmodule;
  1819. begin
  1820. result:=true;
  1821. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1822. if hmodule=nil then
  1823. internalerror(201202041);
  1824. for i:=0 to def.symtable.DefList.Count-1 do
  1825. begin
  1826. hp:=tdef(def.symtable.DefList[i]);
  1827. if hp.typ=procdef then
  1828. begin
  1829. { only generate the code if we need a body }
  1830. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  1831. continue;
  1832. { and the body is available already (which is implicitely the
  1833. case if the generic routine is part of another unit) }
  1834. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and
  1835. { may not be assigned in case it's a synthetic procdef that
  1836. still needs to be generated }
  1837. assigned(tprocdef(hp).genericdef) and
  1838. tprocdef(tprocdef(hp).genericdef).forwarddef then
  1839. begin
  1840. result:=false;
  1841. continue;
  1842. end;
  1843. process_procdef(tprocdef(hp),hmodule);
  1844. end
  1845. else
  1846. if hp.typ in [objectdef,recorddef] then
  1847. { generate code for subtypes as well }
  1848. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  1849. end;
  1850. end;
  1851. procedure generate_specialization_procs;
  1852. var
  1853. i : longint;
  1854. list,
  1855. readdlist : tfpobjectlist;
  1856. def : tstoreddef;
  1857. state : tspecializationstate;
  1858. hmodule : tmodule;
  1859. begin
  1860. { first copy all entries and then work with that list to ensure that
  1861. we don't get an infinite recursion }
  1862. list:=tfpobjectlist.create(false);
  1863. readdlist:=tfpobjectlist.create(false);
  1864. for i:=0 to current_module.pendingspecializations.Count-1 do
  1865. list.add(current_module.pendingspecializations.Items[i]);
  1866. current_module.pendingspecializations.clear;
  1867. for i:=0 to list.count-1 do
  1868. begin
  1869. def:=tstoreddef(list[i]);
  1870. if not tstoreddef(def).is_specialization then
  1871. continue;
  1872. case def.typ of
  1873. procdef:
  1874. begin
  1875. { the use of forwarddef should not backfire as the
  1876. specialization always belongs to the current module }
  1877. if not tprocdef(def).forwarddef then
  1878. continue;
  1879. if not assigned(def.genericdef) then
  1880. internalerror(2015061903);
  1881. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1882. if hmodule=nil then
  1883. internalerror(2015061904);
  1884. { we need to check for a forward declaration only if the
  1885. generic was declared in the same unit (otherwise there
  1886. should be one) }
  1887. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  1888. begin
  1889. readdlist.add(def);
  1890. continue;
  1891. end;
  1892. specialization_init(tstoreddef(def).genericdef,state);
  1893. process_procdef(tprocdef(def),hmodule);
  1894. specialization_done(state);
  1895. end;
  1896. recorddef,
  1897. objectdef:
  1898. begin
  1899. specialization_init(tstoreddef(def).genericdef,state);
  1900. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  1901. readdlist.add(def);
  1902. specialization_done(state);
  1903. end;
  1904. else
  1905. ;
  1906. end;
  1907. end;
  1908. { add those defs back to the pending list for which we don't yet have
  1909. all method bodies }
  1910. for i:=0 to readdlist.count-1 do
  1911. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  1912. readdlist.free;
  1913. list.free;
  1914. end;
  1915. procedure maybe_add_pending_specialization(def:tdef);
  1916. var
  1917. hmodule : tmodule;
  1918. st : tsymtable;
  1919. begin
  1920. if parse_generic then
  1921. exit;
  1922. st:=def.owner;
  1923. while st.symtabletype in [localsymtable] do
  1924. st:=st.defowner.owner;
  1925. hmodule:=find_module_from_symtable(st);
  1926. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  1927. current_module.pendingspecializations.add(def.typename,def);
  1928. end;
  1929. end.