pgenutil.pas 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  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. { node }
  29. node,
  30. { symtable }
  31. symtype,symdef,symbase;
  32. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string;parsedtype:tdef;const symname:string;parsedpos:tfileposinfo);inline;
  33. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string);inline;
  34. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean):tdef;inline;
  35. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;const symname:string;symtable:tsymtable):tdef;inline;
  36. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;parsedtype:tdef;const symname:string;symtable:tsymtable;parsedpos:tfileposinfo):tdef;
  37. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;const _prettyname:ansistring):tdef;
  38. function check_generic_constraints(genericdef:tstoreddef;paramlist:tfpobjectlist;poslist:tfplist):boolean;
  39. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  40. function parse_generic_specialization_types(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  41. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist;isfwd:boolean);
  42. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  43. function generate_generic_name(const name:tidstring;const specializename:ansistring;const owner_hierarchy:ansistring):tidstring;
  44. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  45. procedure add_generic_dummysym(sym:tsym);
  46. function resolve_generic_dummysym(const name:tidstring):tsym;
  47. function could_be_generic(const name:tidstring):boolean;inline;
  48. function try_implicit_specialization(sym:tsym;para:tnode;pdoverloadlist:tfpobjectlist;var unnamed_syms:tfplist;var first_procsym:tsym;var hasoverload:boolean):boolean;
  49. function finalize_specialization(var pd:tprocdef;spezcontext:tspecializationcontext):boolean;
  50. procedure generate_specialization_procs;
  51. procedure generate_specializations_for_forwarddef(def:tdef);
  52. procedure maybe_add_pending_specialization(def:tdef;unnamed_syms:tfplist);
  53. function determine_generic_def(const name:tidstring):tstoreddef;
  54. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  55. procedure specialization_done(var state:tspecializationstate);
  56. implementation
  57. uses
  58. { common }
  59. cutils,fpchash,
  60. { global }
  61. globals,tokens,verbose,finput,constexp,
  62. { symtable }
  63. symconst,symsym,symtable,defcmp,defutil,procinfo,
  64. { modules }
  65. fmodule,
  66. { node }
  67. nobj,ncon,ncal,
  68. { parser }
  69. scanner,
  70. pbase,pexpr,pdecsub,ptype,psub,pparautl,pdecl,procdefutil;
  71. type
  72. tdeftypeset = set of tdeftyp;
  73. const
  74. tgeneric_param_const_types : tdeftypeset = [orddef,stringdef,floatdef,setdef,pointerdef,enumdef];
  75. tgeneric_param_nodes : tnodetypeset = [typen,ordconstn,stringconstn,realconstn,setconstn,niln];
  76. procedure make_prettystring(paramtype:tdef;first:boolean;constprettyname:ansistring;var prettyname,specializename:ansistring);
  77. var
  78. namepart : string;
  79. prettynamepart : ansistring;
  80. module : tmodule;
  81. begin
  82. if assigned(paramtype.owner) then
  83. module:=find_module_from_symtable(paramtype.owner)
  84. else
  85. module:=current_module;
  86. if not assigned(module) then
  87. internalerror(2016112802);
  88. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+paramtype.unique_id_str;
  89. if constprettyname<>'' then
  90. namepart:=namepart+'$$'+constprettyname;
  91. { we use the full name of the type to uniquely identify it }
  92. if (symtablestack.top.symtabletype=parasymtable) and
  93. (symtablestack.top.defowner.typ=procdef) and
  94. (paramtype.owner=symtablestack.top) then
  95. begin
  96. { special handling for specializations inside generic function declarations }
  97. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname(true)+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  98. end
  99. else
  100. begin
  101. prettynamepart:=paramtype.fullownerhierarchyname(true);
  102. end;
  103. specializename:=specializename+namepart;
  104. if not first then
  105. prettyname:=prettyname+',';
  106. if constprettyname<>'' then
  107. prettyname:=prettyname+constprettyname
  108. else
  109. prettyname:=prettyname+prettynamepart+paramtype.typesym.prettyname;
  110. end;
  111. function get_generic_param_def(sym:tsym):tdef;
  112. begin
  113. if sym.typ=constsym then
  114. result:=tconstsym(sym).constdef
  115. else
  116. result:=ttypesym(sym).typedef;
  117. end;
  118. function compare_orddef_by_range(param1,param2:torddef;value:tconstvalue):boolean;
  119. begin
  120. if (value.valueord<param2.low) or (value.valueord>param2.high) then
  121. result:=false
  122. else
  123. result:=true;
  124. end;
  125. function compare_generic_params(param1,param2:tdef;constparamsym:tconstsym):boolean;
  126. begin
  127. if (param1.typ=orddef) and (param2.typ=orddef) then
  128. begin
  129. if is_boolean(param2) then
  130. result:=is_boolean(param1)
  131. else if is_char(param2) then
  132. result:=is_char(param1)
  133. else if compare_orddef_by_range(torddef(param1),torddef(param2),constparamsym.value) then
  134. result:=true
  135. else
  136. result:=false;
  137. end
  138. { arraydef is string constant so it's compatible with stringdef }
  139. else if (param1.typ=arraydef) and (param2.typ=stringdef) then
  140. result:=true
  141. { integer ords are compatible with float }
  142. else if (param1.typ=orddef) and is_integer(param1) and (param2.typ=floatdef) then
  143. result:=true
  144. { chars are compatible with stringdef }
  145. else if (param1.typ=orddef) and is_char(param1) and (param2.typ=stringdef) then
  146. result:=true
  147. { undefined def is compatible with all types }
  148. else if param2.typ=undefineddef then
  149. result:=true
  150. { sets require stricter checks }
  151. else if is_set(param2) then
  152. result:=equal_defs(param1,param2)
  153. else
  154. result:=param1.typ=param2.typ;
  155. end;
  156. function create_generic_constsym(fromdef:tdef;node:tnode;out prettyname:string):tconstsym;
  157. const
  158. undefinedname = 'undefined';
  159. var
  160. sym : tconstsym;
  161. setdef : tsetdef;
  162. enumsym : tsym;
  163. enumname : string;
  164. sp : pchar;
  165. ps : ^tconstset;
  166. pd : ^bestreal;
  167. i : integer;
  168. begin
  169. if node=nil then
  170. internalerror(2020011401);
  171. case node.nodetype of
  172. ordconstn:
  173. begin
  174. sym:=cconstsym.create_ord(undefinedname,constord,tordconstnode(node).value,fromdef);
  175. prettyname:=tostr(tordconstnode(node).value.svalue);
  176. end;
  177. stringconstn:
  178. begin
  179. getmem(sp,tstringconstnode(node).len+1);
  180. move(tstringconstnode(node).value_str^,sp^,tstringconstnode(node).len+1);
  181. sym:=cconstsym.create_string(undefinedname,conststring,sp,tstringconstnode(node).len,fromdef);
  182. prettyname:=''''+tstringconstnode(node).value_str+'''';
  183. end;
  184. realconstn:
  185. begin
  186. new(pd);
  187. pd^:=trealconstnode(node).value_real;
  188. sym:=cconstsym.create_ptr(undefinedname,constreal,pd,fromdef);
  189. prettyname:=realtostr(trealconstnode(node).value_real);
  190. end;
  191. setconstn:
  192. begin
  193. new(ps);
  194. ps^:=tsetconstnode(node).value_set^;
  195. sym:=cconstsym.create_ptr(undefinedname,constset,ps,fromdef);
  196. setdef:=tsetdef(tsetconstnode(node).resultdef);
  197. prettyname:='[';
  198. for i := setdef.setbase to setdef.setmax do
  199. if i in tsetconstnode(node).value_set^ then
  200. begin
  201. if setdef.elementdef.typ=enumdef then
  202. enumsym:=tenumdef(setdef.elementdef).int2enumsym(i)
  203. else
  204. enumsym:=nil;
  205. if assigned(enumsym) then
  206. enumname:=enumsym.realname
  207. else if setdef.elementdef.typ=orddef then
  208. begin
  209. if torddef(setdef.elementdef).ordtype=uchar then
  210. enumname:=chr(i)
  211. else
  212. enumname:=tostr(i);
  213. end
  214. else
  215. enumname:=tostr(i);
  216. if length(prettyname) > 1 then
  217. prettyname:=prettyname+','+enumname
  218. else
  219. prettyname:=prettyname+enumname;
  220. end;
  221. prettyname:=prettyname+']';
  222. end;
  223. niln:
  224. begin
  225. { only "nil" is available for pointer constants }
  226. sym:=cconstsym.create_ord(undefinedname,constnil,0,fromdef);
  227. prettyname:='nil';
  228. end;
  229. else
  230. internalerror(2019021601);
  231. end;
  232. { the sym needs an owner for later checks so use the typeparam owner }
  233. sym.owner:=fromdef.owner;
  234. include(sym.symoptions,sp_generic_const);
  235. result:=sym;
  236. end;
  237. procedure maybe_add_waiting_unit(tt:tdef);
  238. var
  239. hmodule : tmodule;
  240. begin
  241. if not assigned(tt) or
  242. not (df_generic in tt.defoptions) then
  243. exit;
  244. hmodule:=find_module_from_symtable(tt.owner);
  245. if not assigned(hmodule) then
  246. internalerror(2012092401);
  247. if hmodule=current_module then
  248. exit;
  249. if hmodule.state<>ms_compiled then
  250. begin
  251. {$ifdef DEBUG_UNITWAITING}
  252. Writeln('Unit ', current_module.modulename^,
  253. ' waiting for ', hmodule.modulename^);
  254. {$endif DEBUG_UNITWAITING}
  255. if current_module.waitingforunit.indexof(hmodule)<0 then
  256. current_module.waitingforunit.add(hmodule);
  257. if hmodule.waitingunits.indexof(current_module)<0 then
  258. hmodule.waitingunits.add(current_module);
  259. end;
  260. end;
  261. procedure add_forward_generic_def(def:tdef;context:tspecializationcontext);
  262. var
  263. list : tfpobjectlist;
  264. fwdcontext : tspecializationcontext;
  265. begin
  266. if not is_implicit_pointer_object_type(def) then
  267. internalerror(2020070301);
  268. if not (oo_is_forward in tobjectdef(def).objectoptions) then
  269. internalerror(2020070302);
  270. if not assigned(tobjectdef(def).genericdef) then
  271. internalerror(2020070303);
  272. list:=tfpobjectlist(current_module.forwardgenericdefs.find(tobjectdef(def).genericdef.fulltypename));
  273. if not assigned(list) then
  274. begin
  275. list:=tfpobjectlist.create(true);
  276. current_module.forwardgenericdefs.add(tobjectdef(def).genericdef.fulltypename,list);
  277. end;
  278. fwdcontext:=context.getcopy;
  279. fwdcontext.forwarddef:=def;
  280. list.add(fwdcontext);
  281. end;
  282. function check_generic_constraints(genericdef:tstoreddef;paramlist:tfpobjectlist;poslist:tfplist):boolean;
  283. var
  284. i,j,
  285. intfcount : longint;
  286. formaldef,
  287. paradef : tstoreddef;
  288. genparadef : tdef;
  289. objdef,
  290. paraobjdef,
  291. formalobjdef : tobjectdef;
  292. intffound : boolean;
  293. filepos : tfileposinfo;
  294. is_const : boolean;
  295. begin
  296. { check whether the given specialization parameters fit to the eventual
  297. constraints of the generic }
  298. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  299. internalerror(2012101001);
  300. if genericdef.genericparas.count<>paramlist.count then
  301. internalerror(2012101002);
  302. if paramlist.count<>poslist.count then
  303. internalerror(2012120801);
  304. result:=true;
  305. for i:=0 to genericdef.genericparas.count-1 do
  306. begin
  307. filepos:=pfileposinfo(poslist[i])^;
  308. paradef:=tstoreddef(get_generic_param_def(tsym(paramlist[i])));
  309. is_const:=tsym(paramlist[i]).typ=constsym;
  310. genparadef:=genericdef.get_generic_param_def(i);
  311. { validate const params }
  312. if not genericdef.is_generic_param_const(i) and is_const then
  313. begin
  314. MessagePos(filepos,type_e_mismatch);
  315. exit(false);
  316. end
  317. else if genericdef.is_generic_param_const(i) then
  318. begin
  319. { param type mismatch (type <> const) }
  320. if genericdef.is_generic_param_const(i)<>is_const then
  321. begin
  322. MessagePos(filepos,type_e_mismatch);
  323. exit(false);
  324. end;
  325. { type constrained param doesn't match type }
  326. if not compare_generic_params(paradef,genericdef.get_generic_param_def(i),tconstsym(paramlist[i])) then
  327. begin
  328. MessagePos2(filepos,type_e_incompatible_types,FullTypeName(paradef,genparadef),FullTypeName(genparadef,paradef));
  329. exit(false);
  330. end;
  331. end;
  332. { test constraints for non-const params }
  333. if not genericdef.is_generic_param_const(i) then
  334. begin
  335. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  336. if formaldef.typ=undefineddef then
  337. { the parameter is of unspecified type, so no need to check }
  338. continue;
  339. if not (df_genconstraint in formaldef.defoptions) or
  340. not assigned(formaldef.genconstraintdata) then
  341. internalerror(2013021602);
  342. { undefineddef is compatible with anything }
  343. if formaldef.typ=undefineddef then
  344. continue;
  345. if paradef.typ<>formaldef.typ then
  346. begin
  347. case formaldef.typ of
  348. recorddef:
  349. { delphi has own fantasy about record constraint
  350. (almost non-nullable/non-nilable value type) }
  351. if m_delphi in current_settings.modeswitches then
  352. case paradef.typ of
  353. floatdef,enumdef,orddef:
  354. continue;
  355. objectdef:
  356. if tobjectdef(paradef).objecttype=odt_object then
  357. continue
  358. else
  359. MessagePos(filepos,type_e_record_type_expected);
  360. else
  361. MessagePos(filepos,type_e_record_type_expected);
  362. end
  363. else
  364. MessagePos(filepos,type_e_record_type_expected);
  365. objectdef:
  366. case tobjectdef(formaldef).objecttype of
  367. odt_class,
  368. odt_javaclass:
  369. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  370. odt_interfacecom,
  371. odt_interfacecorba,
  372. odt_dispinterface,
  373. odt_interfacejava:
  374. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  375. else
  376. internalerror(2012101003);
  377. end;
  378. errordef:
  379. { ignore }
  380. ;
  381. else
  382. internalerror(2012101004);
  383. end;
  384. result:=false;
  385. end
  386. else
  387. begin
  388. { the paradef types are the same, so do special checks for the
  389. cases in which they are needed }
  390. if formaldef.typ=objectdef then
  391. begin
  392. paraobjdef:=tobjectdef(paradef);
  393. formalobjdef:=tobjectdef(formaldef);
  394. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  395. internalerror(2012101102);
  396. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  397. begin
  398. { this is either a concerete interface or class type (the
  399. latter without specific implemented interfaces) }
  400. case paraobjdef.objecttype of
  401. odt_interfacecom,
  402. odt_interfacecorba,
  403. odt_interfacejava,
  404. odt_dispinterface:
  405. begin
  406. if (oo_is_forward in paraobjdef.objectoptions) and
  407. (paraobjdef.objecttype=formalobjdef.objecttype) and
  408. (df_genconstraint in formalobjdef.defoptions) and
  409. (
  410. (formalobjdef.objecttype=odt_interfacecom) and
  411. (formalobjdef.childof=interface_iunknown)
  412. )
  413. or
  414. (
  415. (formalobjdef.objecttype=odt_interfacecorba) and
  416. (formalobjdef.childof=nil)
  417. ) then
  418. continue;
  419. if not def_is_related(paraobjdef,formalobjdef.childof) then
  420. begin
  421. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  422. result:=false;
  423. end;
  424. end;
  425. odt_class,
  426. odt_javaclass:
  427. begin
  428. objdef:=paraobjdef;
  429. intffound:=false;
  430. while assigned(objdef) do
  431. begin
  432. for j:=0 to objdef.implementedinterfaces.count-1 do
  433. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  434. begin
  435. intffound:=true;
  436. break;
  437. end;
  438. if intffound then
  439. break;
  440. objdef:=objdef.childof;
  441. end;
  442. result:=intffound;
  443. if not result then
  444. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  445. end;
  446. else
  447. begin
  448. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  449. result:=false;
  450. end;
  451. end;
  452. end
  453. else
  454. begin
  455. { this is either a "class" or a concrete instance with
  456. or without implemented interfaces }
  457. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  458. begin
  459. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  460. result:=false;
  461. continue;
  462. end;
  463. { for forward declared classes we allow pure TObject/class declarations }
  464. if (oo_is_forward in paraobjdef.objectoptions) and
  465. (df_genconstraint in formaldef.defoptions) then
  466. begin
  467. if (formalobjdef.childof=class_tobject) and
  468. not formalobjdef.implements_any_interfaces then
  469. continue;
  470. end;
  471. if assigned(formalobjdef.childof) and
  472. not def_is_related(paradef,formalobjdef.childof) then
  473. begin
  474. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  475. result:=false;
  476. end;
  477. intfcount:=0;
  478. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  479. begin
  480. objdef:=paraobjdef;
  481. intffound:=false;
  482. while assigned(objdef) do
  483. begin
  484. intffound:=assigned(
  485. find_implemented_interface(objdef,
  486. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  487. )
  488. );
  489. if intffound then
  490. break;
  491. objdef:=objdef.childof;
  492. end;
  493. if intffound then
  494. inc(intfcount)
  495. else
  496. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  497. end;
  498. if intfcount<>formalobjdef.implementedinterfaces.count then
  499. result:=false;
  500. end;
  501. end;
  502. end;
  503. end;
  504. end;
  505. end;
  506. function parse_generic_specialization_types_internal(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  507. var
  508. old_block_type : tblock_type;
  509. first : boolean;
  510. typeparam : tnode;
  511. parampos : pfileposinfo;
  512. tmpparampos : tfileposinfo;
  513. namepart : string;
  514. module : tmodule;
  515. constprettyname : string;
  516. validparam : boolean;
  517. begin
  518. result:=true;
  519. prettyname:='';
  520. constprettyname:='';
  521. if paramlist=nil then
  522. internalerror(2012061401);
  523. { set the block type to type, so that the parsed type are returned as
  524. ttypenode (e.g. classes are in non type-compatible blocks returned as
  525. tloadvmtaddrnode) }
  526. old_block_type:=block_type;
  527. { if parsedtype is set, then the first type identifer was already parsed
  528. (happens in inline specializations) and thus we only need to parse
  529. the remaining types and do as if the first one was already given }
  530. first:=not assigned(parsedtype);
  531. if assigned(parsedtype) then
  532. begin
  533. paramlist.Add(parsedtype.typesym);
  534. module:=find_module_from_symtable(parsedtype.owner);
  535. if not assigned(module) then
  536. internalerror(2016112801);
  537. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+parsedtype.unique_id_str;
  538. specializename:='$'+namepart;
  539. prettyname:=parsedtype.fullownerhierarchyname(true)+parsedtype.typesym.prettyname;
  540. if assigned(poslist) then
  541. begin
  542. New(parampos);
  543. parampos^:=parsedpos;
  544. poslist.add(parampos);
  545. end;
  546. end
  547. else
  548. specializename:='$';
  549. while not (token in [_GT,_RSHARPBRACKET]) do
  550. begin
  551. { "first" is set to false at the end of the loop! }
  552. if not first then
  553. consume(_COMMA);
  554. block_type:=bt_type;
  555. tmpparampos:=current_filepos;
  556. typeparam:=factor(false,[ef_accept_equal]);
  557. { determine if the typeparam node is a valid type or const }
  558. validparam:=typeparam.nodetype in tgeneric_param_nodes;
  559. if validparam then
  560. begin
  561. if tstoreddef(typeparam.resultdef).is_generic and
  562. (
  563. not parse_generic or
  564. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  565. ) then
  566. Message(parser_e_no_generics_as_params);
  567. if assigned(poslist) then
  568. begin
  569. New(parampos);
  570. parampos^:=tmpparampos;
  571. poslist.add(parampos);
  572. end;
  573. if typeparam.resultdef.typ<>errordef then
  574. begin
  575. if (typeparam.nodetype=typen) and not assigned(typeparam.resultdef.typesym) then
  576. message(type_e_generics_cannot_reference_itself)
  577. else if (typeparam.resultdef.typ<>errordef) then
  578. begin
  579. { all non-type nodes are considered const }
  580. if typeparam.nodetype<>typen then
  581. paramlist.Add(create_generic_constsym(typeparam.resultdef,typeparam,constprettyname))
  582. else
  583. begin
  584. constprettyname:='';
  585. paramlist.Add(typeparam.resultdef.typesym);
  586. end;
  587. make_prettystring(typeparam.resultdef,first,constprettyname,prettyname,specializename);
  588. end;
  589. end
  590. else
  591. begin
  592. result:=false;
  593. end;
  594. end
  595. else
  596. begin
  597. Message(type_e_type_id_expected);
  598. result:=false;
  599. end;
  600. typeparam.free;
  601. first:=false;
  602. end;
  603. block_type:=old_block_type;
  604. end;
  605. function parse_generic_specialization_types(paramlist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  606. var
  607. dummypos : tfileposinfo;
  608. begin
  609. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  610. result:=parse_generic_specialization_types_internal(paramlist,poslist,prettyname,specializename,nil,dummypos);
  611. end;
  612. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string);
  613. var
  614. dummypos : tfileposinfo;
  615. begin
  616. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  617. generate_specialization(tt,enforce_unit,parse_class_parent,_prettyname,nil,'',dummypos);
  618. end;
  619. function finalize_specialization(var pd:tprocdef;spezcontext:tspecializationcontext):boolean;
  620. var
  621. def : tdef;
  622. begin
  623. result:=false;
  624. if assigned(spezcontext) then
  625. begin
  626. if not (df_generic in pd.defoptions) then
  627. internalerror(2015060301);
  628. { check whether the given parameters are compatible
  629. to the def's constraints }
  630. if not check_generic_constraints(pd,spezcontext.paramlist,spezcontext.poslist) then
  631. exit;
  632. def:=generate_specialization_phase2(spezcontext,pd,false,'');
  633. case def.typ of
  634. errordef:
  635. { do nothing }
  636. ;
  637. procdef:
  638. pd:=tprocdef(def);
  639. else
  640. internalerror(2015070303);
  641. end;
  642. end;
  643. result:=true;
  644. end;
  645. procedure transfer_unnamed_symbols(owner:tsymtable;unnamed_syms:tfplist);
  646. var
  647. i : integer;
  648. sym : tsym;
  649. begin
  650. for i:=0 to unnamed_syms.count-1 do
  651. begin
  652. sym:=tsym(unnamed_syms[i]);
  653. sym.ChangeOwnerAndName(owner,sym.realname);
  654. end;
  655. unnamed_syms.clear;
  656. end;
  657. function try_implicit_specialization(sym:tsym;para:tnode;pdoverloadlist:tfpobjectlist;var unnamed_syms:tfplist;var first_procsym:tsym;var hasoverload:boolean):boolean;
  658. { hash key for generic parameter lookups }
  659. function generic_param_hash(def:tdef):string;inline;
  660. begin
  661. result:=def.typename;
  662. end;
  663. { returns true if the def a literal array such as [1,2,3] and not a shortstring }
  664. function is_array_literal(def:tdef):boolean;
  665. begin
  666. result:=(def.typ=arraydef) and not is_conststring_array(def);
  667. end;
  668. { makes the specialization context from the generic proc def and generic params }
  669. procedure generate_implicit_specialization(out context:tspecializationcontext;genericdef:tprocdef;genericparams:tfphashlist);
  670. var
  671. parsedpos:tfileposinfo;
  672. poslist:tfplist;
  673. i: longint;
  674. paramtype: ttypesym;
  675. parampos : pfileposinfo;
  676. tmpparampos : tfileposinfo;
  677. paramname: string;
  678. begin
  679. context:=tspecializationcontext.create;
  680. fillchar(parsedpos,sizeof(parsedpos),0);
  681. poslist:=context.poslist;
  682. tmpparampos:=current_filepos;
  683. if genericparams.count<>genericdef.genericparas.count then
  684. internalerror(2021020901);
  685. for i:=0 to genericparams.count-1 do
  686. begin
  687. paramname:=generic_param_hash(ttypesym(genericdef.genericparas[i]).typedef);
  688. paramtype:=ttypesym(genericparams.find(paramname));
  689. if not assigned(paramtype) then
  690. internalerror(2021020902);
  691. new(parampos);
  692. parampos^:=tmpparampos;
  693. poslist.add(parampos);
  694. context.paramlist.Add(paramtype);
  695. make_prettystring(paramtype.typedef,i=0,'',context.prettyname,context.specializename);
  696. end;
  697. context.genname:=genericdef.procsym.realname;
  698. end;
  699. { specialization context parameter lists require a typesym so we need
  700. to generate a placeholder for unnamed constant types like
  701. short strings, open arrays, function pointers etc... }
  702. function create_unnamed_typesym(def:tdef):tsym;
  703. var
  704. newtype: tsym;
  705. begin
  706. newtype:=nil;
  707. if is_conststring_array(def) then
  708. begin
  709. { for constant strings we need to respect various modeswitches }
  710. if (cs_refcountedstrings in current_settings.localswitches) then
  711. begin
  712. if m_default_unicodestring in current_settings.modeswitches then
  713. newtype:=cunicodestringtype.typesym
  714. else
  715. newtype:=cansistringtype.typesym;
  716. end
  717. else
  718. newtype:=cshortstringtype.typesym;
  719. end
  720. else if def.typ=stringdef then
  721. newtype:=tstringdef(def).get_default_string_type.typesym
  722. else
  723. begin
  724. if is_array_constructor(def) then
  725. begin
  726. { array constructor is not a valid parameter type; getreusable
  727. avoids creating multiple implementations for calls with the
  728. same number of array elements of a particular type }
  729. def:=carraydef.getreusable(tarraydef(def).elementdef,tarraydef(def).highrange-tarraydef(def).lowrange+1);
  730. end;
  731. newtype:=ctypesym.create(def.fullownerhierarchyname(false)+typName[def.typ]+'$'+def.unique_id_str,def);
  732. include(newtype.symoptions,sp_generic_unnamed_type);
  733. newtype.owner:=def.owner;
  734. { ensure that there's no warning }
  735. newtype.refs:=1;
  736. end;
  737. if not assigned(newtype) then
  738. internalerror(2021020904);
  739. result:=newtype;
  740. end;
  741. { searches for the generic param in specializations }
  742. function find_param_in_specialization(owner:tprocdef;genericparam:ttypesym;def:tstoreddef):boolean;
  743. var
  744. parasym: ttypesym;
  745. k, i: integer;
  746. begin
  747. result:=false;
  748. for i:=0 to def.genericparas.count-1 do
  749. begin
  750. parasym:=ttypesym(def.genericparas[i]);
  751. { the generic param must have a named typesym }
  752. if not assigned(parasym.typedef.typesym) then
  753. internalerror(2021020907);
  754. { recurse into inline specialization }
  755. if tstoreddef(parasym.typedef).is_specialization then
  756. begin
  757. result:=find_param_in_specialization(owner,genericparam,tstoreddef(parasym.typedef));
  758. if result then
  759. exit;
  760. end
  761. else if (genericparam=parasym.typedef.typesym) and owner.is_generic_param(parasym.typedef) then
  762. exit(true);
  763. end;
  764. end;
  765. { searches for the generic param in arrays }
  766. function find_param_in_array(owner:tprocdef;genericparam:ttypesym;def:tarraydef):boolean;
  767. var
  768. elementdef:tstoreddef;
  769. begin
  770. elementdef:=tstoreddef(def.elementdef);
  771. { recurse into multi-dimensional array }
  772. if elementdef.typ=arraydef then
  773. result:=find_param_in_array(owner,genericparam,tarraydef(elementdef))
  774. { something went wrong during parsing and the element is invalid }
  775. else if elementdef.typ=errordef then
  776. result:=false
  777. else
  778. begin
  779. { the element must have a named typesym }
  780. if not assigned(elementdef.typesym) then
  781. internalerror(2021020906);
  782. result:=(genericparam=elementdef.typesym) and owner.is_generic_param(elementdef);
  783. end;
  784. end;
  785. { tests if the generic param is used in the parameter list }
  786. function is_generic_param_used(owner:tprocdef;genericparam:ttypesym;paras:tfplist):boolean;
  787. var
  788. paravar:tparavarsym;
  789. i: integer;
  790. begin
  791. result:=false;
  792. for i:=0 to paras.count-1 do
  793. begin
  794. paravar:=tparavarsym(paras[i]);
  795. { handle array types by using element types (for example: array of T) }
  796. if paravar.vardef.typ=arraydef then
  797. result:=find_param_in_array(owner,genericparam,tarraydef(paravar.vardef))
  798. { for specializations check search in generic params }
  799. else if tstoreddef(paravar.vardef).is_specialization then
  800. result:=find_param_in_specialization(owner,genericparam,tstoreddef(paravar.vardef))
  801. { something went wrong during parsing and the parameter is invalid }
  802. else if paravar.vardef.typ=errordef then
  803. exit(false)
  804. else
  805. begin
  806. if not assigned(paravar.vardef.typesym) then
  807. internalerror(2021020905);
  808. result:=(genericparam=paravar.vardef.typesym) and owner.is_generic_param(paravar.vardef)
  809. end;
  810. { exit if we find a used parameter }
  811. if result then
  812. exit;
  813. end;
  814. end;
  815. { handle generic specializations by using generic params from caller
  816. to specialize the target. for example "TRec<Integer>" can use "Integer"
  817. to specialize "TRec<T>" with "Integer" for "T". }
  818. procedure handle_specializations(genericparams:tfphashlist;target_def,caller_def:tstoreddef);
  819. var
  820. i,
  821. index : integer;
  822. key : string;
  823. target_param,
  824. caller_param : ttypesym;
  825. begin
  826. { the target and the caller must the same generic def
  827. with the same set of generic parameters }
  828. if target_def.genericdef<>caller_def.genericdef then
  829. internalerror(2021020909);
  830. for i:=0 to target_def.genericparas.count-1 do
  831. begin
  832. target_param:=ttypesym(target_def.genericparas[i]);
  833. caller_param:=ttypesym(caller_def.genericparas[i]);
  834. { reject generics with constants }
  835. if (target_param.typ=constsym) or (caller_param.typ=constsym) then
  836. exit;
  837. key:=generic_param_hash(target_param.typedef);
  838. { the generic param is already used }
  839. index:=genericparams.findindexof(key);
  840. if index>=0 then
  841. continue;
  842. { add the type to the generic params }
  843. genericparams.add(key,caller_param);
  844. end;
  845. end;
  846. { specialize arrays by using element types but arrays may be multi-dimensional
  847. so we need to examine the caller/target pairs recursively in order to
  848. verify the dimensionality is equal }
  849. function handle_arrays(owner:tprocdef;target_def,caller_def:tarraydef;out target_element,caller_element:tdef):boolean;
  850. begin
  851. { the target and the caller are both arrays and the target is a
  852. specialization so we can recurse into the targets element def }
  853. if is_array_literal(target_def.elementdef) and
  854. is_array_literal(caller_def.elementdef) and
  855. target_def.is_specialization then
  856. result:=handle_arrays(owner,tarraydef(target_def.elementdef),tarraydef(caller_def.elementdef),target_element,caller_element)
  857. else
  858. begin
  859. { the caller is an array which means the dimensionality is unbalanced
  860. and thus the arrays are compatible }
  861. if is_array_literal(caller_def.elementdef) then
  862. exit(false);
  863. { if the element is a generic param then return this type
  864. along with the caller element type at the same level }
  865. result:=owner.is_generic_param(target_def.elementdef);
  866. if result then
  867. begin
  868. target_element:=target_def.elementdef;
  869. caller_element:=caller_def.elementdef;
  870. end;
  871. end;
  872. end;
  873. { handle procvars by using the parameters from the caller to specialize
  874. the parameters of the target generic procedure specialization. for example:
  875. type generic TProc<S> = procedure(value: S);
  876. generic procedure Run<T>(proc: specialize TProc<T>);
  877. procedure DoCallback(value: integer);
  878. Run(@DoCallback);
  879. will specialize as Run<integer> because the signature
  880. of DoCallback() matches TProc<S> so we can specialize "S"
  881. with "integer", as they are both parameter #1
  882. }
  883. function handle_procvars(genericparams:tfphashlist;callerparams:tfplist;target_def:tdef;caller_def:tdef):boolean;
  884. var
  885. newparams : tfphashlist;
  886. procedure handle_generic_param(targetparadef,callerparadef:tdef);
  887. var
  888. key : string;
  889. index : integer;
  890. begin
  891. if not assigned(callerparadef.typesym) then
  892. internalerror(2021020908);
  893. key:=generic_param_hash(targetparadef);
  894. { the generic param must not already be used }
  895. index:=genericparams.findindexof(key);
  896. if index<0 then
  897. begin
  898. { add the type to the list }
  899. index:=newparams.findindexof(key);
  900. if index<0 then
  901. newparams.add(key,callerparadef.typesym);
  902. end;
  903. end;
  904. var
  905. i,j : integer;
  906. paravar : tparavarsym;
  907. target_proc,
  908. caller_proc : tprocvardef;
  909. target_proc_para,
  910. caller_proc_para : tparavarsym;
  911. valid_params : integer;
  912. begin
  913. result := false;
  914. target_proc:=tprocvardef(target_def);
  915. caller_proc:=tprocvardef(caller_def);
  916. { parameter count must match exactly
  917. currently default values are not considered }
  918. if target_proc.paras.count<>caller_proc.paras.count then
  919. exit;
  920. { a mixture of functions and procedures is not allowed }
  921. if (not assigned(target_proc.returndef) or is_void(target_proc.returndef)) xor
  922. (not assigned(caller_proc.returndef) or is_void(caller_proc.returndef)) then
  923. exit;
  924. { reject generics with constants }
  925. for i:=0 to target_proc.genericdef.genericparas.count-1 do
  926. if tsym(target_proc.genericdef.genericparas[i]).typ=constsym then
  927. exit;
  928. newparams:=tfphashlist.create;
  929. valid_params:=0;
  930. for i:=0 to target_proc.paras.count-1 do
  931. begin
  932. target_proc_para:=tparavarsym(target_proc.paras[i]);
  933. caller_proc_para:=tparavarsym(caller_proc.paras[i]);
  934. { the parameters are not compatible }
  935. if compare_defs(caller_proc_para.vardef,target_proc_para.vardef,nothingn)=te_incompatible then
  936. begin
  937. newparams.free;
  938. exit(false);
  939. end;
  940. if sp_generic_para in target_proc_para.vardef.typesym.symoptions then
  941. begin
  942. paravar:=tparavarsym(tprocvardef(target_proc.genericdef).paras[i]);
  943. { find the generic param name in the generic def parameters }
  944. j:=target_proc.genericdef.genericparas.findindexof(paravar.vardef.typesym.name);
  945. handle_generic_param(ttypesym(target_proc.genericparas[j]).typedef,caller_proc_para.vardef);
  946. end;
  947. inc(valid_params);
  948. end;
  949. if assigned(target_proc.returndef) and not is_void(target_proc.returndef) then
  950. begin
  951. { or check for exact? }
  952. if compare_defs(caller_proc.returndef,target_proc.returndef,nothingn)<te_equal then
  953. begin
  954. newparams.free;
  955. exit(false);
  956. end;
  957. if sp_generic_para in target_proc.returndef.typesym.symoptions then
  958. begin
  959. handle_generic_param(target_proc.returndef,caller_proc.returndef);
  960. end;
  961. end;
  962. { if the count of valid params matches the target then
  963. transfer the temporary params to the actual params }
  964. result:=valid_params=target_proc.paras.count;
  965. if result then
  966. for i := 0 to newparams.count-1 do
  967. genericparams.add(newparams.nameofindex(i),newparams[i]);
  968. newparams.free;
  969. end;
  970. function maybe_inherited_specialization(givendef,desireddef:tstoreddef;out basedef:tstoreddef):boolean;
  971. begin
  972. result:=false;
  973. basedef:=nil;
  974. if givendef.typ<>objectdef then
  975. begin
  976. result:=givendef.is_specialization and (givendef.genericdef=desireddef.genericdef);
  977. if result then
  978. basedef:=givendef;
  979. end
  980. else
  981. begin
  982. while assigned(givendef) do
  983. begin
  984. if givendef.is_specialization and (givendef.genericdef=desireddef.genericdef) then
  985. begin
  986. basedef:=givendef;
  987. result:=true;
  988. break;
  989. end;
  990. givendef:=tobjectdef(givendef).childof;
  991. end;
  992. end;
  993. end;
  994. { compare generic parameters <T> with call node parameters. }
  995. function is_possible_specialization(callerparams:tfplist;genericdef:tprocdef;out unnamed_syms:tfplist;out genericparams:tfphashlist):boolean;
  996. var
  997. i,j,
  998. count : integer;
  999. paravar : tparavarsym;
  1000. base_def : tstoreddef;
  1001. target_def,
  1002. caller_def : tdef;
  1003. target_key : string;
  1004. index : integer;
  1005. paras : tfplist;
  1006. target_element,
  1007. caller_element : tdef;
  1008. required_param_count : integer;
  1009. adef : tarraydef;
  1010. begin
  1011. result:=false;
  1012. paras:=nil;
  1013. genericparams:=nil;
  1014. required_param_count:=0;
  1015. unnamed_syms:=nil;
  1016. { first perform a check to reject generics with constants }
  1017. for i:=0 to genericdef.genericparas.count-1 do
  1018. if tsym(genericdef.genericparas[i]).typ=constsym then
  1019. exit;
  1020. { build list of visible target function parameters }
  1021. paras:=tfplist.create;
  1022. for i:=0 to genericdef.paras.count-1 do
  1023. begin
  1024. paravar:=tparavarsym(genericdef.paras[i]);
  1025. { ignore hidden parameters }
  1026. if vo_is_hidden_para in paravar.varoptions then
  1027. continue;
  1028. paras.add(paravar);
  1029. { const non-default parameters are required }
  1030. if not assigned(paravar.defaultconstsym) then
  1031. inc(required_param_count);
  1032. end;
  1033. { not enough parameters were supplied }
  1034. if callerparams.count<required_param_count then
  1035. begin
  1036. paras.free;
  1037. exit;
  1038. end;
  1039. { check to make sure the generic parameters are all used
  1040. at least once in the caller parameters. }
  1041. count:=0;
  1042. for i:=0 to genericdef.genericparas.count-1 do
  1043. if is_generic_param_used(genericdef,ttypesym(genericdef.genericparas[i]),paras) then
  1044. inc(count);
  1045. if count<genericdef.genericparas.count then
  1046. begin
  1047. paras.free;
  1048. exit;
  1049. end;
  1050. genericparams:=tfphashlist.create;
  1051. for i:=0 to callerparams.count-1 do
  1052. begin
  1053. caller_def:=ttypesym(callerparams[i]).typedef;
  1054. { caller parameter exceeded the possible parameters }
  1055. if i=paras.count then
  1056. begin
  1057. genericparams.free;
  1058. paras.free;
  1059. exit;
  1060. end;
  1061. target_def:=tparavarsym(paras[i]).vardef;
  1062. target_key:='';
  1063. { strings are compatible with "array of T" so we
  1064. need to use the element type for specialization }
  1065. if is_stringlike(caller_def) and
  1066. is_array_literal(target_def) and
  1067. genericdef.is_generic_param(tarraydef(target_def).elementdef) then
  1068. begin
  1069. target_def:=tarraydef(target_def).elementdef;
  1070. target_key:=generic_param_hash(target_def);
  1071. caller_def:=chartype_for_stringlike(caller_def);
  1072. end
  1073. { non-uniform array constructors (i.e. array of const) are not compatible
  1074. with normal arrays like "array of T" so we reject them }
  1075. else if is_array_literal(target_def) and
  1076. (caller_def.typ=arraydef) and
  1077. (ado_IsConstructor in tarraydef(caller_def).arrayoptions) and
  1078. (ado_IsArrayOfConst in tarraydef(caller_def).arrayoptions) then
  1079. begin
  1080. continue;
  1081. end
  1082. { handle generic arrays }
  1083. else if is_array_literal(caller_def) and
  1084. is_array_literal(target_def) and
  1085. handle_arrays(genericdef,tarraydef(target_def),tarraydef(caller_def),target_element,caller_element) then
  1086. begin
  1087. target_def:=target_element;
  1088. caller_def:=caller_element;
  1089. target_key:=generic_param_hash(target_def);
  1090. end
  1091. { handle generic procvars }
  1092. else if (caller_def.typ=procvardef) and
  1093. (target_def.typ=procvardef) and
  1094. tprocvardef(target_def).is_specialization and
  1095. handle_procvars(genericparams,callerparams,target_def,caller_def) then
  1096. begin
  1097. continue;
  1098. end
  1099. { handle specialized objects by taking the base class as the type to specialize }
  1100. else if is_class_or_object(caller_def) and
  1101. is_class_or_object(target_def) and
  1102. genericdef.is_generic_param(target_def) then
  1103. begin
  1104. target_key:=generic_param_hash(target_def);
  1105. target_def:=tobjectdef(target_def).childof;
  1106. end
  1107. { handle generic specializations }
  1108. else if tstoreddef(target_def).is_specialization and
  1109. maybe_inherited_specialization(tstoreddef(caller_def),tstoreddef(target_def),base_def) then
  1110. begin
  1111. handle_specializations(genericparams,tstoreddef(target_def),base_def);
  1112. continue;
  1113. end
  1114. { handle all other generic params }
  1115. else if target_def.typ=undefineddef then
  1116. target_key:=generic_param_hash(target_def);
  1117. { the param doesn't have a generic key which means we don't need to consider it }
  1118. if target_key='' then
  1119. continue;
  1120. { the generic param is already used }
  1121. index:=genericparams.findindexof(target_key);
  1122. if index>=0 then
  1123. continue;
  1124. { the caller type may not have a typesym so we need to create an unnamed one }
  1125. if not assigned(caller_def.typesym) then
  1126. begin
  1127. sym:=create_unnamed_typesym(caller_def);
  1128. { add the unnamed sym to the list but only it was allocated manually }
  1129. if sym.owner=caller_def.owner then
  1130. begin
  1131. if not assigned(unnamed_syms) then
  1132. unnamed_syms:=tfplist.create;
  1133. unnamed_syms.add(sym);
  1134. end;
  1135. genericparams.add(target_key,sym);
  1136. end
  1137. else
  1138. genericparams.add(target_key,caller_def.typesym);
  1139. end;
  1140. { if the parameter counts match then the specialization is possible }
  1141. result:=genericparams.count=genericdef.genericparas.count;
  1142. { cleanup }
  1143. paras.free;
  1144. if not result then
  1145. genericparams.free;
  1146. end;
  1147. { make an ordered list of parameters from the caller }
  1148. function make_param_list(dummysym:tsym;para:tnode;var unnamed_syms:tfplist):tfplist;
  1149. var
  1150. pt : tcallparanode;
  1151. paradef : tdef;
  1152. sym : tsym;
  1153. i : integer;
  1154. begin
  1155. result:=tfplist.create;
  1156. pt:=tcallparanode(para);
  1157. while assigned(pt) do
  1158. begin
  1159. paradef:=pt.paravalue.resultdef;
  1160. { unnamed parameter types can not be specialized }
  1161. if not assigned(paradef.typesym) then
  1162. begin
  1163. sym:=create_unnamed_typesym(paradef);
  1164. result.insert(0,sym);
  1165. { add the unnamed sym to the list but only if it was allocated manually }
  1166. if sym.owner=paradef.owner then
  1167. begin
  1168. if not assigned(unnamed_syms) then
  1169. unnamed_syms:=tfplist.create;
  1170. unnamed_syms.add(sym);
  1171. end;
  1172. end
  1173. else
  1174. result.insert(0,paradef.typesym);
  1175. pt:=tcallparanode(pt.nextpara);
  1176. end;
  1177. end;
  1178. var
  1179. i,j,k : integer;
  1180. srsym : tprocsym;
  1181. callerparams : tfplist;
  1182. pd : tprocdef;
  1183. dummysym : tprocsym;
  1184. genericparams : tfphashlist;
  1185. spezcontext : tspecializationcontext;
  1186. pd_unnamed_syms : tfplist;
  1187. begin
  1188. result:=false;
  1189. spezcontext:=nil;
  1190. genericparams:=nil;
  1191. dummysym:=tprocsym(sym);
  1192. callerparams:=make_param_list(dummysym,para,unnamed_syms);
  1193. { failed to build the parameter list }
  1194. if not assigned(callerparams) then
  1195. exit;
  1196. for i:=0 to dummysym.genprocsymovlds.count-1 do
  1197. begin
  1198. srsym:=tprocsym(dummysym.genprocsymovlds[i]);
  1199. for j:=0 to srsym.ProcdefList.Count-1 do
  1200. begin
  1201. pd:=tprocdef(srsym.ProcdefList[j]);
  1202. if is_possible_specialization(callerparams,pd,pd_unnamed_syms,genericparams) then
  1203. begin
  1204. generate_implicit_specialization(spezcontext,pd,genericparams);
  1205. genericparams.free;
  1206. { finalize the specialization so it can be added to the list of overloads }
  1207. if not finalize_specialization(pd,spezcontext) then
  1208. begin
  1209. spezcontext.free;
  1210. continue;
  1211. end;
  1212. { handle unnamed syms used by the specialization }
  1213. if pd_unnamed_syms<>nil then
  1214. begin
  1215. transfer_unnamed_symbols(pd.owner,pd_unnamed_syms);
  1216. pd_unnamed_syms.free;
  1217. end;
  1218. pdoverloadlist.add(pd);
  1219. spezcontext.free;
  1220. if po_overload in pd.procoptions then
  1221. hasoverload:=true;
  1222. { store first procsym found }
  1223. if not assigned(first_procsym) then
  1224. first_procsym:=srsym;
  1225. result:=true;
  1226. end
  1227. else
  1228. begin
  1229. { the specialization was not chosen so clean up any unnamed syms }
  1230. if pd_unnamed_syms<>nil then
  1231. begin
  1232. for k:=0 to pd_unnamed_syms.count-1 do
  1233. tsym(pd_unnamed_syms[k]).free;
  1234. pd_unnamed_syms.free;
  1235. end;
  1236. end;
  1237. end;
  1238. end;
  1239. callerparams.free;
  1240. end;
  1241. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean):tdef;
  1242. var
  1243. dummypos : tfileposinfo;
  1244. {$push}
  1245. {$warn 5036 off}
  1246. begin
  1247. result:=generate_specialization_phase1(context,genericdef,enforce_unit,nil,'',nil,dummypos);
  1248. end;
  1249. {$pop}
  1250. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;const symname:string;symtable:tsymtable):tdef;
  1251. var
  1252. dummypos : tfileposinfo;
  1253. {$push}
  1254. {$warn 5036 off}
  1255. begin
  1256. result:=generate_specialization_phase1(context,genericdef,enforce_unit,nil,symname,symtable,dummypos);
  1257. end;
  1258. {$pop}
  1259. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;enforce_unit:boolean;parsedtype:tdef;const symname:string;symtable:tsymtable;parsedpos:tfileposinfo):tdef;
  1260. var
  1261. found,
  1262. err : boolean;
  1263. i,
  1264. gencount : longint;
  1265. countstr,genname,ugenname : string;
  1266. tmpstack : tfpobjectlist;
  1267. symowner : tsymtable;
  1268. hmodule : tmodule;
  1269. begin
  1270. context:=nil;
  1271. result:=nil;
  1272. { either symname must be given or genericdef needs to be valid }
  1273. if (symname='') and
  1274. (not assigned(genericdef) or
  1275. (
  1276. (genericdef.typ<>procdef) and
  1277. (
  1278. not assigned(genericdef.typesym) or
  1279. (genericdef.typesym.typ<>typesym)
  1280. ) and
  1281. (
  1282. (genericdef.typ<>objectdef) or
  1283. not (oo_is_forward in tobjectdef(genericdef).objectoptions)
  1284. )
  1285. ) or
  1286. (
  1287. (genericdef.typ=procdef) and
  1288. (
  1289. not assigned(tprocdef(genericdef).procsym) or
  1290. (tprocdef(genericdef).procsym.typ<>procsym)
  1291. )
  1292. )
  1293. ) then
  1294. begin
  1295. internalerror(2019112401);
  1296. end;
  1297. if not assigned(parsedtype) and not try_to_consume(_LT) then
  1298. begin
  1299. consume(_LSHARPBRACKET);
  1300. { handle "<>" }
  1301. if (token=_GT) or (token=_RSHARPBRACKET) then
  1302. begin
  1303. Message(type_e_type_id_expected);
  1304. if not try_to_consume(_GT) then
  1305. try_to_consume(_RSHARPBRACKET);
  1306. result:=generrordef;
  1307. exit;
  1308. end;
  1309. end;
  1310. context:=tspecializationcontext.create;
  1311. { Parse type parameters }
  1312. err:=not parse_generic_specialization_types_internal(context.paramlist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  1313. if err then
  1314. begin
  1315. if not try_to_consume(_GT) then
  1316. try_to_consume(_RSHARPBRACKET);
  1317. context.free;
  1318. context:=nil;
  1319. result:=generrordef;
  1320. exit;
  1321. end;
  1322. { use the name of the symbol as procvars return a user friendly version
  1323. of the name }
  1324. if symname='' then
  1325. begin
  1326. if genericdef.typ=procdef then
  1327. genname:=tprocdef(genericdef).procsym.realname
  1328. else if assigned(genericdef.typesym) then
  1329. genname:=ttypesym(genericdef.typesym).realname
  1330. else if (genericdef.typ=objectdef) and (oo_is_forward in tobjectdef(genericdef).objectoptions) then
  1331. genname:=tobjectdef(genericdef).objrealname^
  1332. else
  1333. internalerror(2020071201);
  1334. end
  1335. else
  1336. genname:=symname;
  1337. { in case of non-Delphi mode the type name could already be a generic
  1338. def (but maybe the wrong one) }
  1339. if assigned(genericdef) and
  1340. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  1341. begin
  1342. { remove the type count suffix from the generic's name }
  1343. for i:=Length(genname) downto 1 do
  1344. if genname[i]='$' then
  1345. begin
  1346. genname:=copy(genname,1,i-1);
  1347. break;
  1348. end;
  1349. { in case of a specialization we've only reached the specialization
  1350. checksum yet }
  1351. if df_specialization in genericdef.defoptions then
  1352. for i:=length(genname) downto 1 do
  1353. if genname[i]='$' then
  1354. begin
  1355. genname:=copy(genname,1,i-1);
  1356. break;
  1357. end;
  1358. end
  1359. else
  1360. begin
  1361. split_generic_name(genname,ugenname,gencount);
  1362. if genname<>ugenname then
  1363. genname:=ugenname;
  1364. end;
  1365. { search a generic with the given count of params }
  1366. countstr:='';
  1367. str(context.paramlist.Count,countstr);
  1368. genname:=genname+'$'+countstr;
  1369. ugenname:=upper(genname);
  1370. context.genname:=genname;
  1371. if assigned(genericdef) then
  1372. symowner:=genericdef.owner
  1373. else
  1374. symowner:=symtable;
  1375. if assigned(symowner) and (symowner.symtabletype in [objectsymtable,recordsymtable]) then
  1376. begin
  1377. if symowner.symtabletype = objectsymtable then
  1378. found:=searchsym_in_class(tobjectdef(symowner.defowner),tobjectdef(symowner.defowner),ugenname,context.sym,context.symtable,[])
  1379. else
  1380. found:=searchsym_in_record(tabstractrecorddef(symowner.defowner),ugenname,context.sym,context.symtable);
  1381. if not found then
  1382. found:=searchsym(ugenname,context.sym,context.symtable);
  1383. end
  1384. else if enforce_unit then
  1385. begin
  1386. if not assigned(symowner) then
  1387. internalerror(2022102101);
  1388. if not (symowner.symtabletype in [globalsymtable,recordsymtable]) then
  1389. internalerror(2022102102);
  1390. hmodule:=find_module_from_symtable(symowner);
  1391. if not assigned(hmodule) then
  1392. internalerror(2022102103);
  1393. found:=searchsym_in_module(hmodule,ugenname,context.sym,context.symtable);
  1394. end
  1395. else
  1396. found:=searchsym(ugenname,context.sym,context.symtable);
  1397. if found and (context.sym.typ=absolutevarsym) and
  1398. (vo_is_funcret in tabstractvarsym(context.sym).varoptions) then
  1399. begin
  1400. { we found the function result alias of a generic function; go up the
  1401. symbol stack *before* this alias was inserted, so that we can
  1402. (hopefully) find the correct generic symbol }
  1403. tmpstack:=tfpobjectlist.create(false);
  1404. while assigned(symtablestack.top) do
  1405. begin
  1406. tmpstack.Add(symtablestack.top);
  1407. symtablestack.pop(symtablestack.top);
  1408. if tmpstack.Last=context.symtable then
  1409. break;
  1410. end;
  1411. if not assigned(symtablestack.top) then
  1412. internalerror(2019123001);
  1413. found:=searchsym(ugenname,context.sym,context.symtable);
  1414. for i:=tmpstack.count-1 downto 0 do
  1415. symtablestack.push(tsymtable(tmpstack[i]));
  1416. tmpstack.free;
  1417. end;
  1418. if not found or not (context.sym.typ in [typesym,procsym]) then
  1419. begin
  1420. identifier_not_found(genname);
  1421. if not try_to_consume(_GT) then
  1422. try_to_consume(_RSHARPBRACKET);
  1423. context.free;
  1424. context:=nil;
  1425. result:=generrordef;
  1426. exit;
  1427. end;
  1428. { we've found the correct def }
  1429. if context.sym.typ=typesym then
  1430. result:=tstoreddef(ttypesym(context.sym).typedef)
  1431. else
  1432. begin
  1433. if tprocsym(context.sym).procdeflist.count=0 then
  1434. internalerror(2015061203);
  1435. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  1436. end;
  1437. if not try_to_consume(_GT) then
  1438. consume(_RSHARPBRACKET);
  1439. end;
  1440. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;const _prettyname:ansistring):tdef;
  1441. procedure unset_forwarddef(def: tdef);
  1442. var
  1443. st : TSymtable;
  1444. i : longint;
  1445. begin
  1446. case def.typ of
  1447. procdef:
  1448. tprocdef(def).forwarddef:=false;
  1449. objectdef,
  1450. recorddef:
  1451. begin
  1452. st:=def.getsymtable(gs_record);
  1453. for i:=0 to st.deflist.count-1 do
  1454. unset_forwarddef(tdef(st.deflist[i]));
  1455. end;
  1456. else
  1457. ;
  1458. end;
  1459. end;
  1460. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  1461. var
  1462. i : longint;
  1463. begin
  1464. gendef:=nil;
  1465. psym:=nil;
  1466. case sym.typ of
  1467. typesym:
  1468. begin
  1469. gendef:=ttypesym(sym).typedef
  1470. end;
  1471. procsym:
  1472. begin
  1473. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  1474. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  1475. begin
  1476. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  1477. break;
  1478. end;
  1479. psym:=sym;
  1480. end
  1481. else
  1482. internalerror(200710171);
  1483. end;
  1484. end;
  1485. function find_in_hierarchy(def:tdef;generictypelist:tfphashobjectlist):tdef;
  1486. var
  1487. paramdef1,
  1488. paramdef2 : tdef;
  1489. allequal : boolean;
  1490. i : longint;
  1491. begin
  1492. result:=nil;
  1493. while assigned(def) do
  1494. begin
  1495. if (df_generic in def.defoptions) and (def=genericdef) then
  1496. begin
  1497. result:=def;
  1498. break;
  1499. end;
  1500. { the following happens when a routine with its parent struct
  1501. as parameter is specialized as a parameter or result of a
  1502. generic function }
  1503. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  1504. begin
  1505. if tstoreddef(def).genericparas.count=generictypelist.count then
  1506. begin
  1507. allequal:=true;
  1508. for i:=0 to generictypelist.count-1 do
  1509. begin
  1510. if tsym(generictypelist[i]).typ<>tsym(tstoreddef(def).genericparas[i]).typ then
  1511. begin
  1512. allequal:=false;
  1513. break;
  1514. end;
  1515. if tsym(generictypelist[i]).typ=constsym then
  1516. paramdef1:=tconstsym(generictypelist[i]).constdef
  1517. else
  1518. paramdef1:=ttypesym(generictypelist[i]).typedef;
  1519. if tsym(tstoreddef(def).genericparas[i]).typ=constsym then
  1520. paramdef2:=tconstsym(tstoreddef(def).genericparas[i]).constdef
  1521. else
  1522. paramdef2:=ttypesym(tstoreddef(def).genericparas[i]).typedef;
  1523. if not equal_defs(paramdef1,paramdef2) then
  1524. begin
  1525. allequal:=false;
  1526. break;
  1527. end;
  1528. if (tsym(generictypelist[i]).typ=constsym) and
  1529. (
  1530. (tconstsym(generictypelist[i]).consttyp<>tconstsym(tstoreddef(def).genericparas[i]).consttyp) or
  1531. not same_constvalue(tconstsym(generictypelist[i]).consttyp,tconstsym(generictypelist[i]).value,tconstsym(tstoreddef(def).genericparas[i]).value)
  1532. ) then
  1533. begin
  1534. allequal:=false;
  1535. break;
  1536. end;
  1537. end;
  1538. if allequal then
  1539. begin
  1540. result:=def;
  1541. break;
  1542. end;
  1543. end;
  1544. end;
  1545. if assigned(def.owner) then
  1546. def:=tstoreddef(def.owner.defowner)
  1547. else
  1548. def:=nil;
  1549. end;
  1550. end;
  1551. var
  1552. finalspecializename,
  1553. ufinalspecializename : tidstring;
  1554. hierarchy,
  1555. prettyname : ansistring;
  1556. generictypelist : tfphashobjectlist;
  1557. srsymtable,
  1558. specializest : tsymtable;
  1559. hashedid : thashedidstring;
  1560. tempst : tglobalsymtable;
  1561. tsrsym : ttypesym;
  1562. psym,
  1563. srsym : tsym;
  1564. flags : thccflags;
  1565. paramdef1,
  1566. paramdef2,
  1567. def : tdef;
  1568. old_block_type : tblock_type;
  1569. state : tspecializationstate;
  1570. old_current_structdef : tabstractrecorddef;
  1571. old_current_specializedef,
  1572. old_current_genericdef : tstoreddef;
  1573. old_current_procinfo : tprocinfo;
  1574. old_module_procinfo : tobject;
  1575. hmodule : tmodule;
  1576. oldcurrent_filepos : tfileposinfo;
  1577. recordbuf : tdynamicarray;
  1578. hadtypetoken : boolean;
  1579. i,
  1580. replaydepth : longint;
  1581. item : tobject;
  1582. allequal,
  1583. hintsprocessed : boolean;
  1584. pd : tprocdef;
  1585. pdflags : tpdflags;
  1586. ppflags : tparse_proc_flags;
  1587. begin
  1588. if not assigned(context) then
  1589. internalerror(2015052203);
  1590. result:=nil;
  1591. pd:=nil;
  1592. hmodule:=nil;
  1593. if not check_generic_constraints(genericdef,context.paramlist,context.poslist) then
  1594. begin
  1595. { the parameters didn't fit the constraints, so don't continue with the
  1596. specialization }
  1597. result:=generrordef;
  1598. exit;
  1599. end;
  1600. { build the new type's name }
  1601. hierarchy:=genericdef.ownerhierarchyname;
  1602. if assigned(genericdef.owner) then
  1603. begin
  1604. hmodule:=find_module_from_symtable(genericdef.owner);
  1605. if not assigned(hmodule) then
  1606. internalerror(2022102801);
  1607. if hierarchy<>'' then
  1608. hierarchy:='.'+hierarchy;
  1609. hierarchy:=hmodule.modulename^+hierarchy;
  1610. end;
  1611. finalspecializename:=generate_generic_name(context.genname,context.specializename,hierarchy);
  1612. ufinalspecializename:=upper(finalspecializename);
  1613. if genericdef.typ=procdef then
  1614. prettyname:=tprocdef(genericdef).procsym.prettyname
  1615. else
  1616. prettyname:=genericdef.typesym.prettyname;
  1617. prettyname:=prettyname+'<'+context.prettyname+'>';
  1618. generictypelist:=tfphashobjectlist.create(false);
  1619. { build the list containing the types for the generic params }
  1620. if not assigned(genericdef.genericparas) then
  1621. internalerror(2013092601);
  1622. if context.paramlist.count<>genericdef.genericparas.count then
  1623. internalerror(2013092603);
  1624. for i:=0 to genericdef.genericparas.Count-1 do
  1625. begin
  1626. srsym:=tsym(genericdef.genericparas[i]);
  1627. if not (sp_generic_para in srsym.symoptions) then
  1628. internalerror(2013092602);
  1629. generictypelist.add(srsym.realname,context.paramlist[i]);
  1630. end;
  1631. { Special case if we are referencing the current defined object }
  1632. if assigned(current_structdef) and
  1633. (current_structdef.objname^=ufinalspecializename) then
  1634. result:=current_structdef;
  1635. { Can we reuse an already specialized type? }
  1636. { for this first check whether we are currently specializing a nested
  1637. type of the current (main) specialization (this is necessary, because
  1638. during that time the symbol of the main specialization will still
  1639. contain a reference to an errordef) }
  1640. if not assigned(result) and assigned(current_specializedef) then
  1641. begin
  1642. def:=current_specializedef;
  1643. repeat
  1644. if def.typ in [objectdef,recorddef] then
  1645. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  1646. result:=def;
  1647. break;
  1648. end;
  1649. if assigned(def.owner) then
  1650. def:=tstoreddef(def.owner.defowner)
  1651. else
  1652. { this can happen when specializing a generic function }
  1653. def:=nil;
  1654. until not assigned(def) or not (df_specialization in def.defoptions);
  1655. end;
  1656. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  1657. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  1658. parsing anyway), so for now we treat those still as generic defs without doing a partial
  1659. specialization }
  1660. if not assigned(result) then
  1661. begin
  1662. def:=current_genericdef;
  1663. if def=genericdef then
  1664. result:=def
  1665. else if assigned(current_genericdef) then
  1666. result:=find_in_hierarchy(current_genericdef,generictypelist);
  1667. if not assigned(result) and assigned(current_specializedef) then
  1668. result:=find_in_hierarchy(current_specializedef,generictypelist);
  1669. end;
  1670. { decide in which symtable to put the specialization }
  1671. if assigned(context.forwarddef) then
  1672. begin
  1673. specializest:=context.forwarddef.owner;
  1674. end
  1675. else if parse_generic and not assigned(result) then
  1676. begin
  1677. srsymtable:=symtablestack.top;
  1678. if (srsymtable.symtabletype in [localsymtable,parasymtable]) and tstoreddef(srsymtable.defowner).is_specialization then
  1679. { if we are currently specializing a routine we need to specialize into
  1680. the routine's local- or parasymtable so that they are correctly
  1681. registered should the specialization be finalized }
  1682. specializest:=srsymtable
  1683. else if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  1684. { if we are parsing the definition of a method we specialize into
  1685. the local symtable of it }
  1686. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  1687. else
  1688. begin
  1689. if not assigned(current_genericdef) then
  1690. internalerror(2014050901);
  1691. { we specialize the partial specialization into the symtable of the currently parsed
  1692. generic }
  1693. case current_genericdef.typ of
  1694. procvardef:
  1695. specializest:=current_genericdef.getsymtable(gs_para);
  1696. procdef:
  1697. specializest:=current_genericdef.getsymtable(gs_local);
  1698. objectdef,
  1699. recorddef:
  1700. specializest:=current_genericdef.getsymtable(gs_record);
  1701. arraydef:
  1702. specializest:=tarraydef(current_genericdef).symtable;
  1703. else
  1704. internalerror(2014050902);
  1705. end;
  1706. end;
  1707. end
  1708. else
  1709. if current_module.is_unit and current_module.in_interface then
  1710. specializest:=current_module.globalsymtable
  1711. else
  1712. specializest:=current_module.localsymtable;
  1713. if not assigned(specializest) then
  1714. internalerror(2014050910);
  1715. { now check whether there is a specialization somewhere else }
  1716. psym:=nil;
  1717. if not assigned(result) then
  1718. begin
  1719. hashedid.id:=ufinalspecializename;
  1720. if (specializest.symtabletype=objectsymtable) and not assigned(context.forwarddef) then
  1721. begin
  1722. { search also in parent classes }
  1723. if not assigned(current_genericdef) or (current_genericdef.typ<>objectdef) then
  1724. internalerror(2016112901);
  1725. if not searchsym_in_class(tobjectdef(current_genericdef),tobjectdef(current_genericdef),ufinalspecializename,srsym,srsymtable,[]) then
  1726. srsym:=nil;
  1727. end
  1728. else
  1729. srsym:=tsym(specializest.findwithhash(hashedid));
  1730. if assigned(context.forwarddef) then
  1731. begin
  1732. { just do a few sanity checks }
  1733. if not assigned(srsym) or not (srsym.typ=typesym) then
  1734. internalerror(2020070306);
  1735. if ttypesym(srsym).typedef<>context.forwarddef then
  1736. internalerror(2020070307);
  1737. end
  1738. else if assigned(srsym) then
  1739. begin
  1740. retrieve_genericdef_or_procsym(srsym,result,psym);
  1741. end
  1742. else
  1743. { the generic could have been specialized in the globalsymtable
  1744. already, so search there as well }
  1745. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  1746. begin
  1747. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  1748. if assigned(srsym) then
  1749. begin
  1750. retrieve_genericdef_or_procsym(srsym,result,psym);
  1751. end;
  1752. end;
  1753. end;
  1754. if not assigned(result) then
  1755. begin
  1756. specialization_init(genericdef,state);
  1757. { push a temporary global symtable so that the specialization is
  1758. added to the correct symtable; this symtable does not contain
  1759. any other symbols, so that the type resolution can not be
  1760. influenced by symbols in the current unit }
  1761. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  1762. symtablestack.push(tempst);
  1763. { Reparse the original type definition }
  1764. begin
  1765. old_current_specializedef:=nil;
  1766. old_current_genericdef:=nil;
  1767. old_current_structdef:=nil;
  1768. old_current_procinfo:=current_procinfo;
  1769. old_module_procinfo:=current_module.procinfo;
  1770. current_procinfo:=nil;
  1771. current_module.procinfo:=nil;
  1772. if parse_class_parent then
  1773. begin
  1774. old_current_structdef:=current_structdef;
  1775. old_current_genericdef:=current_genericdef;
  1776. old_current_specializedef:=current_specializedef;
  1777. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  1778. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  1779. else
  1780. current_structdef:=nil;
  1781. current_genericdef:=nil;
  1782. current_specializedef:=nil;
  1783. end;
  1784. maybe_add_waiting_unit(genericdef);
  1785. { First a new sym so we can reuse this specialization and
  1786. references to this specialization can be handled }
  1787. if genericdef.typ=procdef then
  1788. if assigned(psym) then
  1789. srsym:=psym
  1790. else
  1791. srsym:=cprocsym.create(finalspecializename)
  1792. else
  1793. srsym:=ctypesym.create(finalspecializename,generrordef);
  1794. { insert the symbol only if we don't know already that we have
  1795. a procsym to add it to and we aren't dealing with a forwarddef }
  1796. if not assigned(psym) and not assigned(context.forwarddef) then
  1797. specializest.insertsym(srsym);
  1798. { specializations are declarations as such it is the wisest to
  1799. declare set the blocktype to "type"; otherwise we'll
  1800. experience unexpected side effects like the addition of
  1801. classrefdefs if we have a generic that's derived from another
  1802. generic }
  1803. old_block_type:=block_type;
  1804. block_type:=bt_type;
  1805. if (
  1806. (genericdef.typ=procdef) and
  1807. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  1808. ) or (
  1809. (genericdef.typ<>procdef) and
  1810. not assigned(genericdef.generictokenbuf)
  1811. ) then
  1812. internalerror(200511171);
  1813. if hmodule=nil then
  1814. internalerror(2012051202);
  1815. oldcurrent_filepos:=current_filepos;
  1816. { use the index the module got from the current compilation process }
  1817. current_filepos.moduleindex:=hmodule.unit_index;
  1818. current_tokenpos:=current_filepos;
  1819. if parse_generic then
  1820. begin
  1821. recordbuf:=current_scanner.recordtokenbuf;
  1822. current_scanner.recordtokenbuf:=nil;
  1823. end
  1824. else
  1825. recordbuf:=nil;
  1826. replaydepth:=current_scanner.replay_stack_depth;
  1827. if genericdef.typ=procdef then
  1828. begin
  1829. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf,hmodule.change_endian);
  1830. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,[],genericdef,generictypelist,pd);
  1831. if assigned(pd) then
  1832. begin
  1833. if assigned(psym) then
  1834. pd.procsym:=psym
  1835. else
  1836. pd.procsym:=srsym;
  1837. ppflags:=[];
  1838. if po_classmethod in tprocdef(genericdef).procoptions then
  1839. include(ppflags,ppf_classmethod);
  1840. parse_proc_dec_finish(pd,ppflags,tprocdef(genericdef).struct);
  1841. end;
  1842. result:=pd;
  1843. end
  1844. else
  1845. begin
  1846. current_scanner.startreplaytokens(genericdef.generictokenbuf,hmodule.change_endian);
  1847. if assigned(context.forwarddef) then
  1848. begin
  1849. tsrsym:=nil;
  1850. result:=parse_forward_declaration(context.forwarddef.typesym,ufinalspecializename,finalspecializename,genericdef,generictypelist,tsrsym);
  1851. srsym:=tsrsym;
  1852. end
  1853. else
  1854. begin
  1855. hadtypetoken:=false;
  1856. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  1857. ttypesym(srsym).typedef:=result;
  1858. result.typesym:=srsym;
  1859. end;
  1860. if _prettyname<>'' then
  1861. ttypesym(result.typesym).fprettyname:=_prettyname
  1862. else
  1863. ttypesym(result.typesym).fprettyname:=prettyname;
  1864. end;
  1865. current_filepos:=oldcurrent_filepos;
  1866. { Note regarding hint directives:
  1867. There is no need to remove the flags for them from the
  1868. specialized generic symbol, because hint directives that
  1869. follow the specialization are handled by the code in
  1870. pdecl.types_dec and added to the type symbol.
  1871. E.g.: TFoo = TBar<Blubb> deprecated;
  1872. Here the symbol TBar$1$Blubb will contain the
  1873. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  1874. case result.typ of
  1875. { Build VMT indexes for classes and read hint directives }
  1876. objectdef:
  1877. begin
  1878. if replaydepth<current_scanner.replay_stack_depth then
  1879. begin
  1880. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1881. if replaydepth<current_scanner.replay_stack_depth then
  1882. consume(_SEMICOLON);
  1883. end;
  1884. if oo_is_forward in tobjectdef(result).objectoptions then
  1885. add_forward_generic_def(result,context)
  1886. else
  1887. build_vmt(tobjectdef(result));
  1888. end;
  1889. { handle params, calling convention, etc }
  1890. procvardef:
  1891. begin
  1892. hintsprocessed:=false;
  1893. if replaydepth<current_scanner.replay_stack_depth then
  1894. begin
  1895. if not check_proc_directive(true) then
  1896. begin
  1897. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1898. if replaydepth<current_scanner.replay_stack_depth then
  1899. consume(_SEMICOLON);
  1900. end
  1901. else
  1902. hintsprocessed:=true;
  1903. end;
  1904. if replaydepth<current_scanner.replay_stack_depth then
  1905. parse_proctype_directives(tprocvardef(result));
  1906. if po_is_function_ref in tprocvardef(result).procoptions then
  1907. adjust_funcref(result,srsym,nil);
  1908. if result.typ=procvardef then
  1909. flags:=hcc_default_actions_intf
  1910. else
  1911. flags:=hcc_default_actions_intf_struct;
  1912. handle_calling_convention(result,flags);
  1913. if not hintsprocessed and (replaydepth<current_scanner.replay_stack_depth) then
  1914. begin
  1915. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  1916. if replaydepth<current_scanner.replay_stack_depth then
  1917. consume(_SEMICOLON);
  1918. end;
  1919. end;
  1920. procdef:
  1921. begin
  1922. pdflags:=[];
  1923. if genericdef.owner.symtabletype=objectsymtable then
  1924. include(pdflags,pd_object)
  1925. else if genericdef.owner.symtabletype=recordsymtable then
  1926. include(pdflags,pd_record);
  1927. parse_proc_directives(pd,pdflags);
  1928. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1929. consume(_SEMICOLON);
  1930. if parse_generic then
  1931. handle_calling_convention(tprocdef(result),hcc_default_actions_intf)
  1932. else
  1933. handle_calling_convention(tprocdef(result),hcc_default_actions_impl);
  1934. proc_add_definition(tprocdef(result));
  1935. { for partial specializations we implicitely declare the routine as
  1936. having its implementation although we'll not specialize it in reality }
  1937. if parse_generic then
  1938. unset_forwarddef(result);
  1939. end;
  1940. else
  1941. { parse hint directives for records and arrays }
  1942. if replaydepth<current_scanner.replay_stack_depth then begin
  1943. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1944. if replaydepth<current_scanner.replay_stack_depth then
  1945. consume(_SEMICOLON);
  1946. end;
  1947. end;
  1948. { Consume the remainder of the buffer }
  1949. while current_scanner.replay_stack_depth>replaydepth do
  1950. consume(token);
  1951. if assigned(recordbuf) then
  1952. begin
  1953. if assigned(current_scanner.recordtokenbuf) then
  1954. internalerror(2014050909);
  1955. current_scanner.recordtokenbuf:=recordbuf;
  1956. end;
  1957. block_type:=old_block_type;
  1958. current_procinfo:=old_current_procinfo;
  1959. current_module.procinfo:=old_module_procinfo;
  1960. if parse_class_parent then
  1961. begin
  1962. current_structdef:=old_current_structdef;
  1963. current_genericdef:=old_current_genericdef;
  1964. current_specializedef:=old_current_specializedef;
  1965. end;
  1966. end;
  1967. { extract all created symbols and defs from the temporary symtable
  1968. and add them to the specializest }
  1969. for i:=tempst.SymList.Count-1 downto 0 do
  1970. begin
  1971. item:=tempst.SymList.Items[i];
  1972. { using changeowner the symbol is automatically added to the
  1973. new symtable }
  1974. tsym(item).ChangeOwner(specializest);
  1975. end;
  1976. for i:=tempst.DefList.Count-1 downto 0 do
  1977. begin
  1978. item:=tempst.DefList.Items[i];
  1979. { using changeowner the def is automatically added to the new
  1980. symtable }
  1981. tdef(item).ChangeOwner(specializest);
  1982. { for partial specializations we implicitely declare any methods as having their
  1983. implementations although we'll not specialize them in reality }
  1984. if parse_generic then
  1985. unset_forwarddef(tdef(item));
  1986. end;
  1987. { if a generic was declared during the specialization we need to
  1988. flag the specialize symtable accordingly }
  1989. if sto_has_generic in tempst.tableoptions then
  1990. specializest.includeoption(sto_has_generic);
  1991. tempst.free;
  1992. specialization_done(state);
  1993. { procdefs are only added once we know which overload we use }
  1994. if not parse_generic and (result.typ<>procdef) then
  1995. current_module.pendingspecializations.add(result.typename,result);
  1996. end;
  1997. generictypelist.free;
  1998. if assigned(genericdef) then
  1999. begin
  2000. { check the hints of the found generic symbol }
  2001. if genericdef.typ=procdef then
  2002. srsym:=tprocdef(genericdef).procsym
  2003. else
  2004. srsym:=genericdef.typesym;
  2005. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2006. end;
  2007. end;
  2008. procedure generate_specialization(var tt:tdef;enforce_unit:boolean;parse_class_parent:boolean;const _prettyname:string;parsedtype:tdef;const symname:string;parsedpos:tfileposinfo);
  2009. var
  2010. context : tspecializationcontext;
  2011. genericdef : tstoreddef;
  2012. begin
  2013. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,enforce_unit,parsedtype,symname,nil,parsedpos));
  2014. if genericdef<>generrordef then
  2015. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  2016. tt:=genericdef;
  2017. if assigned(context) then
  2018. context.free;
  2019. end;
  2020. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  2021. var
  2022. generictype : tstoredsym;
  2023. i,firstidx,const_list_index : longint;
  2024. srsymtable : tsymtable;
  2025. basedef,def : tdef;
  2026. defname : tidstring;
  2027. allowconst,
  2028. allowconstructor,
  2029. is_const,
  2030. doconsume : boolean;
  2031. constraintdata : tgenericconstraintdata;
  2032. old_block_type : tblock_type;
  2033. fileinfo : tfileposinfo;
  2034. begin
  2035. result:=tfphashobjectlist.create(false);
  2036. firstidx:=0;
  2037. const_list_index:=0;
  2038. old_block_type:=block_type;
  2039. block_type:=bt_type;
  2040. allowconst:=true;
  2041. is_const:=false;
  2042. repeat
  2043. if allowconst and try_to_consume(_CONST) then
  2044. begin
  2045. allowconst:=false;
  2046. is_const:=true;
  2047. const_list_index:=result.count;
  2048. end;
  2049. if token=_ID then
  2050. begin
  2051. if is_const then
  2052. generictype:=cconstsym.create_undefined(orgpattern,cundefinedtype)
  2053. else
  2054. generictype:=ctypesym.create(orgpattern,cundefinedtype);
  2055. { type parameters need to be added as strict private }
  2056. generictype.visibility:=vis_strictprivate;
  2057. include(generictype.symoptions,sp_generic_para);
  2058. result.add(orgpattern,generictype);
  2059. end;
  2060. consume(_ID);
  2061. fileinfo:=current_tokenpos;
  2062. { const restriction }
  2063. if is_const and try_to_consume(_COLON) then
  2064. begin
  2065. def:=nil;
  2066. { parse the type and assign the const type to generictype }
  2067. single_type(def,[]);
  2068. for i:=const_list_index to result.count-1 do
  2069. begin
  2070. { finalize constant information once type is known }
  2071. if assigned(def) and (def.typ in tgeneric_param_const_types) then
  2072. begin
  2073. case def.typ of
  2074. orddef,
  2075. enumdef:
  2076. tconstsym(result[i]).consttyp:=constord;
  2077. stringdef:
  2078. tconstsym(result[i]).consttyp:=conststring;
  2079. floatdef:
  2080. tconstsym(result[i]).consttyp:=constreal;
  2081. setdef:
  2082. tconstsym(result[i]).consttyp:=constset;
  2083. { pointer always refers to nil with constants }
  2084. pointerdef:
  2085. tconstsym(result[i]).consttyp:=constnil;
  2086. else
  2087. internalerror(2020011402);
  2088. end;
  2089. tconstsym(result[i]).constdef:=def;
  2090. end
  2091. else
  2092. Message1(type_e_generic_const_type_not_allowed,def.fulltypename);
  2093. end;
  2094. { after type restriction const list terminates }
  2095. is_const:=false;
  2096. end
  2097. { type restriction }
  2098. else if try_to_consume(_COLON) then
  2099. begin
  2100. if not allowconstraints then
  2101. Message(parser_e_generic_constraints_not_allowed_here);
  2102. { construct a name which can be used for a type specification }
  2103. constraintdata:=tgenericconstraintdata.create;
  2104. constraintdata.fileinfo:=fileinfo;
  2105. defname:='';
  2106. str(current_module.deflist.count,defname);
  2107. defname:='$gendef'+defname;
  2108. allowconstructor:=m_delphi in current_settings.modeswitches;
  2109. basedef:=generrordef;
  2110. repeat
  2111. doconsume:=true;
  2112. case token of
  2113. _CONSTRUCTOR:
  2114. begin
  2115. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  2116. Message(parser_e_illegal_expression);
  2117. include(constraintdata.flags,gcf_constructor);
  2118. allowconstructor:=false;
  2119. end;
  2120. _CLASS:
  2121. begin
  2122. if gcf_class in constraintdata.flags then
  2123. Message(parser_e_illegal_expression);
  2124. if basedef=generrordef then
  2125. include(constraintdata.flags,gcf_class)
  2126. else
  2127. Message(parser_e_illegal_expression);
  2128. end;
  2129. _RECORD:
  2130. begin
  2131. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  2132. or (constraintdata.interfaces.count>0) then
  2133. Message(parser_e_illegal_expression)
  2134. else
  2135. begin
  2136. srsymtable:=trecordsymtable.create(defname,0,1);
  2137. basedef:=crecorddef.create(defname,srsymtable);
  2138. include(constraintdata.flags,gcf_record);
  2139. allowconstructor:=false;
  2140. end;
  2141. end;
  2142. else
  2143. begin
  2144. { after single_type "token" is the trailing ",", ";" or
  2145. ">"! }
  2146. doconsume:=false;
  2147. { def is already set to a class or record }
  2148. if gcf_record in constraintdata.flags then
  2149. Message(parser_e_illegal_expression);
  2150. single_type(def, [stoAllowSpecialization]);
  2151. { only types that are inheritable are allowed }
  2152. if (def.typ<>objectdef) or
  2153. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  2154. Message1(type_e_class_or_interface_type_expected,def.typename)
  2155. else
  2156. case tobjectdef(def).objecttype of
  2157. odt_class,
  2158. odt_javaclass:
  2159. begin
  2160. if gcf_class in constraintdata.flags then
  2161. { "class" + concrete class is not allowed }
  2162. Message(parser_e_illegal_expression)
  2163. else
  2164. { do we already have a concrete class? }
  2165. if basedef<>generrordef then
  2166. Message(parser_e_illegal_expression)
  2167. else
  2168. basedef:=def;
  2169. end;
  2170. odt_interfacecom,
  2171. odt_interfacecorba,
  2172. odt_interfacejava,
  2173. odt_dispinterface:
  2174. constraintdata.interfaces.add(def);
  2175. else
  2176. ;
  2177. end;
  2178. end;
  2179. end;
  2180. if doconsume then
  2181. consume(token);
  2182. until not try_to_consume(_COMMA);
  2183. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  2184. (constraintdata.interfaces.count>1) or
  2185. (
  2186. (basedef.typ=objectdef) and
  2187. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  2188. ) then
  2189. begin
  2190. if basedef.typ=errordef then
  2191. { don't pass an errordef as a parent to a tobjectdef }
  2192. basedef:=class_tobject
  2193. else
  2194. if (basedef.typ<>objectdef) or
  2195. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  2196. internalerror(2012101101);
  2197. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  2198. for i:=0 to constraintdata.interfaces.count-1 do
  2199. tobjectdef(basedef).register_implemented_interface(tobjectdef(constraintdata.interfaces[i]),false);
  2200. end
  2201. else
  2202. if constraintdata.interfaces.count=1 then
  2203. begin
  2204. if basedef.typ<>errordef then
  2205. internalerror(2013021601);
  2206. def:=tdef(constraintdata.interfaces[0]);
  2207. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  2208. constraintdata.interfaces.delete(0);
  2209. end;
  2210. if basedef.typ<>errordef then
  2211. with tstoreddef(basedef) do
  2212. begin
  2213. genconstraintdata:=tgenericconstraintdata.create;
  2214. genconstraintdata.flags:=constraintdata.flags;
  2215. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  2216. genconstraintdata.fileinfo:=constraintdata.fileinfo;
  2217. include(defoptions,df_genconstraint);
  2218. end;
  2219. for i:=firstidx to result.count-1 do
  2220. ttypesym(result[i]).typedef:=basedef;
  2221. { we need a typesym in case we do a Delphi-mode inline
  2222. specialization with this parameter; so just use the first sym }
  2223. if not assigned(basedef.typesym) then
  2224. basedef.typesym:=ttypesym(result[firstidx]);
  2225. firstidx:=result.count;
  2226. constraintdata.free;
  2227. end
  2228. else
  2229. begin
  2230. if token=_SEMICOLON then
  2231. begin
  2232. { two different typeless parameters are considered as incompatible }
  2233. for i:=firstidx to result.count-1 do
  2234. if tsym(result[i]).typ<>constsym then
  2235. begin
  2236. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  2237. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  2238. end;
  2239. { a semicolon terminates a type parameter group }
  2240. firstidx:=result.count;
  2241. end;
  2242. end;
  2243. if token=_SEMICOLON then
  2244. begin
  2245. is_const:=false;
  2246. allowconst:=true;
  2247. end;
  2248. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  2249. { if the constant parameter is not terminated then the type restriction was
  2250. not specified and we need to give an error }
  2251. if is_const then
  2252. consume(_COLON);
  2253. { two different typeless parameters are considered as incompatible }
  2254. for i:=firstidx to result.count-1 do
  2255. if tsym(result[i]).typ<>constsym then
  2256. begin
  2257. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  2258. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  2259. end;
  2260. block_type:=old_block_type;
  2261. end;
  2262. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist;isfwd:boolean);
  2263. var
  2264. i : longint;
  2265. generictype,
  2266. fwdparam : tstoredsym;
  2267. generictypedef : tdef;
  2268. sym : tsym;
  2269. st : tsymtable;
  2270. fwdok : boolean;
  2271. conv : tconverttype;
  2272. op : tprocdef;
  2273. begin
  2274. def.genericdef:=genericdef;
  2275. if not assigned(genericlist) then
  2276. exit;
  2277. if assigned(genericdef) then
  2278. include(def.defoptions,df_specialization)
  2279. else
  2280. if genericlist.count>0 then
  2281. include(def.defoptions,df_generic);
  2282. case def.typ of
  2283. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  2284. arraydef: st:=tarraydef(def).symtable;
  2285. procvardef,procdef: st:=tabstractprocdef(def).parast;
  2286. else
  2287. internalerror(201101020);
  2288. end;
  2289. { if we have a forwarddef we check whether the generic parameters are
  2290. equal and otherwise ignore the list }
  2291. if isfwd then
  2292. begin
  2293. fwdok:=true;
  2294. if (genericlist.count>0) and
  2295. (
  2296. not assigned(def.genericparas)
  2297. or (def.genericparas.count<>genericlist.count)
  2298. ) then
  2299. fwdok:=false
  2300. else
  2301. begin
  2302. for i:=0 to genericlist.count-1 do
  2303. begin
  2304. if def.genericparas.nameofindex(i)<>genericlist.nameofindex(i) then
  2305. begin
  2306. fwdok:=false;
  2307. break;
  2308. end;
  2309. generictype:=tstoredsym(genericlist[i]);
  2310. fwdparam:=tstoredsym(def.genericparas[i]);
  2311. op:=nil;
  2312. conv:=tc_equal;
  2313. if generictype.typ<>fwdparam.typ then
  2314. fwdok:=false
  2315. else if (generictype.typ=typesym) then
  2316. begin
  2317. if compare_defs_ext(ttypesym(generictype).typedef,ttypesym(fwdparam).typedef,nothingn,conv,op,[cdo_strict_genconstraint_check])<te_exact then
  2318. fwdok:=false;
  2319. end
  2320. else if (generictype.typ=constsym) then
  2321. begin
  2322. if (tconstsym(generictype).consttyp<>tconstsym(fwdparam).consttyp) or
  2323. (compare_defs_ext(tconstsym(generictype).constdef,tconstsym(fwdparam).constdef,nothingn,conv,op,[cdo_strict_genconstraint_check])<te_exact) then
  2324. fwdok:=false;
  2325. end
  2326. else
  2327. internalerror(2020070101);
  2328. if not fwdok then
  2329. break;
  2330. end;
  2331. end;
  2332. if not fwdok then
  2333. Message(parser_e_forward_mismatch);
  2334. exit;
  2335. end;
  2336. if (genericlist.count>0) and not assigned(def.genericparas) then
  2337. def.genericparas:=tfphashobjectlist.create(false);
  2338. for i:=0 to genericlist.count-1 do
  2339. begin
  2340. generictype:=tstoredsym(genericlist[i]);
  2341. if assigned(generictype.owner) then
  2342. begin
  2343. if generictype.typ=typesym then
  2344. sym:=ctypesym.create(genericlist.nameofindex(i),ttypesym(generictype).typedef)
  2345. else if generictype.typ=constsym then
  2346. { generictype is a constsym that was created in create_generic_constsym
  2347. during phase 1 so we pass this directly without copying }
  2348. begin
  2349. sym:=generictype;
  2350. { the sym name is still undefined so we set it to match
  2351. the generic param name so it's accessible }
  2352. sym.realname:=genericlist.nameofindex(i);
  2353. include(sym.symoptions,sp_generic_const);
  2354. end
  2355. else
  2356. internalerror(2019021602);
  2357. { type parameters need to be added as strict private }
  2358. sym.visibility:=vis_strictprivate;
  2359. st.insertsym(sym);
  2360. include(sym.symoptions,sp_generic_para);
  2361. end
  2362. else
  2363. begin
  2364. if generictype.typ=typesym then
  2365. begin
  2366. generictypedef:=ttypesym(generictype).typedef;
  2367. if (generictypedef.typ=undefineddef) and (generictypedef<>cundefinedtype) then
  2368. begin
  2369. { the generic parameters were parsed before the genericdef existed thus the
  2370. undefineddefs were added as part of the parent symtable }
  2371. if assigned(generictypedef.owner) then
  2372. generictypedef.owner.DefList.Extract(generictypedef);
  2373. generictypedef.changeowner(st);
  2374. end;
  2375. end;
  2376. st.insertsym(generictype);
  2377. include(generictype.symoptions,sp_generic_para);
  2378. end;
  2379. def.genericparas.add(genericlist.nameofindex(i),generictype);
  2380. end;
  2381. end;
  2382. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  2383. var
  2384. gensym : ttypesym;
  2385. begin
  2386. { for generics in non-Delphi modes we insert a private type symbol
  2387. that has the same base name as the currently parsed generic and
  2388. that references this defs }
  2389. if not (m_delphi in current_settings.modeswitches) and
  2390. (
  2391. (
  2392. parse_generic and
  2393. assigned(genericlist) and
  2394. (genericlist.count>0)
  2395. ) or
  2396. (
  2397. assigned(current_specializedef) and
  2398. assigned(current_structdef.genericdef) and
  2399. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  2400. (pos('$',name)>0)
  2401. )
  2402. ) then
  2403. begin
  2404. { we need to pass nil as def here, because the constructor wants
  2405. to set the typesym of the def which is not what we want }
  2406. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil);
  2407. gensym.typedef:=current_structdef;
  2408. include(gensym.symoptions,sp_internal);
  2409. { the symbol should be only visible to the generic class
  2410. itself }
  2411. gensym.visibility:=vis_strictprivate;
  2412. symtablestack.top.insertsym(gensym);
  2413. end;
  2414. end;
  2415. function generate_generic_name(const name:tidstring;const specializename:ansistring;const owner_hierarchy:ansistring):tidstring;
  2416. var
  2417. crc : cardinal;
  2418. begin
  2419. if specializename='' then
  2420. internalerror(2012061901);
  2421. { build the new type's name }
  2422. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  2423. result:=name+'$crc'+hexstr(crc,8);
  2424. if owner_hierarchy<>'' then
  2425. begin
  2426. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  2427. result:=result+'_crc'+hexstr(crc,8);
  2428. end;
  2429. end;
  2430. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  2431. var
  2432. i,code : longint;
  2433. countstr : string;
  2434. begin
  2435. for i:=length(name) downto 1 do
  2436. if name[i]='$' then
  2437. begin
  2438. nongeneric:=copy(name,1,i-1);
  2439. countstr:=copy(name,i+1,length(name)-i);
  2440. val(countstr,count,code);
  2441. if code<>0 then
  2442. break;
  2443. exit;
  2444. end;
  2445. nongeneric:=name;
  2446. count:=0;
  2447. end;
  2448. procedure add_generic_dummysym(sym:tsym);
  2449. var
  2450. list: TFPObjectList;
  2451. srsym : tsym;
  2452. srsymtable : tsymtable;
  2453. entry : tgenericdummyentry;
  2454. begin
  2455. if sp_generic_dummy in sym.symoptions then
  2456. begin
  2457. { did we already search for a generic with that name? }
  2458. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  2459. if not assigned(list) then
  2460. begin
  2461. list:=tfpobjectlist.create(true);
  2462. current_module.genericdummysyms.add(sym.name,list);
  2463. end;
  2464. { is the dummy sym still "dummy"? }
  2465. if (sym.typ=typesym) and
  2466. (
  2467. { dummy sym defined in mode Delphi }
  2468. (ttypesym(sym).typedef.typ=undefineddef) or
  2469. { dummy sym defined in non-Delphi mode }
  2470. (tstoreddef(ttypesym(sym).typedef).is_generic)
  2471. ) then
  2472. begin
  2473. { do we have a non-generic type of the same name
  2474. available? }
  2475. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  2476. srsym:=nil;
  2477. end
  2478. else if sym.typ=procsym then
  2479. srsym:=sym
  2480. else
  2481. { dummy symbol is already not so dummy anymore }
  2482. srsym:=nil;
  2483. if assigned(srsym) then
  2484. begin
  2485. entry:=tgenericdummyentry.create;
  2486. entry.resolvedsym:=srsym;
  2487. entry.dummysym:=sym;
  2488. list.add(entry);
  2489. end;
  2490. end;
  2491. end;
  2492. function resolve_generic_dummysym(const name:tidstring):tsym;
  2493. var
  2494. list : tfpobjectlist;
  2495. begin
  2496. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  2497. if assigned(list) and (list.count>0) then
  2498. result:=tgenericdummyentry(list.last).resolvedsym
  2499. else
  2500. result:=nil;
  2501. end;
  2502. function could_be_generic(const name:tidstring):boolean;
  2503. begin
  2504. result:=(name<>'') and
  2505. (current_module.genericdummysyms.findindexof(name)>=0);
  2506. end;
  2507. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  2508. var
  2509. pu : tused_unit;
  2510. hmodule : tmodule;
  2511. unitsyms : TFPHashObjectList;
  2512. sym : tsym;
  2513. i : Integer;
  2514. begin
  2515. if not assigned(genericdef) then
  2516. internalerror(200705151);
  2517. { Setup symtablestack at definition time
  2518. to get types right, however this is not perfect, we should probably record
  2519. the resolved symbols }
  2520. state.oldsymtablestack:=symtablestack;
  2521. state.oldextendeddefs:=current_module.extendeddefs;
  2522. state.oldgenericdummysyms:=current_module.genericdummysyms;
  2523. current_module.extendeddefs:=TFPHashObjectList.create(true);
  2524. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  2525. symtablestack:=tdefawaresymtablestack.create;
  2526. if not assigned(genericdef.owner) then
  2527. hmodule:=current_module
  2528. else
  2529. hmodule:=find_module_from_symtable(genericdef.owner);
  2530. if hmodule=nil then
  2531. internalerror(200705152);
  2532. { collect all unit syms in the generic's unit as we need to establish
  2533. their unitsym.module link again so that unit identifiers can be used }
  2534. unitsyms:=tfphashobjectlist.create(false);
  2535. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  2536. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  2537. begin
  2538. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  2539. if sym.typ=unitsym then
  2540. unitsyms.add(upper(sym.realname),sym);
  2541. end;
  2542. { add all units if we are specializing inside the current unit (as the
  2543. generic could have been declared in the implementation part), but load
  2544. only interface units, if we are in a different unit as then the generic
  2545. needs to be in the interface section }
  2546. pu:=tused_unit(hmodule.used_units.first);
  2547. while assigned(pu) do
  2548. begin
  2549. if not assigned(pu.u.globalsymtable) then
  2550. { in certain circular, but valid unit constellations it can happen
  2551. that we specialize a generic in a different unit that was used
  2552. in the implementation section of the generic's unit and were the
  2553. interface is still being parsed and thus the localsymtable is in
  2554. reality the global symtable }
  2555. if pu.u.in_interface then
  2556. symtablestack.push(pu.u.localsymtable)
  2557. else
  2558. internalerror(200705153)
  2559. else
  2560. symtablestack.push(pu.u.globalsymtable);
  2561. sym:=tsym(unitsyms.find(pu.u.modulename^));
  2562. if assigned(sym) and not assigned(tunitsym(sym).module) then
  2563. tunitsym(sym).module:=pu.u;
  2564. pu:=tused_unit(pu.next);
  2565. end;
  2566. unitsyms.free;
  2567. if assigned(hmodule.globalsymtable) then
  2568. symtablestack.push(hmodule.globalsymtable);
  2569. { push the localsymtable if needed }
  2570. if ((hmodule<>current_module) or not current_module.in_interface)
  2571. and assigned(hmodule.localsymtable) then
  2572. symtablestack.push(hmodule.localsymtable);
  2573. end;
  2574. procedure specialization_done(var state: tspecializationstate);
  2575. begin
  2576. { Restore symtablestack }
  2577. current_module.extendeddefs.free;
  2578. current_module.extendeddefs:=state.oldextendeddefs;
  2579. current_module.genericdummysyms.free;
  2580. current_module.genericdummysyms:=state.oldgenericdummysyms;
  2581. symtablestack.free;
  2582. symtablestack:=state.oldsymtablestack;
  2583. { clear the state record to be on the safe side }
  2584. fillchar(state, sizeof(state), 0);
  2585. end;
  2586. {****************************************************************************
  2587. SPECIALIZATION BODY GENERATION
  2588. ****************************************************************************}
  2589. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  2590. var
  2591. oldcurrent_filepos : tfileposinfo;
  2592. begin
  2593. if assigned(def.genericdef) and
  2594. (def.genericdef.typ=procdef) and
  2595. assigned(tprocdef(def.genericdef).generictokenbuf) then
  2596. begin
  2597. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  2598. internalerror(2015061902);
  2599. oldcurrent_filepos:=current_filepos;
  2600. current_filepos:=tprocdef(def.genericdef).fileinfo;
  2601. { use the index the module got from the current compilation process }
  2602. current_filepos.moduleindex:=hmodule.unit_index;
  2603. current_tokenpos:=current_filepos;
  2604. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf,hmodule.change_endian);
  2605. read_proc_body(def);
  2606. current_filepos:=oldcurrent_filepos;
  2607. end
  2608. { synthetic routines will be implemented afterwards }
  2609. else if def.synthetickind=tsk_none then
  2610. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  2611. end;
  2612. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  2613. var
  2614. i : longint;
  2615. hp : tdef;
  2616. hmodule : tmodule;
  2617. begin
  2618. result:=true;
  2619. hmodule:=nil;
  2620. if assigned(def.genericdef) then
  2621. hmodule:=find_module_from_symtable(def.genericdef.owner)
  2622. else if not (df_internal in def.defoptions) then
  2623. internalerror(201202041);
  2624. for i:=0 to def.symtable.DefList.Count-1 do
  2625. begin
  2626. hp:=tdef(def.symtable.DefList[i]);
  2627. if hp.typ=procdef then
  2628. begin
  2629. { only generate the code if we need a body }
  2630. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  2631. continue;
  2632. { and the body is available already (which is implicitely the
  2633. case if the generic routine is part of another unit) }
  2634. if (
  2635. not assigned(hmodule) or
  2636. (hmodule=current_module) or
  2637. (hmodule.state=ms_compile)
  2638. ) and
  2639. { may not be assigned in case it's a synthetic procdef that
  2640. still needs to be generated }
  2641. assigned(tprocdef(hp).genericdef) and
  2642. tprocdef(tprocdef(hp).genericdef).forwarddef then
  2643. begin
  2644. result:=false;
  2645. continue;
  2646. end;
  2647. process_procdef(tprocdef(hp),hmodule);
  2648. end
  2649. else
  2650. if hp.typ in [objectdef,recorddef] then
  2651. { generate code for subtypes as well }
  2652. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  2653. end;
  2654. end;
  2655. procedure generate_specialization_procs;
  2656. var
  2657. i : longint;
  2658. list,
  2659. readdlist : tfpobjectlist;
  2660. def : tstoreddef;
  2661. state : tspecializationstate;
  2662. hmodule : tmodule;
  2663. begin
  2664. { first copy all entries and then work with that list to ensure that
  2665. we don't get an infinite recursion }
  2666. list:=tfpobjectlist.create(false);
  2667. readdlist:=tfpobjectlist.create(false);
  2668. for i:=0 to current_module.pendingspecializations.Count-1 do
  2669. list.add(current_module.pendingspecializations.Items[i]);
  2670. current_module.pendingspecializations.clear;
  2671. for i:=0 to list.count-1 do
  2672. begin
  2673. def:=tstoreddef(list[i]);
  2674. if not tstoreddef(def).is_specialization then
  2675. continue;
  2676. case def.typ of
  2677. procdef:
  2678. begin
  2679. { the use of forwarddef should not backfire as the
  2680. specialization always belongs to the current module }
  2681. if not tprocdef(def).forwarddef then
  2682. continue;
  2683. if not assigned(def.genericdef) then
  2684. internalerror(2015061903);
  2685. hmodule:=find_module_from_symtable(def.genericdef.owner);
  2686. if hmodule=nil then
  2687. internalerror(2015061904);
  2688. { we need to check for a forward declaration only if the
  2689. generic was declared in the same unit (otherwise there
  2690. should be one) }
  2691. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  2692. begin
  2693. readdlist.add(def);
  2694. continue;
  2695. end;
  2696. specialization_init(tstoreddef(def).genericdef,state);
  2697. process_procdef(tprocdef(def),hmodule);
  2698. specialization_done(state);
  2699. end;
  2700. recorddef,
  2701. objectdef:
  2702. begin
  2703. specialization_init(tstoreddef(def).genericdef,state);
  2704. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  2705. readdlist.add(def);
  2706. specialization_done(state);
  2707. end;
  2708. else
  2709. ;
  2710. end;
  2711. end;
  2712. { add those defs back to the pending list for which we don't yet have
  2713. all method bodies }
  2714. for i:=0 to readdlist.count-1 do
  2715. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  2716. readdlist.free;
  2717. list.free;
  2718. end;
  2719. procedure generate_specializations_for_forwarddef(def:tdef);
  2720. var
  2721. list : tfpobjectlist;
  2722. idx,
  2723. i : longint;
  2724. context : tspecializationcontext;
  2725. begin
  2726. if not tstoreddef(def).is_generic then
  2727. internalerror(2020070304);
  2728. idx:=current_module.forwardgenericdefs.findindexof(def.fulltypename);
  2729. if idx<0 then
  2730. exit;
  2731. list:=tfpobjectlist(current_module.forwardgenericdefs.items[idx]);
  2732. if not assigned(list) then
  2733. internalerror(2020070305);
  2734. for i:=0 to list.count-1 do begin
  2735. context:=tspecializationcontext(list[i]);
  2736. generate_specialization_phase2(context,tstoreddef(def),false,'');
  2737. end;
  2738. current_module.forwardgenericdefs.delete(idx);
  2739. end;
  2740. procedure maybe_add_pending_specialization(def:tdef;unnamed_syms: tfplist);
  2741. var
  2742. hmodule : tmodule;
  2743. st : tsymtable;
  2744. i : integer;
  2745. begin
  2746. if parse_generic then
  2747. exit;
  2748. { transfer ownership of any unnamed syms to be the specialization }
  2749. if unnamed_syms<>nil then
  2750. transfer_unnamed_symbols(tprocdef(def).parast,unnamed_syms);
  2751. st:=def.owner;
  2752. while st.symtabletype in [localsymtable] do
  2753. st:=st.defowner.owner;
  2754. hmodule:=find_module_from_symtable(st);
  2755. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  2756. current_module.pendingspecializations.add(def.typename,def);
  2757. end;
  2758. function determine_generic_def(const name:tidstring):tstoreddef;
  2759. var
  2760. hashedid : THashedIDString;
  2761. pd : tprocdef;
  2762. sym : tsym;
  2763. begin
  2764. result:=nil;
  2765. { check whether this is a declaration of a type inside a
  2766. specialization }
  2767. if assigned(current_structdef) and
  2768. (df_specialization in current_structdef.defoptions) then
  2769. begin
  2770. if not assigned(current_structdef.genericdef) or
  2771. not (current_structdef.genericdef.typ in [recorddef,objectdef]) then
  2772. internalerror(2011052301);
  2773. hashedid.id:=name;
  2774. { we could be inside a method of the specialization
  2775. instead of its declaration, so check that first (as
  2776. local nested types aren't allowed we don't need to
  2777. walk the symtablestack to find the localsymtable) }
  2778. if symtablestack.top.symtabletype=localsymtable then
  2779. begin
  2780. { we are in a method }
  2781. if not assigned(symtablestack.top.defowner) or
  2782. (symtablestack.top.defowner.typ<>procdef) then
  2783. internalerror(2011120701);
  2784. pd:=tprocdef(symtablestack.top.defowner);
  2785. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  2786. internalerror(2011120702);
  2787. sym:=tsym(tprocdef(pd.genericdef).localst.findwithhash(hashedid));
  2788. end
  2789. else
  2790. sym:=nil;
  2791. if not assigned(sym) or not (sym.typ=typesym) then
  2792. begin
  2793. { now search in the declaration of the generic }
  2794. sym:=tsym(tabstractrecorddef(current_structdef.genericdef).symtable.findwithhash(hashedid));
  2795. if not assigned(sym) or not (sym.typ=typesym) then
  2796. internalerror(2011052302);
  2797. end;
  2798. { use the corresponding type in the generic's symtable as
  2799. genericdef for the specialized type }
  2800. result:=tstoreddef(ttypesym(sym).typedef);
  2801. end;
  2802. end;
  2803. end.