ncnv.pas 68 KB

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