ncnv.pas 65 KB

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