pgenutil.pas 87 KB

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