ncnv.pas 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,symppu,
  24. defutil,defcmp,
  25. nld
  26. {$ifdef Delphi}
  27. ,dmisc
  28. {$endif}
  29. ;
  30. type
  31. ttypeconvnode = class(tunarynode)
  32. totype : ttype;
  33. convtype : tconverttype;
  34. constructor create(node : tnode;const t : ttype);virtual;
  35. constructor create_explicit(node : tnode;const t : ttype);
  36. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  37. procedure ppuwrite(ppufile:tcompilerppufile);override;
  38. procedure derefimpl;override;
  39. function getcopy : tnode;override;
  40. function pass_1 : tnode;override;
  41. function det_resulttype:tnode;override;
  42. procedure mark_write;override;
  43. function docompare(p: tnode) : boolean; override;
  44. private
  45. function resulttype_cord_to_pointer : tnode;
  46. function resulttype_chararray_to_string : tnode;
  47. function resulttype_string_to_chararray : tnode;
  48. function resulttype_string_to_string : tnode;
  49. function resulttype_char_to_string : tnode;
  50. function resulttype_char_to_chararray : tnode;
  51. function resulttype_int_to_real : tnode;
  52. function resulttype_real_to_real : tnode;
  53. function resulttype_cchar_to_pchar : tnode;
  54. function resulttype_cstring_to_pchar : tnode;
  55. function resulttype_char_to_char : tnode;
  56. function resulttype_arrayconstructor_to_set : tnode;
  57. function resulttype_pchar_to_string : tnode;
  58. function resulttype_interface_to_guid : tnode;
  59. function resulttype_dynarray_to_openarray : tnode;
  60. function resulttype_pwchar_to_string : tnode;
  61. function resulttype_variant_to_dynarray : tnode;
  62. function resulttype_dynarray_to_variant : tnode;
  63. function resulttype_call_helper(c : tconverttype) : tnode;
  64. protected
  65. function first_int_to_int : tnode;virtual;
  66. function first_cstring_to_pchar : tnode;virtual;
  67. function first_string_to_chararray : tnode;virtual;
  68. function first_char_to_string : tnode;virtual;
  69. function first_nothing : tnode;virtual;
  70. function first_array_to_pointer : tnode;virtual;
  71. function first_int_to_real : tnode;virtual;
  72. function first_real_to_real : tnode;virtual;
  73. function first_pointer_to_array : tnode;virtual;
  74. function first_cchar_to_pchar : tnode;virtual;
  75. function first_bool_to_int : tnode;virtual;
  76. function first_int_to_bool : tnode;virtual;
  77. function first_bool_to_bool : tnode;virtual;
  78. function first_proc_to_procvar : tnode;virtual;
  79. function first_load_smallset : tnode;virtual;
  80. function first_cord_to_pointer : tnode;virtual;
  81. function first_ansistring_to_pchar : tnode;virtual;
  82. function first_arrayconstructor_to_set : tnode;virtual;
  83. function first_class_to_intf : tnode;virtual;
  84. function first_char_to_char : tnode;virtual;
  85. function first_call_helper(c : tconverttype) : tnode;
  86. { these wrapper are necessary, because the first_* stuff is called }
  87. { through a table. Without the wrappers override wouldn't have }
  88. { any effect }
  89. function _first_int_to_int : tnode;
  90. function _first_cstring_to_pchar : tnode;
  91. function _first_string_to_chararray : tnode;
  92. function _first_char_to_string : tnode;
  93. function _first_nothing : tnode;
  94. function _first_array_to_pointer : tnode;
  95. function _first_int_to_real : tnode;
  96. function _first_real_to_real : tnode;
  97. function _first_pointer_to_array : tnode;
  98. function _first_cchar_to_pchar : tnode;
  99. function _first_bool_to_int : tnode;
  100. function _first_int_to_bool : tnode;
  101. function _first_bool_to_bool : tnode;
  102. function _first_proc_to_procvar : tnode;
  103. function _first_load_smallset : tnode;
  104. function _first_cord_to_pointer : tnode;
  105. function _first_ansistring_to_pchar : tnode;
  106. function _first_arrayconstructor_to_set : tnode;
  107. function _first_class_to_intf : tnode;
  108. function _first_char_to_char : tnode;
  109. procedure second_int_to_int;virtual;abstract;
  110. procedure second_string_to_string;virtual;abstract;
  111. procedure second_cstring_to_pchar;virtual;abstract;
  112. procedure second_string_to_chararray;virtual;abstract;
  113. procedure second_array_to_pointer;virtual;abstract;
  114. procedure second_pointer_to_array;virtual;abstract;
  115. procedure second_chararray_to_string;virtual;abstract;
  116. procedure second_char_to_string;virtual;abstract;
  117. procedure second_int_to_real;virtual;abstract;
  118. procedure second_real_to_real;virtual;abstract;
  119. procedure second_cord_to_pointer;virtual;abstract;
  120. procedure second_proc_to_procvar;virtual;abstract;
  121. procedure second_bool_to_int;virtual;abstract;
  122. procedure second_int_to_bool;virtual;abstract;
  123. procedure second_bool_to_bool;virtual;abstract;
  124. procedure second_load_smallset;virtual;abstract;
  125. procedure second_ansistring_to_pchar;virtual;abstract;
  126. procedure second_class_to_intf;virtual;abstract;
  127. procedure second_char_to_char;virtual;abstract;
  128. procedure second_nothing; virtual;abstract;
  129. end;
  130. ttypeconvnodeclass = class of ttypeconvnode;
  131. tasnode = class(tbinarynode)
  132. constructor create(l,r : tnode);virtual;
  133. function pass_1 : tnode;override;
  134. function det_resulttype:tnode;override;
  135. function getcopy: tnode;override;
  136. destructor destroy; override;
  137. protected
  138. call: tnode;
  139. end;
  140. tasnodeclass = class of tasnode;
  141. tisnode = class(tbinarynode)
  142. constructor create(l,r : tnode);virtual;
  143. function pass_1 : tnode;override;
  144. function det_resulttype:tnode;override;
  145. procedure pass_2;override;
  146. end;
  147. tisnodeclass = class of tisnode;
  148. var
  149. ctypeconvnode : ttypeconvnodeclass;
  150. casnode : tasnodeclass;
  151. cisnode : tisnodeclass;
  152. procedure inserttypeconv(var p:tnode;const t:ttype);
  153. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  154. procedure arrayconstructor_to_set(var p : tnode);
  155. implementation
  156. uses
  157. globtype,systems,tokens,
  158. cutils,verbose,globals,widestr,
  159. symconst,symdef,symsym,symtable,
  160. ncon,ncal,nset,nadd,ninl,nmem,nmat,
  161. cginfo,cgbase,
  162. htypechk,pass_1,cpubase,cpuinfo;
  163. {*****************************************************************************
  164. Helpers
  165. *****************************************************************************}
  166. procedure inserttypeconv(var p:tnode;const t:ttype);
  167. begin
  168. if not assigned(p.resulttype.def) then
  169. begin
  170. resulttypepass(p);
  171. if codegenerror then
  172. exit;
  173. end;
  174. { don't insert obsolete type conversions }
  175. if equal_defs(p.resulttype.def,t.def) and
  176. not ((p.resulttype.def.deftype=setdef) and
  177. (tsetdef(p.resulttype.def).settype <>
  178. tsetdef(t.def).settype)) then
  179. begin
  180. p.resulttype:=t;
  181. end
  182. else
  183. begin
  184. p:=ctypeconvnode.create(p,t);
  185. resulttypepass(p);
  186. end;
  187. end;
  188. procedure inserttypeconv_explicit(var p:tnode;const t:ttype);
  189. begin
  190. if not assigned(p.resulttype.def) then
  191. begin
  192. resulttypepass(p);
  193. if codegenerror then
  194. exit;
  195. end;
  196. { don't insert obsolete type conversions }
  197. if equal_defs(p.resulttype.def,t.def) and
  198. not ((p.resulttype.def.deftype=setdef) and
  199. (tsetdef(p.resulttype.def).settype <>
  200. tsetdef(t.def).settype)) then
  201. begin
  202. p.resulttype:=t;
  203. end
  204. else
  205. begin
  206. p:=ctypeconvnode.create_explicit(p,t);
  207. resulttypepass(p);
  208. end;
  209. end;
  210. {*****************************************************************************
  211. Array constructor to Set Conversion
  212. *****************************************************************************}
  213. procedure arrayconstructor_to_set(var p : tnode);
  214. var
  215. constp : tsetconstnode;
  216. buildp,
  217. p2,p3,p4 : tnode;
  218. htype : ttype;
  219. constset : Pconstset;
  220. constsetlo,
  221. constsethi : longint;
  222. procedure update_constsethi(t:ttype);
  223. begin
  224. if ((t.def.deftype=orddef) and
  225. (torddef(t.def).high>=constsethi)) then
  226. begin
  227. constsethi:=torddef(t.def).high;
  228. if htype.def=nil then
  229. begin
  230. if (constsethi>255) or
  231. (torddef(t.def).low<0) then
  232. htype:=u8bittype
  233. else
  234. htype:=t;
  235. end;
  236. if constsethi>255 then
  237. constsethi:=255;
  238. end
  239. else if ((t.def.deftype=enumdef) and
  240. (tenumdef(t.def).max>=constsethi)) then
  241. begin
  242. if htype.def=nil then
  243. htype:=t;
  244. constsethi:=tenumdef(t.def).max;
  245. end;
  246. end;
  247. procedure do_set(pos : longint);
  248. {$ifdef oldset}
  249. var
  250. mask,l : longint;
  251. {$endif}
  252. begin
  253. if (pos and not $ff)<>0 then
  254. Message(parser_e_illegal_set_expr);
  255. if pos>constsethi then
  256. constsethi:=pos;
  257. if pos<constsetlo then
  258. constsetlo:=pos;
  259. {$ifdef oldset}
  260. { to do this correctly we use the 32bit array }
  261. l:=pos shr 5;
  262. mask:=1 shl (pos mod 32);
  263. { do we allow the same twice }
  264. if (pconst32bitset(constset)^[l] and mask)<>0 then
  265. Message(parser_e_illegal_set_expr);
  266. pconst32bitset(constset)^[l]:=pconst32bitset(constset)^[l] or mask;
  267. {$else}
  268. if pos in constset^ then
  269. Message(parser_e_illegal_set_expr);
  270. include(constset^,pos);
  271. {$endif}
  272. end;
  273. var
  274. l : Longint;
  275. lr,hr : TConstExprInt;
  276. hp : tarrayconstructornode;
  277. begin
  278. if p.nodetype<>arrayconstructorn then
  279. internalerror(200205105);
  280. new(constset);
  281. {$ifdef oldset}
  282. FillChar(constset^,sizeof(constset^),0);
  283. {$else}
  284. constset^:=[];
  285. {$endif}
  286. htype.reset;
  287. constsetlo:=0;
  288. constsethi:=0;
  289. constp:=csetconstnode.create(nil,htype);
  290. constp.value_set:=constset;
  291. buildp:=constp;
  292. hp:=tarrayconstructornode(p);
  293. if assigned(hp.left) then
  294. begin
  295. while assigned(hp) do
  296. begin
  297. p4:=nil; { will contain the tree to create the set }
  298. {split a range into p2 and p3 }
  299. if hp.left.nodetype=arrayconstructorrangen then
  300. begin
  301. p2:=tarrayconstructorrangenode(hp.left).left;
  302. p3:=tarrayconstructorrangenode(hp.left).right;
  303. tarrayconstructorrangenode(hp.left).left:=nil;
  304. tarrayconstructorrangenode(hp.left).right:=nil;
  305. end
  306. else
  307. begin
  308. p2:=hp.left;
  309. hp.left:=nil;
  310. p3:=nil;
  311. end;
  312. resulttypepass(p2);
  313. if assigned(p3) then
  314. resulttypepass(p3);
  315. if codegenerror then
  316. break;
  317. case p2.resulttype.def.deftype of
  318. enumdef,
  319. orddef:
  320. begin
  321. getrange(p2.resulttype.def,lr,hr);
  322. if assigned(p3) then
  323. begin
  324. { this isn't good, you'll get problems with
  325. type t010 = 0..10;
  326. ts = set of t010;
  327. var s : ts;b : t010
  328. begin s:=[1,2,b]; end.
  329. if is_integer(p3^.resulttype.def) then
  330. begin
  331. inserttypeconv(p3,u8bitdef);
  332. end;
  333. }
  334. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  335. begin
  336. aktfilepos:=p3.fileinfo;
  337. CGMessage(type_e_typeconflict_in_set);
  338. end
  339. else
  340. begin
  341. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  342. begin
  343. if not(is_integer(p3.resulttype.def)) then
  344. htype:=p3.resulttype
  345. else
  346. begin
  347. inserttypeconv(p3,u8bittype);
  348. inserttypeconv(p2,u8bittype);
  349. end;
  350. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  351. do_set(l);
  352. p2.free;
  353. p3.free;
  354. end
  355. else
  356. begin
  357. update_constsethi(p2.resulttype);
  358. inserttypeconv(p2,htype);
  359. update_constsethi(p3.resulttype);
  360. inserttypeconv(p3,htype);
  361. if assigned(htype.def) then
  362. inserttypeconv(p3,htype)
  363. else
  364. inserttypeconv(p3,u8bittype);
  365. p4:=csetelementnode.create(p2,p3);
  366. end;
  367. end;
  368. end
  369. else
  370. begin
  371. { Single value }
  372. if p2.nodetype=ordconstn then
  373. begin
  374. if not(is_integer(p2.resulttype.def)) then
  375. update_constsethi(p2.resulttype)
  376. else
  377. inserttypeconv(p2,u8bittype);
  378. do_set(tordconstnode(p2).value);
  379. p2.free;
  380. end
  381. else
  382. begin
  383. update_constsethi(p2.resulttype);
  384. if assigned(htype.def) then
  385. inserttypeconv(p2,htype)
  386. else
  387. inserttypeconv(p2,u8bittype);
  388. p4:=csetelementnode.create(p2,nil);
  389. end;
  390. end;
  391. end;
  392. stringdef :
  393. begin
  394. { if we've already set elements which are constants }
  395. { throw an error }
  396. if ((htype.def=nil) and assigned(buildp)) or
  397. not(is_char(htype.def)) then
  398. CGMessage(type_e_typeconflict_in_set)
  399. else
  400. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  401. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  402. if htype.def=nil then
  403. htype:=cchartype;
  404. p2.free;
  405. end;
  406. else
  407. CGMessage(type_e_ordinal_expr_expected);
  408. end;
  409. { insert the set creation tree }
  410. if assigned(p4) then
  411. buildp:=caddnode.create(addn,buildp,p4);
  412. { load next and dispose current node }
  413. p2:=hp;
  414. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  415. tarrayconstructornode(p2).right:=nil;
  416. p2.free;
  417. end;
  418. if (htype.def=nil) then
  419. htype:=u8bittype;
  420. end
  421. else
  422. begin
  423. { empty set [], only remove node }
  424. p.free;
  425. end;
  426. { set the initial set type }
  427. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  428. { determine the resulttype for the tree }
  429. resulttypepass(buildp);
  430. { set the new tree }
  431. p:=buildp;
  432. end;
  433. {*****************************************************************************
  434. TTYPECONVNODE
  435. *****************************************************************************}
  436. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  437. begin
  438. inherited create(typeconvn,node);
  439. convtype:=tc_not_possible;
  440. totype:=t;
  441. if t.def=nil then
  442. internalerror(200103281);
  443. set_file_line(node);
  444. end;
  445. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  446. begin
  447. self.create(node,t);
  448. include(flags,nf_explicit);
  449. end;
  450. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  451. begin
  452. inherited ppuload(t,ppufile);
  453. ppufile.gettype(totype);
  454. convtype:=tconverttype(ppufile.getbyte);
  455. end;
  456. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  457. begin
  458. inherited ppuwrite(ppufile);
  459. ppufile.puttype(totype);
  460. ppufile.putbyte(byte(convtype));
  461. end;
  462. procedure ttypeconvnode.derefimpl;
  463. begin
  464. inherited derefimpl;
  465. totype.resolve;
  466. end;
  467. function ttypeconvnode.getcopy : tnode;
  468. var
  469. n : ttypeconvnode;
  470. begin
  471. n:=ttypeconvnode(inherited getcopy);
  472. n.convtype:=convtype;
  473. getcopy:=n;
  474. end;
  475. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  476. var
  477. t : tnode;
  478. begin
  479. result:=nil;
  480. if left.nodetype=ordconstn then
  481. begin
  482. { check if we have a valid pointer constant (JM) }
  483. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  484. if (sizeof(TConstPtrUInt) = 4) then
  485. begin
  486. if (tordconstnode(left).value < low(longint)) or
  487. (tordconstnode(left).value > high(cardinal)) then
  488. CGMessage(parser_e_range_check_error);
  489. end
  490. else if (sizeof(TConstPtrUInt) = 8) then
  491. begin
  492. if (tordconstnode(left).value < low(int64)) or
  493. (tordconstnode(left).value > high(qword)) then
  494. CGMessage(parser_e_range_check_error);
  495. end
  496. else
  497. internalerror(2001020801);
  498. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  499. result:=t;
  500. end
  501. else
  502. internalerror(200104023);
  503. end;
  504. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  505. begin
  506. result := ccallnode.createinternres(
  507. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  508. ccallparanode.create(left,nil),resulttype);
  509. left := nil;
  510. end;
  511. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  512. var
  513. arrsize: longint;
  514. begin
  515. with tarraydef(resulttype.def) do
  516. begin
  517. if highrange<lowrange then
  518. internalerror(75432653);
  519. arrsize := highrange-lowrange+1;
  520. end;
  521. if (left.nodetype = stringconstn) and
  522. { left.length+1 since there's always a terminating #0 character (JM) }
  523. (tstringconstnode(left).len+1 >= arrsize) and
  524. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  525. begin
  526. { handled separately }
  527. result := nil;
  528. exit;
  529. end;
  530. result := ccallnode.createinternres(
  531. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  532. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  533. cordconstnode.create(arrsize,s32bittype,true),nil)),resulttype);
  534. left := nil;
  535. end;
  536. function ttypeconvnode.resulttype_string_to_string : tnode;
  537. var
  538. procname: string[31];
  539. stringpara : tcallparanode;
  540. pw : pcompilerwidestring;
  541. pc : pchar;
  542. begin
  543. result:=nil;
  544. if left.nodetype=stringconstn then
  545. begin
  546. { convert ascii 2 unicode }
  547. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  548. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  549. begin
  550. initwidestring(pw);
  551. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  552. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  553. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  554. end
  555. else
  556. { convert unicode 2 ascii }
  557. if (tstringconstnode(left).st_type=st_widestring) and
  558. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  559. begin
  560. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  561. getmem(pc,getlengthwidestring(pw)+1);
  562. unicode2ascii(pw,pc);
  563. donewidestring(pw);
  564. tstringconstnode(left).value_str:=pc;
  565. end;
  566. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  567. tstringconstnode(left).resulttype:=resulttype;
  568. result:=left;
  569. left:=nil;
  570. end
  571. else
  572. begin
  573. { get the correct procedure name }
  574. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  575. '_to_'+tstringdef(resulttype.def).stringtypname;
  576. { create parameter (and remove left node from typeconvnode }
  577. { since it's reused as parameter) }
  578. stringpara := ccallparanode.create(left,nil);
  579. left := nil;
  580. { when converting to shortstrings, we have to pass high(destination) too }
  581. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  582. stringpara.right := ccallparanode.create(cinlinenode.create(
  583. in_high_x,false,self.getcopy),nil);
  584. { and create the callnode }
  585. result := ccallnode.createinternres(procname,stringpara,resulttype);
  586. end;
  587. end;
  588. function ttypeconvnode.resulttype_char_to_string : tnode;
  589. var
  590. procname: string[31];
  591. para : tcallparanode;
  592. hp : tstringconstnode;
  593. ws : pcompilerwidestring;
  594. begin
  595. result:=nil;
  596. if left.nodetype=ordconstn then
  597. begin
  598. if tstringdef(resulttype.def).string_typ=st_widestring then
  599. begin
  600. initwidestring(ws);
  601. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  602. hp:=cstringconstnode.createwstr(ws);
  603. donewidestring(ws);
  604. end
  605. else
  606. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  607. result:=hp;
  608. end
  609. else
  610. { shortstrings are handled 'inline' }
  611. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  612. begin
  613. { create the parameter }
  614. para := ccallparanode.create(left,nil);
  615. left := nil;
  616. { and the procname }
  617. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  618. { and finally the call }
  619. result := ccallnode.createinternres(procname,para,resulttype);
  620. end
  621. else
  622. begin
  623. { create word(byte(char) shl 8 or 1) for litte endian machines }
  624. { and word(byte(char) or 256) for big endian machines }
  625. left := ctypeconvnode.create_explicit(left,u8bittype);
  626. if (target_info.endian = endian_little) then
  627. left := caddnode.create(orn,
  628. cshlshrnode.create(shln,left,cordconstnode.create(8,s32bittype,false)),
  629. cordconstnode.create(1,s32bittype,false))
  630. else
  631. left := caddnode.create(orn,left,
  632. cordconstnode.create(1 shl 8,s32bittype,false));
  633. left := ctypeconvnode.create_explicit(left,u16bittype);
  634. resulttypepass(left);
  635. end;
  636. end;
  637. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  638. begin
  639. if resulttype.def.size <> 1 then
  640. begin
  641. { convert first to string, then to chararray }
  642. inserttypeconv(left,cshortstringtype);
  643. inserttypeconv(left,resulttype);
  644. result:=left;
  645. left := nil;
  646. exit;
  647. end;
  648. result := nil;
  649. end;
  650. function ttypeconvnode.resulttype_char_to_char : tnode;
  651. var
  652. hp : tordconstnode;
  653. begin
  654. result:=nil;
  655. if left.nodetype=ordconstn then
  656. begin
  657. if (torddef(resulttype.def).typ=uchar) and
  658. (torddef(left.resulttype.def).typ=uwidechar) then
  659. begin
  660. hp:=cordconstnode.create(
  661. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  662. cchartype,true);
  663. result:=hp;
  664. end
  665. else if (torddef(resulttype.def).typ=uwidechar) and
  666. (torddef(left.resulttype.def).typ=uchar) then
  667. begin
  668. hp:=cordconstnode.create(
  669. asciichar2unicode(chr(tordconstnode(left).value)),
  670. cwidechartype,true);
  671. result:=hp;
  672. end
  673. else
  674. internalerror(200105131);
  675. exit;
  676. end;
  677. end;
  678. function ttypeconvnode.resulttype_int_to_real : tnode;
  679. var
  680. t : trealconstnode;
  681. rv : bestreal;
  682. begin
  683. result:=nil;
  684. if left.nodetype=ordconstn then
  685. begin
  686. rv:=tordconstnode(left).value;
  687. if is_currency(resulttype.def) then
  688. rv:=rv*10000.0;
  689. t:=crealconstnode.create(rv,resulttype);
  690. result:=t;
  691. end
  692. else
  693. begin
  694. { multiply by 10000 for currency. We need to use getcopy to pass
  695. the argument because the current node is always disposed. Only
  696. inserting the multiply in the left node is not possible because
  697. it'll get in an infinite loop to convert int->currency }
  698. if is_currency(resulttype.def) then
  699. begin
  700. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  701. include(result.flags,nf_is_currency);
  702. end;
  703. end;
  704. end;
  705. function ttypeconvnode.resulttype_real_to_real : tnode;
  706. var
  707. t : tnode;
  708. begin
  709. result:=nil;
  710. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  711. begin
  712. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  713. include(left.flags,nf_is_currency);
  714. resulttypepass(left);
  715. end
  716. else
  717. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  718. begin
  719. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  720. include(left.flags,nf_is_currency);
  721. resulttypepass(left);
  722. end;
  723. if left.nodetype=realconstn then
  724. begin
  725. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  726. result:=t;
  727. end;
  728. end;
  729. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  730. begin
  731. result:=nil;
  732. if is_pwidechar(resulttype.def) then
  733. inserttypeconv(left,cwidestringtype)
  734. else
  735. inserttypeconv(left,cshortstringtype);
  736. { evaluate again, reset resulttype so the convert_typ
  737. will be calculated again and cstring_to_pchar will
  738. be used for futher conversion }
  739. result:=det_resulttype;
  740. end;
  741. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  742. begin
  743. result:=nil;
  744. if is_pwidechar(resulttype.def) then
  745. inserttypeconv(left,cwidestringtype);
  746. end;
  747. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  748. var
  749. hp : tnode;
  750. begin
  751. result:=nil;
  752. if left.nodetype<>arrayconstructorn then
  753. internalerror(5546);
  754. { remove typeconv node }
  755. hp:=left;
  756. left:=nil;
  757. { create a set constructor tree }
  758. arrayconstructor_to_set(hp);
  759. result:=hp;
  760. end;
  761. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  762. begin
  763. result := ccallnode.createinternres(
  764. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  765. ccallparanode.create(left,nil),resulttype);
  766. left := nil;
  767. end;
  768. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  769. begin
  770. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  771. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  772. end;
  773. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  774. begin
  775. { a dynamic array is a pointer to an array, so to convert it to }
  776. { an open array, we have to dereference it (JM) }
  777. result := ctypeconvnode.create_explicit(left,voidpointertype);
  778. { left is reused }
  779. left := nil;
  780. result := cderefnode.create(result);
  781. result.resulttype := resulttype;
  782. end;
  783. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  784. begin
  785. result := ccallnode.createinternres(
  786. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  787. ccallparanode.create(left,nil),resulttype);
  788. left := nil;
  789. end;
  790. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  791. begin
  792. result := ccallnode.createinternres(
  793. 'fpc_variant_to_dynarray',
  794. ccallparanode.create(caddrnode.create(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  795. ccallparanode.create(left,nil)
  796. ),resulttype);
  797. left := nil;
  798. end;
  799. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  800. begin
  801. result:=nil;
  802. end;
  803. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  804. {$ifdef fpc}
  805. const
  806. resulttypeconvert : array[tconverttype] of pointer = (
  807. {equal} nil,
  808. {not_possible} nil,
  809. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  810. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  811. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  812. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  813. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  814. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  815. { ansistring_2_pchar } nil,
  816. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  817. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  818. { array_2_pointer } nil,
  819. { pointer_2_array } nil,
  820. { int_2_int } nil,
  821. { int_2_bool } nil,
  822. { bool_2_bool } nil,
  823. { bool_2_int } nil,
  824. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  825. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  826. { proc_2_procvar } nil,
  827. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  828. { load_smallset } nil,
  829. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  830. { intf_2_string } nil,
  831. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  832. { class_2_intf } nil,
  833. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  834. { normal_2_smallset} nil,
  835. { dynarray_2_openarray} @resulttype_dynarray_to_openarray,
  836. { pwchar_2_string} @resulttype_pwchar_to_string,
  837. { variant_2_dynarray} @resulttype_variant_to_dynarray,
  838. { dynarray_2_variant} @resulttype_dynarray_to_variant
  839. );
  840. type
  841. tprocedureofobject = function : tnode of object;
  842. var
  843. r : packed record
  844. proc : pointer;
  845. obj : pointer;
  846. end;
  847. begin
  848. result:=nil;
  849. { this is a little bit dirty but it works }
  850. { and should be quite portable too }
  851. r.proc:=resulttypeconvert[c];
  852. r.obj:=self;
  853. if assigned(r.proc) then
  854. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  855. end;
  856. {$else}
  857. begin
  858. case c of
  859. tc_string_2_string: resulttype_string_to_string;
  860. tc_char_2_string : resulttype_char_to_string;
  861. tc_char_2_chararray: resulttype_char_to_chararray;
  862. tc_pchar_2_string : resulttype_pchar_to_string;
  863. tc_cchar_2_pchar : resulttype_cchar_to_pchar;
  864. tc_cstring_2_pchar : resulttype_cstring_to_pchar;
  865. tc_string_2_chararray : resulttype_string_to_chararray;
  866. tc_chararray_2_string : resulttype_chararray_to_string;
  867. tc_real_2_real : resulttype_real_to_real;
  868. tc_int_2_real : resulttype_int_to_real;
  869. tc_arrayconstructor_2_set : resulttype_arrayconstructor_to_set;
  870. tc_cord_2_pointer : resulttype_cord_to_pointer;
  871. tc_intf_2_guid : resulttype_interface_to_guid;
  872. tc_char_2_char : resulttype_char_to_char;
  873. tc_dynarray_2_openarray : resulttype_dynarray_to_openarray;
  874. tc_pwchar_2_string : resulttype_pwchar_to_string;
  875. tc_variant_2_dynarray : resulttype_variant_to_dynarray;
  876. tc_dynarray_2_variant : resulttype_dynarray_to_variant;
  877. end;
  878. end;
  879. {$Endif fpc}
  880. function ttypeconvnode.det_resulttype:tnode;
  881. var
  882. hp : tnode;
  883. currprocdef,
  884. aprocdef : tprocdef;
  885. eq : tequaltype;
  886. begin
  887. result:=nil;
  888. resulttype:=totype;
  889. resulttypepass(left);
  890. if codegenerror then
  891. exit;
  892. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,
  893. nf_explicit in flags,true,convtype,aprocdef);
  894. case eq of
  895. te_exact,
  896. te_equal :
  897. begin
  898. { because is_equal only checks the basetype for sets we need to
  899. check here if we are loading a smallset into a normalset }
  900. if (resulttype.def.deftype=setdef) and
  901. (left.resulttype.def.deftype=setdef) and
  902. ((tsetdef(resulttype.def).settype = smallset) xor
  903. (tsetdef(left.resulttype.def).settype = smallset)) then
  904. begin
  905. { constant sets can be converted by changing the type only }
  906. if (left.nodetype=setconstn) then
  907. begin
  908. tsetdef(left.resulttype.def).changesettype(tsetdef(resulttype.def).settype);
  909. result:=left;
  910. left:=nil;
  911. exit;
  912. end;
  913. if (tsetdef(resulttype.def).settype <> smallset) then
  914. convtype:=tc_load_smallset
  915. else
  916. convtype := tc_normal_2_smallset;
  917. exit;
  918. end
  919. else
  920. begin
  921. { Only leave when there is no conversion to do.
  922. We can still need to call a conversion routine,
  923. like the routine to convert a stringconstnode }
  924. if convtype in [tc_equal,tc_not_possible] then
  925. begin
  926. left.resulttype:=resulttype;
  927. result:=left;
  928. left:=nil;
  929. exit;
  930. end;
  931. end;
  932. end;
  933. te_convert_l1,
  934. te_convert_l2,
  935. te_convert_l3 :
  936. begin
  937. { nothing to do }
  938. end;
  939. te_convert_operator :
  940. begin
  941. procinfo.flags:=procinfo.flags or pi_do_call;
  942. hp:=ccallnode.create(ccallparanode.create(left,nil),
  943. overloaded_operators[_assignment],nil,nil);
  944. { tell explicitly which def we must use !! (PM) }
  945. tcallnode(hp).procdefinition:=aprocdef;
  946. left:=nil;
  947. result:=hp;
  948. exit;
  949. end;
  950. te_incompatible :
  951. begin
  952. { Procedures have a resulttype.def of voiddef and functions of their
  953. own resulttype.def. They will therefore always be incompatible with
  954. a procvar. Because isconvertable cannot check for procedures we
  955. use an extra check for them.}
  956. if (m_tp_procvar in aktmodeswitches) and
  957. (resulttype.def.deftype=procvardef) then
  958. begin
  959. if is_procsym_load(left) then
  960. begin
  961. if (left.nodetype<>addrn) then
  962. begin
  963. convtype:=tc_proc_2_procvar;
  964. { Now check if the procedure we are going to assign to
  965. the procvar, is compatible with the procvar's type }
  966. if proc_to_procvar_equal(tprocsym(tloadnode(left).symtableentry).first_procdef,
  967. tprocvardef(resulttype.def),true)=te_incompatible then
  968. CGMessage2(type_e_incompatible_types,tprocsym(tloadnode(left).symtableentry).first_procdef.typename,resulttype.def.typename);
  969. exit;
  970. end;
  971. end
  972. else
  973. if (left.nodetype=calln) and
  974. not assigned(tcallnode(left).left) then
  975. begin
  976. if assigned(tcallnode(left).right) then
  977. begin
  978. hp:=tcallnode(left).right.getcopy;
  979. currprocdef:=tprocdef(hp.resulttype.def);
  980. end
  981. else
  982. begin
  983. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  984. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  985. currprocdef,tcallnode(left).symtableproc);
  986. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  987. begin
  988. if assigned(tcallnode(left).methodpointer) then
  989. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  990. else
  991. tloadnode(hp).set_mp(cselfnode.create(tobjectdef(tcallnode(left).symtableprocentry.owner.defowner)));
  992. end;
  993. resulttypepass(hp);
  994. end;
  995. left.free;
  996. left:=hp;
  997. convtype:=tc_proc_2_procvar;
  998. { Now check if the procedure we are going to assign to
  999. the procvar, is compatible with the procvar's type }
  1000. if proc_to_procvar_equal(currprocdef,
  1001. tprocvardef(resulttype.def),true)=te_incompatible then
  1002. CGMessage2(type_e_incompatible_types,tprocdef(left.resulttype.def).typename,resulttype.def.typename);
  1003. exit;
  1004. end;
  1005. end;
  1006. { Handle explicit type conversions }
  1007. if nf_explicit in flags then
  1008. begin
  1009. { do common tc_equal cast }
  1010. convtype:=tc_equal;
  1011. { check if the result could be in a register }
  1012. if not(tstoreddef(resulttype.def).is_intregable) and
  1013. not(tstoreddef(resulttype.def).is_fpuregable) then
  1014. make_not_regable(left);
  1015. { class to class or object to object, with checkobject support }
  1016. if (resulttype.def.deftype=objectdef) and
  1017. (left.resulttype.def.deftype=objectdef) then
  1018. begin
  1019. if (cs_check_object in aktlocalswitches) then
  1020. begin
  1021. if is_class_or_interface(resulttype.def) then
  1022. begin
  1023. { we can translate the typeconvnode to 'as' when
  1024. typecasting to a class or interface }
  1025. hp:=casnode.create(left,cloadvmtnode.create(ctypenode.create(resulttype)));
  1026. left:=nil;
  1027. result:=hp;
  1028. exit;
  1029. end;
  1030. end
  1031. else
  1032. begin
  1033. { check if the types are related }
  1034. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1035. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1036. CGMessage2(type_w_classes_not_related,left.resulttype.def.typename,resulttype.def.typename);
  1037. end;
  1038. end
  1039. else
  1040. begin
  1041. { only if the same size or formal def }
  1042. if not(
  1043. (left.resulttype.def.deftype=formaldef) or
  1044. (
  1045. not(is_open_array(left.resulttype.def)) and
  1046. (left.resulttype.def.size=resulttype.def.size)
  1047. ) or
  1048. (
  1049. is_void(left.resulttype.def) and
  1050. (left.nodetype=derefn)
  1051. )
  1052. ) or
  1053. (left.resulttype.def.deftype=classrefdef) then
  1054. CGMessage(cg_e_illegal_type_conversion);
  1055. end;
  1056. end
  1057. else
  1058. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1059. end;
  1060. else
  1061. internalerror(200211231);
  1062. end;
  1063. { Give hint for unportable code }
  1064. if ((left.resulttype.def.deftype=orddef) and
  1065. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1066. ((resulttype.def.deftype=orddef) and
  1067. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) then
  1068. CGMessage(cg_h_pointer_to_longint_conv_not_portable);
  1069. { Constant folding and other node transitions to
  1070. remove the typeconv node }
  1071. case left.nodetype of
  1072. loadn :
  1073. begin
  1074. { tp7 procvar support, when right is not a procvardef and we got a
  1075. loadn of a procvar (ignore procedures as void can not be converted)
  1076. then convert to a calln, the check for the result is already done
  1077. in is_convertible, also no conflict with @procvar is here because
  1078. that has an extra addrn }
  1079. if (m_tp_procvar in aktmodeswitches) and
  1080. (resulttype.def.deftype<>procvardef) and
  1081. (left.resulttype.def.deftype=procvardef) and
  1082. (not is_void(tprocvardef(left.resulttype.def).rettype.def)) then
  1083. begin
  1084. hp:=ccallnode.create(nil,nil,nil,nil);
  1085. tcallnode(hp).set_procvar(left);
  1086. resulttypepass(hp);
  1087. left:=hp;
  1088. end;
  1089. end;
  1090. niln :
  1091. begin
  1092. { nil to ordinal node }
  1093. if (resulttype.def.deftype=orddef) then
  1094. begin
  1095. hp:=cordconstnode.create(0,resulttype,true);
  1096. result:=hp;
  1097. exit;
  1098. end
  1099. else
  1100. { fold nil to any pointer type }
  1101. if (resulttype.def.deftype=pointerdef) then
  1102. begin
  1103. hp:=cnilnode.create;
  1104. hp.resulttype:=resulttype;
  1105. result:=hp;
  1106. exit;
  1107. end
  1108. else
  1109. { remove typeconv after niln, but not when the result is a
  1110. methodpointer. The typeconv of the methodpointer will then
  1111. take care of updateing size of niln to OS_64 }
  1112. if not((resulttype.def.deftype=procvardef) and
  1113. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1114. begin
  1115. left.resulttype:=resulttype;
  1116. result:=left;
  1117. left:=nil;
  1118. exit;
  1119. end;
  1120. end;
  1121. ordconstn :
  1122. begin
  1123. { ordinal contants can be directly converted }
  1124. { but not char to char because it is a widechar to char or via versa }
  1125. { which needs extra code to do the code page transistion }
  1126. if is_ordinal(resulttype.def) and
  1127. not(convtype=tc_char_2_char) then
  1128. begin
  1129. { replace the resulttype and recheck the range }
  1130. left.resulttype:=resulttype;
  1131. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1132. result:=left;
  1133. left:=nil;
  1134. exit;
  1135. end;
  1136. end;
  1137. pointerconstn :
  1138. begin
  1139. { pointerconstn to any pointer is folded too }
  1140. if (resulttype.def.deftype=pointerdef) then
  1141. begin
  1142. left.resulttype:=resulttype;
  1143. result:=left;
  1144. left:=nil;
  1145. exit;
  1146. end
  1147. { constant pointer to ordinal }
  1148. else if is_ordinal(resulttype.def) then
  1149. begin
  1150. hp:=cordconstnode.create(tpointerconstnode(left).value,
  1151. resulttype,true);
  1152. result:=hp;
  1153. exit;
  1154. end;
  1155. end;
  1156. end;
  1157. { now call the resulttype helper to do constant folding }
  1158. result:=resulttype_call_helper(convtype);
  1159. end;
  1160. procedure Ttypeconvnode.mark_write;
  1161. begin
  1162. left.mark_write;
  1163. end;
  1164. function ttypeconvnode.first_cord_to_pointer : tnode;
  1165. begin
  1166. result:=nil;
  1167. internalerror(200104043);
  1168. end;
  1169. function ttypeconvnode.first_int_to_int : tnode;
  1170. begin
  1171. first_int_to_int:=nil;
  1172. if (left.expectloc<>LOC_REGISTER) and
  1173. (resulttype.def.size>left.resulttype.def.size) then
  1174. expectloc:=LOC_REGISTER
  1175. else
  1176. expectloc:=left.expectloc;
  1177. if is_64bitint(resulttype.def) then
  1178. registers32:=max(registers32,2)
  1179. else
  1180. registers32:=max(registers32,1);
  1181. end;
  1182. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1183. begin
  1184. first_cstring_to_pchar:=nil;
  1185. registers32:=1;
  1186. expectloc:=LOC_REGISTER;
  1187. end;
  1188. function ttypeconvnode.first_string_to_chararray : tnode;
  1189. begin
  1190. first_string_to_chararray:=nil;
  1191. expectloc:=left.expectloc;
  1192. end;
  1193. function ttypeconvnode.first_char_to_string : tnode;
  1194. begin
  1195. first_char_to_string:=nil;
  1196. expectloc:=LOC_REFERENCE;
  1197. end;
  1198. function ttypeconvnode.first_nothing : tnode;
  1199. begin
  1200. first_nothing:=nil;
  1201. end;
  1202. function ttypeconvnode.first_array_to_pointer : tnode;
  1203. begin
  1204. first_array_to_pointer:=nil;
  1205. if registers32<1 then
  1206. registers32:=1;
  1207. expectloc:=LOC_REGISTER;
  1208. end;
  1209. function ttypeconvnode.first_int_to_real: tnode;
  1210. var
  1211. fname: string[32];
  1212. typname : string[12];
  1213. begin
  1214. { Get the type name }
  1215. { Normally the typename should be one of the following:
  1216. single, double - carl
  1217. }
  1218. typname := lower(pbestrealtype^.def.gettypename);
  1219. { converting a 64bit integer to a float requires a helper }
  1220. if is_64bitint(left.resulttype.def) then
  1221. begin
  1222. if is_signed(left.resulttype.def) then
  1223. fname := 'fpc_int64_to_'+typname
  1224. else
  1225. {$warning generic conversion from int to float does not support unsigned integers}
  1226. fname := 'fpc_int64_to_'+typname;
  1227. result := ccallnode.createintern(fname,ccallparanode.create(
  1228. left,nil));
  1229. left:=nil;
  1230. firstpass(result);
  1231. exit;
  1232. end
  1233. else
  1234. { other integers are supposed to be 32 bit }
  1235. begin
  1236. {$warning generic conversion from int to float does not support unsigned integers}
  1237. if is_signed(left.resulttype.def) then
  1238. fname := 'fpc_longint_to_'+typname
  1239. else
  1240. fname := 'fpc_longint_to_'+typname;
  1241. result := ccallnode.createintern(fname,ccallparanode.create(
  1242. left,nil));
  1243. left:=nil;
  1244. firstpass(result);
  1245. exit;
  1246. end;
  1247. end;
  1248. function ttypeconvnode.first_real_to_real : tnode;
  1249. begin
  1250. first_real_to_real:=nil;
  1251. { comp isn't a floating type }
  1252. {$ifdef i386}
  1253. if (tfloatdef(resulttype.def).typ=s64comp) and
  1254. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1255. not (nf_explicit in flags) then
  1256. CGMessage(type_w_convert_real_2_comp);
  1257. {$endif}
  1258. if registersfpu<1 then
  1259. registersfpu:=1;
  1260. expectloc:=LOC_FPUREGISTER;
  1261. end;
  1262. function ttypeconvnode.first_pointer_to_array : tnode;
  1263. begin
  1264. first_pointer_to_array:=nil;
  1265. if registers32<1 then
  1266. registers32:=1;
  1267. expectloc:=LOC_REFERENCE;
  1268. end;
  1269. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1270. begin
  1271. first_cchar_to_pchar:=nil;
  1272. internalerror(200104021);
  1273. end;
  1274. function ttypeconvnode.first_bool_to_int : tnode;
  1275. begin
  1276. first_bool_to_int:=nil;
  1277. { byte(boolean) or word(wordbool) or longint(longbool) must
  1278. be accepted for var parameters }
  1279. if (nf_explicit in flags) and
  1280. (left.resulttype.def.size=resulttype.def.size) and
  1281. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1282. exit;
  1283. { when converting to 64bit, first convert to a 32bit int and then }
  1284. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1285. if resulttype.def.size > sizeof(aword) then
  1286. begin
  1287. result := ctypeconvnode.create_explicit(left,u32bittype);
  1288. result := ctypeconvnode.create(result,resulttype);
  1289. left := nil;
  1290. firstpass(result);
  1291. exit;
  1292. end;
  1293. expectloc:=LOC_REGISTER;
  1294. if registers32<1 then
  1295. registers32:=1;
  1296. end;
  1297. function ttypeconvnode.first_int_to_bool : tnode;
  1298. begin
  1299. first_int_to_bool:=nil;
  1300. { byte(boolean) or word(wordbool) or longint(longbool) must
  1301. be accepted for var parameters }
  1302. if (nf_explicit in flags) and
  1303. (left.resulttype.def.size=resulttype.def.size) and
  1304. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1305. exit;
  1306. expectloc:=LOC_REGISTER;
  1307. { need if bool to bool !!
  1308. not very nice !!
  1309. insertypeconv(left,s32bittype);
  1310. left.explizit:=true;
  1311. firstpass(left); }
  1312. if registers32<1 then
  1313. registers32:=1;
  1314. end;
  1315. function ttypeconvnode.first_bool_to_bool : tnode;
  1316. begin
  1317. first_bool_to_bool:=nil;
  1318. expectloc:=LOC_REGISTER;
  1319. if registers32<1 then
  1320. registers32:=1;
  1321. end;
  1322. function ttypeconvnode.first_char_to_char : tnode;
  1323. begin
  1324. first_char_to_char:=first_int_to_int;
  1325. end;
  1326. function ttypeconvnode.first_proc_to_procvar : tnode;
  1327. begin
  1328. first_proc_to_procvar:=nil;
  1329. if assigned(tunarynode(left).left) then
  1330. begin
  1331. if (left.expectloc<>LOC_CREFERENCE) then
  1332. CGMessage(cg_e_illegal_expression);
  1333. registers32:=left.registers32;
  1334. expectloc:=left.expectloc
  1335. end
  1336. else
  1337. begin
  1338. registers32:=left.registers32;
  1339. if registers32<1 then
  1340. registers32:=1;
  1341. expectloc:=LOC_REGISTER;
  1342. end
  1343. end;
  1344. function ttypeconvnode.first_load_smallset : tnode;
  1345. var
  1346. srsym: ttypesym;
  1347. p: tcallparanode;
  1348. begin
  1349. if not searchsystype('FPC_SMALL_SET',srsym) then
  1350. internalerror(200108313);
  1351. p := ccallparanode.create(left,nil);
  1352. { reused }
  1353. left := nil;
  1354. { convert parameter explicitely to fpc_small_set }
  1355. p.left := ctypeconvnode.create_explicit(p.left,srsym.restype);
  1356. { create call, adjust resulttype }
  1357. result :=
  1358. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1359. firstpass(result);
  1360. end;
  1361. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1362. begin
  1363. first_ansistring_to_pchar:=nil;
  1364. expectloc:=LOC_REGISTER;
  1365. if registers32<1 then
  1366. registers32:=1;
  1367. end;
  1368. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1369. begin
  1370. first_arrayconstructor_to_set:=nil;
  1371. internalerror(200104022);
  1372. end;
  1373. function ttypeconvnode.first_class_to_intf : tnode;
  1374. begin
  1375. first_class_to_intf:=nil;
  1376. expectloc:=LOC_REFERENCE;
  1377. if registers32<1 then
  1378. registers32:=1;
  1379. end;
  1380. function ttypeconvnode._first_int_to_int : tnode;
  1381. begin
  1382. result:=first_int_to_int;
  1383. end;
  1384. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1385. begin
  1386. result:=first_cstring_to_pchar;
  1387. end;
  1388. function ttypeconvnode._first_string_to_chararray : tnode;
  1389. begin
  1390. result:=first_string_to_chararray;
  1391. end;
  1392. function ttypeconvnode._first_char_to_string : tnode;
  1393. begin
  1394. result:=first_char_to_string;
  1395. end;
  1396. function ttypeconvnode._first_nothing : tnode;
  1397. begin
  1398. result:=first_nothing;
  1399. end;
  1400. function ttypeconvnode._first_array_to_pointer : tnode;
  1401. begin
  1402. result:=first_array_to_pointer;
  1403. end;
  1404. function ttypeconvnode._first_int_to_real : tnode;
  1405. begin
  1406. result:=first_int_to_real;
  1407. end;
  1408. function ttypeconvnode._first_real_to_real : tnode;
  1409. begin
  1410. result:=first_real_to_real;
  1411. end;
  1412. function ttypeconvnode._first_pointer_to_array : tnode;
  1413. begin
  1414. result:=first_pointer_to_array;
  1415. end;
  1416. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1417. begin
  1418. result:=first_cchar_to_pchar;
  1419. end;
  1420. function ttypeconvnode._first_bool_to_int : tnode;
  1421. begin
  1422. result:=first_bool_to_int;
  1423. end;
  1424. function ttypeconvnode._first_int_to_bool : tnode;
  1425. begin
  1426. result:=first_int_to_bool;
  1427. end;
  1428. function ttypeconvnode._first_bool_to_bool : tnode;
  1429. begin
  1430. result:=first_bool_to_bool;
  1431. end;
  1432. function ttypeconvnode._first_proc_to_procvar : tnode;
  1433. begin
  1434. result:=first_proc_to_procvar;
  1435. end;
  1436. function ttypeconvnode._first_load_smallset : tnode;
  1437. begin
  1438. result:=first_load_smallset;
  1439. end;
  1440. function ttypeconvnode._first_cord_to_pointer : tnode;
  1441. begin
  1442. result:=first_cord_to_pointer;
  1443. end;
  1444. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1445. begin
  1446. result:=first_ansistring_to_pchar;
  1447. end;
  1448. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1449. begin
  1450. result:=first_arrayconstructor_to_set;
  1451. end;
  1452. function ttypeconvnode._first_class_to_intf : tnode;
  1453. begin
  1454. result:=first_class_to_intf;
  1455. end;
  1456. function ttypeconvnode._first_char_to_char : tnode;
  1457. begin
  1458. result:=first_char_to_char;
  1459. end;
  1460. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1461. const
  1462. firstconvert : array[tconverttype] of pointer = (
  1463. @ttypeconvnode._first_nothing, {equal}
  1464. @ttypeconvnode._first_nothing, {not_possible}
  1465. nil, { removed in resulttype_string_to_string }
  1466. @ttypeconvnode._first_char_to_string,
  1467. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1468. nil, { removed in resulttype_chararray_to_string }
  1469. @ttypeconvnode._first_cchar_to_pchar,
  1470. @ttypeconvnode._first_cstring_to_pchar,
  1471. @ttypeconvnode._first_ansistring_to_pchar,
  1472. @ttypeconvnode._first_string_to_chararray,
  1473. nil, { removed in resulttype_chararray_to_string }
  1474. @ttypeconvnode._first_array_to_pointer,
  1475. @ttypeconvnode._first_pointer_to_array,
  1476. @ttypeconvnode._first_int_to_int,
  1477. @ttypeconvnode._first_int_to_bool,
  1478. @ttypeconvnode._first_bool_to_bool,
  1479. @ttypeconvnode._first_bool_to_int,
  1480. @ttypeconvnode._first_real_to_real,
  1481. @ttypeconvnode._first_int_to_real,
  1482. @ttypeconvnode._first_proc_to_procvar,
  1483. @ttypeconvnode._first_arrayconstructor_to_set,
  1484. @ttypeconvnode._first_load_smallset,
  1485. @ttypeconvnode._first_cord_to_pointer,
  1486. @ttypeconvnode._first_nothing,
  1487. @ttypeconvnode._first_nothing,
  1488. @ttypeconvnode._first_class_to_intf,
  1489. @ttypeconvnode._first_char_to_char,
  1490. @ttypeconvnode._first_nothing,
  1491. @ttypeconvnode._first_nothing,
  1492. nil,
  1493. nil,
  1494. nil
  1495. );
  1496. type
  1497. tprocedureofobject = function : tnode of object;
  1498. var
  1499. r : packed record
  1500. proc : pointer;
  1501. obj : pointer;
  1502. end;
  1503. begin
  1504. { this is a little bit dirty but it works }
  1505. { and should be quite portable too }
  1506. r.proc:=firstconvert[c];
  1507. r.obj:=self;
  1508. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1509. end;
  1510. function ttypeconvnode.pass_1 : tnode;
  1511. begin
  1512. result:=nil;
  1513. firstpass(left);
  1514. if codegenerror then
  1515. exit;
  1516. { load the value_str from the left part }
  1517. registers32:=left.registers32;
  1518. registersfpu:=left.registersfpu;
  1519. {$ifdef SUPPORT_MMX}
  1520. registersmmx:=left.registersmmx;
  1521. {$endif}
  1522. expectloc:=left.expectloc;
  1523. if nf_explicit in flags then
  1524. begin
  1525. { check if the result could be in a register }
  1526. if not(tstoreddef(resulttype.def).is_intregable) and
  1527. not(tstoreddef(resulttype.def).is_fpuregable) then
  1528. make_not_regable(left);
  1529. end;
  1530. result:=first_call_helper(convtype);
  1531. end;
  1532. function ttypeconvnode.docompare(p: tnode) : boolean;
  1533. begin
  1534. docompare :=
  1535. inherited docompare(p) and
  1536. (convtype = ttypeconvnode(p).convtype);
  1537. end;
  1538. {*****************************************************************************
  1539. TISNODE
  1540. *****************************************************************************}
  1541. constructor tisnode.create(l,r : tnode);
  1542. begin
  1543. inherited create(isn,l,r);
  1544. end;
  1545. function tisnode.det_resulttype:tnode;
  1546. var
  1547. paras: tcallparanode;
  1548. begin
  1549. result:=nil;
  1550. resulttypepass(left);
  1551. resulttypepass(right);
  1552. set_varstate(left,true);
  1553. set_varstate(right,true);
  1554. if codegenerror then
  1555. exit;
  1556. if (right.resulttype.def.deftype=classrefdef) then
  1557. begin
  1558. { left must be a class }
  1559. if is_class(left.resulttype.def) then
  1560. begin
  1561. { the operands must be related }
  1562. if (not(tobjectdef(left.resulttype.def).is_related(
  1563. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1564. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1565. tobjectdef(left.resulttype.def)))) then
  1566. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1567. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1568. end
  1569. else
  1570. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1571. { call fpc_do_is helper }
  1572. paras := ccallparanode.create(
  1573. left,
  1574. ccallparanode.create(
  1575. right,nil));
  1576. result := ccallnode.createintern('fpc_do_is',paras);
  1577. left := nil;
  1578. right := nil;
  1579. end
  1580. else if is_interface(right.resulttype.def) then
  1581. begin
  1582. { left is a class }
  1583. if is_class(left.resulttype.def) then
  1584. begin
  1585. { the operands must be related }
  1586. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1587. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1588. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1589. end
  1590. { left is an interface }
  1591. else if is_interface(left.resulttype.def) then
  1592. begin
  1593. { the operands must be related }
  1594. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1595. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1596. CGMessage(type_e_mismatch);
  1597. end
  1598. else
  1599. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1600. { call fpc_do_is helper }
  1601. paras := ccallparanode.create(
  1602. left,
  1603. ccallparanode.create(
  1604. right,nil));
  1605. result := ccallnode.createintern('fpc_do_is',paras);
  1606. left := nil;
  1607. right := nil;
  1608. end
  1609. else
  1610. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1611. resulttype:=booltype;
  1612. end;
  1613. function tisnode.pass_1 : tnode;
  1614. begin
  1615. internalerror(200204254);
  1616. result:=nil;
  1617. end;
  1618. { dummy pass_2, it will never be called, but we need one since }
  1619. { you can't instantiate an abstract class }
  1620. procedure tisnode.pass_2;
  1621. begin
  1622. end;
  1623. {*****************************************************************************
  1624. TASNODE
  1625. *****************************************************************************}
  1626. constructor tasnode.create(l,r : tnode);
  1627. begin
  1628. inherited create(asn,l,r);
  1629. call := nil;
  1630. end;
  1631. destructor tasnode.destroy;
  1632. begin
  1633. call.free;
  1634. inherited destroy;
  1635. end;
  1636. function tasnode.det_resulttype:tnode;
  1637. var
  1638. hp : tnode;
  1639. begin
  1640. result:=nil;
  1641. resulttypepass(right);
  1642. resulttypepass(left);
  1643. set_varstate(right,true);
  1644. set_varstate(left,true);
  1645. if codegenerror then
  1646. exit;
  1647. if (right.resulttype.def.deftype=classrefdef) then
  1648. begin
  1649. { left must be a class }
  1650. if is_class(left.resulttype.def) then
  1651. begin
  1652. { the operands must be related }
  1653. if (not(tobjectdef(left.resulttype.def).is_related(
  1654. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1655. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1656. tobjectdef(left.resulttype.def)))) then
  1657. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1658. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1659. end
  1660. else
  1661. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1662. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1663. end
  1664. else if is_interface(right.resulttype.def) then
  1665. begin
  1666. { left is a class }
  1667. if is_class(left.resulttype.def) then
  1668. begin
  1669. { the operands must be related
  1670. no, because the class instance could be a child class of the current one which
  1671. implements additional interfaces (FK)
  1672. b:=false;
  1673. o:=tobjectdef(left.resulttype.def);
  1674. while assigned(o) do
  1675. begin
  1676. if assigned(o.implementedinterfaces) and
  1677. (o.implementedinterfaces.searchintf(right.resulttype.def)<>-1) then
  1678. begin
  1679. b:=true;
  1680. break;
  1681. end;
  1682. o:=o.childof;
  1683. end;
  1684. if not(b) then
  1685. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1686. }
  1687. end
  1688. { left is an interface }
  1689. else if is_interface(left.resulttype.def) then
  1690. begin
  1691. { the operands must be related
  1692. we don't necessarily know how the both interfaces are implemented, so we can't do this check (FK)
  1693. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1694. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1695. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1696. }
  1697. end
  1698. else
  1699. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1700. resulttype:=right.resulttype;
  1701. { load the GUID of the interface }
  1702. if (right.nodetype=typen) then
  1703. begin
  1704. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  1705. begin
  1706. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  1707. right.free;
  1708. right:=hp;
  1709. end
  1710. else
  1711. internalerror(200206282);
  1712. resulttypepass(right);
  1713. end;
  1714. end
  1715. else
  1716. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1717. end;
  1718. function tasnode.getcopy: tnode;
  1719. begin
  1720. result := inherited getcopy;
  1721. if assigned(call) then
  1722. tasnode(result).call := call.getcopy
  1723. else
  1724. tasnode(result).call := nil;
  1725. end;
  1726. function tasnode.pass_1 : tnode;
  1727. var
  1728. procname: string;
  1729. begin
  1730. result:=nil;
  1731. if not assigned(call) then
  1732. begin
  1733. if is_class(left.resulttype.def) and
  1734. (right.resulttype.def.deftype=classrefdef) then
  1735. call := ccallnode.createinternres('fpc_do_as',
  1736. ccallparanode.create(left,ccallparanode.create(right,nil)),
  1737. resulttype)
  1738. else
  1739. begin
  1740. if is_class(left.resulttype.def) then
  1741. procname := 'fpc_class_as_intf'
  1742. else
  1743. procname := 'fpc_intf_as';
  1744. call := ccallnode.createinternres(procname,
  1745. ccallparanode.create(right,ccallparanode.create(left,nil)),
  1746. resulttype);
  1747. end;
  1748. left := nil;
  1749. right := nil;
  1750. firstpass(call);
  1751. if codegenerror then
  1752. exit;
  1753. expectloc:=call.expectloc;
  1754. registers32:=call.registers32;
  1755. registersfpu:=call.registersfpu;
  1756. {$ifdef SUPPORT_MMX}
  1757. registersmmx:=call.registersmmx;
  1758. {$endif SUPPORT_MMX}
  1759. end;
  1760. end;
  1761. begin
  1762. ctypeconvnode:=ttypeconvnode;
  1763. casnode:=tasnode;
  1764. cisnode:=tisnode;
  1765. end.
  1766. {
  1767. $Log$
  1768. Revision 1.107 2003-04-23 13:13:08 peter
  1769. * fix checking of procdef type which was broken since loadn returned
  1770. pointertype for tp procvar
  1771. Revision 1.106 2003/04/23 10:10:07 peter
  1772. * expectloc fixes
  1773. Revision 1.105 2003/04/22 23:50:23 peter
  1774. * firstpass uses expectloc
  1775. * checks if there are differences between the expectloc and
  1776. location.loc from secondpass in EXTDEBUG
  1777. Revision 1.104 2003/04/22 09:52:30 peter
  1778. * do not convert procvars with void return to callnode
  1779. Revision 1.103 2003/03/17 18:54:23 peter
  1780. * fix missing self setting for method to procvar conversion in
  1781. tp_procvar mode
  1782. Revision 1.102 2003/02/15 22:15:57 carl
  1783. * generic conversaion routines only work on signed types
  1784. Revision 1.101 2003/01/16 22:13:52 peter
  1785. * convert_l3 convertlevel added. This level is used for conversions
  1786. where information can be lost like converting widestring->ansistring
  1787. or dword->byte
  1788. Revision 1.100 2003/01/15 01:44:32 peter
  1789. * merged methodpointer fixes from 1.0.x
  1790. Revision 1.99 2003/01/09 21:43:39 peter
  1791. * constant string conversion fixed, it's now equal to both
  1792. shortstring, ansistring and the typeconvnode will return
  1793. te_equal but still return convtype to change the constnode
  1794. Revision 1.98 2003/01/05 22:41:40 peter
  1795. * move code that checks for longint-pointer conversion hint
  1796. Revision 1.97 2003/01/03 12:15:56 daniel
  1797. * Removed ifdefs around notifications
  1798. ifdefs around for loop optimizations remain
  1799. Revision 1.96 2002/12/22 16:34:49 peter
  1800. * proc-procvar crash fixed (tw2277)
  1801. Revision 1.95 2002/12/20 16:01:26 peter
  1802. * don't allow class(classref) conversion
  1803. Revision 1.94 2002/12/05 14:27:26 florian
  1804. * some variant <-> dyn. array stuff
  1805. Revision 1.93 2002/11/30 10:45:14 carl
  1806. * fix bug with checking of duplicated items in sets (new sets bug only)
  1807. Revision 1.92 2002/11/27 19:43:21 carl
  1808. * updated notes and hints
  1809. Revision 1.91 2002/11/27 13:11:38 peter
  1810. * more currency fixes, taddcurr runs now successfull
  1811. Revision 1.90 2002/11/27 11:29:21 peter
  1812. * when converting from and to currency divide or multiple the
  1813. result by 10000
  1814. Revision 1.89 2002/11/25 17:43:18 peter
  1815. * splitted defbase in defutil,symutil,defcmp
  1816. * merged isconvertable and is_equal into compare_defs(_ext)
  1817. * made operator search faster by walking the list only once
  1818. Revision 1.88 2002/11/17 16:31:56 carl
  1819. * memory optimization (3-4%) : cleanup of tai fields,
  1820. cleanup of tdef and tsym fields.
  1821. * make it work for m68k
  1822. Revision 1.87 2002/10/10 16:07:57 florian
  1823. + several widestring/pwidechar related stuff added
  1824. Revision 1.86 2002/10/06 16:10:23 florian
  1825. * when compiling <interface> as <interface> we can't assume
  1826. anything about relation
  1827. Revision 1.85 2002/10/05 12:43:25 carl
  1828. * fixes for Delphi 6 compilation
  1829. (warning : Some features do not work under Delphi)
  1830. Revision 1.84 2002/10/02 20:23:50 florian
  1831. - removed the relation check for <class> as <interface> because we don't
  1832. know the runtime type of <class>! It could be a child class of the given type
  1833. which implements additional interfaces
  1834. Revision 1.83 2002/10/02 20:17:14 florian
  1835. + the as operator for <class> as <interface> has to check the parent classes as well
  1836. Revision 1.82 2002/09/30 07:00:47 florian
  1837. * fixes to common code to get the alpha compiler compiled applied
  1838. Revision 1.81 2002/09/16 14:11:13 peter
  1839. * add argument to equal_paras() to support default values or not
  1840. Revision 1.80 2002/09/07 20:40:23 carl
  1841. * cardinal -> longword
  1842. Revision 1.79 2002/09/07 15:25:03 peter
  1843. * old logs removed and tabs fixed
  1844. Revision 1.78 2002/09/07 12:16:04 carl
  1845. * second part bug report 1996 fix, testrange in cordconstnode
  1846. only called if option is set (also make parsing a tiny faster)
  1847. Revision 1.77 2002/09/05 05:56:07 jonas
  1848. - reverted my last commit, it was completely bogus :(
  1849. Revision 1.75 2002/09/02 19:24:42 peter
  1850. * array of char support for Str()
  1851. Revision 1.74 2002/09/01 08:01:16 daniel
  1852. * Removed sets from Tcallnode.det_resulttype
  1853. + Added read/write notifications of variables. These will be usefull
  1854. for providing information for several optimizations. For example
  1855. the value of the loop variable of a for loop does matter is the
  1856. variable is read after the for loop, but if it's no longer used
  1857. or written, it doesn't matter and this can be used to optimize
  1858. the loop code generation.
  1859. Revision 1.73 2002/08/23 16:14:49 peter
  1860. * tempgen cleanup
  1861. * tt_noreuse temp type added that will be used in genentrycode
  1862. Revision 1.72 2002/08/20 18:23:33 jonas
  1863. * the as node again uses a compilerproc
  1864. + (untested) support for interface "as" statements
  1865. Revision 1.71 2002/08/19 19:36:43 peter
  1866. * More fixes for cross unit inlining, all tnodes are now implemented
  1867. * Moved pocall_internconst to po_internconst because it is not a
  1868. calling type at all and it conflicted when inlining of these small
  1869. functions was requested
  1870. Revision 1.70 2002/08/17 09:23:36 florian
  1871. * first part of procinfo rewrite
  1872. Revision 1.69 2002/08/14 19:26:55 carl
  1873. + generic int_to_real type conversion
  1874. + generic unaryminus node
  1875. Revision 1.68 2002/08/11 16:08:55 florian
  1876. + support of explicit type case boolean->char
  1877. Revision 1.67 2002/08/11 15:28:00 florian
  1878. + support of explicit type case <any ordinal type>->pointer
  1879. (delphi mode only)
  1880. Revision 1.66 2002/08/09 07:33:01 florian
  1881. * a couple of interface related fixes
  1882. Revision 1.65 2002/07/29 21:23:42 florian
  1883. * more fixes for the ppc
  1884. + wrappers for the tcnvnode.first_* stuff introduced
  1885. Revision 1.64 2002/07/23 12:34:30 daniel
  1886. * Readded old set code. To use it define 'oldset'. Activated by default
  1887. for ppc.
  1888. Revision 1.63 2002/07/23 09:51:22 daniel
  1889. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1890. are worth comitting.
  1891. Revision 1.62 2002/07/22 11:48:04 daniel
  1892. * Sets are now internally sets.
  1893. Revision 1.61 2002/07/20 17:16:02 florian
  1894. + source code page support
  1895. Revision 1.60 2002/07/20 11:57:54 florian
  1896. * types.pas renamed to defbase.pas because D6 contains a types
  1897. unit so this would conflicts if D6 programms are compiled
  1898. + Willamette/SSE2 instructions to assembler added
  1899. Revision 1.59 2002/07/01 16:23:53 peter
  1900. * cg64 patch
  1901. * basics for currency
  1902. * asnode updates for class and interface (not finished)
  1903. Revision 1.58 2002/05/18 13:34:09 peter
  1904. * readded missing revisions
  1905. }