ncnv.pas 83 KB

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