pgenutil.pas 86 KB

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