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,floatdef,setdef,pointerdef,enumdef];
  68. tgeneric_param_nodes : tnodetypeset = [typen,ordconstn,stringconstn,realconstn,setconstn,niln];
  69. function get_generic_param_def(sym:tsym):tdef;
  70. begin
  71. if sym.typ=constsym then
  72. result:=tconstsym(sym).constdef
  73. else
  74. result:=ttypesym(sym).typedef;
  75. end;
  76. function compare_orddef_by_range(param1,param2:torddef;value:tconstvalue):boolean;
  77. begin
  78. if (value.valueord<param2.low) or (value.valueord>param2.high) then
  79. result:=false
  80. else
  81. result:=true;
  82. end;
  83. function compare_generic_params(param1,param2:tdef;constparamsym:tconstsym):boolean;
  84. begin
  85. if (param1.typ=orddef) and (param2.typ=orddef) then
  86. begin
  87. if is_boolean(param2) then
  88. result:=is_boolean(param1)
  89. else if is_char(param2) then
  90. result:=is_char(param1)
  91. else if compare_orddef_by_range(torddef(param1),torddef(param2),constparamsym.value) then
  92. result:=true
  93. else
  94. result:=false;
  95. end
  96. { arraydef is string constant so it's compatible with stringdef }
  97. else if (param1.typ=arraydef) and (param2.typ=stringdef) then
  98. result:=true
  99. { integer ords are compatible with float }
  100. else if (param1.typ=orddef) and is_integer(param1) and (param2.typ=floatdef) then
  101. result:=true
  102. { undefined def is compatible with all types }
  103. else if param2.typ=undefineddef then
  104. result:=true
  105. { sets require stricter checks }
  106. else if is_set(param2) then
  107. result:=equal_defs(param1,param2)
  108. else
  109. result:=param1.typ=param2.typ;
  110. end;
  111. function create_generic_constsym(fromdef:tdef;node:tnode;out prettyname:string):tconstsym;
  112. const
  113. undefinedname = 'undefined';
  114. var
  115. sym : tconstsym;
  116. setdef : tsetdef;
  117. enumsym : tsym;
  118. enumname : string;
  119. sp : pchar;
  120. ps : ^tconstset;
  121. pd : ^bestreal;
  122. i : integer;
  123. begin
  124. if node=nil then
  125. internalerror(2020011401);
  126. case node.nodetype of
  127. ordconstn:
  128. begin
  129. sym:=cconstsym.create_ord(undefinedname,constord,tordconstnode(node).value,fromdef);
  130. prettyname:=tostr(tordconstnode(node).value.svalue);
  131. end;
  132. stringconstn:
  133. begin
  134. getmem(sp,tstringconstnode(node).len+1);
  135. move(tstringconstnode(node).value_str^,sp^,tstringconstnode(node).len+1);
  136. sym:=cconstsym.create_string(undefinedname,conststring,sp,tstringconstnode(node).len,fromdef);
  137. prettyname:=''''+tstringconstnode(node).value_str+'''';
  138. end;
  139. realconstn:
  140. begin
  141. new(pd);
  142. pd^:=trealconstnode(node).value_real;
  143. sym:=cconstsym.create_ptr(undefinedname,constreal,pd,fromdef);
  144. prettyname:=realtostr(trealconstnode(node).value_real);
  145. end;
  146. setconstn:
  147. begin
  148. new(ps);
  149. ps^:=tsetconstnode(node).value_set^;
  150. sym:=cconstsym.create_ptr(undefinedname,constset,ps,fromdef);
  151. setdef:=tsetdef(tsetconstnode(node).resultdef);
  152. prettyname:='[';
  153. for i := setdef.setbase to setdef.setmax do
  154. if i in tsetconstnode(node).value_set^ then
  155. begin
  156. if setdef.elementdef.typ=enumdef then
  157. enumsym:=tenumdef(setdef.elementdef).int2enumsym(i)
  158. else
  159. enumsym:=nil;
  160. if assigned(enumsym) then
  161. enumname:=enumsym.realname
  162. else if setdef.elementdef.typ=orddef then
  163. begin
  164. if torddef(setdef.elementdef).ordtype=uchar then
  165. enumname:=chr(i)
  166. else
  167. enumname:=tostr(i);
  168. end
  169. else
  170. enumname:=tostr(i);
  171. if length(prettyname) > 1 then
  172. prettyname:=prettyname+','+enumname
  173. else
  174. prettyname:=prettyname+enumname;
  175. end;
  176. prettyname:=prettyname+']';
  177. end;
  178. niln:
  179. begin
  180. { only "nil" is available for pointer constants }
  181. sym:=cconstsym.create_ord(undefinedname,constnil,0,fromdef);
  182. prettyname:='nil';
  183. end;
  184. else
  185. internalerror(2019021601);
  186. end;
  187. { the sym needs an owner for later checks so use the typeparam owner }
  188. sym.owner:=fromdef.owner;
  189. include(sym.symoptions,sp_generic_const);
  190. result:=sym;
  191. end;
  192. procedure maybe_add_waiting_unit(tt:tdef);
  193. var
  194. hmodule : tmodule;
  195. begin
  196. if not assigned(tt) or
  197. not (df_generic in tt.defoptions) then
  198. exit;
  199. hmodule:=find_module_from_symtable(tt.owner);
  200. if not assigned(hmodule) then
  201. internalerror(2012092401);
  202. if hmodule=current_module then
  203. exit;
  204. if hmodule.state<>ms_compiled then
  205. begin
  206. {$ifdef DEBUG_UNITWAITING}
  207. Writeln('Unit ', current_module.modulename^,
  208. ' waiting for ', hmodule.modulename^);
  209. {$endif DEBUG_UNITWAITING}
  210. if current_module.waitingforunit.indexof(hmodule)<0 then
  211. current_module.waitingforunit.add(hmodule);
  212. if hmodule.waitingunits.indexof(current_module)<0 then
  213. hmodule.waitingunits.add(current_module);
  214. end;
  215. end;
  216. function check_generic_constraints(genericdef:tstoreddef;paramlist:tfpobjectlist;poslist:tfplist):boolean;
  217. var
  218. i,j,
  219. intfcount : longint;
  220. formaldef,
  221. paradef : tstoreddef;
  222. genparadef : tdef;
  223. objdef,
  224. paraobjdef,
  225. formalobjdef : tobjectdef;
  226. intffound : boolean;
  227. filepos : tfileposinfo;
  228. is_const : boolean;
  229. begin
  230. { check whether the given specialization parameters fit to the eventual
  231. constraints of the generic }
  232. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  233. internalerror(2012101001);
  234. if genericdef.genericparas.count<>paramlist.count then
  235. internalerror(2012101002);
  236. if paramlist.count<>poslist.count then
  237. internalerror(2012120801);
  238. result:=true;
  239. for i:=0 to genericdef.genericparas.count-1 do
  240. begin
  241. filepos:=pfileposinfo(poslist[i])^;
  242. paradef:=tstoreddef(get_generic_param_def(tsym(paramlist[i])));
  243. is_const:=tsym(paramlist[i]).typ=constsym;
  244. genparadef:=genericdef.get_generic_param_def(i);
  245. { validate const params }
  246. if not genericdef.is_generic_param_const(i) and is_const then
  247. begin
  248. MessagePos(filepos,type_e_mismatch);
  249. exit(false);
  250. end
  251. else if genericdef.is_generic_param_const(i) then
  252. begin
  253. { param type mismatch (type <> const) }
  254. if genericdef.is_generic_param_const(i)<>is_const then
  255. begin
  256. MessagePos(filepos,type_e_mismatch);
  257. exit(false);
  258. end;
  259. { type constrained param doesn't match type }
  260. if not compare_generic_params(paradef,genericdef.get_generic_param_def(i),tconstsym(paramlist[i])) then
  261. begin
  262. MessagePos2(filepos,type_e_incompatible_types,FullTypeName(paradef,genparadef),FullTypeName(genparadef,paradef));
  263. exit(false);
  264. end;
  265. end;
  266. { test constraints for non-const params }
  267. if not genericdef.is_generic_param_const(i) then
  268. begin
  269. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  270. if formaldef.typ=undefineddef then
  271. { the parameter is of unspecified type, so no need to check }
  272. continue;
  273. if not (df_genconstraint in formaldef.defoptions) or
  274. not assigned(formaldef.genconstraintdata) then
  275. internalerror(2013021602);
  276. { undefineddef is compatible with anything }
  277. if formaldef.typ=undefineddef then
  278. continue;
  279. if paradef.typ<>formaldef.typ then
  280. begin
  281. case formaldef.typ of
  282. recorddef:
  283. { delphi has own fantasy about record constraint
  284. (almost non-nullable/non-nilable value type) }
  285. if m_delphi in current_settings.modeswitches then
  286. case paradef.typ of
  287. floatdef,enumdef,orddef:
  288. continue;
  289. objectdef:
  290. if tobjectdef(paradef).objecttype=odt_object then
  291. continue
  292. else
  293. MessagePos(filepos,type_e_record_type_expected);
  294. else
  295. MessagePos(filepos,type_e_record_type_expected);
  296. end
  297. else
  298. MessagePos(filepos,type_e_record_type_expected);
  299. objectdef:
  300. case tobjectdef(formaldef).objecttype of
  301. odt_class,
  302. odt_javaclass:
  303. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  304. odt_interfacecom,
  305. odt_interfacecorba,
  306. odt_dispinterface,
  307. odt_interfacejava:
  308. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  309. else
  310. internalerror(2012101003);
  311. end;
  312. errordef:
  313. { ignore }
  314. ;
  315. else
  316. internalerror(2012101004);
  317. end;
  318. result:=false;
  319. end
  320. else
  321. begin
  322. { the paradef types are the same, so do special checks for the
  323. cases in which they are needed }
  324. if formaldef.typ=objectdef then
  325. begin
  326. paraobjdef:=tobjectdef(paradef);
  327. formalobjdef:=tobjectdef(formaldef);
  328. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  329. internalerror(2012101102);
  330. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  331. begin
  332. { this is either a concerete interface or class type (the
  333. latter without specific implemented interfaces) }
  334. case paraobjdef.objecttype of
  335. odt_interfacecom,
  336. odt_interfacecorba,
  337. odt_interfacejava,
  338. odt_dispinterface:
  339. begin
  340. if (oo_is_forward in paraobjdef.objectoptions) and
  341. (paraobjdef.objecttype=formalobjdef.objecttype) and
  342. (df_genconstraint in formalobjdef.defoptions) and
  343. (
  344. (formalobjdef.objecttype=odt_interfacecom) and
  345. (formalobjdef.childof=interface_iunknown)
  346. )
  347. or
  348. (
  349. (formalobjdef.objecttype=odt_interfacecorba) and
  350. (formalobjdef.childof=nil)
  351. ) then
  352. continue;
  353. if not def_is_related(paraobjdef,formalobjdef.childof) then
  354. begin
  355. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  356. result:=false;
  357. end;
  358. end;
  359. odt_class,
  360. odt_javaclass:
  361. begin
  362. objdef:=paraobjdef;
  363. intffound:=false;
  364. while assigned(objdef) do
  365. begin
  366. for j:=0 to objdef.implementedinterfaces.count-1 do
  367. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  368. begin
  369. intffound:=true;
  370. break;
  371. end;
  372. if intffound then
  373. break;
  374. objdef:=objdef.childof;
  375. end;
  376. result:=intffound;
  377. if not result then
  378. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  379. end;
  380. else
  381. begin
  382. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  383. result:=false;
  384. end;
  385. end;
  386. end
  387. else
  388. begin
  389. { this is either a "class" or a concrete instance with
  390. or without implemented interfaces }
  391. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  392. begin
  393. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  394. result:=false;
  395. continue;
  396. end;
  397. { for forward declared classes we allow pure TObject/class declarations }
  398. if (oo_is_forward in paraobjdef.objectoptions) and
  399. (df_genconstraint in formaldef.defoptions) then
  400. begin
  401. if (formalobjdef.childof=class_tobject) and
  402. not formalobjdef.implements_any_interfaces then
  403. continue;
  404. end;
  405. if assigned(formalobjdef.childof) and
  406. not def_is_related(paradef,formalobjdef.childof) then
  407. begin
  408. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  409. result:=false;
  410. end;
  411. intfcount:=0;
  412. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  413. begin
  414. objdef:=paraobjdef;
  415. while assigned(objdef) do
  416. begin
  417. intffound:=assigned(
  418. find_implemented_interface(objdef,
  419. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  420. )
  421. );
  422. if intffound then
  423. break;
  424. objdef:=objdef.childof;
  425. end;
  426. if intffound then
  427. inc(intfcount)
  428. else
  429. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  430. end;
  431. if intfcount<>formalobjdef.implementedinterfaces.count then
  432. result:=false;
  433. end;
  434. end;
  435. end;
  436. end;
  437. end;
  438. end;
  439. function parse_generic_specialization_types_internal(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  440. var
  441. old_block_type : tblock_type;
  442. first : boolean;
  443. typeparam : tnode;
  444. parampos : pfileposinfo;
  445. tmpparampos : tfileposinfo;
  446. namepart : string;
  447. prettynamepart : ansistring;
  448. module : tmodule;
  449. constprettyname : string;
  450. validparam : boolean;
  451. begin
  452. result:=true;
  453. prettyname:='';
  454. prettynamepart:='';
  455. if paramlist=nil then
  456. internalerror(2012061401);
  457. { set the block type to type, so that the parsed type are returned as
  458. ttypenode (e.g. classes are in non type-compatible blocks returned as
  459. tloadvmtaddrnode) }
  460. old_block_type:=block_type;
  461. { if parsedtype is set, then the first type identifer was already parsed
  462. (happens in inline specializations) and thus we only need to parse
  463. the remaining types and do as if the first one was already given }
  464. first:=not assigned(parsedtype);
  465. if assigned(parsedtype) then
  466. begin
  467. paramlist.Add(parsedtype.typesym);
  468. module:=find_module_from_symtable(parsedtype.owner);
  469. if not assigned(module) then
  470. internalerror(2016112801);
  471. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+parsedtype.unique_id_str;
  472. specializename:='$'+namepart;
  473. prettyname:=parsedtype.fullownerhierarchyname(true)+parsedtype.typesym.prettyname;
  474. if assigned(poslist) then
  475. begin
  476. New(parampos);
  477. parampos^:=parsedpos;
  478. poslist.add(parampos);
  479. end;
  480. end
  481. else
  482. specializename:='$';
  483. while not (token in [_GT,_RSHARPBRACKET]) do
  484. begin
  485. { "first" is set to false at the end of the loop! }
  486. if not first then
  487. consume(_COMMA);
  488. block_type:=bt_type;
  489. tmpparampos:=current_filepos;
  490. typeparam:=factor(false,[ef_accept_equal]);
  491. { determine if the typeparam node is a valid type or const }
  492. validparam:=typeparam.nodetype in tgeneric_param_nodes;
  493. if validparam then
  494. begin
  495. if tstoreddef(typeparam.resultdef).is_generic and
  496. (
  497. not parse_generic or
  498. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  499. ) then
  500. Message(parser_e_no_generics_as_params);
  501. if assigned(poslist) then
  502. begin
  503. New(parampos);
  504. parampos^:=tmpparampos;
  505. poslist.add(parampos);
  506. end;
  507. if typeparam.resultdef.typ<>errordef then
  508. begin
  509. if (typeparam.nodetype=typen) and not assigned(typeparam.resultdef.typesym) then
  510. message(type_e_generics_cannot_reference_itself)
  511. else if (typeparam.resultdef.typ<>errordef) then
  512. begin
  513. { all non-type nodes are considered const }
  514. if typeparam.nodetype<>typen then
  515. paramlist.Add(create_generic_constsym(typeparam.resultdef,typeparam,constprettyname))
  516. else
  517. begin
  518. constprettyname:='';
  519. paramlist.Add(typeparam.resultdef.typesym);
  520. end;
  521. module:=find_module_from_symtable(typeparam.resultdef.owner);
  522. if not assigned(module) then
  523. internalerror(2016112802);
  524. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+typeparam.resultdef.unique_id_str;
  525. if constprettyname<>'' then
  526. namepart:=namepart+'$$'+constprettyname;
  527. { we use the full name of the type to uniquely identify it }
  528. if typeparam.nodetype=typen then
  529. begin
  530. if (symtablestack.top.symtabletype=parasymtable) and
  531. (symtablestack.top.defowner.typ=procdef) and
  532. (typeparam.resultdef.owner=symtablestack.top) then
  533. begin
  534. { special handling for specializations inside generic function declarations }
  535. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname(true)+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  536. end
  537. else
  538. begin
  539. prettynamepart:=typeparam.resultdef.fullownerhierarchyname(true);
  540. end;
  541. end;
  542. specializename:=specializename+namepart;
  543. if not first then
  544. prettyname:=prettyname+',';
  545. if constprettyname<>'' then
  546. prettyname:=prettyname+constprettyname
  547. else
  548. prettyname:=prettyname+prettynamepart+typeparam.resultdef.typesym.prettyname;
  549. end;
  550. end
  551. else
  552. begin
  553. result:=false;
  554. end;
  555. end
  556. else
  557. begin
  558. Message(type_e_type_id_expected);
  559. result:=false;
  560. end;
  561. typeparam.free;
  562. first:=false;
  563. end;
  564. block_type:=old_block_type;
  565. end;
  566. function parse_generic_specialization_types(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  567. var
  568. dummypos : tfileposinfo;
  569. begin
  570. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  571. result:=parse_generic_specialization_types_internal(paramlist,poslist,prettyname,specializename,nil,dummypos);
  572. end;
  573. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  574. var
  575. dummypos : tfileposinfo;
  576. begin
  577. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  578. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  579. end;
  580. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;
  581. var
  582. dummypos : tfileposinfo;
  583. {$push}
  584. {$warn 5036 off}
  585. begin
  586. result:=generate_specialization_phase1(context,genericdef,nil,'',dummypos);
  587. end;
  588. {$pop}
  589. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string):tdef;
  590. var
  591. dummypos : tfileposinfo;
  592. {$push}
  593. {$warn 5036 off}
  594. begin
  595. result:=generate_specialization_phase1(context,genericdef,nil,symname,dummypos);
  596. end;
  597. {$pop}
  598. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  599. var
  600. found,
  601. err : boolean;
  602. i,
  603. gencount : longint;
  604. countstr,genname,ugenname : string;
  605. tmpstack : tfpobjectlist;
  606. begin
  607. context:=nil;
  608. result:=nil;
  609. { either symname must be given or genericdef needs to be valid }
  610. if (symname='') and
  611. (not assigned(genericdef) or
  612. (
  613. (genericdef.typ<>procdef) and
  614. (
  615. not assigned(genericdef.typesym) or
  616. (genericdef.typesym.typ<>typesym)
  617. )
  618. ) or
  619. (
  620. (genericdef.typ=procdef) and
  621. (
  622. not assigned(tprocdef(genericdef).procsym) or
  623. (tprocdef(genericdef).procsym.typ<>procsym)
  624. )
  625. )
  626. ) then
  627. begin
  628. internalerror(2019112401);
  629. end;
  630. if not assigned(parsedtype) and not try_to_consume(_LT) then
  631. begin
  632. consume(_LSHARPBRACKET);
  633. { handle "<>" }
  634. if (token=_GT) or (token=_RSHARPBRACKET) then
  635. begin
  636. Message(type_e_type_id_expected);
  637. if not try_to_consume(_GT) then
  638. try_to_consume(_RSHARPBRACKET);
  639. result:=generrordef;
  640. exit;
  641. end;
  642. end;
  643. context:=tspecializationcontext.create;
  644. { Parse type parameters }
  645. err:=not parse_generic_specialization_types_internal(context.paramlist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  646. if err then
  647. begin
  648. if not try_to_consume(_GT) then
  649. try_to_consume(_RSHARPBRACKET);
  650. context.free;
  651. context:=nil;
  652. result:=generrordef;
  653. exit;
  654. end;
  655. { use the name of the symbol as procvars return a user friendly version
  656. of the name }
  657. if symname='' then
  658. begin
  659. if genericdef.typ=procdef then
  660. genname:=tprocdef(genericdef).procsym.realname
  661. else
  662. genname:=ttypesym(genericdef.typesym).realname;
  663. end
  664. else
  665. genname:=symname;
  666. { in case of non-Delphi mode the type name could already be a generic
  667. def (but maybe the wrong one) }
  668. if assigned(genericdef) and
  669. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  670. begin
  671. { remove the type count suffix from the generic's name }
  672. for i:=Length(genname) downto 1 do
  673. if genname[i]='$' then
  674. begin
  675. genname:=copy(genname,1,i-1);
  676. break;
  677. end;
  678. { in case of a specialization we've only reached the specialization
  679. checksum yet }
  680. if df_specialization in genericdef.defoptions then
  681. for i:=length(genname) downto 1 do
  682. if genname[i]='$' then
  683. begin
  684. genname:=copy(genname,1,i-1);
  685. break;
  686. end;
  687. end
  688. else
  689. begin
  690. split_generic_name(genname,ugenname,gencount);
  691. if genname<>ugenname then
  692. genname:=ugenname;
  693. end;
  694. { search a generic with the given count of params }
  695. countstr:='';
  696. str(context.paramlist.Count,countstr);
  697. genname:=genname+'$'+countstr;
  698. ugenname:=upper(genname);
  699. context.genname:=genname;
  700. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  701. begin
  702. if genericdef.owner.symtabletype = objectsymtable then
  703. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,context.sym,context.symtable,[])
  704. else
  705. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,context.sym,context.symtable);
  706. if not found then
  707. found:=searchsym(ugenname,context.sym,context.symtable);
  708. end
  709. else
  710. found:=searchsym(ugenname,context.sym,context.symtable);
  711. if found and (context.sym.typ=absolutevarsym) and
  712. (vo_is_funcret in tabstractvarsym(context.sym).varoptions) then
  713. begin
  714. { we found the function result alias of a generic function; go up the
  715. symbol stack *before* this alias was inserted, so that we can
  716. (hopefully) find the correct generic symbol }
  717. tmpstack:=tfpobjectlist.create(false);
  718. while assigned(symtablestack.top) do
  719. begin
  720. tmpstack.Add(symtablestack.top);
  721. symtablestack.pop(symtablestack.top);
  722. if tmpstack.Last=context.symtable then
  723. break;
  724. end;
  725. if not assigned(symtablestack.top) then
  726. internalerror(2019123001);
  727. found:=searchsym(ugenname,context.sym,context.symtable);
  728. for i:=tmpstack.count-1 downto 0 do
  729. symtablestack.push(tsymtable(tmpstack[i]));
  730. tmpstack.free;
  731. end;
  732. if not found or not (context.sym.typ in [typesym,procsym]) then
  733. begin
  734. identifier_not_found(genname);
  735. if not try_to_consume(_GT) then
  736. try_to_consume(_RSHARPBRACKET);
  737. context.free;
  738. context:=nil;
  739. result:=generrordef;
  740. exit;
  741. end;
  742. { we've found the correct def }
  743. if context.sym.typ=typesym then
  744. result:=tstoreddef(ttypesym(context.sym).typedef)
  745. else
  746. begin
  747. if tprocsym(context.sym).procdeflist.count=0 then
  748. internalerror(2015061203);
  749. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  750. end;
  751. if not try_to_consume(_GT) then
  752. consume(_RSHARPBRACKET);
  753. end;
  754. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  755. procedure unset_forwarddef(def: tdef);
  756. var
  757. st : TSymtable;
  758. i : longint;
  759. begin
  760. case def.typ of
  761. procdef:
  762. tprocdef(def).forwarddef:=false;
  763. objectdef,
  764. recorddef:
  765. begin
  766. st:=def.getsymtable(gs_record);
  767. for i:=0 to st.deflist.count-1 do
  768. unset_forwarddef(tdef(st.deflist[i]));
  769. end;
  770. else
  771. ;
  772. end;
  773. end;
  774. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  775. var
  776. i : longint;
  777. begin
  778. gendef:=nil;
  779. psym:=nil;
  780. case sym.typ of
  781. typesym:
  782. begin
  783. gendef:=ttypesym(sym).typedef
  784. end;
  785. procsym:
  786. begin
  787. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  788. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  789. begin
  790. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  791. break;
  792. end;
  793. psym:=sym;
  794. end
  795. else
  796. internalerror(200710171);
  797. end;
  798. end;
  799. var
  800. finalspecializename,
  801. ufinalspecializename : tidstring;
  802. prettyname : ansistring;
  803. generictypelist : tfphashobjectlist;
  804. srsymtable,
  805. specializest : tsymtable;
  806. hashedid : thashedidstring;
  807. tempst : tglobalsymtable;
  808. psym,
  809. srsym : tsym;
  810. paramdef1,
  811. paramdef2,
  812. def : tdef;
  813. old_block_type : tblock_type;
  814. state : tspecializationstate;
  815. old_current_structdef : tabstractrecorddef;
  816. old_current_specializedef,
  817. old_current_genericdef : tstoreddef;
  818. old_current_procinfo : tprocinfo;
  819. old_module_procinfo : tobject;
  820. hmodule : tmodule;
  821. oldcurrent_filepos : tfileposinfo;
  822. recordbuf : tdynamicarray;
  823. hadtypetoken : boolean;
  824. i,
  825. replaydepth : longint;
  826. item : tobject;
  827. allequal,
  828. hintsprocessed : boolean;
  829. pd : tprocdef;
  830. pdflags : tpdflags;
  831. begin
  832. if not assigned(context) then
  833. internalerror(2015052203);
  834. result:=nil;
  835. pd:=nil;
  836. if not check_generic_constraints(genericdef,context.paramlist,context.poslist) then
  837. begin
  838. { the parameters didn't fit the constraints, so don't continue with the
  839. specialization }
  840. result:=generrordef;
  841. exit;
  842. end;
  843. { build the new type's name }
  844. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  845. ufinalspecializename:=upper(finalspecializename);
  846. if genericdef.typ=procdef then
  847. prettyname:=tprocdef(genericdef).procsym.prettyname
  848. else
  849. prettyname:=genericdef.typesym.prettyname;
  850. prettyname:=prettyname+'<'+context.prettyname+'>';
  851. generictypelist:=tfphashobjectlist.create(false);
  852. { build the list containing the types for the generic params }
  853. if not assigned(genericdef.genericparas) then
  854. internalerror(2013092601);
  855. if context.paramlist.count<>genericdef.genericparas.count then
  856. internalerror(2013092603);
  857. for i:=0 to genericdef.genericparas.Count-1 do
  858. begin
  859. srsym:=tsym(genericdef.genericparas[i]);
  860. if not (sp_generic_para in srsym.symoptions) then
  861. internalerror(2013092602);
  862. generictypelist.add(srsym.realname,context.paramlist[i]);
  863. end;
  864. { Special case if we are referencing the current defined object }
  865. if assigned(current_structdef) and
  866. (current_structdef.objname^=ufinalspecializename) then
  867. result:=current_structdef;
  868. { Can we reuse an already specialized type? }
  869. { for this first check whether we are currently specializing a nested
  870. type of the current (main) specialization (this is necessary, because
  871. during that time the symbol of the main specialization will still
  872. contain a reference to an errordef) }
  873. if not assigned(result) and assigned(current_specializedef) then
  874. begin
  875. def:=current_specializedef;
  876. repeat
  877. if def.typ in [objectdef,recorddef] then
  878. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  879. result:=def;
  880. break;
  881. end;
  882. if assigned(def.owner) then
  883. def:=tstoreddef(def.owner.defowner)
  884. else
  885. { this can happen when specializing a generic function }
  886. def:=nil;
  887. until not assigned(def) or not (df_specialization in def.defoptions);
  888. end;
  889. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  890. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  891. parsing anyway), so for now we treat those still as generic defs without doing a partial
  892. specialization }
  893. if not assigned(result) then
  894. begin
  895. def:=current_genericdef;
  896. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  897. begin
  898. if (df_generic in def.defoptions) and (def=genericdef) then
  899. begin
  900. result:=def;
  901. break;
  902. end;
  903. { the following happens when a routine with its parent struct
  904. as parameter is specialized as a parameter or result of a
  905. generic function }
  906. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  907. begin
  908. if tstoreddef(def).genericparas.count=generictypelist.count then
  909. begin
  910. allequal:=true;
  911. for i:=0 to generictypelist.count-1 do
  912. begin
  913. if tsym(generictypelist[i]).typ<>tsym(tstoreddef(def).genericparas[i]).typ then
  914. begin
  915. allequal:=false;
  916. break;
  917. end;
  918. if tsym(generictypelist[i]).typ=constsym then
  919. paramdef1:=tconstsym(generictypelist[i]).constdef
  920. else
  921. paramdef1:=ttypesym(generictypelist[i]).typedef;
  922. if tsym(tstoreddef(def).genericparas[i]).typ=constsym then
  923. paramdef2:=tconstsym(tstoreddef(def).genericparas[i]).constdef
  924. else
  925. paramdef2:=ttypesym(tstoreddef(def).genericparas[i]).typedef;
  926. if not equal_defs(paramdef2,paramdef2) then
  927. begin
  928. allequal:=false;
  929. break;
  930. end;
  931. if (tsym(generictypelist[i]).typ=constsym) and
  932. (
  933. (tconstsym(generictypelist[i]).consttyp<>tconstsym(tstoreddef(def).genericparas[i]).consttyp) or
  934. not same_constvalue(tconstsym(generictypelist[i]).consttyp,tconstsym(generictypelist[i]).value,tconstsym(tstoreddef(def).genericparas[i]).value)
  935. ) then
  936. begin
  937. allequal:=false;
  938. break;
  939. end;
  940. end;
  941. if allequal then
  942. begin
  943. result:=def;
  944. break;
  945. end;
  946. end;
  947. end;
  948. def:=tstoreddef(def.owner.defowner);
  949. end;
  950. end;
  951. { decide in which symtable to put the specialization }
  952. if parse_generic and not assigned(result) then
  953. begin
  954. srsymtable:=symtablestack.top;
  955. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  956. { if we are currently specializing a routine we need to specialize into
  957. the routine's local- or parasymtable so that they are correctly
  958. registered should the specialization be finalized }
  959. specializest:=srsymtable
  960. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  961. { if we are parsing the definition of a method we specialize into
  962. the local symtable of it }
  963. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  964. else
  965. begin
  966. if not assigned(current_genericdef) then
  967. internalerror(2014050901);
  968. { we specialize the partial specialization into the symtable of the currently parsed
  969. generic }
  970. case current_genericdef.typ of
  971. procvardef:
  972. specializest:=current_genericdef.getsymtable(gs_para);
  973. procdef:
  974. specializest:=current_genericdef.getsymtable(gs_local);
  975. objectdef,
  976. recorddef:
  977. specializest:=current_genericdef.getsymtable(gs_record);
  978. arraydef:
  979. specializest:=tarraydef(current_genericdef).symtable;
  980. else
  981. internalerror(2014050902);
  982. end;
  983. end;
  984. end
  985. else
  986. if current_module.is_unit and current_module.in_interface then
  987. specializest:=current_module.globalsymtable
  988. else
  989. specializest:=current_module.localsymtable;
  990. if not assigned(specializest) then
  991. internalerror(2014050910);
  992. { now check whether there is a specialization somewhere else }
  993. psym:=nil;
  994. if not assigned(result) then
  995. begin
  996. hashedid.id:=ufinalspecializename;
  997. if specializest.symtabletype=objectsymtable then
  998. begin
  999. { search also in parent classes }
  1000. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  1001. internalerror(2016112901);
  1002. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  1003. srsym:=nil;
  1004. end
  1005. else
  1006. srsym:=tsym(specializest.findwithhash(hashedid));
  1007. if assigned(srsym) then
  1008. begin
  1009. retrieve_genericdef_or_procsym(srsym,result,psym);
  1010. end
  1011. else
  1012. { the generic could have been specialized in the globalsymtable
  1013. already, so search there as well }
  1014. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  1015. begin
  1016. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  1017. if assigned(srsym) then
  1018. begin
  1019. retrieve_genericdef_or_procsym(srsym,result,psym);
  1020. end;
  1021. end;
  1022. end;
  1023. if not assigned(result) then
  1024. begin
  1025. specialization_init(genericdef,state);
  1026. { push a temporary global symtable so that the specialization is
  1027. added to the correct symtable; this symtable does not contain
  1028. any other symbols, so that the type resolution can not be
  1029. influenced by symbols in the current unit }
  1030. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  1031. symtablestack.push(tempst);
  1032. { Reparse the original type definition }
  1033. begin
  1034. old_current_specializedef:=nil;
  1035. old_current_genericdef:=nil;
  1036. old_current_structdef:=nil;
  1037. old_current_procinfo:=current_procinfo;
  1038. old_module_procinfo:=current_module.procinfo;
  1039. current_procinfo:=nil;
  1040. current_module.procinfo:=nil;
  1041. if parse_class_parent then
  1042. begin
  1043. old_current_structdef:=current_structdef;
  1044. old_current_genericdef:=current_genericdef;
  1045. old_current_specializedef:=current_specializedef;
  1046. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  1047. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  1048. else
  1049. current_structdef:=nil;
  1050. current_genericdef:=nil;
  1051. current_specializedef:=nil;
  1052. end;
  1053. maybe_add_waiting_unit(genericdef);
  1054. { First a new sym so we can reuse this specialization and
  1055. references to this specialization can be handled }
  1056. if genericdef.typ=procdef then
  1057. if assigned(psym) then
  1058. srsym:=psym
  1059. else
  1060. srsym:=cprocsym.create(finalspecializename)
  1061. else
  1062. srsym:=ctypesym.create(finalspecializename,generrordef);
  1063. { insert the symbol only if we don't know already that we have
  1064. a procsym to add it to }
  1065. if not assigned(psym) then
  1066. specializest.insert(srsym);
  1067. { specializations are declarations as such it is the wisest to
  1068. declare set the blocktype to "type"; otherwise we'll
  1069. experience unexpected side effects like the addition of
  1070. classrefdefs if we have a generic that's derived from another
  1071. generic }
  1072. old_block_type:=block_type;
  1073. block_type:=bt_type;
  1074. if (
  1075. (genericdef.typ=procdef) and
  1076. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  1077. ) or (
  1078. (genericdef.typ<>procdef) and
  1079. not assigned(genericdef.generictokenbuf)
  1080. ) then
  1081. internalerror(200511171);
  1082. hmodule:=find_module_from_symtable(genericdef.owner);
  1083. if hmodule=nil then
  1084. internalerror(2012051202);
  1085. oldcurrent_filepos:=current_filepos;
  1086. { use the index the module got from the current compilation process }
  1087. current_filepos.moduleindex:=hmodule.unit_index;
  1088. current_tokenpos:=current_filepos;
  1089. if parse_generic then
  1090. begin
  1091. recordbuf:=current_scanner.recordtokenbuf;
  1092. current_scanner.recordtokenbuf:=nil;
  1093. end
  1094. else
  1095. recordbuf:=nil;
  1096. replaydepth:=current_scanner.replay_stack_depth;
  1097. if genericdef.typ=procdef then
  1098. begin
  1099. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf,hmodule.change_endian);
  1100. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  1101. if assigned(pd) then
  1102. begin
  1103. if assigned(psym) then
  1104. pd.procsym:=psym
  1105. else
  1106. pd.procsym:=srsym;
  1107. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions,tprocdef(genericdef).struct);
  1108. end;
  1109. result:=pd;
  1110. end
  1111. else
  1112. begin
  1113. current_scanner.startreplaytokens(genericdef.generictokenbuf,hmodule.change_endian);
  1114. hadtypetoken:=false;
  1115. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  1116. ttypesym(srsym).typedef:=result;
  1117. result.typesym:=srsym;
  1118. if _prettyname<>'' then
  1119. ttypesym(result.typesym).fprettyname:=_prettyname
  1120. else
  1121. ttypesym(result.typesym).fprettyname:=prettyname;
  1122. end;
  1123. current_filepos:=oldcurrent_filepos;
  1124. { Note regarding hint directives:
  1125. There is no need to remove the flags for them from the
  1126. specialized generic symbol, because hint directives that
  1127. follow the specialization are handled by the code in
  1128. pdecl.types_dec and added to the type symbol.
  1129. E.g.: TFoo = TBar<Blubb> deprecated;
  1130. Here the symbol TBar$1$Blubb will contain the
  1131. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1132. case result.typ of
  1133. { Build VMT indexes for classes and read hint directives }
  1134. objectdef:
  1135. begin
  1136. if replaydepth>current_scanner.replay_stack_depth then
  1137. begin
  1138. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1139. if replaydepth>current_scanner.replay_stack_depth then
  1140. consume(_SEMICOLON);
  1141. end;
  1142. build_vmt(tobjectdef(result));
  1143. end;
  1144. { handle params, calling convention, etc }
  1145. procvardef:
  1146. begin
  1147. hintsprocessed:=false;
  1148. if replaydepth>current_scanner.replay_stack_depth then
  1149. begin
  1150. if not check_proc_directive(true) then
  1151. begin
  1152. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1153. if replaydepth>current_scanner.replay_stack_depth then
  1154. consume(_SEMICOLON);
  1155. end
  1156. else
  1157. hintsprocessed:=true;
  1158. end;
  1159. if replaydepth>current_scanner.replay_stack_depth then
  1160. parse_var_proc_directives(ttypesym(srsym));
  1161. handle_calling_convention(tprocvardef(result),hcc_default_actions_intf);
  1162. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  1163. begin
  1164. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1165. if replaydepth>current_scanner.replay_stack_depth then
  1166. consume(_SEMICOLON);
  1167. end;
  1168. end;
  1169. procdef:
  1170. begin
  1171. pdflags:=[];
  1172. if genericdef.owner.symtabletype=objectsymtable then
  1173. include(pdflags,pd_object)
  1174. else if genericdef.owner.symtabletype=recordsymtable then
  1175. include(pdflags,pd_record);
  1176. parse_proc_directives(pd,pdflags);
  1177. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1178. consume(_SEMICOLON);
  1179. if parse_generic then
  1180. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1181. else
  1182. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1183. pdflags:=pdflags+[pd_body,pd_implemen];
  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. Message1(type_e_generic_const_type_not_allowed,def.fulltypename);
  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.