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