pgenutil.pas 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. {
  2. Copyright (c) 2011
  3. Contains different functions that are used in the context of
  4. parsing generics.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pgenutil;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cclasses,
  24. { global }
  25. globtype,
  26. { parser }
  27. pgentype,
  28. { symtable }
  29. symtype,symdef,symbase;
  30. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);inline;
  31. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);inline;
  32. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;inline;
  33. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string):tdef;inline;
  34. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  35. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  36. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  37. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  38. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  39. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  40. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  41. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  42. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  43. function resolve_generic_dummysym(const name:tidstring):tsym;
  44. function could_be_generic(const name:tidstring):boolean;inline;
  45. procedure generate_specialization_procs;
  46. procedure maybe_add_pending_specialization(def:tdef);
  47. procedure specialization_init(genericdef:tdef;var state:tspecializationstate);
  48. procedure specialization_done(var state:tspecializationstate);
  49. implementation
  50. uses
  51. { common }
  52. cutils,fpccrc,
  53. { global }
  54. globals,tokens,verbose,finput,
  55. { symtable }
  56. symconst,symsym,symtable,defcmp,procinfo,
  57. { modules }
  58. fmodule,
  59. { pass 1 }
  60. htypechk,
  61. node,nobj,nmem,
  62. { parser }
  63. scanner,
  64. pbase,pexpr,pdecsub,ptype,psub;
  65. procedure maybe_add_waiting_unit(tt:tdef);
  66. var
  67. hmodule : tmodule;
  68. begin
  69. if not assigned(tt) or
  70. not (df_generic in tt.defoptions) then
  71. exit;
  72. hmodule:=find_module_from_symtable(tt.owner);
  73. if not assigned(hmodule) then
  74. internalerror(2012092401);
  75. if hmodule=current_module then
  76. exit;
  77. if hmodule.state<>ms_compiled then
  78. begin
  79. {$ifdef DEBUG_UNITWAITING}
  80. Writeln('Unit ', current_module.modulename^,
  81. ' waiting for ', hmodule.modulename^);
  82. {$endif DEBUG_UNITWAITING}
  83. if current_module.waitingforunit.indexof(hmodule)<0 then
  84. current_module.waitingforunit.add(hmodule);
  85. if hmodule.waitingunits.indexof(current_module)<0 then
  86. hmodule.waitingunits.add(current_module);
  87. end;
  88. end;
  89. function check_generic_constraints(genericdef:tstoreddef;paradeflist:tfpobjectlist;poslist:tfplist):boolean;
  90. var
  91. i,j,
  92. intfcount : longint;
  93. formaldef,
  94. paradef : tstoreddef;
  95. objdef,
  96. paraobjdef,
  97. formalobjdef : tobjectdef;
  98. intffound : boolean;
  99. filepos : tfileposinfo;
  100. begin
  101. { check whether the given specialization parameters fit to the eventual
  102. constraints of the generic }
  103. if not assigned(genericdef.genericparas) or (genericdef.genericparas.count=0) then
  104. internalerror(2012101001);
  105. if genericdef.genericparas.count<>paradeflist.count then
  106. internalerror(2012101002);
  107. if paradeflist.count<>poslist.count then
  108. internalerror(2012120801);
  109. result:=true;
  110. for i:=0 to genericdef.genericparas.count-1 do
  111. begin
  112. filepos:=pfileposinfo(poslist[i])^;
  113. formaldef:=tstoreddef(ttypesym(genericdef.genericparas[i]).typedef);
  114. if formaldef.typ=undefineddef then
  115. { the parameter is of unspecified type, so no need to check }
  116. continue;
  117. if not (df_genconstraint in formaldef.defoptions) or
  118. not assigned(formaldef.genconstraintdata) then
  119. internalerror(2013021602);
  120. paradef:=tstoreddef(paradeflist[i]);
  121. { undefineddef is compatible with anything }
  122. if formaldef.typ=undefineddef then
  123. continue;
  124. if paradef.typ<>formaldef.typ then
  125. begin
  126. case formaldef.typ of
  127. recorddef:
  128. MessagePos(filepos,type_e_record_type_expected);
  129. objectdef:
  130. case tobjectdef(formaldef).objecttype of
  131. odt_class,
  132. odt_javaclass:
  133. MessagePos1(filepos,type_e_class_type_expected,paradef.typename);
  134. odt_interfacecom,
  135. odt_interfacecorba,
  136. odt_dispinterface,
  137. odt_interfacejava:
  138. MessagePos1(filepos,type_e_interface_type_expected,paradef.typename);
  139. else
  140. internalerror(2012101003);
  141. end;
  142. errordef:
  143. { ignore }
  144. ;
  145. else
  146. internalerror(2012101004);
  147. end;
  148. result:=false;
  149. end
  150. else
  151. begin
  152. { the paradef types are the same, so do special checks for the
  153. cases in which they are needed }
  154. if formaldef.typ=objectdef then
  155. begin
  156. paraobjdef:=tobjectdef(paradef);
  157. formalobjdef:=tobjectdef(formaldef);
  158. if not (formalobjdef.objecttype in [odt_class,odt_javaclass,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface]) then
  159. internalerror(2012101102);
  160. if formalobjdef.objecttype in [odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_dispinterface] then
  161. begin
  162. { this is either a concerete interface or class type (the
  163. latter without specific implemented interfaces) }
  164. case paraobjdef.objecttype of
  165. odt_interfacecom,
  166. odt_interfacecorba,
  167. odt_interfacejava,
  168. odt_dispinterface:
  169. begin
  170. if (oo_is_forward in paraobjdef.objectoptions) and
  171. (paraobjdef.objecttype=formalobjdef.objecttype) and
  172. (df_genconstraint in formalobjdef.defoptions) and
  173. (
  174. (formalobjdef.objecttype=odt_interfacecom) and
  175. (formalobjdef.childof=interface_iunknown)
  176. )
  177. or
  178. (
  179. (formalobjdef.objecttype=odt_interfacecorba) and
  180. (formalobjdef.childof=nil)
  181. ) then
  182. continue;
  183. if not def_is_related(paraobjdef,formalobjdef.childof) then
  184. begin
  185. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  186. result:=false;
  187. end;
  188. end;
  189. odt_class,
  190. odt_javaclass:
  191. begin
  192. objdef:=paraobjdef;
  193. intffound:=false;
  194. while assigned(objdef) do
  195. begin
  196. for j:=0 to objdef.implementedinterfaces.count-1 do
  197. if timplementedinterface(objdef.implementedinterfaces[j]).intfdef=formalobjdef.childof then
  198. begin
  199. intffound:=true;
  200. break;
  201. end;
  202. if intffound then
  203. break;
  204. objdef:=objdef.childof;
  205. end;
  206. result:=intffound;
  207. if not result then
  208. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,formalobjdef.childof.typename);
  209. end;
  210. else
  211. begin
  212. MessagePos1(filepos,type_e_class_or_interface_type_expected,paraobjdef.typename);
  213. result:=false;
  214. end;
  215. end;
  216. end
  217. else
  218. begin
  219. { this is either a "class" or a concrete instance with
  220. or without implemented interfaces }
  221. if not (paraobjdef.objecttype in [odt_class,odt_javaclass]) then
  222. begin
  223. MessagePos1(filepos,type_e_class_type_expected,paraobjdef.typename);
  224. result:=false;
  225. continue;
  226. end;
  227. { for forward declared classes we allow pure TObject/class declarations }
  228. if (oo_is_forward in paraobjdef.objectoptions) and
  229. (df_genconstraint in formaldef.defoptions) then
  230. begin
  231. if (formalobjdef.childof=class_tobject) and
  232. not formalobjdef.implements_any_interfaces then
  233. continue;
  234. end;
  235. if assigned(formalobjdef.childof) and
  236. not def_is_related(paradef,formalobjdef.childof) then
  237. begin
  238. MessagePos2(filepos,type_e_incompatible_types,paraobjdef.typename,formalobjdef.childof.typename);
  239. result:=false;
  240. end;
  241. intfcount:=0;
  242. for j:=0 to formalobjdef.implementedinterfaces.count-1 do
  243. begin
  244. objdef:=paraobjdef;
  245. while assigned(objdef) do
  246. begin
  247. intffound:=assigned(
  248. find_implemented_interface(objdef,
  249. timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef
  250. )
  251. );
  252. if intffound then
  253. break;
  254. objdef:=objdef.childof;
  255. end;
  256. if intffound then
  257. inc(intfcount)
  258. else
  259. MessagePos2(filepos,parser_e_class_doesnt_implement_interface,paraobjdef.typename,timplementedinterface(formalobjdef.implementedinterfaces[j]).intfdef.typename);
  260. end;
  261. if intfcount<>formalobjdef.implementedinterfaces.count then
  262. result:=false;
  263. end;
  264. end;
  265. end;
  266. end;
  267. end;
  268. function parse_generic_specialization_types_internal(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring;parsedtype:tdef;parsedpos:tfileposinfo):boolean;
  269. var
  270. old_block_type : tblock_type;
  271. first : boolean;
  272. typeparam : tnode;
  273. parampos : pfileposinfo;
  274. tmpparampos : tfileposinfo;
  275. namepart : string;
  276. prettynamepart : ansistring;
  277. module : tmodule;
  278. begin
  279. result:=true;
  280. if genericdeflist=nil then
  281. internalerror(2012061401);
  282. { set the block type to type, so that the parsed type are returned as
  283. ttypenode (e.g. classes are in non type-compatible blocks returned as
  284. tloadvmtaddrnode) }
  285. old_block_type:=block_type;
  286. { if parsedtype is set, then the first type identifer was already parsed
  287. (happens in inline specializations) and thus we only need to parse
  288. the remaining types and do as if the first one was already given }
  289. first:=not assigned(parsedtype);
  290. if assigned(parsedtype) then
  291. begin
  292. genericdeflist.Add(parsedtype);
  293. module:=find_module_from_symtable(parsedtype.owner);
  294. if not assigned(module) then
  295. internalerror(2016112801);
  296. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+parsedtype.unique_id_str;
  297. specializename:='$'+namepart;
  298. prettyname:=parsedtype.fullownerhierarchyname(true)+parsedtype.typesym.prettyname;
  299. if assigned(poslist) then
  300. begin
  301. New(parampos);
  302. parampos^:=parsedpos;
  303. poslist.add(parampos);
  304. end;
  305. end
  306. else
  307. begin
  308. specializename:='$';
  309. prettyname:='';
  310. end;
  311. while not (token in [_GT,_RSHARPBRACKET]) do
  312. begin
  313. { "first" is set to false at the end of the loop! }
  314. if not first then
  315. consume(_COMMA);
  316. block_type:=bt_type;
  317. tmpparampos:=current_filepos;
  318. typeparam:=factor(false,[ef_type_only]);
  319. if typeparam.nodetype=typen then
  320. begin
  321. if tstoreddef(typeparam.resultdef).is_generic and
  322. (
  323. not parse_generic or
  324. not defs_belong_to_same_generic(typeparam.resultdef,current_genericdef)
  325. ) then
  326. Message(parser_e_no_generics_as_params);
  327. if assigned(poslist) then
  328. begin
  329. New(parampos);
  330. parampos^:=tmpparampos;
  331. poslist.add(parampos);
  332. end;
  333. if typeparam.resultdef.typ<>errordef then
  334. begin
  335. if not assigned(typeparam.resultdef.typesym) then
  336. message(type_e_generics_cannot_reference_itself)
  337. else if (typeparam.resultdef.typ<>errordef) then
  338. begin
  339. genericdeflist.Add(typeparam.resultdef);
  340. module:=find_module_from_symtable(typeparam.resultdef.owner);
  341. if not assigned(module) then
  342. internalerror(2016112802);
  343. namepart:='_$'+hexstr(module.moduleid,8)+'$$'+typeparam.resultdef.unique_id_str;
  344. { we use the full name of the type to uniquely identify it }
  345. if (symtablestack.top.symtabletype=parasymtable) and
  346. (symtablestack.top.defowner.typ=procdef) and
  347. (typeparam.resultdef.owner=symtablestack.top) then
  348. begin
  349. { special handling for specializations inside generic function declarations }
  350. prettynamepart:=tdef(symtablestack.top.defowner).fullownerhierarchyname(true)+tprocdef(symtablestack.top.defowner).procsym.prettyname;
  351. end
  352. else
  353. begin
  354. prettynamepart:=typeparam.resultdef.fullownerhierarchyname(true);
  355. end;
  356. specializename:=specializename+namepart;
  357. if not first then
  358. prettyname:=prettyname+',';
  359. prettyname:=prettyname+prettynamepart+typeparam.resultdef.typesym.prettyname;
  360. end;
  361. end
  362. else
  363. begin
  364. result:=false;
  365. end;
  366. end
  367. else
  368. begin
  369. Message(type_e_type_id_expected);
  370. result:=false;
  371. end;
  372. typeparam.free;
  373. first:=false;
  374. end;
  375. block_type:=old_block_type;
  376. end;
  377. function parse_generic_specialization_types(genericdeflist:tfpobjectlist;poslist:tfplist;out prettyname,specializename:ansistring):boolean;
  378. var
  379. dummypos : tfileposinfo;
  380. begin
  381. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  382. result:=parse_generic_specialization_types_internal(genericdeflist,poslist,prettyname,specializename,nil,dummypos);
  383. end;
  384. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string);
  385. var
  386. dummypos : tfileposinfo;
  387. begin
  388. FillChar(dummypos, SizeOf(tfileposinfo), 0);
  389. generate_specialization(tt,parse_class_parent,_prettyname,nil,'',dummypos);
  390. end;
  391. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef):tdef;
  392. var
  393. dummypos : tfileposinfo;
  394. {$push}
  395. {$warn 5036 off}
  396. begin
  397. result:=generate_specialization_phase1(context,genericdef,nil,'',dummypos);
  398. end;
  399. {$pop}
  400. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;symname:string):tdef;
  401. var
  402. dummypos : tfileposinfo;
  403. {$push}
  404. {$warn 5036 off}
  405. begin
  406. result:=generate_specialization_phase1(context,genericdef,nil,symname,dummypos);
  407. end;
  408. {$pop}
  409. function generate_specialization_phase1(out context:tspecializationcontext;genericdef:tdef;parsedtype:tdef;symname:string;parsedpos:tfileposinfo):tdef;
  410. var
  411. pt2 : tnode;
  412. errorrecovery,
  413. found,
  414. first,
  415. err : boolean;
  416. i,
  417. gencount : longint;
  418. def : tstoreddef;
  419. countstr,genname,ugenname : string;
  420. srsym : tsym;
  421. st : tsymtable;
  422. begin
  423. context:=nil;
  424. result:=nil;
  425. { either symname must be given or genericdef needs to be valid }
  426. errorrecovery:=false;
  427. if (symname='') and
  428. (not assigned(genericdef) or
  429. not assigned(genericdef.typesym) or
  430. (genericdef.typesym.typ<>typesym)) then
  431. begin
  432. errorrecovery:=true;
  433. result:=generrordef;
  434. end;
  435. { Only parse the parameters for recovery or
  436. for recording in genericbuf }
  437. if errorrecovery then
  438. begin
  439. first:=assigned(parsedtype);
  440. if not first and not try_to_consume(_LT) then
  441. consume(_LSHARPBRACKET);
  442. gencount:=0;
  443. { handle "<>" }
  444. if not first and ((token=_RSHARPBRACKET) or (token=_GT)) then
  445. Message(type_e_type_id_expected)
  446. else
  447. repeat
  448. if not first then
  449. begin
  450. pt2:=factor(false,[ef_type_only]);
  451. pt2.free;
  452. end;
  453. first:=false;
  454. inc(gencount);
  455. until not try_to_consume(_COMMA);
  456. if not try_to_consume(_GT) then
  457. consume(_RSHARPBRACKET);
  458. { we need to return a def that can later pass some checks like
  459. whether it's an interface or not }
  460. if not errorrecovery and
  461. (not assigned(result) or (result.typ=undefineddef)) then
  462. begin
  463. if (symname='') and tstoreddef(genericdef).is_generic then
  464. { this happens in non-Delphi modes }
  465. result:=genericdef
  466. else
  467. begin
  468. { find the corresponding generic symbol so that any checks
  469. done on the returned def will be handled correctly }
  470. str(gencount,countstr);
  471. if symname='' then
  472. genname:=ttypesym(genericdef.typesym).realname
  473. else
  474. genname:=symname;
  475. genname:=genname+'$'+countstr;
  476. ugenname:=upper(genname);
  477. { first check whether the found name is the same as that of
  478. the current def or one of its (generic) surrounding defs;
  479. this is necessary as the symbol of the generic can not yet
  480. be used for lookup as it still contains a reference to an
  481. errordef) }
  482. def:=current_genericdef;
  483. repeat
  484. if def.typ in [objectdef,recorddef] then
  485. if tabstractrecorddef(def).objname^=ugenname then
  486. begin
  487. result:=def;
  488. break;
  489. end;
  490. def:=tstoreddef(def.owner.defowner);
  491. until not assigned(def) or not (df_generic in def.defoptions);
  492. { it's not part of the current object hierarchy, so search
  493. for the symbol }
  494. if not assigned(result) then
  495. begin
  496. srsym:=nil;
  497. if not searchsym(ugenname,srsym,st) or
  498. (srsym.typ<>typesym) then
  499. begin
  500. identifier_not_found(genname);
  501. result:=generrordef;
  502. exit;
  503. end;
  504. result:=ttypesym(srsym).typedef;
  505. { this happens in non-Delphi modes if we encounter a
  506. specialization of the generic class or record we're
  507. currently parsing }
  508. if (result.typ=errordef) and assigned(current_structdef) and
  509. (current_structdef.objname^=ugenname) then
  510. result:=current_structdef;
  511. end;
  512. end;
  513. end;
  514. exit;
  515. end;
  516. if not assigned(parsedtype) and not try_to_consume(_LT) then
  517. begin
  518. consume(_LSHARPBRACKET);
  519. { handle "<>" }
  520. if (token=_GT) or (token=_RSHARPBRACKET) then
  521. begin
  522. Message(type_e_type_id_expected);
  523. if not try_to_consume(_GT) then
  524. try_to_consume(_RSHARPBRACKET);
  525. result:=generrordef;
  526. exit;
  527. end;
  528. end;
  529. context:=tspecializationcontext.create;
  530. { Parse type parameters }
  531. err:=not parse_generic_specialization_types_internal(context.genericdeflist,context.poslist,context.prettyname,context.specializename,parsedtype,parsedpos);
  532. if err then
  533. begin
  534. if not try_to_consume(_GT) then
  535. try_to_consume(_RSHARPBRACKET);
  536. context.free;
  537. context:=nil;
  538. result:=generrordef;
  539. exit;
  540. end;
  541. { use the name of the symbol as procvars return a user friendly version
  542. of the name }
  543. if symname='' then
  544. genname:=ttypesym(genericdef.typesym).realname
  545. else
  546. genname:=symname;
  547. { in case of non-Delphi mode the type name could already be a generic
  548. def (but maybe the wrong one) }
  549. if assigned(genericdef) and
  550. ([df_generic,df_specialization]*genericdef.defoptions<>[]) then
  551. begin
  552. { remove the type count suffix from the generic's name }
  553. for i:=Length(genname) downto 1 do
  554. if genname[i]='$' then
  555. begin
  556. genname:=copy(genname,1,i-1);
  557. break;
  558. end;
  559. { in case of a specialization we've only reached the specialization
  560. checksum yet }
  561. if df_specialization in genericdef.defoptions then
  562. for i:=length(genname) downto 1 do
  563. if genname[i]='$' then
  564. begin
  565. genname:=copy(genname,1,i-1);
  566. break;
  567. end;
  568. end
  569. else
  570. begin
  571. split_generic_name(genname,ugenname,gencount);
  572. if genname<>ugenname then
  573. genname:=ugenname;
  574. end;
  575. { search a generic with the given count of params }
  576. countstr:='';
  577. str(context.genericdeflist.Count,countstr);
  578. genname:=genname+'$'+countstr;
  579. ugenname:=upper(genname);
  580. context.genname:=genname;
  581. if assigned(genericdef) and (genericdef.owner.symtabletype in [objectsymtable,recordsymtable]) then
  582. begin
  583. if genericdef.owner.symtabletype = objectsymtable then
  584. found:=searchsym_in_class(tobjectdef(genericdef.owner.defowner),tobjectdef(genericdef.owner.defowner),ugenname,context.sym,context.symtable,[])
  585. else
  586. found:=searchsym_in_record(tabstractrecorddef(genericdef.owner.defowner),ugenname,context.sym,context.symtable);
  587. if not found then
  588. found:=searchsym(ugenname,context.sym,context.symtable);
  589. end
  590. else
  591. found:=searchsym(ugenname,context.sym,context.symtable);
  592. if not found or not (context.sym.typ in [typesym,procsym]) then
  593. begin
  594. identifier_not_found(genname);
  595. if not try_to_consume(_GT) then
  596. try_to_consume(_RSHARPBRACKET);
  597. context.free;
  598. context:=nil;
  599. result:=generrordef;
  600. exit;
  601. end;
  602. { we've found the correct def }
  603. if context.sym.typ=typesym then
  604. result:=tstoreddef(ttypesym(context.sym).typedef)
  605. else
  606. begin
  607. if tprocsym(context.sym).procdeflist.count=0 then
  608. internalerror(2015061203);
  609. result:=tstoreddef(tprocsym(context.sym).procdefList[0]);
  610. end;
  611. if not try_to_consume(_GT) then
  612. consume(_RSHARPBRACKET);
  613. end;
  614. function generate_specialization_phase2(context:tspecializationcontext;genericdef:tstoreddef;parse_class_parent:boolean;_prettyname:ansistring):tdef;
  615. procedure unset_forwarddef(def: tdef);
  616. var
  617. st : TSymtable;
  618. i : longint;
  619. begin
  620. case def.typ of
  621. procdef:
  622. tprocdef(def).forwarddef:=false;
  623. objectdef,
  624. recorddef:
  625. begin
  626. st:=def.getsymtable(gs_record);
  627. for i:=0 to st.deflist.count-1 do
  628. unset_forwarddef(tdef(st.deflist[i]));
  629. end;
  630. end;
  631. end;
  632. procedure retrieve_genericdef_or_procsym(sym:tsym;out gendef:tdef;out psym:tsym);
  633. var
  634. i : longint;
  635. begin
  636. gendef:=nil;
  637. psym:=nil;
  638. case sym.typ of
  639. typesym:
  640. begin
  641. gendef:=ttypesym(sym).typedef
  642. end;
  643. procsym:
  644. begin
  645. for i:=0 to tprocsym(sym).procdeflist.count-1 do
  646. if tstoreddef(tprocsym(sym).procdeflist[i]).genericdef=genericdef then
  647. begin
  648. gendef:=tdef(tprocsym(sym).procdeflist[i]);
  649. break;
  650. end;
  651. psym:=sym;
  652. end
  653. else
  654. internalerror(200710171);
  655. end;
  656. end;
  657. var
  658. finalspecializename,
  659. ufinalspecializename : tidstring;
  660. prettyname : ansistring;
  661. generictypelist : tfphashobjectlist;
  662. specializest : tsymtable;
  663. hashedid : thashedidstring;
  664. tempst : tglobalsymtable;
  665. psym,
  666. srsym : tsym;
  667. def : tdef;
  668. old_block_type : tblock_type;
  669. state : tspecializationstate;
  670. old_current_structdef : tabstractrecorddef;
  671. old_current_specializedef,
  672. old_current_genericdef : tstoreddef;
  673. old_current_procinfo : tprocinfo;
  674. hmodule : tmodule;
  675. oldcurrent_filepos : tfileposinfo;
  676. recordbuf : tdynamicarray;
  677. hadtypetoken : boolean;
  678. vmtbuilder : tvmtbuilder;
  679. i,
  680. replaydepth : longint;
  681. item : tobject;
  682. allequal,
  683. hintsprocessed : boolean;
  684. pd : tprocdef;
  685. pdflags : tpdflags;
  686. begin
  687. if not assigned(context) then
  688. internalerror(2015052203);
  689. result:=nil;
  690. pd:=nil;
  691. if not check_generic_constraints(genericdef,context.genericdeflist,context.poslist) then
  692. begin
  693. { the parameters didn't fit the constraints, so don't continue with the
  694. specialization }
  695. result:=generrordef;
  696. exit;
  697. end;
  698. { build the new type's name }
  699. finalspecializename:=generate_generic_name(context.genname,context.specializename,genericdef.ownerhierarchyname);
  700. ufinalspecializename:=upper(finalspecializename);
  701. if genericdef.typ=procdef then
  702. prettyname:=tprocdef(genericdef).procsym.prettyname
  703. else
  704. prettyname:=genericdef.typesym.prettyname;
  705. prettyname:=prettyname+'<'+context.prettyname+'>';
  706. generictypelist:=tfphashobjectlist.create(false);
  707. { build the list containing the types for the generic params }
  708. if not assigned(genericdef.genericparas) then
  709. internalerror(2013092601);
  710. if context.genericdeflist.count<>genericdef.genericparas.count then
  711. internalerror(2013092603);
  712. for i:=0 to genericdef.genericparas.Count-1 do
  713. begin
  714. srsym:=tsym(genericdef.genericparas[i]);
  715. if not (sp_generic_para in srsym.symoptions) then
  716. internalerror(2013092602);
  717. generictypelist.add(srsym.realname,tdef(context.genericdeflist[i]).typesym);
  718. end;
  719. { Special case if we are referencing the current defined object }
  720. if assigned(current_structdef) and
  721. (current_structdef.objname^=ufinalspecializename) then
  722. result:=current_structdef;
  723. { Can we reuse an already specialized type? }
  724. { for this first check whether we are currently specializing a nested
  725. type of the current (main) specialization (this is necessary, because
  726. during that time the symbol of the main specialization will still
  727. contain a reference to an errordef) }
  728. if not assigned(result) and assigned(current_specializedef) then
  729. begin
  730. def:=current_specializedef;
  731. repeat
  732. if def.typ in [objectdef,recorddef] then
  733. if tabstractrecorddef(def).objname^=ufinalspecializename then begin
  734. result:=def;
  735. break;
  736. end;
  737. if assigned(def.owner) then
  738. def:=tstoreddef(def.owner.defowner)
  739. else
  740. { this can happen when specializing a generic function }
  741. def:=nil;
  742. until not assigned(def) or not (df_specialization in def.defoptions);
  743. end;
  744. { if the genericdef is the def we are currently parsing (or one of its parents) then we can
  745. not use it for specializing as the tokenbuffer is not yet set (and we aren't done with
  746. parsing anyway), so for now we treat those still as generic defs without doing a partial
  747. specialization }
  748. if not assigned(result) then
  749. begin
  750. def:=current_genericdef;
  751. while assigned(def) and (def.typ in [recorddef,objectdef]) do
  752. begin
  753. if (df_generic in def.defoptions) and (def=genericdef) then
  754. begin
  755. result:=def;
  756. break;
  757. end;
  758. { the following happens when a routine with its parent struct
  759. as parameter is specialized as a parameter or result of a
  760. generic function }
  761. if (df_specialization in def.defoptions) and (tstoreddef(def).genericdef=genericdef) then
  762. begin
  763. if tstoreddef(def).genericparas.count=generictypelist.count then
  764. begin
  765. allequal:=true;
  766. for i:=0 to generictypelist.count-1 do
  767. begin
  768. if not equal_defs(ttypesym(generictypelist[i]).typedef,ttypesym(tstoreddef(def).genericparas[i]).typedef) then
  769. begin
  770. allequal:=false;
  771. break;
  772. end;
  773. end;
  774. if allequal then
  775. begin
  776. result:=def;
  777. break;
  778. end;
  779. end;
  780. end;
  781. def:=tstoreddef(def.owner.defowner);
  782. end;
  783. end;
  784. { decide in which symtable to put the specialization }
  785. if parse_generic and not assigned(result) then
  786. begin
  787. if not assigned(current_genericdef) then
  788. internalerror(2014050901);
  789. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) then
  790. { if we are parsing the definition of a method we specialize into
  791. the local symtable of it }
  792. specializest:=current_procinfo.procdef.getsymtable(gs_local)
  793. else
  794. { we specialize the partial specialization into the symtable of the currently parsed
  795. generic }
  796. case current_genericdef.typ of
  797. procvardef:
  798. specializest:=current_genericdef.getsymtable(gs_para);
  799. procdef:
  800. specializest:=current_genericdef.getsymtable(gs_local);
  801. objectdef,
  802. recorddef:
  803. specializest:=current_genericdef.getsymtable(gs_record);
  804. arraydef:
  805. specializest:=tarraydef(current_genericdef).symtable;
  806. else
  807. internalerror(2014050902);
  808. end;
  809. end
  810. else
  811. if current_module.is_unit and current_module.in_interface then
  812. specializest:=current_module.globalsymtable
  813. else
  814. specializest:=current_module.localsymtable;
  815. if not assigned(specializest) then
  816. internalerror(2014050910);
  817. { now check whether there is a specialization somewhere else }
  818. psym:=nil;
  819. if not assigned(result) then
  820. begin
  821. hashedid.id:=ufinalspecializename;
  822. srsym:=tsym(specializest.findwithhash(hashedid));
  823. if assigned(srsym) then
  824. begin
  825. retrieve_genericdef_or_procsym(srsym,result,psym);
  826. end
  827. else
  828. { the generic could have been specialized in the globalsymtable
  829. already, so search there as well }
  830. if (specializest<>current_module.globalsymtable) and assigned(current_module.globalsymtable) then
  831. begin
  832. srsym:=tsym(current_module.globalsymtable.findwithhash(hashedid));
  833. if assigned(srsym) then
  834. begin
  835. retrieve_genericdef_or_procsym(srsym,result,psym);
  836. end;
  837. end;
  838. end;
  839. if not assigned(result) then
  840. begin
  841. specialization_init(genericdef,state);
  842. { push a temporary global symtable so that the specialization is
  843. added to the correct symtable; this symtable does not contain
  844. any other symbols, so that the type resolution can not be
  845. influenced by symbols in the current unit }
  846. tempst:=tspecializesymtable.create(current_module.modulename^,current_module.moduleid);
  847. symtablestack.push(tempst);
  848. { Reparse the original type definition }
  849. begin
  850. old_current_specializedef:=nil;
  851. old_current_genericdef:=nil;
  852. old_current_structdef:=nil;
  853. old_current_procinfo:=current_procinfo;
  854. current_procinfo:=nil;
  855. if parse_class_parent then
  856. begin
  857. old_current_structdef:=current_structdef;
  858. old_current_genericdef:=current_genericdef;
  859. old_current_specializedef:=current_specializedef;
  860. if genericdef.owner.symtabletype in [recordsymtable,objectsymtable] then
  861. current_structdef:=tabstractrecorddef(genericdef.owner.defowner)
  862. else
  863. current_structdef:=nil;
  864. current_genericdef:=nil;
  865. current_specializedef:=nil;
  866. end;
  867. maybe_add_waiting_unit(genericdef);
  868. { First a new sym so we can reuse this specialization and
  869. references to this specialization can be handled }
  870. if genericdef.typ=procdef then
  871. srsym:=cprocsym.create(finalspecializename)
  872. else
  873. srsym:=ctypesym.create(finalspecializename,generrordef,true);
  874. { insert the symbol only if we don't know already that we have
  875. a procsym to add it to }
  876. if not assigned(psym) then
  877. specializest.insert(srsym);
  878. { specializations are declarations as such it is the wisest to
  879. declare set the blocktype to "type"; otherwise we'll
  880. experience unexpected side effects like the addition of
  881. classrefdefs if we have a generic that's derived from another
  882. generic }
  883. old_block_type:=block_type;
  884. block_type:=bt_type;
  885. if (
  886. (genericdef.typ=procdef) and
  887. not assigned(tprocdef(genericdef).genericdecltokenbuf)
  888. ) or (
  889. (genericdef.typ<>procdef) and
  890. not assigned(genericdef.generictokenbuf)
  891. ) then
  892. internalerror(200511171);
  893. hmodule:=find_module_from_symtable(genericdef.owner);
  894. if hmodule=nil then
  895. internalerror(2012051202);
  896. oldcurrent_filepos:=current_filepos;
  897. { use the index the module got from the current compilation process }
  898. current_filepos.moduleindex:=hmodule.unit_index;
  899. current_tokenpos:=current_filepos;
  900. if parse_generic then
  901. begin
  902. recordbuf:=current_scanner.recordtokenbuf;
  903. current_scanner.recordtokenbuf:=nil;
  904. end
  905. else
  906. recordbuf:=nil;
  907. replaydepth:=current_scanner.replay_stack_depth;
  908. if genericdef.typ=procdef then
  909. begin
  910. current_scanner.startreplaytokens(tprocdef(genericdef).genericdecltokenbuf);
  911. parse_proc_head(tprocdef(genericdef).struct,tprocdef(genericdef).proctypeoption,false,genericdef,generictypelist,pd);
  912. if assigned(pd) then
  913. begin
  914. if assigned(psym) then
  915. pd.procsym:=psym
  916. else
  917. pd.procsym:=srsym;
  918. parse_proc_dec_finish(pd,po_classmethod in tprocdef(genericdef).procoptions);
  919. end;
  920. result:=pd;
  921. end
  922. else
  923. begin
  924. current_scanner.startreplaytokens(genericdef.generictokenbuf);
  925. hadtypetoken:=false;
  926. read_named_type(result,srsym,genericdef,generictypelist,false,hadtypetoken);
  927. ttypesym(srsym).typedef:=result;
  928. result.typesym:=srsym;
  929. if _prettyname<>'' then
  930. ttypesym(result.typesym).fprettyname:=_prettyname
  931. else
  932. ttypesym(result.typesym).fprettyname:=prettyname;
  933. end;
  934. current_filepos:=oldcurrent_filepos;
  935. { Note regarding hint directives:
  936. There is no need to remove the flags for them from the
  937. specialized generic symbol, because hint directives that
  938. follow the specialization are handled by the code in
  939. pdecl.types_dec and added to the type symbol.
  940. E.g.: TFoo = TBar<Blubb> deprecated;
  941. Here the symbol TBar$1$Blubb will contain the
  942. "sp_hint_deprecated" flag while the TFoo symbol won't.}
  943. case result.typ of
  944. { Build VMT indexes for classes and read hint directives }
  945. objectdef:
  946. begin
  947. if replaydepth>current_scanner.replay_stack_depth then
  948. begin
  949. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  950. if replaydepth>current_scanner.replay_stack_depth then
  951. consume(_SEMICOLON);
  952. end;
  953. vmtbuilder:=TVMTBuilder.Create(tobjectdef(result));
  954. vmtbuilder.generate_vmt;
  955. vmtbuilder.free;
  956. end;
  957. { handle params, calling convention, etc }
  958. procvardef:
  959. begin
  960. hintsprocessed:=false;
  961. if replaydepth>current_scanner.replay_stack_depth then
  962. begin
  963. if not check_proc_directive(true) then
  964. begin
  965. hintsprocessed:=try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  966. if replaydepth>current_scanner.replay_stack_depth then
  967. consume(_SEMICOLON);
  968. end
  969. else
  970. hintsprocessed:=true;
  971. end;
  972. if replaydepth>current_scanner.replay_stack_depth then
  973. parse_var_proc_directives(ttypesym(srsym));
  974. handle_calling_convention(tprocvardef(result));
  975. if not hintsprocessed and (replaydepth>current_scanner.replay_stack_depth) then
  976. begin
  977. try_consume_hintdirective(ttypesym(srsym).symoptions,ttypesym(srsym).deprecatedmsg);
  978. if replaydepth>current_scanner.replay_stack_depth then
  979. consume(_SEMICOLON);
  980. end;
  981. end;
  982. procdef:
  983. begin
  984. pdflags:=[pd_body,pd_implemen];
  985. if genericdef.owner.symtabletype=objectsymtable then
  986. include(pdflags,pd_object)
  987. else if genericdef.owner.symtabletype=recordsymtable then
  988. include(pdflags,pd_record);
  989. parse_proc_directives(pd,pdflags);
  990. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  991. consume(_SEMICOLON);
  992. handle_calling_convention(tprocdef(result),hcc_all);
  993. proc_add_definition(tprocdef(result));
  994. { for partial specializations we implicitely declare the routine as
  995. having its implementation although we'll not specialize it in reality }
  996. if parse_generic then
  997. unset_forwarddef(result);
  998. end;
  999. else
  1000. { parse hint directives for records and arrays }
  1001. if replaydepth>current_scanner.replay_stack_depth then begin
  1002. try_consume_hintdirective(srsym.symoptions,srsym.deprecatedmsg);
  1003. if replaydepth>current_scanner.replay_stack_depth then
  1004. consume(_SEMICOLON);
  1005. end;
  1006. end;
  1007. { Consume the remainder of the buffer }
  1008. while current_scanner.replay_stack_depth>replaydepth do
  1009. consume(token);
  1010. if assigned(recordbuf) then
  1011. begin
  1012. if assigned(current_scanner.recordtokenbuf) then
  1013. internalerror(2014050909);
  1014. current_scanner.recordtokenbuf:=recordbuf;
  1015. end;
  1016. block_type:=old_block_type;
  1017. current_procinfo:=old_current_procinfo;
  1018. if parse_class_parent then
  1019. begin
  1020. current_structdef:=old_current_structdef;
  1021. current_genericdef:=old_current_genericdef;
  1022. current_specializedef:=old_current_specializedef;
  1023. end;
  1024. end;
  1025. { extract all created symbols and defs from the temporary symtable
  1026. and add them to the specializest }
  1027. for i:=tempst.SymList.Count-1 downto 0 do
  1028. begin
  1029. item:=tempst.SymList.Items[i];
  1030. { using changeowner the symbol is automatically added to the
  1031. new symtable }
  1032. tsym(item).ChangeOwner(specializest);
  1033. end;
  1034. for i:=tempst.DefList.Count-1 downto 0 do
  1035. begin
  1036. item:=tempst.DefList.Items[i];
  1037. { using changeowner the def is automatically added to the new
  1038. symtable }
  1039. tdef(item).ChangeOwner(specializest);
  1040. { for partial specializations we implicitely declare any methods as having their
  1041. implementations although we'll not specialize them in reality }
  1042. if parse_generic then
  1043. unset_forwarddef(tdef(item));
  1044. end;
  1045. { if a generic was declared during the specialization we need to
  1046. flag the specialize symtable accordingly }
  1047. if sto_has_generic in tempst.tableoptions then
  1048. specializest.includeoption(sto_has_generic);
  1049. tempst.free;
  1050. specialization_done(state);
  1051. { procdefs are only added once we know which overload we use }
  1052. if not parse_generic and (result.typ<>procdef) then
  1053. current_module.pendingspecializations.add(result.typename,result);
  1054. end;
  1055. generictypelist.free;
  1056. if assigned(genericdef) then
  1057. begin
  1058. { check the hints of the found generic symbol }
  1059. if genericdef.typ=procdef then
  1060. srsym:=tprocdef(genericdef).procsym
  1061. else
  1062. srsym:=genericdef.typesym;
  1063. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1064. end;
  1065. end;
  1066. procedure generate_specialization(var tt:tdef;parse_class_parent:boolean;_prettyname:string;parsedtype:tdef;symname:string;parsedpos:tfileposinfo);
  1067. var
  1068. context : tspecializationcontext;
  1069. genericdef : tstoreddef;
  1070. begin
  1071. genericdef:=tstoreddef(generate_specialization_phase1(context,tt,parsedtype,symname,parsedpos));
  1072. if genericdef<>generrordef then
  1073. genericdef:=tstoreddef(generate_specialization_phase2(context,genericdef,parse_class_parent,_prettyname));
  1074. tt:=genericdef;
  1075. if assigned(context) then
  1076. context.free;
  1077. end;
  1078. function parse_generic_parameters(allowconstraints:boolean):tfphashobjectlist;
  1079. var
  1080. generictype : ttypesym;
  1081. i,firstidx : longint;
  1082. srsymtable : tsymtable;
  1083. basedef,def : tdef;
  1084. defname : tidstring;
  1085. allowconstructor,
  1086. doconsume : boolean;
  1087. constraintdata : tgenericconstraintdata;
  1088. old_block_type : tblock_type;
  1089. begin
  1090. result:=tfphashobjectlist.create(false);
  1091. firstidx:=0;
  1092. old_block_type:=block_type;
  1093. block_type:=bt_type;
  1094. repeat
  1095. if token=_ID then
  1096. begin
  1097. generictype:=ctypesym.create(orgpattern,cundefinedtype,false);
  1098. { type parameters need to be added as strict private }
  1099. generictype.visibility:=vis_strictprivate;
  1100. include(generictype.symoptions,sp_generic_para);
  1101. result.add(orgpattern,generictype);
  1102. end;
  1103. consume(_ID);
  1104. if try_to_consume(_COLON) then
  1105. begin
  1106. if not allowconstraints then
  1107. { TODO }
  1108. Message(parser_e_illegal_expression{ parser_e_generic_constraints_not_allowed_here});
  1109. { construct a name which can be used for a type specification }
  1110. constraintdata:=tgenericconstraintdata.create;
  1111. defname:='';
  1112. str(current_module.deflist.count,defname);
  1113. defname:='$gendef'+defname;
  1114. allowconstructor:=m_delphi in current_settings.modeswitches;
  1115. basedef:=generrordef;
  1116. repeat
  1117. doconsume:=true;
  1118. case token of
  1119. _CONSTRUCTOR:
  1120. begin
  1121. if not allowconstructor or (gcf_constructor in constraintdata.flags) then
  1122. Message(parser_e_illegal_expression);
  1123. include(constraintdata.flags,gcf_constructor);
  1124. allowconstructor:=false;
  1125. end;
  1126. _CLASS:
  1127. begin
  1128. if gcf_class in constraintdata.flags then
  1129. Message(parser_e_illegal_expression);
  1130. if basedef=generrordef then
  1131. include(constraintdata.flags,gcf_class)
  1132. else
  1133. Message(parser_e_illegal_expression);
  1134. end;
  1135. _RECORD:
  1136. begin
  1137. if ([gcf_constructor,gcf_class]*constraintdata.flags<>[])
  1138. or (constraintdata.interfaces.count>0) then
  1139. Message(parser_e_illegal_expression)
  1140. else
  1141. begin
  1142. srsymtable:=trecordsymtable.create(defname,0,1,1);
  1143. basedef:=crecorddef.create(defname,srsymtable);
  1144. include(constraintdata.flags,gcf_record);
  1145. allowconstructor:=false;
  1146. end;
  1147. end;
  1148. else
  1149. begin
  1150. { after single_type "token" is the trailing ",", ";" or
  1151. ">"! }
  1152. doconsume:=false;
  1153. { def is already set to a class or record }
  1154. if gcf_record in constraintdata.flags then
  1155. Message(parser_e_illegal_expression);
  1156. single_type(def, [stoAllowSpecialization]);
  1157. { only types that are inheritable are allowed }
  1158. if (def.typ<>objectdef) or
  1159. not (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_interfacejava,odt_javaclass]) then
  1160. Message1(type_e_class_or_interface_type_expected,def.typename)
  1161. else
  1162. case tobjectdef(def).objecttype of
  1163. odt_class,
  1164. odt_javaclass:
  1165. begin
  1166. if gcf_class in constraintdata.flags then
  1167. { "class" + concrete class is not allowed }
  1168. Message(parser_e_illegal_expression)
  1169. else
  1170. { do we already have a concrete class? }
  1171. if basedef<>generrordef then
  1172. Message(parser_e_illegal_expression)
  1173. else
  1174. basedef:=def;
  1175. end;
  1176. odt_interfacecom,
  1177. odt_interfacecorba,
  1178. odt_interfacejava,
  1179. odt_dispinterface:
  1180. constraintdata.interfaces.add(def);
  1181. end;
  1182. end;
  1183. end;
  1184. if doconsume then
  1185. consume(token);
  1186. until not try_to_consume(_COMMA);
  1187. if ([gcf_class,gcf_constructor]*constraintdata.flags<>[]) or
  1188. (constraintdata.interfaces.count>1) or
  1189. (
  1190. (basedef.typ=objectdef) and
  1191. (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class])
  1192. ) then
  1193. begin
  1194. if basedef.typ=errordef then
  1195. { don't pass an errordef as a parent to a tobjectdef }
  1196. basedef:=class_tobject
  1197. else
  1198. if (basedef.typ<>objectdef) or
  1199. not (tobjectdef(basedef).objecttype in [odt_javaclass,odt_class]) then
  1200. internalerror(2012101101);
  1201. basedef:=cobjectdef.create(tobjectdef(basedef).objecttype,defname,tobjectdef(basedef),false);
  1202. for i:=0 to constraintdata.interfaces.count-1 do
  1203. tobjectdef(basedef).implementedinterfaces.add(
  1204. timplementedinterface.create(tobjectdef(constraintdata.interfaces[i])));
  1205. end
  1206. else
  1207. if constraintdata.interfaces.count=1 then
  1208. begin
  1209. if basedef.typ<>errordef then
  1210. internalerror(2013021601);
  1211. def:=tdef(constraintdata.interfaces[0]);
  1212. basedef:=cobjectdef.create(tobjectdef(def).objecttype,defname,tobjectdef(def),false);
  1213. constraintdata.interfaces.delete(0);
  1214. end;
  1215. if basedef.typ<>errordef then
  1216. with tstoreddef(basedef) do
  1217. begin
  1218. genconstraintdata:=tgenericconstraintdata.create;
  1219. genconstraintdata.flags:=constraintdata.flags;
  1220. genconstraintdata.interfaces.assign(constraintdata.interfaces);
  1221. include(defoptions,df_genconstraint);
  1222. end;
  1223. for i:=firstidx to result.count-1 do
  1224. ttypesym(result[i]).typedef:=basedef;
  1225. { we need a typesym in case we do a Delphi-mode inline
  1226. specialization with this parameter; so just use the first sym }
  1227. if not assigned(basedef.typesym) then
  1228. basedef.typesym:=ttypesym(result[firstidx]);
  1229. firstidx:=result.count;
  1230. constraintdata.free;
  1231. end
  1232. else
  1233. begin
  1234. if token=_SEMICOLON then
  1235. begin
  1236. { two different typeless parameters are considered as incompatible }
  1237. for i:=firstidx to result.count-1 do
  1238. begin
  1239. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1240. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1241. end;
  1242. { a semicolon terminates a type parameter group }
  1243. firstidx:=result.count;
  1244. end;
  1245. end;
  1246. until not (try_to_consume(_COMMA) or try_to_consume(_SEMICOLON));
  1247. { two different typeless parameters are considered as incompatible }
  1248. for i:=firstidx to result.count-1 do
  1249. begin
  1250. ttypesym(result[i]).typedef:=cundefineddef.create(false);
  1251. ttypesym(result[i]).typedef.typesym:=ttypesym(result[i]);
  1252. end;
  1253. block_type:=old_block_type;
  1254. end;
  1255. procedure insert_generic_parameter_types(def:tstoreddef;genericdef:tstoreddef;genericlist:tfphashobjectlist);
  1256. var
  1257. i : longint;
  1258. generictype,sym : ttypesym;
  1259. st : tsymtable;
  1260. begin
  1261. def.genericdef:=genericdef;
  1262. if not assigned(genericlist) then
  1263. exit;
  1264. if assigned(genericdef) then
  1265. include(def.defoptions,df_specialization)
  1266. else
  1267. if genericlist.count>0 then
  1268. include(def.defoptions,df_generic);
  1269. case def.typ of
  1270. recorddef,objectdef: st:=tabstractrecorddef(def).symtable;
  1271. arraydef: st:=tarraydef(def).symtable;
  1272. procvardef,procdef: st:=tabstractprocdef(def).parast;
  1273. else
  1274. internalerror(201101020);
  1275. end;
  1276. if (genericlist.count>0) and not assigned(def.genericparas) then
  1277. def.genericparas:=tfphashobjectlist.create(false);
  1278. for i:=0 to genericlist.count-1 do
  1279. begin
  1280. generictype:=ttypesym(genericlist[i]);
  1281. if assigned(generictype.owner) then
  1282. begin
  1283. sym:=ctypesym.create(genericlist.nameofindex(i),generictype.typedef,true);
  1284. { type parameters need to be added as strict private }
  1285. sym.visibility:=vis_strictprivate;
  1286. st.insert(sym);
  1287. include(sym.symoptions,sp_generic_para);
  1288. end
  1289. else
  1290. begin
  1291. if (generictype.typedef.typ=undefineddef) and (generictype.typedef<>cundefinedtype) then
  1292. begin
  1293. { the generic parameters were parsed before the genericdef existed thus the
  1294. undefineddefs were added as part of the parent symtable }
  1295. if assigned(generictype.typedef.owner) then
  1296. generictype.typedef.owner.DefList.Extract(generictype.typedef);
  1297. generictype.typedef.changeowner(st);
  1298. end;
  1299. st.insert(generictype);
  1300. include(generictype.symoptions,sp_generic_para);
  1301. end;
  1302. def.genericparas.add(genericlist.nameofindex(i),generictype);
  1303. end;
  1304. end;
  1305. procedure maybe_insert_generic_rename_symbol(const name:tidstring;genericlist:tfphashobjectlist);
  1306. var
  1307. gensym : ttypesym;
  1308. begin
  1309. { for generics in non-Delphi modes we insert a private type symbol
  1310. that has the same base name as the currently parsed generic and
  1311. that references this defs }
  1312. if not (m_delphi in current_settings.modeswitches) and
  1313. (
  1314. (
  1315. parse_generic and
  1316. assigned(genericlist) and
  1317. (genericlist.count>0)
  1318. ) or
  1319. (
  1320. assigned(current_specializedef) and
  1321. assigned(current_structdef.genericdef) and
  1322. (current_structdef.genericdef.typ in [objectdef,recorddef]) and
  1323. (pos('$',name)>0)
  1324. )
  1325. ) then
  1326. begin
  1327. { we need to pass nil as def here, because the constructor wants
  1328. to set the typesym of the def which is not what we want }
  1329. gensym:=ctypesym.create(copy(name,1,pos('$',name)-1),nil,true);
  1330. gensym.typedef:=current_structdef;
  1331. include(gensym.symoptions,sp_internal);
  1332. { the symbol should be only visible to the generic class
  1333. itself }
  1334. gensym.visibility:=vis_strictprivate;
  1335. symtablestack.top.insert(gensym);
  1336. end;
  1337. end;
  1338. function generate_generic_name(const name:tidstring;specializename:ansistring;owner_hierarchy:string):tidstring;
  1339. var
  1340. crc : cardinal;
  1341. begin
  1342. if specializename='' then
  1343. internalerror(2012061901);
  1344. { build the new type's name }
  1345. crc:=UpdateCrc32(0,specializename[1],length(specializename));
  1346. result:=name+'$crc'+hexstr(crc,8);
  1347. if owner_hierarchy<>'' then
  1348. begin
  1349. crc:=UpdateCrc32(0,owner_hierarchy[1],length(owner_hierarchy));
  1350. result:=result+'$crc'+hexstr(crc,8);
  1351. end;
  1352. end;
  1353. procedure split_generic_name(const name:tidstring;out nongeneric:string;out count:longint);
  1354. var
  1355. i,code : longint;
  1356. countstr : string;
  1357. begin
  1358. for i:=length(name) downto 1 do
  1359. if name[i]='$' then
  1360. begin
  1361. nongeneric:=copy(name,1,i-1);
  1362. countstr:=copy(name,i+1,length(name)-i);
  1363. val(countstr,count,code);
  1364. if code<>0 then
  1365. break;
  1366. exit;
  1367. end;
  1368. nongeneric:=name;
  1369. count:=0;
  1370. end;
  1371. function resolve_generic_dummysym(const name:tidstring):tsym;
  1372. var
  1373. list : tfpobjectlist;
  1374. begin
  1375. list:=tfpobjectlist(current_module.genericdummysyms.find(name));
  1376. if assigned(list) and (list.count>0) then
  1377. result:=tgenericdummyentry(list.last).resolvedsym
  1378. else
  1379. result:=nil;
  1380. end;
  1381. function could_be_generic(const name:tidstring):boolean;
  1382. begin
  1383. result:=(name<>'') and
  1384. (current_module.genericdummysyms.findindexof(name)>=0);
  1385. end;
  1386. procedure specialization_init(genericdef:tdef;var state: tspecializationstate);
  1387. var
  1388. pu : tused_unit;
  1389. hmodule : tmodule;
  1390. unitsyms : TFPHashObjectList;
  1391. sym : tsym;
  1392. i : Integer;
  1393. begin
  1394. if not assigned(genericdef) then
  1395. internalerror(200705151);
  1396. { Setup symtablestack at definition time
  1397. to get types right, however this is not perfect, we should probably record
  1398. the resolved symbols }
  1399. state.oldsymtablestack:=symtablestack;
  1400. state.oldextendeddefs:=current_module.extendeddefs;
  1401. state.oldgenericdummysyms:=current_module.genericdummysyms;
  1402. current_module.extendeddefs:=TFPHashObjectList.create(true);
  1403. current_module.genericdummysyms:=tfphashobjectlist.create(true);
  1404. symtablestack:=tdefawaresymtablestack.create;
  1405. hmodule:=find_module_from_symtable(genericdef.owner);
  1406. if hmodule=nil then
  1407. internalerror(200705152);
  1408. { collect all unit syms in the generic's unit as we need to establish
  1409. their unitsym.module link again so that unit identifiers can be used }
  1410. unitsyms:=tfphashobjectlist.create(false);
  1411. if (hmodule<>current_module) and assigned(hmodule.globalsymtable) then
  1412. for i:=0 to hmodule.globalsymtable.symlist.count-1 do
  1413. begin
  1414. sym:=tsym(hmodule.globalsymtable.symlist[i]);
  1415. if sym.typ=unitsym then
  1416. unitsyms.add(upper(sym.realname),sym);
  1417. end;
  1418. { add all units if we are specializing inside the current unit (as the
  1419. generic could have been declared in the implementation part), but load
  1420. only interface units, if we are in a different unit as then the generic
  1421. needs to be in the interface section }
  1422. pu:=tused_unit(hmodule.used_units.first);
  1423. while assigned(pu) do
  1424. begin
  1425. if not assigned(pu.u.globalsymtable) then
  1426. { in certain circular, but valid unit constellations it can happen
  1427. that we specialize a generic in a different unit that was used
  1428. in the implementation section of the generic's unit and were the
  1429. interface is still being parsed and thus the localsymtable is in
  1430. reality the global symtable }
  1431. if pu.u.in_interface then
  1432. symtablestack.push(pu.u.localsymtable)
  1433. else
  1434. internalerror(200705153)
  1435. else
  1436. symtablestack.push(pu.u.globalsymtable);
  1437. sym:=tsym(unitsyms.find(pu.u.modulename^));
  1438. if assigned(sym) and not assigned(tunitsym(sym).module) then
  1439. tunitsym(sym).module:=pu.u;
  1440. pu:=tused_unit(pu.next);
  1441. end;
  1442. unitsyms.free;
  1443. if assigned(hmodule.globalsymtable) then
  1444. symtablestack.push(hmodule.globalsymtable);
  1445. { push the localsymtable if needed }
  1446. if ((hmodule<>current_module) or not current_module.in_interface)
  1447. and assigned(hmodule.localsymtable) then
  1448. symtablestack.push(hmodule.localsymtable);
  1449. end;
  1450. procedure specialization_done(var state: tspecializationstate);
  1451. begin
  1452. { Restore symtablestack }
  1453. current_module.extendeddefs.free;
  1454. current_module.extendeddefs:=state.oldextendeddefs;
  1455. current_module.genericdummysyms.free;
  1456. current_module.genericdummysyms:=state.oldgenericdummysyms;
  1457. symtablestack.free;
  1458. symtablestack:=state.oldsymtablestack;
  1459. { clear the state record to be on the safe side }
  1460. fillchar(state, sizeof(state), 0);
  1461. end;
  1462. {****************************************************************************
  1463. SPECIALIZATION BODY GENERATION
  1464. ****************************************************************************}
  1465. procedure process_procdef(def:tprocdef;hmodule:tmodule);
  1466. var
  1467. oldcurrent_filepos : tfileposinfo;
  1468. begin
  1469. if assigned(def.genericdef) and
  1470. (def.genericdef.typ=procdef) and
  1471. assigned(tprocdef(def.genericdef).generictokenbuf) then
  1472. begin
  1473. if not assigned(tprocdef(def.genericdef).generictokenbuf) then
  1474. internalerror(2015061902);
  1475. oldcurrent_filepos:=current_filepos;
  1476. current_filepos:=tprocdef(def.genericdef).fileinfo;
  1477. { use the index the module got from the current compilation process }
  1478. current_filepos.moduleindex:=hmodule.unit_index;
  1479. current_tokenpos:=current_filepos;
  1480. current_scanner.startreplaytokens(tprocdef(def.genericdef).generictokenbuf);
  1481. read_proc_body(def);
  1482. current_filepos:=oldcurrent_filepos;
  1483. end
  1484. { synthetic routines will be implemented afterwards }
  1485. else if def.synthetickind=tsk_none then
  1486. MessagePos1(def.fileinfo,sym_e_forward_not_resolved,def.fullprocname(false));
  1487. end;
  1488. function process_abstractrecorddef(def:tabstractrecorddef):boolean;
  1489. var
  1490. i : longint;
  1491. hp : tdef;
  1492. hmodule : tmodule;
  1493. begin
  1494. result:=true;
  1495. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1496. if hmodule=nil then
  1497. internalerror(201202041);
  1498. for i:=0 to def.symtable.DefList.Count-1 do
  1499. begin
  1500. hp:=tdef(def.symtable.DefList[i]);
  1501. if hp.typ=procdef then
  1502. begin
  1503. { only generate the code if we need a body }
  1504. if assigned(tprocdef(hp).struct) and not tprocdef(hp).forwarddef then
  1505. continue;
  1506. { and the body is available already (which is implicitely the
  1507. case if the generic routine is part of another unit) }
  1508. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and
  1509. { may not be assigned in case it's a synthetic procdef that
  1510. still needs to be generated }
  1511. assigned(tprocdef(hp).genericdef) and
  1512. tprocdef(tprocdef(hp).genericdef).forwarddef then
  1513. begin
  1514. result:=false;
  1515. continue;
  1516. end;
  1517. process_procdef(tprocdef(hp),hmodule);
  1518. end
  1519. else
  1520. if hp.typ in [objectdef,recorddef] then
  1521. { generate code for subtypes as well }
  1522. result:=process_abstractrecorddef(tabstractrecorddef(hp)) and result;
  1523. end;
  1524. end;
  1525. procedure generate_specialization_procs;
  1526. var
  1527. i : longint;
  1528. list,
  1529. readdlist : tfpobjectlist;
  1530. def : tstoreddef;
  1531. state : tspecializationstate;
  1532. hmodule : tmodule;
  1533. begin
  1534. { first copy all entries and then work with that list to ensure that
  1535. we don't get an infinite recursion }
  1536. list:=tfpobjectlist.create(false);
  1537. readdlist:=tfpobjectlist.create(false);
  1538. for i:=0 to current_module.pendingspecializations.Count-1 do
  1539. list.add(current_module.pendingspecializations.Items[i]);
  1540. current_module.pendingspecializations.clear;
  1541. for i:=0 to list.count-1 do
  1542. begin
  1543. def:=tstoreddef(list[i]);
  1544. if not tstoreddef(def).is_specialization then
  1545. continue;
  1546. case def.typ of
  1547. procdef:
  1548. begin
  1549. { the use of forwarddef should not backfire as the
  1550. specialization always belongs to the current module }
  1551. if not tprocdef(def).forwarddef then
  1552. continue;
  1553. if not assigned(def.genericdef) then
  1554. internalerror(2015061903);
  1555. hmodule:=find_module_from_symtable(def.genericdef.owner);
  1556. if hmodule=nil then
  1557. internalerror(2015061904);
  1558. { we need to check for a forward declaration only if the
  1559. generic was declared in the same unit (otherwise there
  1560. should be one) }
  1561. if ((hmodule=current_module) or (hmodule.state=ms_compile)) and tprocdef(def.genericdef).forwarddef then
  1562. begin
  1563. readdlist.add(def);
  1564. continue;
  1565. end;
  1566. specialization_init(tstoreddef(def).genericdef,state);
  1567. process_procdef(tprocdef(def),hmodule);
  1568. specialization_done(state);
  1569. end;
  1570. recorddef,
  1571. objectdef:
  1572. begin
  1573. specialization_init(tstoreddef(def).genericdef,state);
  1574. if not process_abstractrecorddef(tabstractrecorddef(def)) then
  1575. readdlist.add(def);
  1576. specialization_done(state);
  1577. end;
  1578. end;
  1579. end;
  1580. { add those defs back to the pending list for which we don't yet have
  1581. all method bodies }
  1582. for i:=0 to readdlist.count-1 do
  1583. current_module.pendingspecializations.add(tstoreddef(readdlist[i]).typename,readdlist[i]);
  1584. readdlist.free;
  1585. list.free;
  1586. end;
  1587. procedure maybe_add_pending_specialization(def:tdef);
  1588. var
  1589. hmodule : tmodule;
  1590. st : tsymtable;
  1591. begin
  1592. if parse_generic then
  1593. exit;
  1594. st:=def.owner;
  1595. while st.symtabletype in [localsymtable] do
  1596. st:=st.defowner.owner;
  1597. hmodule:=find_module_from_symtable(st);
  1598. if tstoreddef(def).is_specialization and (hmodule=current_module) then
  1599. current_module.pendingspecializations.add(def.typename,def);
  1600. end;
  1601. end.