pgenutil.pas 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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. { since commit 48986 deflist might have NIL entries }
  766. if not assigned(def) then
  767. exit;
  768. case def.typ of
  769. procdef:
  770. tprocdef(def).forwarddef:=false;
  771. objectdef,
  772. recorddef:
  773. begin
  774. st:=def.getsymtable(gs_record);
  775. for i:=0 to st.deflist.count-1 do
  776. unset_forwarddef(tdef(st.deflist[i]));
  777. end;
  778. else
  779. ;
  780. end;
  781. end;
  782. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  783. var
  784. i : longint;
  785. begin
  786. gendef:=nil;
  787. psym:=nil;
  788. case sym.typ of
  789. typesym:
  790. begin
  791. gendef:=ttypesym(sym).typedef
  792. end;
  793. procsym:
  794. begin
  795. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  796. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  797. begin
  798. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  799. break;
  800. end;
  801. psym:=sym;
  802. end
  803. else
  804. internalerror(200710171);
  805. end;
  806. end;
  807. var
  808. finalspecializename,
  809. ufinalspecializename : tidstring;
  810. prettyname : ansistring;
  811. generictypelist : tfphashobjectlist;
  812. srsymtable,
  813. specializest : tsymtable;
  814. hashedid : thashedidstring;
  815. tempst : tglobalsymtable;
  816. psym,
  817. srsym : tsym;
  818. paramdef1,
  819. paramdef2,
  820. def : tdef;
  821. old_block_type : tblock_type;
  822. state : tspecializationstate;
  823. old_current_structdef : tabstractrecorddef;
  824. old_current_specializedef,
  825. old_current_genericdef : tstoreddef;
  826. old_current_procinfo : tprocinfo;
  827. old_module_procinfo : tobject;
  828. hmodule : tmodule;
  829. oldcurrent_filepos : tfileposinfo;
  830. recordbuf : tdynamicarray;
  831. hadtypetoken : boolean;
  832. i,
  833. replaydepth : longint;
  834. item : tobject;
  835. allequal,
  836. hintsprocessed : boolean;
  837. pd : tprocdef;
  838. pdflags : tpdflags;
  839. begin
  840. if not assigned(context) then
  841. internalerror(2015052203);
  842. result:=nil;
  843. pd:=nil;
  844. if not check_generic_constraints(genericdef,context.paramlist,context.poslist) then
  845. begin
  846. { the parameters didn't fit the constraints, so don't continue with the
  847. specialization }
  848. result:=generrordef;
  849. exit;
  850. end;
  851. { build the new type's name }
  852. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  853. ufinalspecializename:=upper(finalspecializename);
  854. if genericdef.typ=procdef then
  855. prettyname:=tprocdef(genericdef).procsym.prettyname
  856. else
  857. prettyname:=genericdef.typesym.prettyname;
  858. prettyname:=prettyname+'<'+context.prettyname+'>';
  859. generictypelist:=tfphashobjectlist.create(false);
  860. { build the list containing the types for the generic params }
  861. if not assigned(genericdef.genericparas) then
  862. internalerror(2013092601);
  863. if context.paramlist.count<>genericdef.genericparas.count then
  864. internalerror(2013092603);
  865. for i:=0 to genericdef.genericparas.Count-1 do
  866. begin
  867. srsym:=tsym(genericdef.genericparas[i]);
  868. if not (sp_generic_para in srsym.symoptions) then
  869. internalerror(2013092602);
  870. generictypelist.add(srsym.realname,context.paramlist[i]);
  871. end;
  872. { Special case if we are referencing the current defined object }
  873. if assigned(current_structdef) and
  874. (current_structdef.objname^=ufinalspecializename) then
  875. result:=current_structdef;
  876. { Can we reuse an already specialized type? }
  877. { for this first check whether we are currently specializing a nested
  878. type of the current (main) specialization (this is necessary, because
  879. during that time the symbol of the main specialization will still
  880. contain a reference to an errordef) }
  881. if not assigned(result) and assigned(current_specializedef) then
  882. begin
  883. def:=current_specializedef;
  884. repeat
  885. if def.typ in [objectdef,recorddef] then
  886. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  887. result:=def;
  888. break;
  889. end;
  890. if assigned(def.owner) then
  891. def:=tstoreddef(def.owner.defowner)
  892. else
  893. { this can happen when specializing a generic function }
  894. def:=nil;
  895. until not assigned(def) or not (df_specialization in def.defoptions);
  896. end;
  897. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  898. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  899. parsing anyway), so for now we treat those still as generic defs without doing a partial
  900. specialization }
  901. if not assigned(result) then
  902. begin
  903. def:=current_genericdef;
  904. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  905. begin
  906. if (df_generic in def.defoptions) and (def=genericdef) then
  907. begin
  908. result:=def;
  909. break;
  910. end;
  911. { the following happens when a routine with its parent struct
  912. as parameter is specialized as a parameter or result of a
  913. generic function }
  914. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  915. begin
  916. if tstoreddef(def).genericparas.count=generictypelist.count then
  917. begin
  918. allequal:=true;
  919. for i:=0 to generictypelist.count-1 do
  920. begin
  921. if tsym(generictypelist[i]).typ<>tsym(tstoreddef(def).genericparas[i]).typ then
  922. begin
  923. allequal:=false;
  924. break;
  925. end;
  926. if tsym(generictypelist[i]).typ=constsym then
  927. paramdef1:=tconstsym(generictypelist[i]).constdef
  928. else
  929. paramdef1:=ttypesym(generictypelist[i]).typedef;
  930. if tsym(tstoreddef(def).genericparas[i]).typ=constsym then
  931. paramdef2:=tconstsym(tstoreddef(def).genericparas[i]).constdef
  932. else
  933. paramdef2:=ttypesym(tstoreddef(def).genericparas[i]).typedef;
  934. if not equal_defs(paramdef1,paramdef2) then
  935. begin
  936. allequal:=false;
  937. break;
  938. end;
  939. if (tsym(generictypelist[i]).typ=constsym) and
  940. (
  941. (tconstsym(generictypelist[i]).consttyp<>tconstsym(tstoreddef(def).genericparas[i]).consttyp) or
  942. not same_constvalue(tconstsym(generictypelist[i]).consttyp,tconstsym(generictypelist[i]).value,tconstsym(tstoreddef(def).genericparas[i]).value)
  943. ) then
  944. begin
  945. allequal:=false;
  946. break;
  947. end;
  948. end;
  949. if allequal then
  950. begin
  951. result:=def;
  952. break;
  953. end;
  954. end;
  955. end;
  956. def:=tstoreddef(def.owner.defowner);
  957. end;
  958. end;
  959. { decide in which symtable to put the specialization }
  960. if parse_generic and not assigned(result) then
  961. begin
  962. srsymtable:=symtablestack.top;
  963. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  964. { if we are currently specializing a routine we need to specialize into
  965. the routine's local- or parasymtable so that they are correctly
  966. registered should the specialization be finalized }
  967. specializest:=srsymtable
  968. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  969. { if we are parsing the definition of a method we specialize into
  970. the local symtable of it }
  971. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  972. else
  973. begin
  974. if not assigned(current_genericdef) then
  975. internalerror(2014050901);
  976. { we specialize the partial specialization into the symtable of the currently parsed
  977. generic }
  978. case current_genericdef.typ of
  979. procvardef:
  980. specializest:=current_genericdef.getsymtable(gs_para);
  981. procdef:
  982. specializest:=current_genericdef.getsymtable(gs_local);
  983. objectdef,
  984. recorddef:
  985. specializest:=current_genericdef.getsymtable(gs_record);
  986. arraydef:
  987. specializest:=tarraydef(current_genericdef).symtable;
  988. else
  989. internalerror(2014050902);
  990. end;
  991. end;
  992. end
  993. else
  994. if current_module.is_unit and current_module.in_interface then
  995. specializest:=current_module.globalsymtable
  996. else
  997. specializest:=current_module.localsymtable;
  998. if not assigned(specializest) then
  999. internalerror(2014050910);
  1000. { now check whether there is a specialization somewhere else }
  1001. psym:=nil;
  1002. if not assigned(result) then
  1003. begin
  1004. hashedid.id:=ufinalspecializename;
  1005. if specializest.symtabletype=objectsymtable then
  1006. begin
  1007. { search also in parent classes }
  1008. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  1009. internalerror(2016112901);
  1010. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  1011. srsym:=nil;
  1012. end
  1013. else
  1014. srsym:=tsym(specializest.findwithhash(hashedid));
  1015. if assigned(srsym) then
  1016. begin
  1017. retrieve_genericdef_or_procsym(srsym,result,psym);
  1018. end
  1019. else
  1020. { the generic could have been specialized in the globalsymtable
  1021. already, so search there as well }
  1022. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  1023. begin
  1024. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  1025. if assigned(srsym) then
  1026. begin
  1027. retrieve_genericdef_or_procsym(srsym,result,psym);
  1028. end;
  1029. end;
  1030. end;
  1031. if not assigned(result) then
  1032. begin
  1033. specialization_init(genericdef,state);
  1034. { push a temporary global symtable so that the specialization is
  1035. added to the correct symtable; this symtable does not contain
  1036. any other symbols, so that the type resolution can not be
  1037. influenced by symbols in the current unit }
  1038. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  1039. symtablestack.push(tempst);
  1040. { Reparse the original type definition }
  1041. begin
  1042. old_current_specializedef:=nil;
  1043. old_current_genericdef:=nil;
  1044. old_current_structdef:=nil;
  1045. old_current_procinfo:=current_procinfo;
  1046. old_module_procinfo:=current_module.procinfo;
  1047. current_procinfo:=nil;
  1048. current_module.procinfo:=nil;
  1049. if parse_class_parent then
  1050. begin
  1051. old_current_structdef:=current_structdef;
  1052. old_current_genericdef:=current_genericdef;
  1053. old_current_specializedef:=current_specializedef;
  1054. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  1055. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  1056. else
  1057. current_structdef:=nil;
  1058. current_genericdef:=nil;
  1059. current_specializedef:=nil;
  1060. end;
  1061. maybe_add_waiting_unit(genericdef);
  1062. { First a new sym so we can reuse this specialization and
  1063. references to this specialization can be handled }
  1064. if genericdef.typ=procdef then
  1065. if assigned(psym) then
  1066. srsym:=psym
  1067. else
  1068. srsym:=cprocsym.create(finalspecializename)
  1069. else
  1070. srsym:=ctypesym.create(finalspecializename,generrordef);
  1071. { insert the symbol only if we don't know already that we have
  1072. a procsym to add it to }
  1073. if not assigned(psym) then
  1074. specializest.insert(srsym);
  1075. { specializations are declarations as such it is the wisest to
  1076. declare set the blocktype to "type"; otherwise we'll
  1077. experience unexpected side effects like the addition of
  1078. classrefdefs if we have a generic that's derived from another
  1079. generic }
  1080. old_block_type:=block_type;
  1081. block_type:=bt_type;
  1082. if (
  1083. (genericdef.typ=procdef) and
  1084. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  1085. ) or (
  1086. (genericdef.typ<>procdef) and
  1087. not assigned(genericdef.generictokenbuf)
  1088. ) then
  1089. internalerror(200511171);
  1090. hmodule:=find_module_from_symtable(genericdef.owner);
  1091. if hmodule=nil then
  1092. internalerror(2012051202);
  1093. oldcurrent_filepos:=current_filepos;
  1094. { use the index the module got from the current compilation process }
  1095. current_filepos.moduleindex:=hmodule.unit_index;
  1096. current_tokenpos:=current_filepos;
  1097. if parse_generic then
  1098. begin
  1099. recordbuf:=current_scanner.recordtokenbuf;
  1100. current_scanner.recordtokenbuf:=nil;
  1101. end
  1102. else
  1103. recordbuf:=nil;
  1104. replaydepth:=current_scanner.replay_stack_depth;
  1105. if genericdef.typ=procdef then
  1106. begin
  1107. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf,hmodule.change_endian);
  1108. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  1109. if assigned(pd) then
  1110. begin
  1111. if assigned(psym) then
  1112. pd.procsym:=psym
  1113. else
  1114. pd.procsym:=srsym;
  1115. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions,tprocdef(genericdef).struct);
  1116. end;
  1117. result:=pd;
  1118. end
  1119. else
  1120. begin
  1121. current_scanner.startreplaytokens(genericdef.generictokenbuf,hmodule.change_endian);
  1122. hadtypetoken:=false;
  1123. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  1124. ttypesym(srsym).typedef:=result;
  1125. result.typesym:=srsym;
  1126. if _prettyname<>'' then
  1127. ttypesym(result.typesym).fprettyname:=_prettyname
  1128. else
  1129. ttypesym(result.typesym).fprettyname:=prettyname;
  1130. end;
  1131. current_filepos:=oldcurrent_filepos;
  1132. { Note regarding hint directives:
  1133. There is no need to remove the flags for them from the
  1134. specialized generic symbol, because hint directives that
  1135. follow the specialization are handled by the code in
  1136. pdecl.types_dec and added to the type symbol.
  1137. E.g.: TFoo = TBar<Blubb> deprecated;
  1138. Here the symbol TBar$1$Blubb will contain the
  1139. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1140. case result.typ of
  1141. { Build VMT indexes for classes and read hint directives }
  1142. objectdef:
  1143. begin
  1144. if replaydepth>current_scanner.replay_stack_depth then
  1145. begin
  1146. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1147. if replaydepth>current_scanner.replay_stack_depth then
  1148. consume(_SEMICOLON);
  1149. end;
  1150. build_vmt(tobjectdef(result));
  1151. end;
  1152. { handle params, calling convention, etc }
  1153. procvardef:
  1154. begin
  1155. hintsprocessed:=false;
  1156. if replaydepth>current_scanner.replay_stack_depth then
  1157. begin
  1158. if not check_proc_directive(true) then
  1159. begin
  1160. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1161. if replaydepth>current_scanner.replay_stack_depth then
  1162. consume(_SEMICOLON);
  1163. end
  1164. else
  1165. hintsprocessed:=true;
  1166. end;
  1167. if replaydepth>current_scanner.replay_stack_depth then
  1168. parse_var_proc_directives(ttypesym(srsym));
  1169. handle_calling_convention(tprocvardef(result),hcc_default_actions_intf);
  1170. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  1171. begin
  1172. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1173. if replaydepth>current_scanner.replay_stack_depth then
  1174. consume(_SEMICOLON);
  1175. end;
  1176. end;
  1177. procdef:
  1178. begin
  1179. pdflags:=[];
  1180. if genericdef.owner.symtabletype=objectsymtable then
  1181. include(pdflags,pd_object)
  1182. else if genericdef.owner.symtabletype=recordsymtable then
  1183. include(pdflags,pd_record);
  1184. parse_proc_directives(pd,pdflags);
  1185. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1186. consume(_SEMICOLON);
  1187. if parse_generic then
  1188. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1189. else
  1190. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1191. pdflags:=pdflags+[pd_body,pd_implemen];
  1192. proc_add_definition(tprocdef(result));
  1193. { for partial specializations we implicitely declare the routine as
  1194. having its implementation although we'll not specialize it in reality }
  1195. if parse_generic then
  1196. unset_forwarddef(result);
  1197. end;
  1198. else
  1199. { parse hint directives for records and arrays }
  1200. if replaydepth>current_scanner.replay_stack_depth then begin
  1201. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1202. if replaydepth>current_scanner.replay_stack_depth then
  1203. consume(_SEMICOLON);
  1204. end;
  1205. end;
  1206. { Consume the remainder of the buffer }
  1207. while current_scanner.replay_stack_depth>replaydepth do
  1208. consume(token);
  1209. if assigned(recordbuf) then
  1210. begin
  1211. if assigned(current_scanner.recordtokenbuf) then
  1212. internalerror(2014050909);
  1213. current_scanner.recordtokenbuf:=recordbuf;
  1214. end;
  1215. block_type:=old_block_type;
  1216. current_procinfo:=old_current_procinfo;
  1217. current_module.procinfo:=old_module_procinfo;
  1218. if parse_class_parent then
  1219. begin
  1220. current_structdef:=old_current_structdef;
  1221. current_genericdef:=old_current_genericdef;
  1222. current_specializedef:=old_current_specializedef;
  1223. end;
  1224. end;
  1225. { extract all created symbols and defs from the temporary symtable
  1226. and add them to the specializest }
  1227. for i:=tempst.SymList.Count-1 downto 0 do
  1228. begin
  1229. item:=tempst.SymList.Items[i];
  1230. { using changeowner the symbol is automatically added to the
  1231. new symtable }
  1232. tsym(item).ChangeOwner(specializest);
  1233. end;
  1234. for i:=tempst.DefList.Count-1 downto 0 do
  1235. begin
  1236. item:=tempst.DefList.Items[i];
  1237. { using changeowner the def is automatically added to the new
  1238. symtable }
  1239. tdef(item).ChangeOwner(specializest);
  1240. { for partial specializations we implicitely declare any methods as having their
  1241. implementations although we'll not specialize them in reality }
  1242. if parse_generic then
  1243. unset_forwarddef(tdef(item));
  1244. end;
  1245. { if a generic was declared during the specialization we need to
  1246. flag the specialize symtable accordingly }
  1247. if sto_has_generic in tempst.tableoptions then
  1248. specializest.includeoption(sto_has_generic);
  1249. tempst.free;
  1250. specialization_done(state);
  1251. { procdefs are only added once we know which overload we use }
  1252. if not parse_generic and (result.typ<>procdef) then
  1253. current_module.pendingspecializations.add(result.typename,result);
  1254. end;
  1255. generictypelist.free;
  1256. if assigned(genericdef) then
  1257. begin
  1258. { check the hints of the found generic symbol }
  1259. if genericdef.typ=procdef then
  1260. srsym:=tprocdef(genericdef).procsym
  1261. else
  1262. srsym:=genericdef.typesym;
  1263. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1264. end;
  1265. end;
  1266. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  1267. var
  1268. context : tspecializationcontext;
  1269. genericdef : tstoreddef;
  1270. begin
  1271. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,nil,parsedpos));
  1272. if genericdef<>generrordef then
  1273. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1274. tt:=genericdef;
  1275. if assigned(context) then
  1276. context.free;
  1277. end;
  1278. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1279. var
  1280. generictype : tstoredsym;
  1281. i,firstidx,const_list_index : longint;
  1282. srsymtable : tsymtable;
  1283. basedef,def : tdef;
  1284. defname : tidstring;
  1285. allowconst,
  1286. allowconstructor,
  1287. is_const,
  1288. doconsume : boolean;
  1289. constraintdata : tgenericconstraintdata;
  1290. old_block_type : tblock_type;
  1291. fileinfo : tfileposinfo;
  1292. begin
  1293. result:=tfphashobjectlist.create(false);
  1294. firstidx:=0;
  1295. const_list_index:=0;
  1296. old_block_type:=block_type;
  1297. block_type:=bt_type;
  1298. allowconst:=true;
  1299. is_const:=false;
  1300. repeat
  1301. if allowconst and try_to_consume(_CONST) then
  1302. begin
  1303. allowconst:=false;
  1304. is_const:=true;
  1305. const_list_index:=result.count;
  1306. end;
  1307. if token=_ID then
  1308. begin
  1309. if is_const then
  1310. generictype:=cconstsym.create_undefined(orgpattern,cundefinedtype)
  1311. else
  1312. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  1313. { type parameters need to be added as strict private }
  1314. generictype.visibility:=vis_strictprivate;
  1315. include(generictype.symoptions,sp_generic_para);
  1316. result.add(orgpattern,generictype);
  1317. end;
  1318. consume(_ID);
  1319. fileinfo:=current_tokenpos;
  1320. { const restriction }
  1321. if is_const and try_to_consume(_COLON) then
  1322. begin
  1323. def:=nil;
  1324. { parse the type and assign the const type to generictype }
  1325. single_type(def,[]);
  1326. for i:=const_list_index to result.count-1 do
  1327. begin
  1328. { finalize constant information once type is known }
  1329. if assigned(def) and (def.typ in tgeneric_param_const_types) then
  1330. begin
  1331. case def.typ of
  1332. orddef,
  1333. enumdef:
  1334. tconstsym(result[i]).consttyp:=constord;
  1335. stringdef:
  1336. tconstsym(result[i]).consttyp:=conststring;
  1337. floatdef:
  1338. tconstsym(result[i]).consttyp:=constreal;
  1339. setdef:
  1340. tconstsym(result[i]).consttyp:=constset;
  1341. { pointer always refers to nil with constants }
  1342. pointerdef:
  1343. tconstsym(result[i]).consttyp:=constnil;
  1344. else
  1345. internalerror(2020011402);
  1346. end;
  1347. tconstsym(result[i]).constdef:=def;
  1348. end
  1349. else
  1350. Message1(type_e_generic_const_type_not_allowed,def.fulltypename);
  1351. end;
  1352. { after type restriction const list terminates }
  1353. is_const:=false;
  1354. end
  1355. { type restriction }
  1356. else if try_to_consume(_COLON) then
  1357. begin
  1358. if not allowconstraints then
  1359. Message(parser_e_generic_constraints_not_allowed_here);
  1360. { construct a name which can be used for a type specification }
  1361. constraintdata:=tgenericconstraintdata.create;
  1362. constraintdata.fileinfo:=fileinfo;
  1363. defname:='';
  1364. str(current_module.deflist.count,defname);
  1365. defname:='$gendef'+defname;
  1366. allowconstructor:=m_delphi in current_settings.modeswitches;
  1367. basedef:=generrordef;
  1368. repeat
  1369. doconsume:=true;
  1370. case token of
  1371. _CONSTRUCTOR:
  1372. begin
  1373. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1374. Message(parser_e_illegal_expression);
  1375. include(constraintdata.flags,gcf_constructor);
  1376. allowconstructor:=false;
  1377. end;
  1378. _CLASS:
  1379. begin
  1380. if gcf_class in constraintdata.flags then
  1381. Message(parser_e_illegal_expression);
  1382. if basedef=generrordef then
  1383. include(constraintdata.flags,gcf_class)
  1384. else
  1385. Message(parser_e_illegal_expression);
  1386. end;
  1387. _RECORD:
  1388. begin
  1389. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1390. or (constraintdata.interfaces.count>0) then
  1391. Message(parser_e_illegal_expression)
  1392. else
  1393. begin
  1394. srsymtable:=trecordsymtable.create(defname,0,1);
  1395. basedef:=crecorddef.create(defname,srsymtable);
  1396. include(constraintdata.flags,gcf_record);
  1397. allowconstructor:=false;
  1398. end;
  1399. end;
  1400. else
  1401. begin
  1402. { after single_type "token" is the trailing ",", ";" or
  1403. ">"! }
  1404. doconsume:=false;
  1405. { def is already set to a class or record }
  1406. if gcf_record in constraintdata.flags then
  1407. Message(parser_e_illegal_expression);
  1408. single_type(def, [stoAllowSpecialization]);
  1409. { only types that are inheritable are allowed }
  1410. if (def.typ<>objectdef) or
  1411. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1412. Message1(type_e_class_or_interface_type_expected,def.typename)
  1413. else
  1414. case tobjectdef(def).objecttype of
  1415. odt_class,
  1416. odt_javaclass:
  1417. begin
  1418. if gcf_class in constraintdata.flags then
  1419. { "class" + concrete class is not allowed }
  1420. Message(parser_e_illegal_expression)
  1421. else
  1422. { do we already have a concrete class? }
  1423. if basedef<>generrordef then
  1424. Message(parser_e_illegal_expression)
  1425. else
  1426. basedef:=def;
  1427. end;
  1428. odt_interfacecom,
  1429. odt_interfacecorba,
  1430. odt_interfacejava,
  1431. odt_dispinterface:
  1432. constraintdata.interfaces.add(def);
  1433. else
  1434. ;
  1435. end;
  1436. end;
  1437. end;
  1438. if doconsume then
  1439. consume(token);
  1440. until not try_to_consume(_COMMA);
  1441. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1442. (constraintdata.interfaces.count>1) or
  1443. (
  1444. (basedef.typ=objectdef) and
  1445. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1446. ) then
  1447. begin
  1448. if basedef.typ=errordef then
  1449. { don't pass an errordef as a parent to a tobjectdef }
  1450. basedef:=class_tobject
  1451. else
  1452. if (basedef.typ<>objectdef) or
  1453. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1454. internalerror(2012101101);
  1455. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1456. for i:=0 to constraintdata.interfaces.count-1 do
  1457. tobjectdef(basedef).implementedinterfaces.add(
  1458. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1459. end
  1460. else
  1461. if constraintdata.interfaces.count=1 then
  1462. begin
  1463. if basedef.typ<>errordef then
  1464. internalerror(2013021601);
  1465. def:=tdef(constraintdata.interfaces[0]);
  1466. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1467. constraintdata.interfaces.delete(0);
  1468. end;
  1469. if basedef.typ<>errordef then
  1470. with tstoreddef(basedef) do
  1471. begin
  1472. genconstraintdata:=tgenericconstraintdata.create;
  1473. genconstraintdata.flags:=constraintdata.flags;
  1474. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1475. genconstraintdata.fileinfo:=constraintdata.fileinfo;
  1476. include(defoptions,df_genconstraint);
  1477. end;
  1478. for i:=firstidx to result.count-1 do
  1479. ttypesym(result[i]).typedef:=basedef;
  1480. { we need a typesym in case we do a Delphi-mode inline
  1481. specialization with this parameter; so just use the first sym }
  1482. if not assigned(basedef.typesym) then
  1483. basedef.typesym:=ttypesym(result[firstidx]);
  1484. firstidx:=result.count;
  1485. constraintdata.free;
  1486. end
  1487. else
  1488. begin
  1489. if token=_SEMICOLON then
  1490. begin
  1491. { two different typeless parameters are considered as incompatible }
  1492. for i:=firstidx to result.count-1 do
  1493. if tsym(result[i]).typ<>constsym then
  1494. begin
  1495. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1496. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1497. end;
  1498. { a semicolon terminates a type parameter group }
  1499. firstidx:=result.count;
  1500. end;
  1501. end;
  1502. if token=_SEMICOLON then
  1503. begin
  1504. is_const:=false;
  1505. allowconst:=true;
  1506. end;
  1507. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1508. { if the constant parameter is not terminated then the type restriction was
  1509. not specified and we need to give an error }
  1510. if is_const then
  1511. consume(_COLON);
  1512. { two different typeless parameters are considered as incompatible }
  1513. for i:=firstidx to result.count-1 do
  1514. if tsym(result[i]).typ<>constsym then
  1515. begin
  1516. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1517. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1518. end;
  1519. block_type:=old_block_type;
  1520. end;
  1521. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1522. var
  1523. i : longint;
  1524. generictype : tstoredsym;
  1525. generictypedef : tdef;
  1526. sym : tsym;
  1527. st : tsymtable;
  1528. begin
  1529. def.genericdef:=genericdef;
  1530. if not assigned(genericlist) then
  1531. exit;
  1532. if assigned(genericdef) then
  1533. include(def.defoptions,df_specialization)
  1534. else
  1535. if genericlist.count>0 then
  1536. include(def.defoptions,df_generic);
  1537. case def.typ of
  1538. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1539. arraydef: st:=tarraydef(def).symtable;
  1540. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1541. else
  1542. internalerror(201101020);
  1543. end;
  1544. if (genericlist.count>0) and not assigned(def.genericparas) then
  1545. def.genericparas:=tfphashobjectlist.create(false);
  1546. for i:=0 to genericlist.count-1 do
  1547. begin
  1548. generictype:=tstoredsym(genericlist[i]);
  1549. if assigned(generictype.owner) then
  1550. begin
  1551. if generictype.typ=typesym then
  1552. sym:=ctypesym.create(genericlist.nameofindex(i),ttypesym(generictype).typedef)
  1553. else if generictype.typ=constsym then
  1554. { generictype is a constsym that was created in create_generic_constsym
  1555. during phase 1 so we pass this directly without copying }
  1556. begin
  1557. sym:=generictype;
  1558. { the sym name is still undefined so we set it to match
  1559. the generic param name so it's accessible }
  1560. sym.realname:=genericlist.nameofindex(i);
  1561. include(sym.symoptions,sp_generic_const);
  1562. end
  1563. else
  1564. internalerror(2019021602);
  1565. { type parameters need to be added as strict private }
  1566. sym.visibility:=vis_strictprivate;
  1567. st.insert(sym);
  1568. include(sym.symoptions,sp_generic_para);
  1569. end
  1570. else
  1571. begin
  1572. if generictype.typ=typesym then
  1573. begin
  1574. generictypedef:=ttypesym(generictype).typedef;
  1575. if (generictypedef.typ=undefineddef) and (generictypedef<>cundefinedtype) then
  1576. begin
  1577. { the generic parameters were parsed before the genericdef existed thus the
  1578. undefineddefs were added as part of the parent symtable }
  1579. if assigned(generictypedef.owner) then
  1580. generictypedef.owner.DefList.Extract(generictypedef);
  1581. generictypedef.changeowner(st);
  1582. end;
  1583. end;
  1584. st.insert(generictype);
  1585. include(generictype.symoptions,sp_generic_para);
  1586. end;
  1587. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1588. end;
  1589. end;
  1590. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1591. var
  1592. gensym : ttypesym;
  1593. begin
  1594. { for generics in non-Delphi modes we insert a private type symbol
  1595. that has the same base name as the currently parsed generic and
  1596. that references this defs }
  1597. if not (m_delphi in current_settings.modeswitches) and
  1598. (
  1599. (
  1600. parse_generic and
  1601. assigned(genericlist) and
  1602. (genericlist.count>0)
  1603. ) or
  1604. (
  1605. assigned(current_specializedef) and
  1606. assigned(current_structdef.genericdef) and
  1607. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1608. (pos('$',name)>0)
  1609. )
  1610. ) then
  1611. begin
  1612. { we need to pass nil as def here, because the constructor wants
  1613. to set the typesym of the def which is not what we want }
  1614. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  1615. gensym.typedef:=current_structdef;
  1616. include(gensym.symoptions,sp_internal);
  1617. { the symbol should be only visible to the generic class
  1618. itself }
  1619. gensym.visibility:=vis_strictprivate;
  1620. symtablestack.top.insert(gensym);
  1621. end;
  1622. end;
  1623. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1624. var
  1625. crc : cardinal;
  1626. begin
  1627. if specializename='' then
  1628. internalerror(2012061901);
  1629. { build the new type's name }
  1630. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1631. result:=name+'$crc'+hexstr(crc,8);
  1632. if owner_hierarchy<>'' then
  1633. begin
  1634. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1635. result:=result+'$crc'+hexstr(crc,8);
  1636. end;
  1637. end;
  1638. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1639. var
  1640. i,code : longint;
  1641. countstr : string;
  1642. begin
  1643. for i:=length(name) downto 1 do
  1644. if name[i]='$' then
  1645. begin
  1646. nongeneric:=copy(name,1,i-1);
  1647. countstr:=copy(name,i+1,length(name)-i);
  1648. val(countstr,count,code);
  1649. if code<>0 then
  1650. break;
  1651. exit;
  1652. end;
  1653. nongeneric:=name;
  1654. count:=0;
  1655. end;
  1656. procedure add_generic_dummysym(sym:tsym);
  1657. var
  1658. list: TFPObjectList;
  1659. srsym : tsym;
  1660. srsymtable : tsymtable;
  1661. entry : tgenericdummyentry;
  1662. begin
  1663. if sp_generic_dummy in sym.symoptions then
  1664. begin
  1665. { did we already search for a generic with that name? }
  1666. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  1667. if not assigned(list) then
  1668. begin
  1669. list:=tfpobjectlist.create(true);
  1670. current_module.genericdummysyms.add(sym.name,list);
  1671. end;
  1672. { is the dummy sym still "dummy"? }
  1673. if (sym.typ=typesym) and
  1674. (
  1675. { dummy sym defined in mode Delphi }
  1676. (ttypesym(sym).typedef.typ=undefineddef) or
  1677. { dummy sym defined in non-Delphi mode }
  1678. (tstoreddef(ttypesym(sym).typedef).is_generic)
  1679. ) then
  1680. begin
  1681. { do we have a non-generic type of the same name
  1682. available? }
  1683. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  1684. srsym:=nil;
  1685. end
  1686. else if sym.typ=procsym then
  1687. srsym:=sym
  1688. else
  1689. { dummy symbol is already not so dummy anymore }
  1690. srsym:=nil;
  1691. if assigned(srsym) then
  1692. begin
  1693. entry:=tgenericdummyentry.create;
  1694. entry.resolvedsym:=srsym;
  1695. entry.dummysym:=sym;
  1696. list.add(entry);
  1697. end;
  1698. end;
  1699. end;
  1700. function resolve_generic_dummysym(const name:tidstring):tsym;
  1701. var
  1702. list : tfpobjectlist;
  1703. begin
  1704. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1705. if assigned(list) and (list.count>0) then
  1706. result:=tgenericdummyentry(list.last).resolvedsym
  1707. else
  1708. result:=nil;
  1709. end;
  1710. function could_be_generic(const name:tidstring):boolean;
  1711. begin
  1712. result:=(name<>'') and
  1713. (current_module.genericdummysyms.findindexof(name)>=0);
  1714. end;
  1715. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1716. var
  1717. pu : tused_unit;
  1718. hmodule : tmodule;
  1719. unitsyms : TFPHashObjectList;
  1720. sym : tsym;
  1721. i : Integer;
  1722. begin
  1723. if not assigned(genericdef) then
  1724. internalerror(200705151);
  1725. { Setup symtablestack at definition time
  1726. to get types right, however this is not perfect, we should probably record
  1727. the resolved symbols }
  1728. state.oldsymtablestack:=symtablestack;
  1729. state.oldextendeddefs:=current_module.extendeddefs;
  1730. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1731. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1732. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1733. symtablestack:=tdefawaresymtablestack.create;
  1734. hmodule:=find_module_from_symtable(genericdef.owner);
  1735. if hmodule=nil then
  1736. internalerror(200705152);
  1737. { collect all unit syms in the generic's unit as we need to establish
  1738. their unitsym.module link again so that unit identifiers can be used }
  1739. unitsyms:=tfphashobjectlist.create(false);
  1740. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1741. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1742. begin
  1743. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1744. if sym.typ=unitsym then
  1745. unitsyms.add(upper(sym.realname),sym);
  1746. end;
  1747. { add all units if we are specializing inside the current unit (as the
  1748. generic could have been declared in the implementation part), but load
  1749. only interface units, if we are in a different unit as then the generic
  1750. needs to be in the interface section }
  1751. pu:=tused_unit(hmodule.used_units.first);
  1752. while assigned(pu) do
  1753. begin
  1754. if not assigned(pu.u.globalsymtable) then
  1755. { in certain circular, but valid unit constellations it can happen
  1756. that we specialize a generic in a different unit that was used
  1757. in the implementation section of the generic's unit and were the
  1758. interface is still being parsed and thus the localsymtable is in
  1759. reality the global symtable }
  1760. if pu.u.in_interface then
  1761. symtablestack.push(pu.u.localsymtable)
  1762. else
  1763. internalerror(200705153)
  1764. else
  1765. symtablestack.push(pu.u.globalsymtable);
  1766. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1767. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1768. tunitsym(sym).module:=pu.u;
  1769. pu:=tused_unit(pu.next);
  1770. end;
  1771. unitsyms.free;
  1772. if assigned(hmodule.globalsymtable) then
  1773. symtablestack.push(hmodule.globalsymtable);
  1774. { push the localsymtable if needed }
  1775. if ((hmodule<>current_module) or not current_module.in_interface)
  1776. and assigned(hmodule.localsymtable) then
  1777. symtablestack.push(hmodule.localsymtable);
  1778. end;
  1779. procedure specialization_done(var state: tspecializationstate);
  1780. begin
  1781. { Restore symtablestack }
  1782. current_module.extendeddefs.free;
  1783. current_module.extendeddefs:=state.oldextendeddefs;
  1784. current_module.genericdummysyms.free;
  1785. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1786. symtablestack.free;
  1787. symtablestack:=state.oldsymtablestack;
  1788. { clear the state record to be on the safe side }
  1789. fillchar(state, sizeof(state), 0);
  1790. end;
  1791. {****************************************************************************
  1792. SPECIALIZATION BODY GENERATION
  1793. ****************************************************************************}
  1794. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  1795. var
  1796. oldcurrent_filepos : tfileposinfo;
  1797. begin
  1798. if assigned(def.genericdef) and
  1799. (def.genericdef.typ=procdef) and
  1800. assigned(tprocdef(def.genericdef).generictokenbuf) then
  1801. begin
  1802. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  1803. internalerror(2015061902);
  1804. oldcurrent_filepos:=current_filepos;
  1805. current_filepos:=tprocdef(def.genericdef).fileinfo;
  1806. { use the index the module got from the current compilation process }
  1807. current_filepos.moduleindex:=hmodule.unit_index;
  1808. current_tokenpos:=current_filepos;
  1809. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf,hmodule.change_endian);
  1810. read_proc_body(def);
  1811. current_filepos:=oldcurrent_filepos;
  1812. end
  1813. { synthetic routines will be implemented afterwards }
  1814. else if def.synthetickind=tsk_none then
  1815. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  1816. end;
  1817. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  1818. var
  1819. i : longint;
  1820. hp : tdef;
  1821. hmodule : tmodule;
  1822. begin
  1823. result:=true;
  1824. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1825. if hmodule=nil then
  1826. internalerror(201202041);
  1827. for i:=0 to def.symtable.DefList.Count-1 do
  1828. begin
  1829. hp:=tdef(def.symtable.DefList[i]);
  1830. if hp.typ=procdef then
  1831. begin
  1832. { only generate the code if we need a body }
  1833. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  1834. continue;
  1835. { and the body is available already (which is implicitely the
  1836. case if the generic routine is part of another unit) }
  1837. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and
  1838. { may not be assigned in case it's a synthetic procdef that
  1839. still needs to be generated }
  1840. assigned(tprocdef(hp).genericdef) and
  1841. tprocdef(tprocdef(hp).genericdef).forwarddef then
  1842. begin
  1843. result:=false;
  1844. continue;
  1845. end;
  1846. process_procdef(tprocdef(hp),hmodule);
  1847. end
  1848. else
  1849. if hp.typ in [objectdef,recorddef] then
  1850. { generate code for subtypes as well }
  1851. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  1852. end;
  1853. end;
  1854. procedure generate_specialization_procs;
  1855. var
  1856. i : longint;
  1857. list,
  1858. readdlist : tfpobjectlist;
  1859. def : tstoreddef;
  1860. state : tspecializationstate;
  1861. hmodule : tmodule;
  1862. begin
  1863. { first copy all entries and then work with that list to ensure that
  1864. we don't get an infinite recursion }
  1865. list:=tfpobjectlist.create(false);
  1866. readdlist:=tfpobjectlist.create(false);
  1867. for i:=0 to current_module.pendingspecializations.Count-1 do
  1868. list.add(current_module.pendingspecializations.Items[i]);
  1869. current_module.pendingspecializations.clear;
  1870. for i:=0 to list.count-1 do
  1871. begin
  1872. def:=tstoreddef(list[i]);
  1873. if not tstoreddef(def).is_specialization then
  1874. continue;
  1875. case def.typ of
  1876. procdef:
  1877. begin
  1878. { the use of forwarddef should not backfire as the
  1879. specialization always belongs to the current module }
  1880. if not tprocdef(def).forwarddef then
  1881. continue;
  1882. if not assigned(def.genericdef) then
  1883. internalerror(2015061903);
  1884. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1885. if hmodule=nil then
  1886. internalerror(2015061904);
  1887. { we need to check for a forward declaration only if the
  1888. generic was declared in the same unit (otherwise there
  1889. should be one) }
  1890. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  1891. begin
  1892. readdlist.add(def);
  1893. continue;
  1894. end;
  1895. specialization_init(tstoreddef(def).genericdef,state);
  1896. process_procdef(tprocdef(def),hmodule);
  1897. specialization_done(state);
  1898. end;
  1899. recorddef,
  1900. objectdef:
  1901. begin
  1902. specialization_init(tstoreddef(def).genericdef,state);
  1903. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  1904. readdlist.add(def);
  1905. specialization_done(state);
  1906. end;
  1907. else
  1908. ;
  1909. end;
  1910. end;
  1911. { add those defs back to the pending list for which we don't yet have
  1912. all method bodies }
  1913. for i:=0 to readdlist.count-1 do
  1914. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  1915. readdlist.free;
  1916. list.free;
  1917. end;
  1918. procedure maybe_add_pending_specialization(def:tdef);
  1919. var
  1920. hmodule : tmodule;
  1921. st : tsymtable;
  1922. begin
  1923. if parse_generic then
  1924. exit;
  1925. st:=def.owner;
  1926. while st.symtabletype in [localsymtable] do
  1927. st:=st.defowner.owner;
  1928. hmodule:=find_module_from_symtable(st);
  1929. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  1930. current_module.pendingspecializations.add(def.typename,def);
  1931. end;
  1932. end.