ncnv.pas 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  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 ncnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,defbase,
  24. nld;
  25. type
  26. ttypeconvnode = class(tunarynode)
  27. totype : ttype;
  28. convtype : tconverttype;
  29. constructor create(node : tnode;const t : ttype);virtual;
  30. constructor create_explicit(node : tnode;const t : ttype);
  31. function getcopy : tnode;override;
  32. function pass_1 : tnode;override;
  33. function det_resulttype:tnode;override;
  34. function docompare(p: tnode) : boolean; override;
  35. private
  36. function resulttype_cord_to_pointer : tnode;
  37. function resulttype_chararray_to_string : tnode;
  38. function resulttype_string_to_chararray : tnode;
  39. function resulttype_string_to_string : tnode;
  40. function resulttype_char_to_string : tnode;
  41. function resulttype_char_to_chararray : tnode;
  42. function resulttype_int_to_real : tnode;
  43. function resulttype_real_to_real : tnode;
  44. function resulttype_cchar_to_pchar : tnode;
  45. function resulttype_cstring_to_pchar : tnode;
  46. function resulttype_char_to_char : tnode;
  47. function resulttype_arrayconstructor_to_set : tnode;
  48. function resulttype_pchar_to_string : tnode;
  49. function resulttype_interface_to_guid : tnode;
  50. function resulttype_dynarray_to_openarray : tnode;
  51. function resulttype_call_helper(c : tconverttype) : tnode;
  52. protected
  53. function first_int_to_int : tnode;virtual;
  54. function first_cstring_to_pchar : tnode;virtual;
  55. function first_string_to_chararray : tnode;virtual;
  56. function first_char_to_string : tnode;virtual;
  57. function first_nothing : tnode;virtual;
  58. function first_array_to_pointer : tnode;virtual;
  59. function first_int_to_real : tnode;virtual;
  60. function first_real_to_real : tnode;virtual;
  61. function first_pointer_to_array : tnode;virtual;
  62. function first_cchar_to_pchar : tnode;virtual;
  63. function first_bool_to_int : tnode;virtual;
  64. function first_int_to_bool : tnode;virtual;
  65. function first_bool_to_bool : tnode;virtual;
  66. function first_proc_to_procvar : tnode;virtual;
  67. function first_load_smallset : tnode;virtual;
  68. function first_cord_to_pointer : tnode;virtual;
  69. function first_ansistring_to_pchar : tnode;virtual;
  70. function first_arrayconstructor_to_set : tnode;virtual;
  71. function first_class_to_intf : tnode;virtual;
  72. function first_char_to_char : tnode;virtual;
  73. function first_call_helper(c : tconverttype) : tnode;
  74. procedure second_int_to_int;virtual;abstract;
  75. procedure second_string_to_string;virtual;abstract;
  76. procedure second_cstring_to_pchar;virtual;abstract;
  77. procedure second_string_to_chararray;virtual;abstract;
  78. procedure second_array_to_pointer;virtual;abstract;
  79. procedure second_pointer_to_array;virtual;abstract;
  80. procedure second_chararray_to_string;virtual;abstract;
  81. procedure second_char_to_string;virtual;abstract;
  82. procedure second_int_to_real;virtual;abstract;
  83. procedure second_real_to_real;virtual;abstract;
  84. procedure second_cord_to_pointer;virtual;abstract;
  85. procedure second_proc_to_procvar;virtual;abstract;
  86. procedure second_bool_to_int;virtual;abstract;
  87. procedure second_int_to_bool;virtual;abstract;
  88. procedure second_bool_to_bool;virtual;abstract;
  89. procedure second_load_smallset;virtual;abstract;
  90. procedure second_ansistring_to_pchar;virtual;abstract;
  91. procedure second_pchar_to_string;virtual;abstract;
  92. procedure second_class_to_intf;virtual;abstract;
  93. procedure second_char_to_char;virtual;abstract;
  94. procedure second_nothing; virtual;abstract;
  95. end;
  96. ttypeconvnodeclass = class of ttypeconvnode;
  97. tasnode = class(tbinarynode)
  98. constructor create(l,r : tnode);virtual;
  99. function pass_1 : tnode;override;
  100. function det_resulttype:tnode;override;
  101. end;
  102. tasnodeclass = class of tasnode;
  103. tisnode = class(tbinarynode)
  104. constructor create(l,r : tnode);virtual;
  105. function pass_1 : tnode;override;
  106. function det_resulttype:tnode;override;
  107. procedure pass_2;override;
  108. end;
  109. tisnodeclass = class of tisnode;
  110. var
  111. ctypeconvnode : ttypeconvnodeclass;
  112. casnode : tasnodeclass;
  113. cisnode : tisnodeclass;
  114. procedure inserttypeconv(var p:tnode;const t:ttype);
  115. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  116. procedure arrayconstructor_to_set(var p : tnode);
  117. implementation
  118. uses
  119. globtype,systems,tokens,
  120. cutils,verbose,globals,widestr,
  121. symconst,symdef,symsym,symtable,
  122. ncon,ncal,nset,nadd,ninl,nmem,nmat,
  123. cgbase,
  124. htypechk,pass_1,cpubase,cpuinfo;
  125. {*****************************************************************************
  126. Helpers
  127. *****************************************************************************}
  128. procedure inserttypeconv(var p:tnode;const t:ttype);
  129. begin
  130. if not assigned(p.resulttype.def) then
  131. begin
  132. resulttypepass(p);
  133. if codegenerror then
  134. exit;
  135. end;
  136. { don't insert obsolete type conversions }
  137. if is_equal(p.resulttype.def,t.def) and
  138. not ((p.resulttype.def.deftype=setdef) and
  139. (tsetdef(p.resulttype.def).settype <>
  140. tsetdef(t.def).settype)) then
  141. begin
  142. p.resulttype:=t;
  143. end
  144. else
  145. begin
  146. p:=ctypeconvnode.create(p,t);
  147. resulttypepass(p);
  148. end;
  149. end;
  150. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  151. begin
  152. if not assigned(p.resulttype.def) then
  153. begin
  154. resulttypepass(p);
  155. if codegenerror then
  156. exit;
  157. end;
  158. { don't insert obsolete type conversions }
  159. if is_equal(p.resulttype.def,t.def) and
  160. not ((p.resulttype.def.deftype=setdef) and
  161. (tsetdef(p.resulttype.def).settype <>
  162. tsetdef(t.def).settype)) then
  163. begin
  164. p.resulttype:=t;
  165. end
  166. else
  167. begin
  168. p:=ctypeconvnode.create_explicit(p,t);
  169. resulttypepass(p);
  170. end;
  171. end;
  172. {*****************************************************************************
  173. Array constructor to Set Conversion
  174. *****************************************************************************}
  175. procedure arrayconstructor_to_set(var p : tnode);
  176. var
  177. constp : tsetconstnode;
  178. buildp,
  179. p2,p3,p4 : tnode;
  180. htype : ttype;
  181. constset : Pconstset;
  182. constsetlo,
  183. constsethi : longint;
  184. procedure update_constsethi(t:ttype);
  185. begin
  186. if ((t.def.deftype=orddef) and
  187. (torddef(t.def).high>=constsethi)) then
  188. begin
  189. constsethi:=torddef(t.def).high;
  190. if htype.def=nil then
  191. begin
  192. if (constsethi>255) or
  193. (torddef(t.def).low<0) then
  194. htype:=u8bittype
  195. else
  196. htype:=t;
  197. end;
  198. if constsethi>255 then
  199. constsethi:=255;
  200. end
  201. else if ((t.def.deftype=enumdef) and
  202. (tenumdef(t.def).max>=constsethi)) then
  203. begin
  204. if htype.def=nil then
  205. htype:=t;
  206. constsethi:=tenumdef(t.def).max;
  207. end;
  208. end;
  209. procedure do_set(pos : longint);
  210. begin
  211. if (pos and not $ff)<>0 then
  212. Message(parser_e_illegal_set_expr);
  213. if pos>constsethi then
  214. constsethi:=pos;
  215. if pos<constsetlo then
  216. constsetlo:=pos;
  217. include(constset^,pos);
  218. end;
  219. var
  220. l : Longint;
  221. lr,hr : TConstExprInt;
  222. hp : tarrayconstructornode;
  223. begin
  224. if p.nodetype<>arrayconstructorn then
  225. internalerror(200205105);
  226. new(constset);
  227. constset^:=[];
  228. htype.reset;
  229. constsetlo:=0;
  230. constsethi:=0;
  231. constp:=csetconstnode.create(nil,htype);
  232. constp.value_set:=constset;
  233. buildp:=constp;
  234. hp:=tarrayconstructornode(p);
  235. if assigned(hp.left) then
  236. begin
  237. while assigned(hp) do
  238. begin
  239. p4:=nil; { will contain the tree to create the set }
  240. {split a range into p2 and p3 }
  241. if hp.left.nodetype=arrayconstructorrangen then
  242. begin
  243. p2:=tarrayconstructorrangenode(hp.left).left;
  244. p3:=tarrayconstructorrangenode(hp.left).right;
  245. tarrayconstructorrangenode(hp.left).left:=nil;
  246. tarrayconstructorrangenode(hp.left).right:=nil;
  247. end
  248. else
  249. begin
  250. p2:=hp.left;
  251. hp.left:=nil;
  252. p3:=nil;
  253. end;
  254. resulttypepass(p2);
  255. if assigned(p3) then
  256. resulttypepass(p3);
  257. if codegenerror then
  258. break;
  259. case p2.resulttype.def.deftype of
  260. enumdef,
  261. orddef:
  262. begin
  263. getrange(p2.resulttype.def,lr,hr);
  264. if assigned(p3) then
  265. begin
  266. { this isn't good, you'll get problems with
  267. type t010 = 0..10;
  268. ts = set of t010;
  269. var s : ts;b : t010
  270. begin s:=[1,2,b]; end.
  271. if is_integer(p3^.resulttype.def) then
  272. begin
  273. inserttypeconv(p3,u8bitdef);
  274. end;
  275. }
  276. if assigned(htype.def) and not(is_equal(htype.def,p3.resulttype.def)) then
  277. begin
  278. aktfilepos:=p3.fileinfo;
  279. CGMessage(type_e_typeconflict_in_set);
  280. end
  281. else
  282. begin
  283. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  284. begin
  285. if not(is_integer(p3.resulttype.def)) then
  286. htype:=p3.resulttype
  287. else
  288. begin
  289. inserttypeconv(p3,u8bittype);
  290. inserttypeconv(p2,u8bittype);
  291. end;
  292. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  293. do_set(l);
  294. p2.free;
  295. p3.free;
  296. end
  297. else
  298. begin
  299. update_constsethi(p2.resulttype);
  300. inserttypeconv(p2,htype);
  301. update_constsethi(p3.resulttype);
  302. inserttypeconv(p3,htype);
  303. if assigned(htype.def) then
  304. inserttypeconv(p3,htype)
  305. else
  306. inserttypeconv(p3,u8bittype);
  307. p4:=csetelementnode.create(p2,p3);
  308. end;
  309. end;
  310. end
  311. else
  312. begin
  313. { Single value }
  314. if p2.nodetype=ordconstn then
  315. begin
  316. if not(is_integer(p2.resulttype.def)) then
  317. update_constsethi(p2.resulttype)
  318. else
  319. inserttypeconv(p2,u8bittype);
  320. do_set(tordconstnode(p2).value);
  321. p2.free;
  322. end
  323. else
  324. begin
  325. update_constsethi(p2.resulttype);
  326. if assigned(htype.def) then
  327. inserttypeconv(p2,htype)
  328. else
  329. inserttypeconv(p2,u8bittype);
  330. p4:=csetelementnode.create(p2,nil);
  331. end;
  332. end;
  333. end;
  334. stringdef :
  335. begin
  336. { if we've already set elements which are constants }
  337. { throw an error }
  338. if ((htype.def=nil) and assigned(buildp)) or
  339. not(is_char(htype.def)) then
  340. CGMessage(type_e_typeconflict_in_set)
  341. else
  342. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  343. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  344. if htype.def=nil then
  345. htype:=cchartype;
  346. p2.free;
  347. end;
  348. else
  349. CGMessage(type_e_ordinal_expr_expected);
  350. end;
  351. { insert the set creation tree }
  352. if assigned(p4) then
  353. buildp:=caddnode.create(addn,buildp,p4);
  354. { load next and dispose current node }
  355. p2:=hp;
  356. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  357. tarrayconstructornode(p2).right:=nil;
  358. p2.free;
  359. end;
  360. if (htype.def=nil) then
  361. htype:=u8bittype;
  362. end
  363. else
  364. begin
  365. { empty set [], only remove node }
  366. p.free;
  367. end;
  368. { set the initial set type }
  369. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  370. { determine the resulttype for the tree }
  371. resulttypepass(buildp);
  372. { set the new tree }
  373. p:=buildp;
  374. end;
  375. {*****************************************************************************
  376. TTYPECONVNODE
  377. *****************************************************************************}
  378. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  379. begin
  380. inherited create(typeconvn,node);
  381. convtype:=tc_not_possible;
  382. totype:=t;
  383. if t.def=nil then
  384. internalerror(200103281);
  385. set_file_line(node);
  386. end;
  387. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  388. begin
  389. self.create(node,t);
  390. toggleflag(nf_explizit);
  391. end;
  392. function ttypeconvnode.getcopy : tnode;
  393. var
  394. n : ttypeconvnode;
  395. begin
  396. n:=ttypeconvnode(inherited getcopy);
  397. n.convtype:=convtype;
  398. getcopy:=n;
  399. end;
  400. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  401. var
  402. t : tnode;
  403. begin
  404. result:=nil;
  405. if left.nodetype=ordconstn then
  406. begin
  407. { check if we have a valid pointer constant (JM) }
  408. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  409. if (sizeof(TConstPtrUInt) = 4) then
  410. begin
  411. if (tordconstnode(left).value < low(longint)) or
  412. (tordconstnode(left).value > high(cardinal)) then
  413. CGMessage(parser_e_range_check_error);
  414. end
  415. else if (sizeof(TConstPtrUInt) = 8) then
  416. begin
  417. if (tordconstnode(left).value < low(int64)) or
  418. (tordconstnode(left).value > high(qword)) then
  419. CGMessage(parser_e_range_check_error);
  420. end
  421. else
  422. internalerror(2001020801);
  423. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  424. result:=t;
  425. end
  426. else
  427. internalerror(200104023);
  428. end;
  429. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  430. begin
  431. result := ccallnode.createinternres(
  432. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  433. ccallparanode.create(left,nil),resulttype);
  434. left := nil;
  435. end;
  436. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  437. var
  438. arrsize: longint;
  439. begin
  440. with tarraydef(resulttype.def) do
  441. begin
  442. if highrange<lowrange then
  443. internalerror(75432653);
  444. arrsize := highrange-lowrange+1;
  445. end;
  446. if (left.nodetype = stringconstn) and
  447. { left.length+1 since there's always a terminating #0 character (JM) }
  448. (tstringconstnode(left).len+1 >= arrsize) and
  449. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  450. begin
  451. { handled separately }
  452. result := nil;
  453. exit;
  454. end;
  455. result := ccallnode.createinternres(
  456. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  457. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  458. cordconstnode.create(arrsize,s32bittype),nil)),resulttype);
  459. left := nil;
  460. end;
  461. function ttypeconvnode.resulttype_string_to_string : tnode;
  462. var
  463. procname: string[31];
  464. stringpara : tcallparanode;
  465. pw : pcompilerwidestring;
  466. pc : pchar;
  467. begin
  468. result:=nil;
  469. if left.nodetype=stringconstn then
  470. begin
  471. { convert ascii 2 unicode }
  472. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  473. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  474. begin
  475. initwidestring(pw);
  476. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  477. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  478. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  479. end
  480. else
  481. { convert unicode 2 ascii }
  482. if (tstringconstnode(left).st_type=st_widestring) and
  483. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  484. begin
  485. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  486. getmem(pc,getlengthwidestring(pw)+1);
  487. unicode2ascii(pw,pc);
  488. donewidestring(pw);
  489. tstringconstnode(left).value_str:=pc;
  490. end;
  491. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  492. tstringconstnode(left).resulttype:=resulttype;
  493. result:=left;
  494. left:=nil;
  495. end
  496. else
  497. begin
  498. { get the correct procedure name }
  499. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  500. '_to_'+tstringdef(resulttype.def).stringtypname;
  501. { create parameter (and remove left node from typeconvnode }
  502. { since it's reused as parameter) }
  503. stringpara := ccallparanode.create(left,nil);
  504. left := nil;
  505. { when converting to shortstrings, we have to pass high(destination) too }
  506. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  507. stringpara.right := ccallparanode.create(cinlinenode.create(
  508. in_high_x,false,self.getcopy),nil);
  509. { and create the callnode }
  510. result := ccallnode.createinternres(procname,stringpara,resulttype);
  511. end;
  512. end;
  513. function ttypeconvnode.resulttype_char_to_string : tnode;
  514. var
  515. procname: string[31];
  516. para : tcallparanode;
  517. hp : tstringconstnode;
  518. ws : pcompilerwidestring;
  519. begin
  520. result:=nil;
  521. if left.nodetype=ordconstn then
  522. begin
  523. if tstringdef(resulttype.def).string_typ=st_widestring then
  524. begin
  525. initwidestring(ws);
  526. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  527. hp:=cstringconstnode.createwstr(ws);
  528. donewidestring(ws);
  529. end
  530. else
  531. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  532. result:=hp;
  533. end
  534. else
  535. { shortstrings are handled 'inline' }
  536. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  537. begin
  538. { create the parameter }
  539. para := ccallparanode.create(left,nil);
  540. left := nil;
  541. { and the procname }
  542. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  543. { and finally the call }
  544. result := ccallnode.createinternres(procname,para,resulttype);
  545. end
  546. else
  547. begin
  548. { create word(byte(char) shl 8 or 1) for litte endian machines }
  549. { and word(byte(char) or 256) for big endian machines }
  550. left := ctypeconvnode.create(left,u8bittype);
  551. left.toggleflag(nf_explizit);
  552. if (target_info.endian = endian_little) then
  553. left := caddnode.create(orn,
  554. cshlshrnode.create(shln,left,cordconstnode.create(8,s32bittype)),
  555. cordconstnode.create(1,s32bittype))
  556. else
  557. left := caddnode.create(orn,left,
  558. cordconstnode.create(1 shl 8,s32bittype));
  559. left := ctypeconvnode.create(left,u16bittype);
  560. left.toggleflag(nf_explizit);
  561. resulttypepass(left);
  562. end;
  563. end;
  564. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  565. begin
  566. if resulttype.def.size <> 1 then
  567. begin
  568. { convert first to string, then to chararray }
  569. inserttypeconv(left,cshortstringtype);
  570. inserttypeconv(left,resulttype);
  571. result:=left;
  572. left := nil;
  573. exit;
  574. end;
  575. result := nil;
  576. end;
  577. function ttypeconvnode.resulttype_char_to_char : tnode;
  578. var
  579. hp : tordconstnode;
  580. begin
  581. result:=nil;
  582. if left.nodetype=ordconstn then
  583. begin
  584. if (torddef(resulttype.def).typ=uchar) and
  585. (torddef(left.resulttype.def).typ=uwidechar) then
  586. begin
  587. hp:=cordconstnode.create(
  588. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),cchartype);
  589. result:=hp;
  590. end
  591. else if (torddef(resulttype.def).typ=uwidechar) and
  592. (torddef(left.resulttype.def).typ=uchar) then
  593. begin
  594. hp:=cordconstnode.create(
  595. asciichar2unicode(chr(tordconstnode(left).value)),cwidechartype);
  596. result:=hp;
  597. end
  598. else
  599. internalerror(200105131);
  600. exit;
  601. end;
  602. end;
  603. function ttypeconvnode.resulttype_int_to_real : tnode;
  604. var
  605. t : trealconstnode;
  606. begin
  607. result:=nil;
  608. if left.nodetype=ordconstn then
  609. begin
  610. t:=crealconstnode.create(tordconstnode(left).value,resulttype);
  611. result:=t;
  612. end;
  613. end;
  614. function ttypeconvnode.resulttype_real_to_real : tnode;
  615. var
  616. t : tnode;
  617. begin
  618. result:=nil;
  619. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  620. begin
  621. end
  622. else
  623. if is_currency(resulttype.def) then
  624. begin
  625. end;
  626. if left.nodetype=realconstn then
  627. begin
  628. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  629. result:=t;
  630. end;
  631. end;
  632. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  633. begin
  634. result:=nil;
  635. if is_pwidechar(resulttype.def) then
  636. inserttypeconv(left,cwidestringtype)
  637. else
  638. inserttypeconv(left,cshortstringtype);
  639. { evaluate again, reset resulttype so the convert_typ
  640. will be calculated again and cstring_to_pchar will
  641. be used for futher conversion }
  642. result:=det_resulttype;
  643. end;
  644. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  645. begin
  646. result:=nil;
  647. if is_pwidechar(resulttype.def) then
  648. inserttypeconv(left,cwidestringtype);
  649. end;
  650. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  651. var
  652. hp : tnode;
  653. begin
  654. result:=nil;
  655. if left.nodetype<>arrayconstructorn then
  656. internalerror(5546);
  657. { remove typeconv node }
  658. hp:=left;
  659. left:=nil;
  660. { create a set constructor tree }
  661. arrayconstructor_to_set(hp);
  662. result:=hp;
  663. end;
  664. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  665. begin
  666. result := ccallnode.createinternres(
  667. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  668. ccallparanode.create(left,nil),resulttype);
  669. left := nil;
  670. end;
  671. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  672. begin
  673. if tobjectdef(left.resulttype.def).isiidguidvalid then
  674. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid);
  675. end;
  676. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  677. begin
  678. { a dynamic array is a pointer to an array, so to convert it to }
  679. { an open array, we have to dereference it (JM) }
  680. result := ctypeconvnode.create(left,voidpointertype);
  681. { left is reused }
  682. left := nil;
  683. result.toggleflag(nf_explizit);
  684. result := cderefnode.create(result);
  685. result.resulttype := resulttype;
  686. end;
  687. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  688. const
  689. resulttypeconvert : array[tconverttype] of pointer = (
  690. {equal} nil,
  691. {not_possible} nil,
  692. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  693. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  694. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  695. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  696. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  697. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  698. { ansistring_2_pchar } nil,
  699. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  700. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  701. { array_2_pointer } nil,
  702. { pointer_2_array } nil,
  703. { int_2_int } nil,
  704. { int_2_bool } nil,
  705. { bool_2_bool } nil,
  706. { bool_2_int } nil,
  707. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  708. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  709. { proc_2_procvar } nil,
  710. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  711. { load_smallset } nil,
  712. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  713. { intf_2_string } nil,
  714. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  715. { class_2_intf } nil,
  716. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  717. { normal_2_smallset} nil,
  718. { dynarray_2_openarray} @resulttype_dynarray_to_openarray
  719. );
  720. type
  721. tprocedureofobject = function : tnode of object;
  722. var
  723. r : packed record
  724. proc : pointer;
  725. obj : pointer;
  726. end;
  727. begin
  728. result:=nil;
  729. { this is a little bit dirty but it works }
  730. { and should be quite portable too }
  731. r.proc:=resulttypeconvert[c];
  732. r.obj:=self;
  733. if assigned(r.proc) then
  734. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  735. end;
  736. function ttypeconvnode.det_resulttype:tnode;
  737. var
  738. hp : tnode;
  739. currprocdef,
  740. aprocdef : tprocdef;
  741. begin
  742. result:=nil;
  743. resulttype:=totype;
  744. resulttypepass(left);
  745. if codegenerror then
  746. exit;
  747. { remove obsolete type conversions }
  748. if is_equal(left.resulttype.def,resulttype.def) then
  749. begin
  750. { becuase is_equal only checks the basetype for sets we need to
  751. check here if we are loading a smallset into a normalset }
  752. if (resulttype.def.deftype=setdef) and
  753. (left.resulttype.def.deftype=setdef) and
  754. ((tsetdef(resulttype.def).settype = smallset) xor
  755. (tsetdef(left.resulttype.def).settype = smallset)) then
  756. begin
  757. { constant sets can be converted by changing the type only }
  758. if (left.nodetype=setconstn) then
  759. begin
  760. tsetdef(left.resulttype.def).changesettype(tsetdef(resulttype.def).settype);
  761. result:=left;
  762. left:=nil;
  763. exit;
  764. end;
  765. if (tsetdef(resulttype.def).settype <> smallset) then
  766. convtype:=tc_load_smallset
  767. else
  768. convtype := tc_normal_2_smallset;
  769. exit;
  770. end
  771. else
  772. begin
  773. left.resulttype:=resulttype;
  774. result:=left;
  775. left:=nil;
  776. exit;
  777. end;
  778. end;
  779. aprocdef:=assignment_overloaded(left.resulttype.def,resulttype.def);
  780. if assigned(aprocdef) then
  781. begin
  782. procinfo^.flags:=procinfo^.flags or pi_do_call;
  783. hp:=ccallnode.create(ccallparanode.create(left,nil),
  784. overloaded_operators[_assignment],nil,nil);
  785. { tell explicitly which def we must use !! (PM) }
  786. tcallnode(hp).procdefinition:=aprocdef;
  787. left:=nil;
  788. result:=hp;
  789. exit;
  790. end;
  791. if isconvertable(left.resulttype.def,resulttype.def,convtype,left.nodetype,nf_explizit in flags)=0 then
  792. begin
  793. {Procedures have a resulttype.def of voiddef and functions of their
  794. own resulttype.def. They will therefore always be incompatible with
  795. a procvar. Because isconvertable cannot check for procedures we
  796. use an extra check for them.}
  797. if (m_tp_procvar in aktmodeswitches) then
  798. begin
  799. if (resulttype.def.deftype=procvardef) and
  800. (is_procsym_load(left) or is_procsym_call(left)) then
  801. begin
  802. if is_procsym_call(left) then
  803. begin
  804. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  805. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  806. currprocdef,tcallnode(left).symtableproc);
  807. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) and
  808. assigned(tcallnode(left).methodpointer) then
  809. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  810. resulttypepass(hp);
  811. left.free;
  812. left:=hp;
  813. aprocdef:=tprocdef(left.resulttype.def);
  814. end
  815. else
  816. begin
  817. if (left.nodetype<>addrn) then
  818. aprocdef:=tprocsym(tloadnode(left).symtableentry).first_procdef;
  819. end;
  820. convtype:=tc_proc_2_procvar;
  821. { Now check if the procedure we are going to assign to
  822. the procvar, is compatible with the procvar's type }
  823. if assigned(aprocdef) then
  824. begin
  825. if not proc_to_procvar_equal(aprocdef,tprocvardef(resulttype.def),false) then
  826. CGMessage2(type_e_incompatible_types,aprocdef.typename,resulttype.def.typename);
  827. end
  828. else
  829. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  830. exit;
  831. end;
  832. end;
  833. if nf_explizit in flags then
  834. begin
  835. { check if the result could be in a register }
  836. if not(tstoreddef(resulttype.def).is_intregable) and
  837. not(tstoreddef(resulttype.def).is_fpuregable) then
  838. make_not_regable(left);
  839. { boolean to byte are special because the
  840. location can be different }
  841. if is_integer(resulttype.def) and
  842. is_boolean(left.resulttype.def) then
  843. begin
  844. convtype:=tc_bool_2_int;
  845. exit;
  846. end;
  847. { ansistring to pchar }
  848. if is_pchar(resulttype.def) and
  849. is_ansistring(left.resulttype.def) then
  850. begin
  851. convtype:=tc_ansistring_2_pchar;
  852. exit;
  853. end;
  854. { do common tc_equal cast }
  855. convtype:=tc_equal;
  856. { enum to ordinal will always be s32bit }
  857. if (left.resulttype.def.deftype=enumdef) and
  858. is_ordinal(resulttype.def) then
  859. begin
  860. if left.nodetype=ordconstn then
  861. begin
  862. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  863. result:=hp;
  864. exit;
  865. end
  866. else
  867. begin
  868. if isconvertable(s32bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  869. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  870. end;
  871. end
  872. { ordinal to enumeration }
  873. else
  874. if (resulttype.def.deftype=enumdef) and
  875. is_ordinal(left.resulttype.def) then
  876. begin
  877. if left.nodetype=ordconstn then
  878. begin
  879. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  880. result:=hp;
  881. exit;
  882. end
  883. else
  884. begin
  885. if IsConvertable(left.resulttype.def,s32bittype.def,convtype,ordconstn,false)=0 then
  886. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  887. end;
  888. end
  889. { nil to ordinal node }
  890. else if (left.nodetype=niln) and is_ordinal(resulttype.def) then
  891. begin
  892. hp:=cordconstnode.create(0,resulttype);
  893. result:=hp;
  894. exit;
  895. end
  896. { constant pointer to ordinal }
  897. else if is_ordinal(resulttype.def) and
  898. (left.nodetype=pointerconstn) then
  899. begin
  900. hp:=cordconstnode.create(tpointerconstnode(left).value,resulttype);
  901. result:=hp;
  902. exit;
  903. end
  904. { class to class or object to object, with checkobject support }
  905. else if (resulttype.def.deftype=objectdef) and
  906. (left.resulttype.def.deftype=objectdef) then
  907. begin
  908. if (cs_check_object in aktlocalswitches) then
  909. begin
  910. if is_class_or_interface(resulttype.def) then
  911. begin
  912. { we can translate the typeconvnode to 'as' when
  913. typecasting to a class or interface }
  914. hp:=casnode.create(left,cloadvmtnode.create(ctypenode.create(resulttype)));
  915. left:=nil;
  916. result:=hp;
  917. exit;
  918. end;
  919. end
  920. else
  921. begin
  922. { check if the types are related }
  923. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  924. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  925. CGMessage2(type_w_classes_not_related,left.resulttype.def.typename,resulttype.def.typename);
  926. end;
  927. end
  928. {Are we typecasting an ordconst to a char?}
  929. else
  930. if is_char(resulttype.def) and
  931. is_ordinal(left.resulttype.def) then
  932. begin
  933. if left.nodetype=ordconstn then
  934. begin
  935. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  936. result:=hp;
  937. exit;
  938. end
  939. else
  940. begin
  941. if IsConvertable(left.resulttype.def,u8bittype.def,convtype,ordconstn,false)=0 then
  942. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  943. end;
  944. end
  945. {Are we typecasting an ordconst to a wchar?}
  946. else
  947. if is_widechar(resulttype.def) and
  948. is_ordinal(left.resulttype.def) then
  949. begin
  950. if left.nodetype=ordconstn then
  951. begin
  952. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  953. result:=hp;
  954. exit;
  955. end
  956. else
  957. begin
  958. if IsConvertable(left.resulttype.def,u16bittype.def,convtype,ordconstn,false)=0 then
  959. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  960. end;
  961. end
  962. { char to ordinal }
  963. else
  964. if is_char(left.resulttype.def) and
  965. is_ordinal(resulttype.def) then
  966. begin
  967. if left.nodetype=ordconstn then
  968. begin
  969. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  970. result:=hp;
  971. exit;
  972. end
  973. else
  974. begin
  975. if IsConvertable(u8bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  976. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  977. end;
  978. end
  979. { widechar to ordinal }
  980. else
  981. if is_widechar(left.resulttype.def) and
  982. is_ordinal(resulttype.def) then
  983. begin
  984. if left.nodetype=ordconstn then
  985. begin
  986. hp:=cordconstnode.create(tordconstnode(left).value,resulttype);
  987. result:=hp;
  988. exit;
  989. end
  990. else
  991. begin
  992. if IsConvertable(u16bittype.def,resulttype.def,convtype,ordconstn,false)=0 then
  993. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  994. end;
  995. end
  996. { only if the same size or formal def }
  997. { why do we allow typecasting of voiddef ?? (PM) }
  998. else
  999. begin
  1000. if not(
  1001. (left.resulttype.def.deftype=formaldef) or
  1002. (left.resulttype.def.size=resulttype.def.size) or
  1003. (is_void(left.resulttype.def) and
  1004. (left.nodetype=derefn))
  1005. ) then
  1006. CGMessage(cg_e_illegal_type_conversion);
  1007. if ((left.resulttype.def.deftype=orddef) and
  1008. (resulttype.def.deftype=pointerdef)) or
  1009. ((resulttype.def.deftype=orddef) and
  1010. (left.resulttype.def.deftype=pointerdef)) then
  1011. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  1012. end;
  1013. { the conversion into a strutured type is only }
  1014. { possible, if the source is not a register }
  1015. if ((resulttype.def.deftype in [recorddef,stringdef,arraydef]) or
  1016. ((resulttype.def.deftype=objectdef) and not(is_class(resulttype.def)))
  1017. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  1018. it also works if the assignment is overloaded
  1019. YES but this code is not executed if assignment is overloaded (PM)
  1020. not assigned(assignment_overloaded(left.resulttype.def,resulttype.def))} then
  1021. CGMessage(cg_e_illegal_type_conversion);
  1022. end
  1023. else
  1024. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1025. end;
  1026. { tp7 procvar support, when right is not a procvardef and we got a
  1027. loadn of a procvar then convert to a calln, the check for the
  1028. result is already done in is_convertible, also no conflict with
  1029. @procvar is here because that has an extra addrn }
  1030. if (m_tp_procvar in aktmodeswitches) and
  1031. (resulttype.def.deftype<>procvardef) and
  1032. (left.resulttype.def.deftype=procvardef) and
  1033. (left.nodetype=loadn) then
  1034. begin
  1035. hp:=ccallnode.create(nil,nil,nil,nil);
  1036. tcallnode(hp).set_procvar(left);
  1037. resulttypepass(hp);
  1038. left:=hp;
  1039. end;
  1040. { remove typeconv after niln, but not when the result is a
  1041. methodpointer. The typeconv of the methodpointer will then
  1042. take care of updateing size of niln to OS_64 }
  1043. if (left.nodetype=niln) and
  1044. not((resulttype.def.deftype=procvardef) and
  1045. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1046. begin
  1047. left.resulttype:=resulttype;
  1048. result:=left;
  1049. left:=nil;
  1050. exit;
  1051. end;
  1052. { ordinal contants can be directly converted }
  1053. if (left.nodetype=ordconstn) and is_ordinal(resulttype.def) and
  1054. { but not char to char because it is a widechar to char or via versa }
  1055. { which needs extra code to do the code page transistion }
  1056. not(convtype=tc_char_2_char) then
  1057. begin
  1058. { replace the resulttype and recheck the range }
  1059. left.resulttype:=resulttype;
  1060. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explizit in flags));
  1061. result:=left;
  1062. left:=nil;
  1063. exit;
  1064. end;
  1065. { fold nil to any pointer type }
  1066. if (left.nodetype=niln) and (resulttype.def.deftype=pointerdef) then
  1067. begin
  1068. hp:=cnilnode.create;
  1069. hp.resulttype:=resulttype;
  1070. result:=hp;
  1071. exit;
  1072. end;
  1073. { further, pointerconstn to any pointer is folded too }
  1074. if (left.nodetype=pointerconstn) and (resulttype.def.deftype=pointerdef) then
  1075. begin
  1076. left.resulttype:=resulttype;
  1077. result:=left;
  1078. left:=nil;
  1079. exit;
  1080. end;
  1081. { now call the resulttype helper to do constant folding }
  1082. result:=resulttype_call_helper(convtype);
  1083. end;
  1084. function ttypeconvnode.first_cord_to_pointer : tnode;
  1085. begin
  1086. result:=nil;
  1087. internalerror(200104043);
  1088. end;
  1089. function ttypeconvnode.first_int_to_int : tnode;
  1090. begin
  1091. first_int_to_int:=nil;
  1092. if (left.location.loc<>LOC_REGISTER) and
  1093. (resulttype.def.size>left.resulttype.def.size) then
  1094. location.loc:=LOC_REGISTER;
  1095. if is_64bitint(resulttype.def) then
  1096. registers32:=max(registers32,2)
  1097. else
  1098. registers32:=max(registers32,1);
  1099. end;
  1100. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1101. begin
  1102. first_cstring_to_pchar:=nil;
  1103. registers32:=1;
  1104. location.loc:=LOC_REGISTER;
  1105. end;
  1106. function ttypeconvnode.first_string_to_chararray : tnode;
  1107. begin
  1108. first_string_to_chararray:=nil;
  1109. registers32:=1;
  1110. location.loc:=LOC_REGISTER;
  1111. end;
  1112. function ttypeconvnode.first_char_to_string : tnode;
  1113. begin
  1114. first_char_to_string:=nil;
  1115. location.loc:=LOC_CREFERENCE;
  1116. end;
  1117. function ttypeconvnode.first_nothing : tnode;
  1118. begin
  1119. first_nothing:=nil;
  1120. end;
  1121. function ttypeconvnode.first_array_to_pointer : tnode;
  1122. begin
  1123. first_array_to_pointer:=nil;
  1124. if registers32<1 then
  1125. registers32:=1;
  1126. location.loc:=LOC_REGISTER;
  1127. end;
  1128. function ttypeconvnode.first_int_to_real : tnode;
  1129. begin
  1130. first_int_to_real:=nil;
  1131. {$ifdef m68k}
  1132. if (cs_fp_emulation in aktmoduleswitches) or
  1133. (tfloatdef(resulttype.def).typ=s32real) then
  1134. begin
  1135. if registers32<1 then
  1136. registers32:=1;
  1137. end
  1138. else
  1139. if registersfpu<1 then
  1140. registersfpu:=1;
  1141. {$else not m68k}
  1142. if registersfpu<1 then
  1143. registersfpu:=1;
  1144. {$endif not m68k}
  1145. location.loc:=LOC_FPUREGISTER;
  1146. end;
  1147. function ttypeconvnode.first_real_to_real : tnode;
  1148. begin
  1149. first_real_to_real:=nil;
  1150. { comp isn't a floating type }
  1151. {$ifdef i386}
  1152. if (tfloatdef(resulttype.def).typ=s64comp) and
  1153. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1154. not (nf_explizit in flags) then
  1155. CGMessage(type_w_convert_real_2_comp);
  1156. {$endif}
  1157. if registersfpu<1 then
  1158. registersfpu:=1;
  1159. location.loc:=LOC_FPUREGISTER;
  1160. end;
  1161. function ttypeconvnode.first_pointer_to_array : tnode;
  1162. begin
  1163. first_pointer_to_array:=nil;
  1164. if registers32<1 then
  1165. registers32:=1;
  1166. location.loc:=LOC_REFERENCE;
  1167. end;
  1168. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1169. begin
  1170. first_cchar_to_pchar:=nil;
  1171. internalerror(200104021);
  1172. end;
  1173. function ttypeconvnode.first_bool_to_int : tnode;
  1174. begin
  1175. first_bool_to_int:=nil;
  1176. { byte(boolean) or word(wordbool) or longint(longbool) must
  1177. be accepted for var parameters }
  1178. if (nf_explizit in flags) and
  1179. (left.resulttype.def.size=resulttype.def.size) and
  1180. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1181. exit;
  1182. { when converting to 64bit, first convert to a 32bit int and then }
  1183. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1184. if resulttype.def.size > sizeof(aword) then
  1185. begin
  1186. result := ctypeconvnode.create(left,u32bittype);
  1187. result.toggleflag(nf_explizit);
  1188. result := ctypeconvnode.create(result,resulttype);
  1189. left := nil;
  1190. firstpass(result);
  1191. exit;
  1192. end;
  1193. location.loc:=LOC_REGISTER;
  1194. if registers32<1 then
  1195. registers32:=1;
  1196. end;
  1197. function ttypeconvnode.first_int_to_bool : tnode;
  1198. begin
  1199. first_int_to_bool:=nil;
  1200. { byte(boolean) or word(wordbool) or longint(longbool) must
  1201. be accepted for var parameters }
  1202. if (nf_explizit in flags) and
  1203. (left.resulttype.def.size=resulttype.def.size) and
  1204. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1205. exit;
  1206. location.loc:=LOC_REGISTER;
  1207. { need if bool to bool !!
  1208. not very nice !!
  1209. insertypeconv(left,s32bittype);
  1210. left.explizit:=true;
  1211. firstpass(left); }
  1212. if registers32<1 then
  1213. registers32:=1;
  1214. end;
  1215. function ttypeconvnode.first_bool_to_bool : tnode;
  1216. begin
  1217. first_bool_to_bool:=nil;
  1218. location.loc:=LOC_REGISTER;
  1219. if registers32<1 then
  1220. registers32:=1;
  1221. end;
  1222. function ttypeconvnode.first_char_to_char : tnode;
  1223. begin
  1224. first_char_to_char:=nil;
  1225. location.loc:=LOC_REGISTER;
  1226. if registers32<1 then
  1227. registers32:=1;
  1228. end;
  1229. function ttypeconvnode.first_proc_to_procvar : tnode;
  1230. begin
  1231. first_proc_to_procvar:=nil;
  1232. if (left.location.loc<>LOC_REFERENCE) then
  1233. CGMessage(cg_e_illegal_expression);
  1234. registers32:=left.registers32;
  1235. if registers32<1 then
  1236. registers32:=1;
  1237. location.loc:=LOC_REGISTER;
  1238. end;
  1239. function ttypeconvnode.first_load_smallset : tnode;
  1240. var
  1241. srsym: ttypesym;
  1242. p: tcallparanode;
  1243. begin
  1244. if not searchsystype('FPC_SMALL_SET',srsym) then
  1245. internalerror(200108313);
  1246. p := ccallparanode.create(left,nil);
  1247. { reused }
  1248. left := nil;
  1249. { convert parameter explicitely to fpc_small_set }
  1250. p.left := ctypeconvnode.create(p.left,srsym.restype);
  1251. p.left.toggleflag(nf_explizit);
  1252. { create call, adjust resulttype }
  1253. result :=
  1254. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1255. firstpass(result);
  1256. end;
  1257. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1258. begin
  1259. first_ansistring_to_pchar:=nil;
  1260. location.loc:=LOC_REGISTER;
  1261. if registers32<1 then
  1262. registers32:=1;
  1263. end;
  1264. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1265. begin
  1266. first_arrayconstructor_to_set:=nil;
  1267. internalerror(200104022);
  1268. end;
  1269. function ttypeconvnode.first_class_to_intf : tnode;
  1270. begin
  1271. first_class_to_intf:=nil;
  1272. location.loc:=LOC_REFERENCE;
  1273. if registers32<1 then
  1274. registers32:=1;
  1275. end;
  1276. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1277. const
  1278. firstconvert : array[tconverttype] of pointer = (
  1279. @ttypeconvnode.first_nothing, {equal}
  1280. @ttypeconvnode.first_nothing, {not_possible}
  1281. nil, { removed in resulttype_string_to_string }
  1282. @ttypeconvnode.first_char_to_string,
  1283. @ttypeconvnode.first_nothing, { char_2_chararray, needs nothing extra }
  1284. nil, { removed in resulttype_chararray_to_string }
  1285. @ttypeconvnode.first_cchar_to_pchar,
  1286. @ttypeconvnode.first_cstring_to_pchar,
  1287. @ttypeconvnode.first_ansistring_to_pchar,
  1288. @ttypeconvnode.first_string_to_chararray,
  1289. nil, { removed in resulttype_chararray_to_string }
  1290. @ttypeconvnode.first_array_to_pointer,
  1291. @ttypeconvnode.first_pointer_to_array,
  1292. @ttypeconvnode.first_int_to_int,
  1293. @ttypeconvnode.first_int_to_bool,
  1294. @ttypeconvnode.first_bool_to_bool,
  1295. @ttypeconvnode.first_bool_to_int,
  1296. @ttypeconvnode.first_real_to_real,
  1297. @ttypeconvnode.first_int_to_real,
  1298. @ttypeconvnode.first_proc_to_procvar,
  1299. @ttypeconvnode.first_arrayconstructor_to_set,
  1300. @ttypeconvnode.first_load_smallset,
  1301. @ttypeconvnode.first_cord_to_pointer,
  1302. @ttypeconvnode.first_nothing,
  1303. @ttypeconvnode.first_nothing,
  1304. @ttypeconvnode.first_class_to_intf,
  1305. @ttypeconvnode.first_char_to_char,
  1306. @ttypeconvnode.first_nothing,
  1307. @ttypeconvnode.first_nothing
  1308. );
  1309. type
  1310. tprocedureofobject = function : tnode of object;
  1311. var
  1312. r : packed record
  1313. proc : pointer;
  1314. obj : pointer;
  1315. end;
  1316. begin
  1317. { this is a little bit dirty but it works }
  1318. { and should be quite portable too }
  1319. r.proc:=firstconvert[c];
  1320. r.obj:=self;
  1321. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1322. end;
  1323. function ttypeconvnode.pass_1 : tnode;
  1324. begin
  1325. result:=nil;
  1326. firstpass(left);
  1327. if codegenerror then
  1328. exit;
  1329. { load the value_str from the left part }
  1330. registers32:=left.registers32;
  1331. registersfpu:=left.registersfpu;
  1332. {$ifdef SUPPORT_MMX}
  1333. registersmmx:=left.registersmmx;
  1334. {$endif}
  1335. location.loc:=left.location.loc;
  1336. if nf_explizit in flags then
  1337. begin
  1338. { check if the result could be in a register }
  1339. if not(tstoreddef(resulttype.def).is_intregable) and
  1340. not(tstoreddef(resulttype.def).is_fpuregable) then
  1341. make_not_regable(left);
  1342. end;
  1343. result:=first_call_helper(convtype);
  1344. end;
  1345. function ttypeconvnode.docompare(p: tnode) : boolean;
  1346. begin
  1347. docompare :=
  1348. inherited docompare(p) and
  1349. (convtype = ttypeconvnode(p).convtype);
  1350. end;
  1351. {*****************************************************************************
  1352. TISNODE
  1353. *****************************************************************************}
  1354. constructor tisnode.create(l,r : tnode);
  1355. begin
  1356. inherited create(isn,l,r);
  1357. end;
  1358. function tisnode.det_resulttype:tnode;
  1359. var
  1360. paras: tcallparanode;
  1361. begin
  1362. result:=nil;
  1363. resulttypepass(left);
  1364. resulttypepass(right);
  1365. set_varstate(left,true);
  1366. set_varstate(right,true);
  1367. if codegenerror then
  1368. exit;
  1369. if (right.resulttype.def.deftype=classrefdef) then
  1370. begin
  1371. { left must be a class }
  1372. if is_class(left.resulttype.def) then
  1373. begin
  1374. { the operands must be related }
  1375. if (not(tobjectdef(left.resulttype.def).is_related(
  1376. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1377. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1378. tobjectdef(left.resulttype.def)))) then
  1379. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1380. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1381. end
  1382. else
  1383. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1384. { call fpc_do_is helper }
  1385. paras := ccallparanode.create(
  1386. left,
  1387. ccallparanode.create(
  1388. right,nil));
  1389. result := ccallnode.createintern('fpc_do_is',paras);
  1390. left := nil;
  1391. right := nil;
  1392. end
  1393. else if is_interface(right.resulttype.def) then
  1394. begin
  1395. { left is a class }
  1396. if is_class(left.resulttype.def) then
  1397. begin
  1398. { the operands must be related }
  1399. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1400. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1401. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1402. end
  1403. { left is an interface }
  1404. else if is_interface(left.resulttype.def) then
  1405. begin
  1406. { the operands must be related }
  1407. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1408. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1409. CGMessage(type_e_mismatch);
  1410. end
  1411. else
  1412. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1413. { call fpc_do_is helper }
  1414. paras := ccallparanode.create(
  1415. left,
  1416. ccallparanode.create(
  1417. right,nil));
  1418. result := ccallnode.createintern('fpc_do_is',paras);
  1419. left := nil;
  1420. right := nil;
  1421. end
  1422. else
  1423. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1424. resulttype:=booltype;
  1425. end;
  1426. function tisnode.pass_1 : tnode;
  1427. begin
  1428. internalerror(200204254);
  1429. result:=nil;
  1430. end;
  1431. { dummy pass_2, it will never be called, but we need one since }
  1432. { you can't instantiate an abstract class }
  1433. procedure tisnode.pass_2;
  1434. begin
  1435. end;
  1436. {*****************************************************************************
  1437. TASNODE
  1438. *****************************************************************************}
  1439. constructor tasnode.create(l,r : tnode);
  1440. begin
  1441. inherited create(asn,l,r);
  1442. end;
  1443. function tasnode.det_resulttype:tnode;
  1444. begin
  1445. result:=nil;
  1446. resulttypepass(right);
  1447. resulttypepass(left);
  1448. set_varstate(right,true);
  1449. set_varstate(left,true);
  1450. if codegenerror then
  1451. exit;
  1452. if (right.resulttype.def.deftype=classrefdef) then
  1453. begin
  1454. { left must be a class }
  1455. if is_class(left.resulttype.def) then
  1456. begin
  1457. { the operands must be related }
  1458. if (not(tobjectdef(left.resulttype.def).is_related(
  1459. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1460. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1461. tobjectdef(left.resulttype.def)))) then
  1462. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1463. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1464. end
  1465. else
  1466. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1467. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1468. end
  1469. else if is_interface(right.resulttype.def) then
  1470. begin
  1471. { left is a class }
  1472. if is_class(left.resulttype.def) then
  1473. begin
  1474. { the operands must be related }
  1475. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1476. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1477. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1478. end
  1479. { left is an interface }
  1480. else if is_interface(left.resulttype.def) then
  1481. begin
  1482. { the operands must be related }
  1483. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1484. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1485. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1486. end
  1487. else
  1488. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1489. resulttype:=right.resulttype;
  1490. { load the GUID of the interface }
  1491. if (right.nodetype=typen) then
  1492. begin
  1493. if tobjectdef(left.resulttype.def).isiidguidvalid then
  1494. right:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid)
  1495. else
  1496. internalerror(200206282);
  1497. resulttypepass(right);
  1498. end;
  1499. end
  1500. else
  1501. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1502. end;
  1503. function tasnode.pass_1 : tnode;
  1504. begin
  1505. firstpass(left);
  1506. firstpass(right);
  1507. if codegenerror then
  1508. exit;
  1509. left_right_max;
  1510. location.loc:=left.location.loc;
  1511. result:=nil;
  1512. end;
  1513. begin
  1514. ctypeconvnode:=ttypeconvnode;
  1515. casnode:=tasnode;
  1516. cisnode:=tisnode;
  1517. end.
  1518. {
  1519. $Log$
  1520. Revision 1.63 2002-07-23 09:51:22 daniel
  1521. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1522. are worth comitting.
  1523. Revision 1.62 2002/07/22 11:48:04 daniel
  1524. * Sets are now internally sets.
  1525. Revision 1.61 2002/07/20 17:16:02 florian
  1526. + source code page support
  1527. Revision 1.60 2002/07/20 11:57:54 florian
  1528. * types.pas renamed to defbase.pas because D6 contains a types
  1529. unit so this would conflicts if D6 programms are compiled
  1530. + Willamette/SSE2 instructions to assembler added
  1531. Revision 1.59 2002/07/01 16:23:53 peter
  1532. * cg64 patch
  1533. * basics for currency
  1534. * asnode updates for class and interface (not finished)
  1535. Revision 1.58 2002/05/18 13:34:09 peter
  1536. * readded missing revisions
  1537. Revision 1.57 2002/05/16 19:46:37 carl
  1538. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1539. + try to fix temp allocation (still in ifdef)
  1540. + generic constructor calls
  1541. + start of tassembler / tmodulebase class cleanup
  1542. Revision 1.55 2002/05/12 16:53:07 peter
  1543. * moved entry and exitcode to ncgutil and cgobj
  1544. * foreach gets extra argument for passing local data to the
  1545. iterator function
  1546. * -CR checks also class typecasts at runtime by changing them
  1547. into as
  1548. * fixed compiler to cycle with the -CR option
  1549. * fixed stabs with elf writer, finally the global variables can
  1550. be watched
  1551. * removed a lot of routines from cga unit and replaced them by
  1552. calls to cgobj
  1553. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1554. u32bit then the other is typecasted also to u32bit without giving
  1555. a rangecheck warning/error.
  1556. * fixed pascal calling method with reversing also the high tree in
  1557. the parast, detected by tcalcst3 test
  1558. Revision 1.54 2002/04/25 20:16:38 peter
  1559. * moved more routines from cga/n386util
  1560. Revision 1.53 2002/04/23 19:16:34 peter
  1561. * add pinline unit that inserts compiler supported functions using
  1562. one or more statements
  1563. * moved finalize and setlength from ninl to pinline
  1564. Revision 1.52 2002/04/21 19:02:03 peter
  1565. * removed newn and disposen nodes, the code is now directly
  1566. inlined from pexpr
  1567. * -an option that will write the secondpass nodes to the .s file, this
  1568. requires EXTDEBUG define to actually write the info
  1569. * fixed various internal errors and crashes due recent code changes
  1570. Revision 1.51 2002/04/06 18:10:42 jonas
  1571. * several powerpc-related additions and fixes
  1572. Revision 1.50 2002/04/04 19:05:58 peter
  1573. * removed unused units
  1574. * use tlocation.size in cg.a_*loc*() routines
  1575. Revision 1.49 2002/04/02 17:11:28 peter
  1576. * tlocation,treference update
  1577. * LOC_CONSTANT added for better constant handling
  1578. * secondadd splitted in multiple routines
  1579. * location_force_reg added for loading a location to a register
  1580. of a specified size
  1581. * secondassignment parses now first the right and then the left node
  1582. (this is compatible with Kylix). This saves a lot of push/pop especially
  1583. with string operations
  1584. * adapted some routines to use the new cg methods
  1585. Revision 1.48 2002/02/03 09:30:03 peter
  1586. * more fixes for protected handling
  1587. }