ncnv.pas 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totype : ttype;
  29. convtype : tconverttype;
  30. constructor create(node : tnode;const t : ttype);virtual;
  31. constructor create_explicit(node : tnode;const t : ttype);
  32. constructor create_internal(node : tnode;const t : ttype);
  33. constructor create_proc_to_procvar(node : tnode);
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure buildderefimpl;override;
  37. procedure derefimpl;override;
  38. function _getcopy : tnode;override;
  39. procedure printnodeinfo(var t : text);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. function assign_allowed:boolean;
  45. procedure second_call_helper(c : tconverttype);
  46. private
  47. function resulttype_int_to_int : tnode;
  48. function resulttype_cord_to_pointer : tnode;
  49. function resulttype_chararray_to_string : tnode;
  50. function resulttype_string_to_chararray : tnode;
  51. function resulttype_string_to_string : tnode;
  52. function resulttype_char_to_string : tnode;
  53. function resulttype_char_to_chararray : tnode;
  54. function resulttype_int_to_real : tnode;
  55. function resulttype_real_to_real : tnode;
  56. function resulttype_real_to_currency : tnode;
  57. function resulttype_cchar_to_pchar : tnode;
  58. function resulttype_cstring_to_pchar : tnode;
  59. function resulttype_cstring_to_int : tnode;
  60. function resulttype_char_to_char : tnode;
  61. function resulttype_arrayconstructor_to_set : tnode;
  62. function resulttype_pchar_to_string : tnode;
  63. function resulttype_interface_to_guid : tnode;
  64. function resulttype_dynarray_to_openarray : tnode;
  65. function resulttype_pwchar_to_string : tnode;
  66. function resulttype_variant_to_dynarray : tnode;
  67. function resulttype_dynarray_to_variant : tnode;
  68. function resulttype_call_helper(c : tconverttype) : tnode;
  69. function resulttype_variant_to_enum : tnode;
  70. function resulttype_enum_to_variant : tnode;
  71. function resulttype_proc_to_procvar : tnode;
  72. function resulttype_variant_to_interface : tnode;
  73. function resulttype_interface_to_variant : tnode;
  74. function resulttype_array_2_dynarray : tnode;
  75. protected
  76. function first_int_to_int : tnode;virtual;
  77. function first_cstring_to_pchar : tnode;virtual;
  78. function first_cstring_to_int : tnode;virtual;
  79. function first_string_to_chararray : tnode;virtual;
  80. function first_char_to_string : tnode;virtual;
  81. function first_nothing : tnode;virtual;
  82. function first_array_to_pointer : tnode;virtual;
  83. function first_int_to_real : tnode;virtual;
  84. function first_real_to_real : tnode;virtual;
  85. function first_pointer_to_array : tnode;virtual;
  86. function first_cchar_to_pchar : tnode;virtual;
  87. function first_bool_to_int : tnode;virtual;
  88. function first_int_to_bool : tnode;virtual;
  89. function first_bool_to_bool : tnode;virtual;
  90. function first_proc_to_procvar : tnode;virtual;
  91. function first_load_smallset : tnode;virtual;
  92. function first_cord_to_pointer : tnode;virtual;
  93. function first_ansistring_to_pchar : tnode;virtual;
  94. function first_arrayconstructor_to_set : tnode;virtual;
  95. function first_class_to_intf : tnode;virtual;
  96. function first_char_to_char : tnode;virtual;
  97. function first_call_helper(c : tconverttype) : tnode;
  98. { these wrapper are necessary, because the first_* stuff is called }
  99. { through a table. Without the wrappers override wouldn't have }
  100. { any effect }
  101. function _first_int_to_int : tnode;
  102. function _first_cstring_to_pchar : tnode;
  103. function _first_cstring_to_int : tnode;
  104. function _first_string_to_chararray : tnode;
  105. function _first_char_to_string : tnode;
  106. function _first_nothing : tnode;
  107. function _first_array_to_pointer : tnode;
  108. function _first_int_to_real : tnode;
  109. function _first_real_to_real: tnode;
  110. function _first_pointer_to_array : tnode;
  111. function _first_cchar_to_pchar : tnode;
  112. function _first_bool_to_int : tnode;
  113. function _first_int_to_bool : tnode;
  114. function _first_bool_to_bool : tnode;
  115. function _first_proc_to_procvar : tnode;
  116. function _first_load_smallset : tnode;
  117. function _first_cord_to_pointer : tnode;
  118. function _first_ansistring_to_pchar : tnode;
  119. function _first_arrayconstructor_to_set : tnode;
  120. function _first_class_to_intf : tnode;
  121. function _first_char_to_char : tnode;
  122. procedure _second_int_to_int;virtual;
  123. procedure _second_string_to_string;virtual;
  124. procedure _second_cstring_to_pchar;virtual;
  125. procedure _second_cstring_to_int;virtual;
  126. procedure _second_string_to_chararray;virtual;
  127. procedure _second_array_to_pointer;virtual;
  128. procedure _second_pointer_to_array;virtual;
  129. procedure _second_chararray_to_string;virtual;
  130. procedure _second_char_to_string;virtual;
  131. procedure _second_int_to_real;virtual;
  132. procedure _second_real_to_real;virtual;
  133. procedure _second_cord_to_pointer;virtual;
  134. procedure _second_proc_to_procvar;virtual;
  135. procedure _second_bool_to_int;virtual;
  136. procedure _second_int_to_bool;virtual;
  137. procedure _second_bool_to_bool;virtual;
  138. procedure _second_load_smallset;virtual;
  139. procedure _second_ansistring_to_pchar;virtual;
  140. procedure _second_class_to_intf;virtual;
  141. procedure _second_char_to_char;virtual;
  142. procedure _second_nothing; virtual;
  143. procedure second_int_to_int;virtual;abstract;
  144. procedure second_string_to_string;virtual;abstract;
  145. procedure second_cstring_to_pchar;virtual;abstract;
  146. procedure second_cstring_to_int;virtual;abstract;
  147. procedure second_string_to_chararray;virtual;abstract;
  148. procedure second_array_to_pointer;virtual;abstract;
  149. procedure second_pointer_to_array;virtual;abstract;
  150. procedure second_chararray_to_string;virtual;abstract;
  151. procedure second_char_to_string;virtual;abstract;
  152. procedure second_int_to_real;virtual;abstract;
  153. procedure second_real_to_real;virtual;abstract;
  154. procedure second_cord_to_pointer;virtual;abstract;
  155. procedure second_proc_to_procvar;virtual;abstract;
  156. procedure second_bool_to_int;virtual;abstract;
  157. procedure second_int_to_bool;virtual;abstract;
  158. procedure second_bool_to_bool;virtual;abstract;
  159. procedure second_load_smallset;virtual;abstract;
  160. procedure second_ansistring_to_pchar;virtual;abstract;
  161. procedure second_class_to_intf;virtual;abstract;
  162. procedure second_char_to_char;virtual;abstract;
  163. procedure second_nothing; virtual;abstract;
  164. end;
  165. ttypeconvnodeclass = class of ttypeconvnode;
  166. tasnode = class(tbinarynode)
  167. constructor create(l,r : tnode);virtual;
  168. function pass_1 : tnode;override;
  169. function det_resulttype:tnode;override;
  170. function _getcopy: tnode;override;
  171. destructor destroy; override;
  172. protected
  173. call: tnode;
  174. end;
  175. tasnodeclass = class of tasnode;
  176. tisnode = class(tbinarynode)
  177. constructor create(l,r : tnode);virtual;
  178. function pass_1 : tnode;override;
  179. function det_resulttype:tnode;override;
  180. procedure pass_2;override;
  181. end;
  182. tisnodeclass = class of tisnode;
  183. var
  184. ctypeconvnode : ttypeconvnodeclass;
  185. casnode : tasnodeclass;
  186. cisnode : tisnodeclass;
  187. procedure inserttypeconv(var p:tnode;const t:ttype);
  188. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  189. procedure arrayconstructor_to_set(var p : tnode);
  190. implementation
  191. uses
  192. cclasses,globtype,systems,
  193. cutils,verbose,globals,widestr,
  194. symconst,symdef,symsym,symbase,symtable,
  195. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  196. cgbase,procinfo,
  197. htypechk,pass_1,cpuinfo;
  198. {*****************************************************************************
  199. Helpers
  200. *****************************************************************************}
  201. procedure inserttypeconv(var p:tnode;const t:ttype);
  202. begin
  203. if not assigned(p.resulttype.def) then
  204. begin
  205. resulttypepass(p);
  206. if codegenerror then
  207. exit;
  208. end;
  209. { don't insert obsolete type conversions }
  210. if equal_defs(p.resulttype.def,t.def) and
  211. not ((p.resulttype.def.deftype=setdef) and
  212. (tsetdef(p.resulttype.def).settype <>
  213. tsetdef(t.def).settype)) then
  214. begin
  215. p.resulttype:=t;
  216. end
  217. else
  218. begin
  219. p:=ctypeconvnode.create(p,t);
  220. resulttypepass(p);
  221. end;
  222. end;
  223. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  224. begin
  225. if not assigned(p.resulttype.def) then
  226. begin
  227. resulttypepass(p);
  228. if codegenerror then
  229. exit;
  230. end;
  231. { don't insert obsolete type conversions }
  232. if equal_defs(p.resulttype.def,t.def) and
  233. not ((p.resulttype.def.deftype=setdef) and
  234. (tsetdef(p.resulttype.def).settype <>
  235. tsetdef(t.def).settype)) then
  236. begin
  237. p.resulttype:=t;
  238. end
  239. else
  240. begin
  241. p:=ctypeconvnode.create_internal(p,t);
  242. resulttypepass(p);
  243. end;
  244. end;
  245. {*****************************************************************************
  246. Array constructor to Set Conversion
  247. *****************************************************************************}
  248. procedure arrayconstructor_to_set(var p : tnode);
  249. var
  250. constp : tsetconstnode;
  251. buildp,
  252. p2,p3,p4 : tnode;
  253. htype : ttype;
  254. constset : Pconstset;
  255. constsetlo,
  256. constsethi : TConstExprInt;
  257. procedure update_constsethi(t:ttype);
  258. begin
  259. if ((t.def.deftype=orddef) and
  260. (torddef(t.def).high>=constsethi)) then
  261. begin
  262. if torddef(t.def).typ=uwidechar then
  263. begin
  264. constsethi:=255;
  265. if htype.def=nil then
  266. htype:=t;
  267. end
  268. else
  269. begin
  270. constsethi:=torddef(t.def).high;
  271. if htype.def=nil then
  272. begin
  273. if (constsethi>255) or
  274. (torddef(t.def).low<0) then
  275. htype:=u8inttype
  276. else
  277. htype:=t;
  278. end;
  279. if constsethi>255 then
  280. constsethi:=255;
  281. end;
  282. end
  283. else if ((t.def.deftype=enumdef) and
  284. (tenumdef(t.def).max>=constsethi)) then
  285. begin
  286. if htype.def=nil then
  287. htype:=t;
  288. constsethi:=tenumdef(t.def).max;
  289. end;
  290. end;
  291. procedure do_set(pos : longint);
  292. begin
  293. if (pos and not $ff)<>0 then
  294. Message(parser_e_illegal_set_expr);
  295. if pos>constsethi then
  296. constsethi:=pos;
  297. if pos<constsetlo then
  298. constsetlo:=pos;
  299. if pos in constset^ then
  300. Message(parser_e_illegal_set_expr);
  301. include(constset^,pos);
  302. end;
  303. var
  304. l : Longint;
  305. lr,hr : TConstExprInt;
  306. hp : tarrayconstructornode;
  307. begin
  308. if p.nodetype<>arrayconstructorn then
  309. internalerror(200205105);
  310. new(constset);
  311. constset^:=[];
  312. htype.reset;
  313. constsetlo:=0;
  314. constsethi:=0;
  315. constp:=csetconstnode.create(nil,htype);
  316. constp.value_set:=constset;
  317. buildp:=constp;
  318. hp:=tarrayconstructornode(p);
  319. if assigned(hp.left) then
  320. begin
  321. while assigned(hp) do
  322. begin
  323. p4:=nil; { will contain the tree to create the set }
  324. {split a range into p2 and p3 }
  325. if hp.left.nodetype=arrayconstructorrangen then
  326. begin
  327. p2:=tarrayconstructorrangenode(hp.left).left;
  328. p3:=tarrayconstructorrangenode(hp.left).right;
  329. tarrayconstructorrangenode(hp.left).left:=nil;
  330. tarrayconstructorrangenode(hp.left).right:=nil;
  331. end
  332. else
  333. begin
  334. p2:=hp.left;
  335. hp.left:=nil;
  336. p3:=nil;
  337. end;
  338. resulttypepass(p2);
  339. set_varstate(p2,vs_used,[vsf_must_be_valid]);
  340. if assigned(p3) then
  341. begin
  342. resulttypepass(p3);
  343. set_varstate(p3,vs_used,[vsf_must_be_valid]);
  344. end;
  345. if codegenerror then
  346. break;
  347. case p2.resulttype.def.deftype of
  348. enumdef,
  349. orddef:
  350. begin
  351. getrange(p2.resulttype.def,lr,hr);
  352. if assigned(p3) then
  353. begin
  354. { this isn't good, you'll get problems with
  355. type t010 = 0..10;
  356. ts = set of t010;
  357. var s : ts;b : t010
  358. begin s:=[1,2,b]; end.
  359. if is_integer(p3^.resulttype.def) then
  360. begin
  361. inserttypeconv(p3,u8bitdef);
  362. end;
  363. }
  364. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  365. begin
  366. aktfilepos:=p3.fileinfo;
  367. CGMessage(type_e_typeconflict_in_set);
  368. end
  369. else
  370. begin
  371. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  372. begin
  373. if not(is_integer(p3.resulttype.def)) then
  374. htype:=p3.resulttype
  375. else
  376. begin
  377. inserttypeconv(p3,u8inttype);
  378. inserttypeconv(p2,u8inttype);
  379. end;
  380. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  381. do_set(l);
  382. p2.free;
  383. p3.free;
  384. end
  385. else
  386. begin
  387. update_constsethi(p2.resulttype);
  388. inserttypeconv(p2,htype);
  389. update_constsethi(p3.resulttype);
  390. inserttypeconv(p3,htype);
  391. if assigned(htype.def) then
  392. inserttypeconv(p3,htype)
  393. else
  394. inserttypeconv(p3,u8inttype);
  395. p4:=csetelementnode.create(p2,p3);
  396. end;
  397. end;
  398. end
  399. else
  400. begin
  401. { Single value }
  402. if p2.nodetype=ordconstn then
  403. begin
  404. if not(is_integer(p2.resulttype.def)) then
  405. begin
  406. { for constant set elements, delphi allows the usage of elements of enumerations which
  407. have value>255 if there is no element with a value > 255 used }
  408. if (m_delphi in aktmodeswitches) and (p2.resulttype.def.deftype=enumdef) then
  409. begin
  410. if tordconstnode(p2).value>constsethi then
  411. constsethi:=tordconstnode(p2).value;
  412. if htype.def=nil then
  413. htype:=p2.resulttype;
  414. end
  415. else
  416. update_constsethi(p2.resulttype);
  417. end;
  418. if assigned(htype.def) then
  419. inserttypeconv(p2,htype)
  420. else
  421. inserttypeconv(p2,u8inttype);
  422. do_set(tordconstnode(p2).value);
  423. p2.free;
  424. end
  425. else
  426. begin
  427. update_constsethi(p2.resulttype);
  428. if assigned(htype.def) then
  429. inserttypeconv(p2,htype)
  430. else
  431. inserttypeconv(p2,u8inttype);
  432. p4:=csetelementnode.create(p2,nil);
  433. end;
  434. end;
  435. end;
  436. stringdef :
  437. begin
  438. { if we've already set elements which are constants }
  439. { throw an error }
  440. if ((htype.def=nil) and assigned(buildp)) or
  441. not(is_char(htype.def)) then
  442. CGMessage(type_e_typeconflict_in_set)
  443. else
  444. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  445. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  446. if htype.def=nil then
  447. htype:=cchartype;
  448. p2.free;
  449. end;
  450. else
  451. CGMessage(type_e_ordinal_expr_expected);
  452. end;
  453. { insert the set creation tree }
  454. if assigned(p4) then
  455. buildp:=caddnode.create(addn,buildp,p4);
  456. { load next and dispose current node }
  457. p2:=hp;
  458. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  459. tarrayconstructornode(p2).right:=nil;
  460. p2.free;
  461. end;
  462. if (htype.def=nil) then
  463. htype:=u8inttype;
  464. end
  465. else
  466. begin
  467. { empty set [], only remove node }
  468. p.free;
  469. end;
  470. { set the initial set type }
  471. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  472. { determine the resulttype for the tree }
  473. resulttypepass(buildp);
  474. { set the new tree }
  475. p:=buildp;
  476. end;
  477. {*****************************************************************************
  478. TTYPECONVNODE
  479. *****************************************************************************}
  480. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  481. begin
  482. inherited create(typeconvn,node);
  483. convtype:=tc_none;
  484. totype:=t;
  485. if t.def=nil then
  486. internalerror(200103281);
  487. fileinfo:=node.fileinfo;
  488. end;
  489. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  490. begin
  491. self.create(node,t);
  492. include(flags,nf_explicit);
  493. end;
  494. constructor ttypeconvnode.create_internal(node : tnode;const t:ttype);
  495. begin
  496. self.create(node,t);
  497. { handle like explicit conversions }
  498. include(flags,nf_explicit);
  499. include(flags,nf_internal);
  500. end;
  501. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  502. begin
  503. self.create(node,voidtype);
  504. convtype:=tc_proc_2_procvar;
  505. end;
  506. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  507. begin
  508. inherited ppuload(t,ppufile);
  509. ppufile.gettype(totype);
  510. convtype:=tconverttype(ppufile.getbyte);
  511. end;
  512. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  513. begin
  514. inherited ppuwrite(ppufile);
  515. ppufile.puttype(totype);
  516. ppufile.putbyte(byte(convtype));
  517. end;
  518. procedure ttypeconvnode.buildderefimpl;
  519. begin
  520. inherited buildderefimpl;
  521. totype.buildderef;
  522. end;
  523. procedure ttypeconvnode.derefimpl;
  524. begin
  525. inherited derefimpl;
  526. totype.resolve;
  527. end;
  528. function ttypeconvnode._getcopy : tnode;
  529. var
  530. n : ttypeconvnode;
  531. begin
  532. n:=ttypeconvnode(inherited _getcopy);
  533. n.convtype:=convtype;
  534. n.totype:=totype;
  535. _getcopy:=n;
  536. end;
  537. procedure ttypeconvnode.printnodeinfo(var t : text);
  538. const
  539. convtyp2str : array[tconverttype] of pchar = (
  540. 'tc_none',
  541. 'tc_equal',
  542. 'tc_not_possible',
  543. 'tc_string_2_string',
  544. 'tc_char_2_string',
  545. 'tc_char_2_chararray',
  546. 'tc_pchar_2_string',
  547. 'tc_cchar_2_pchar',
  548. 'tc_cstring_2_pchar',
  549. 'tc_cstring_2_int',
  550. 'tc_ansistring_2_pchar',
  551. 'tc_string_2_chararray',
  552. 'tc_chararray_2_string',
  553. 'tc_array_2_pointer',
  554. 'tc_pointer_2_array',
  555. 'tc_int_2_int',
  556. 'tc_int_2_bool',
  557. 'tc_bool_2_bool',
  558. 'tc_bool_2_int',
  559. 'tc_real_2_real',
  560. 'tc_int_2_real',
  561. 'tc_real_2_currency',
  562. 'tc_proc_2_procvar',
  563. 'tc_arrayconstructor_2_set',
  564. 'tc_load_smallset',
  565. 'tc_cord_2_pointer',
  566. 'tc_intf_2_string',
  567. 'tc_intf_2_guid',
  568. 'tc_class_2_intf',
  569. 'tc_char_2_char',
  570. 'tc_normal_2_smallset',
  571. 'tc_dynarray_2_openarray',
  572. 'tc_pwchar_2_string',
  573. 'tc_variant_2_dynarray',
  574. 'tc_dynarray_2_variant',
  575. 'tc_variant_2_enum',
  576. 'tc_enum_2_variant',
  577. 'tc_interface_2_variant',
  578. 'tc_variant_2_interface',
  579. 'tc_array_2_dynarray'
  580. );
  581. begin
  582. inherited printnodeinfo(t);
  583. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  584. end;
  585. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  586. var
  587. t : tnode;
  588. begin
  589. result:=nil;
  590. if left.nodetype=ordconstn then
  591. begin
  592. { check if we have a valid pointer constant (JM) }
  593. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  594. if (sizeof(TConstPtrUInt) = 4) then
  595. begin
  596. if (tordconstnode(left).value < low(longint)) or
  597. (tordconstnode(left).value > high(cardinal)) then
  598. CGMessage(parser_e_range_check_error);
  599. end
  600. else if (sizeof(TConstPtrUInt) = 8) then
  601. begin
  602. if (tordconstnode(left).value < low(int64)) or
  603. (tordconstnode(left).value > high(qword)) then
  604. CGMessage(parser_e_range_check_error);
  605. end
  606. else
  607. internalerror(2001020801);
  608. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  609. result:=t;
  610. end
  611. else
  612. internalerror(200104023);
  613. end;
  614. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  615. var
  616. chartype : string[8];
  617. begin
  618. if is_widechar(tarraydef(left.resulttype.def).elementtype.def) then
  619. chartype:='widechar'
  620. else
  621. chartype:='char';
  622. result := ccallnode.createinternres(
  623. 'fpc_'+chartype+'array_to_'+tstringdef(resulttype.def).stringtypname,
  624. ccallparanode.create(left,nil),resulttype);
  625. left := nil;
  626. end;
  627. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  628. var
  629. arrsize : aint;
  630. chartype : string[8];
  631. begin
  632. result := nil;
  633. with tarraydef(resulttype.def) do
  634. begin
  635. if highrange<lowrange then
  636. internalerror(200501051);
  637. arrsize := highrange-lowrange+1;
  638. end;
  639. if (left.nodetype = stringconstn) and
  640. (tstringdef(left.resulttype.def).string_typ=st_conststring) then
  641. begin
  642. { if the array of char is large enough we can use the string
  643. constant directly. This is handled in ncgcnv }
  644. if (arrsize>=tstringconstnode(left).len) and
  645. is_char(tarraydef(resulttype.def).elementtype.def) then
  646. exit;
  647. { Convert to wide/short/ansistring and call default helper }
  648. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  649. inserttypeconv(left,cwidestringtype)
  650. else
  651. begin
  652. if tstringconstnode(left).len>255 then
  653. inserttypeconv(left,cansistringtype)
  654. else
  655. inserttypeconv(left,cshortstringtype);
  656. end;
  657. end;
  658. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  659. chartype:='widechar'
  660. else
  661. chartype:='char';
  662. result := ccallnode.createinternres(
  663. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  664. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  665. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  666. left := nil;
  667. end;
  668. function ttypeconvnode.resulttype_string_to_string : tnode;
  669. var
  670. procname: string[31];
  671. stringpara : tcallparanode;
  672. begin
  673. result:=nil;
  674. if left.nodetype=stringconstn then
  675. begin
  676. tstringconstnode(left).changestringtype(resulttype);
  677. result:=left;
  678. left:=nil;
  679. end
  680. else
  681. begin
  682. { get the correct procedure name }
  683. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  684. '_to_'+tstringdef(resulttype.def).stringtypname;
  685. { create parameter (and remove left node from typeconvnode }
  686. { since it's reused as parameter) }
  687. stringpara := ccallparanode.create(left,nil);
  688. left := nil;
  689. { when converting to shortstrings, we have to pass high(destination) too }
  690. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  691. stringpara.right := ccallparanode.create(cinlinenode.create(
  692. in_high_x,false,self.getcopy),nil);
  693. { and create the callnode }
  694. result := ccallnode.createinternres(procname,stringpara,resulttype);
  695. end;
  696. end;
  697. function ttypeconvnode.resulttype_char_to_string : tnode;
  698. var
  699. procname: string[31];
  700. para : tcallparanode;
  701. hp : tstringconstnode;
  702. ws : pcompilerwidestring;
  703. begin
  704. result:=nil;
  705. if left.nodetype=ordconstn then
  706. begin
  707. if tstringdef(resulttype.def).string_typ=st_widestring then
  708. begin
  709. initwidestring(ws);
  710. if torddef(left.resulttype.def).typ=uwidechar then
  711. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  712. else
  713. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  714. hp:=cstringconstnode.createwstr(ws);
  715. donewidestring(ws);
  716. end
  717. else
  718. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  719. result:=hp;
  720. end
  721. else
  722. { shortstrings are handled 'inline' }
  723. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  724. begin
  725. { create the parameter }
  726. para := ccallparanode.create(left,nil);
  727. left := nil;
  728. { and the procname }
  729. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  730. { and finally the call }
  731. result := ccallnode.createinternres(procname,para,resulttype);
  732. end
  733. else
  734. begin
  735. { create word(byte(char) shl 8 or 1) for litte endian machines }
  736. { and word(byte(char) or 256) for big endian machines }
  737. left := ctypeconvnode.create_internal(left,u8inttype);
  738. if (target_info.endian = endian_little) then
  739. left := caddnode.create(orn,
  740. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  741. cordconstnode.create(1,s32inttype,false))
  742. else
  743. left := caddnode.create(orn,left,
  744. cordconstnode.create(1 shl 8,s32inttype,false));
  745. left := ctypeconvnode.create_internal(left,u16inttype);
  746. resulttypepass(left);
  747. end;
  748. end;
  749. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  750. begin
  751. if resulttype.def.size <> 1 then
  752. begin
  753. { convert first to string, then to chararray }
  754. inserttypeconv(left,cshortstringtype);
  755. inserttypeconv(left,resulttype);
  756. result:=left;
  757. left := nil;
  758. exit;
  759. end;
  760. result := nil;
  761. end;
  762. function ttypeconvnode.resulttype_char_to_char : tnode;
  763. var
  764. hp : tordconstnode;
  765. begin
  766. result:=nil;
  767. if left.nodetype=ordconstn then
  768. begin
  769. if (torddef(resulttype.def).typ=uchar) and
  770. (torddef(left.resulttype.def).typ=uwidechar) then
  771. begin
  772. hp:=cordconstnode.create(
  773. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  774. cchartype,true);
  775. result:=hp;
  776. end
  777. else if (torddef(resulttype.def).typ=uwidechar) and
  778. (torddef(left.resulttype.def).typ=uchar) then
  779. begin
  780. hp:=cordconstnode.create(
  781. asciichar2unicode(chr(tordconstnode(left).value)),
  782. cwidechartype,true);
  783. result:=hp;
  784. end
  785. else
  786. internalerror(200105131);
  787. exit;
  788. end;
  789. end;
  790. function ttypeconvnode.resulttype_int_to_int : tnode;
  791. var
  792. v : TConstExprInt;
  793. begin
  794. result:=nil;
  795. if left.nodetype=ordconstn then
  796. begin
  797. v:=tordconstnode(left).value;
  798. if is_currency(resulttype.def) then
  799. v:=v*10000;
  800. if (resulttype.def.deftype=pointerdef) then
  801. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  802. else
  803. begin
  804. if is_currency(left.resulttype.def) then
  805. v:=v div 10000;
  806. result:=cordconstnode.create(v,resulttype,false);
  807. end;
  808. end
  809. else if left.nodetype=pointerconstn then
  810. begin
  811. v:=tpointerconstnode(left).value;
  812. if (resulttype.def.deftype=pointerdef) then
  813. result:=cpointerconstnode.create(v,resulttype)
  814. else
  815. begin
  816. if is_currency(resulttype.def) then
  817. v:=v*10000;
  818. result:=cordconstnode.create(v,resulttype,false);
  819. end;
  820. end
  821. else
  822. begin
  823. { multiply by 10000 for currency. We need to use getcopy to pass
  824. the argument because the current node is always disposed. Only
  825. inserting the multiply in the left node is not possible because
  826. it'll get in an infinite loop to convert int->currency }
  827. if is_currency(resulttype.def) then
  828. begin
  829. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  830. include(result.flags,nf_is_currency);
  831. end
  832. else if is_currency(left.resulttype.def) then
  833. begin
  834. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  835. include(result.flags,nf_is_currency);
  836. end;
  837. end;
  838. end;
  839. function ttypeconvnode.resulttype_int_to_real : tnode;
  840. var
  841. rv : bestreal;
  842. begin
  843. result:=nil;
  844. if left.nodetype=ordconstn then
  845. begin
  846. rv:=tordconstnode(left).value;
  847. if is_currency(resulttype.def) then
  848. rv:=rv*10000.0
  849. else if is_currency(left.resulttype.def) then
  850. rv:=rv/10000.0;
  851. result:=crealconstnode.create(rv,resulttype);
  852. end
  853. else
  854. begin
  855. { multiply by 10000 for currency. We need to use getcopy to pass
  856. the argument because the current node is always disposed. Only
  857. inserting the multiply in the left node is not possible because
  858. it'll get in an infinite loop to convert int->currency }
  859. if is_currency(resulttype.def) then
  860. begin
  861. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  862. include(result.flags,nf_is_currency);
  863. end
  864. else if is_currency(left.resulttype.def) then
  865. begin
  866. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  867. include(result.flags,nf_is_currency);
  868. end;
  869. end;
  870. end;
  871. function ttypeconvnode.resulttype_real_to_currency : tnode;
  872. begin
  873. if not is_currency(resulttype.def) then
  874. internalerror(200304221);
  875. result:=nil;
  876. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  877. include(left.flags,nf_is_currency);
  878. resulttypepass(left);
  879. { Convert constants directly, else call Round() }
  880. if left.nodetype=realconstn then
  881. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  882. else
  883. result:=ccallnode.createinternres('fpc_round_real',
  884. ccallparanode.create(left,nil),resulttype);
  885. left:=nil;
  886. end;
  887. function ttypeconvnode.resulttype_real_to_real : tnode;
  888. begin
  889. result:=nil;
  890. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  891. begin
  892. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  893. include(left.flags,nf_is_currency);
  894. resulttypepass(left);
  895. end
  896. else
  897. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  898. begin
  899. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  900. include(left.flags,nf_is_currency);
  901. resulttypepass(left);
  902. end;
  903. if left.nodetype=realconstn then
  904. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  905. end;
  906. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  907. begin
  908. result:=nil;
  909. if is_pwidechar(resulttype.def) then
  910. inserttypeconv(left,cwidestringtype)
  911. else
  912. inserttypeconv(left,cshortstringtype);
  913. { evaluate again, reset resulttype so the convert_typ
  914. will be calculated again and cstring_to_pchar will
  915. be used for futher conversion }
  916. convtype:=tc_none;
  917. result:=det_resulttype;
  918. end;
  919. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  920. begin
  921. result:=nil;
  922. if is_pwidechar(resulttype.def) then
  923. inserttypeconv(left,cwidestringtype)
  924. else
  925. if is_pchar(resulttype.def) and
  926. is_widestring(left.resulttype.def) then
  927. inserttypeconv(left,cansistringtype);
  928. end;
  929. function ttypeconvnode.resulttype_cstring_to_int : tnode;
  930. var
  931. fcc : cardinal;
  932. pb : pbyte;
  933. begin
  934. result:=nil;
  935. if left.nodetype<>stringconstn then
  936. internalerror(200510012);
  937. if tstringconstnode(left).len=4 then
  938. begin
  939. pb:=pbyte(tstringconstnode(left).value_str);
  940. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  941. result:=cordconstnode.create(fcc,u32inttype,false);
  942. end
  943. else
  944. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  945. end;
  946. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  947. var
  948. hp : tnode;
  949. begin
  950. result:=nil;
  951. if left.nodetype<>arrayconstructorn then
  952. internalerror(5546);
  953. { remove typeconv node }
  954. hp:=left;
  955. left:=nil;
  956. { create a set constructor tree }
  957. arrayconstructor_to_set(hp);
  958. result:=hp;
  959. end;
  960. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  961. begin
  962. result := ccallnode.createinternres(
  963. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  964. ccallparanode.create(left,nil),resulttype);
  965. left := nil;
  966. end;
  967. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  968. begin
  969. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  970. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  971. end;
  972. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  973. begin
  974. { a dynamic array is a pointer to an array, so to convert it to }
  975. { an open array, we have to dereference it (JM) }
  976. result := ctypeconvnode.create_internal(left,voidpointertype);
  977. resulttypepass(result);
  978. { left is reused }
  979. left := nil;
  980. result := cderefnode.create(result);
  981. include(result.flags,nf_no_checkpointer);
  982. result.resulttype := resulttype;
  983. end;
  984. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  985. begin
  986. result := ccallnode.createinternres(
  987. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  988. ccallparanode.create(left,nil),resulttype);
  989. left := nil;
  990. end;
  991. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  992. begin
  993. result := ccallnode.createinternres(
  994. 'fpc_variant_to_dynarray',
  995. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  996. ccallparanode.create(left,nil)
  997. ),resulttype);
  998. resulttypepass(result);
  999. left:=nil;
  1000. end;
  1001. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  1002. begin
  1003. result := ccallnode.createinternres(
  1004. 'fpc_dynarray_to_variant',
  1005. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1006. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1007. ),resulttype);
  1008. resulttypepass(result);
  1009. left:=nil;
  1010. end;
  1011. function ttypeconvnode.resulttype_variant_to_interface : tnode;
  1012. begin
  1013. result := ccallnode.createinternres(
  1014. 'fpc_variant_to_interface',
  1015. ccallparanode.create(left,nil)
  1016. ,resulttype);
  1017. resulttypepass(result);
  1018. left:=nil;
  1019. end;
  1020. function ttypeconvnode.resulttype_interface_to_variant : tnode;
  1021. begin
  1022. result := ccallnode.createinternres(
  1023. 'fpc_interface_to_variant',
  1024. ccallparanode.create(left,nil)
  1025. ,resulttype);
  1026. resulttypepass(result);
  1027. left:=nil;
  1028. end;
  1029. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  1030. begin
  1031. result := ctypeconvnode.create_internal(left,sinttype);
  1032. result := ctypeconvnode.create_internal(result,resulttype);
  1033. resulttypepass(result);
  1034. { left is reused }
  1035. left := nil;
  1036. end;
  1037. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  1038. begin
  1039. result := ctypeconvnode.create_internal(left,sinttype);
  1040. result := ctypeconvnode.create_internal(result,cvarianttype);
  1041. resulttypepass(result);
  1042. { left is reused }
  1043. left := nil;
  1044. end;
  1045. function ttypeconvnode.resulttype_array_2_dynarray : tnode;
  1046. var
  1047. newstatement : tstatementnode;
  1048. temp : ttempcreatenode;
  1049. temp2 : ttempcreatenode;
  1050. begin
  1051. { create statements with call to getmem+initialize }
  1052. result:=internalstatements(newstatement);
  1053. { create temp for result }
  1054. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,true);
  1055. addstatement(newstatement,temp);
  1056. { get temp for array of lengths }
  1057. temp2:=ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  1058. addstatement(newstatement,temp2);
  1059. { one dimensional }
  1060. addstatement(newstatement,cassignmentnode.create(
  1061. ctemprefnode.create_offset(temp2,0),
  1062. cordconstnode.create
  1063. (tarraydef(left.resulttype.def).highrange+1,s32inttype,true)));
  1064. { create call to fpc_dynarr_setlength }
  1065. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1066. ccallparanode.create(caddrnode.create_internal
  1067. (ctemprefnode.create(temp2)),
  1068. ccallparanode.create(cordconstnode.create
  1069. (1,s32inttype,true),
  1070. ccallparanode.create(caddrnode.create_internal
  1071. (crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1072. ccallparanode.create(
  1073. ctypeconvnode.create_internal(
  1074. ctemprefnode.create(temp),voidpointertype),
  1075. nil))))
  1076. ));
  1077. addstatement(newstatement,ctempdeletenode.create(temp2));
  1078. { copy ... }
  1079. addstatement(newstatement,cassignmentnode.create(
  1080. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resulttype),
  1081. left
  1082. ));
  1083. { left is reused }
  1084. left:=nil;
  1085. { the last statement should return the value as
  1086. location and type, this is done be referencing the
  1087. temp and converting it first from a persistent temp to
  1088. normal temp }
  1089. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1090. addstatement(newstatement,ctemprefnode.create(temp));
  1091. end;
  1092. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  1093. var
  1094. newparast : tsymtable absolute arg;
  1095. vs : tparavarsym;
  1096. begin
  1097. if tsym(p).typ<>paravarsym then
  1098. exit;
  1099. with tparavarsym(p) do
  1100. begin
  1101. vs:=tparavarsym.create(realname,paranr,varspez,vartype,varoptions);
  1102. vs.defaultconstsym:=defaultconstsym;
  1103. newparast.insert(vs);
  1104. end;
  1105. end;
  1106. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  1107. var
  1108. pd : tabstractprocdef;
  1109. begin
  1110. result:=nil;
  1111. pd:=tabstractprocdef(left.resulttype.def);
  1112. { create procvardef }
  1113. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  1114. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  1115. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  1116. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  1117. tprocvardef(resulttype.def).rettype:=pd.rettype;
  1118. { method ? then set the methodpointer flag }
  1119. if (pd.owner.symtabletype=objectsymtable) then
  1120. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  1121. { was it a local procedure? }
  1122. if (pd.owner.symtabletype=localsymtable) then
  1123. include(tprocvardef(resulttype.def).procoptions,po_local);
  1124. { only need the address of the method? this is needed
  1125. for @tobject.create. In this case there will be a loadn without
  1126. a methodpointer. }
  1127. if (left.nodetype=loadn) and
  1128. not assigned(tloadnode(left).left) then
  1129. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  1130. { Add parameters use only references, we don't need to keep the
  1131. parast. We use the parast from the original function to calculate
  1132. our parameter data and reset it afterwards }
  1133. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  1134. tprocvardef(resulttype.def).calcparas;
  1135. end;
  1136. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  1137. const
  1138. resulttypeconvert : array[tconverttype] of pointer = (
  1139. {none} nil,
  1140. {equal} nil,
  1141. {not_possible} nil,
  1142. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  1143. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  1144. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  1145. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  1146. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  1147. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  1148. { cstring_2_int } @ttypeconvnode.resulttype_cstring_to_int,
  1149. { ansistring_2_pchar } nil,
  1150. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  1151. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1152. { array_2_pointer } nil,
  1153. { pointer_2_array } nil,
  1154. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1155. { int_2_bool } nil,
  1156. { bool_2_bool } nil,
  1157. { bool_2_int } nil,
  1158. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1159. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1160. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1161. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1162. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1163. { load_smallset } nil,
  1164. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1165. { intf_2_string } nil,
  1166. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1167. { class_2_intf } nil,
  1168. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1169. { normal_2_smallset} nil,
  1170. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1171. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1172. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1173. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1174. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1175. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant,
  1176. { variant_2_interface} @ttypeconvnode.resulttype_interface_to_variant,
  1177. { interface_2_variant} @ttypeconvnode.resulttype_variant_to_interface,
  1178. { array_2_dynarray} @ttypeconvnode.resulttype_array_2_dynarray
  1179. );
  1180. type
  1181. tprocedureofobject = function : tnode of object;
  1182. var
  1183. r : packed record
  1184. proc : pointer;
  1185. obj : pointer;
  1186. end;
  1187. begin
  1188. result:=nil;
  1189. { this is a little bit dirty but it works }
  1190. { and should be quite portable too }
  1191. r.proc:=resulttypeconvert[c];
  1192. r.obj:=self;
  1193. if assigned(r.proc) then
  1194. result:=tprocedureofobject(r)();
  1195. end;
  1196. function ttypeconvnode.det_resulttype:tnode;
  1197. var
  1198. htype : ttype;
  1199. hp : tnode;
  1200. currprocdef : tabstractprocdef;
  1201. aprocdef : tprocdef;
  1202. eq : tequaltype;
  1203. cdoptions : tcompare_defs_options;
  1204. begin
  1205. result:=nil;
  1206. resulttype:=totype;
  1207. resulttypepass(left);
  1208. if codegenerror then
  1209. exit;
  1210. { When absolute force tc_equal }
  1211. if (nf_absolute in flags) then
  1212. begin
  1213. convtype:=tc_equal;
  1214. if not(tstoreddef(resulttype.def).is_intregable) and
  1215. not(tstoreddef(resulttype.def).is_fpuregable) then
  1216. make_not_regable(left);
  1217. exit;
  1218. end;
  1219. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1220. convert on the procvar value. This is used to access the
  1221. fields of a methodpointer }
  1222. if not(nf_load_procvar in flags) and
  1223. not(resulttype.def.deftype in [procvardef,recorddef,setdef]) then
  1224. maybe_call_procvar(left,true);
  1225. { convert array constructors to sets, because there is no conversion
  1226. possible for array constructors }
  1227. if (resulttype.def.deftype<>arraydef) and
  1228. is_array_constructor(left.resulttype.def) then
  1229. begin
  1230. arrayconstructor_to_set(left);
  1231. resulttypepass(left);
  1232. end;
  1233. if convtype=tc_none then
  1234. begin
  1235. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1236. if nf_explicit in flags then
  1237. include(cdoptions,cdo_explicit);
  1238. if nf_internal in flags then
  1239. include(cdoptions,cdo_internal);
  1240. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1241. case eq of
  1242. te_exact,
  1243. te_equal :
  1244. begin
  1245. { because is_equal only checks the basetype for sets we need to
  1246. check here if we are loading a smallset into a normalset }
  1247. if (resulttype.def.deftype=setdef) and
  1248. (left.resulttype.def.deftype=setdef) and
  1249. ((tsetdef(resulttype.def).settype = smallset) xor
  1250. (tsetdef(left.resulttype.def).settype = smallset)) then
  1251. begin
  1252. { constant sets can be converted by changing the type only }
  1253. if (left.nodetype=setconstn) then
  1254. begin
  1255. left.resulttype:=resulttype;
  1256. result:=left;
  1257. left:=nil;
  1258. exit;
  1259. end;
  1260. if (tsetdef(resulttype.def).settype <> smallset) then
  1261. convtype:=tc_load_smallset
  1262. else
  1263. convtype := tc_normal_2_smallset;
  1264. exit;
  1265. end
  1266. else
  1267. begin
  1268. { Only leave when there is no conversion to do.
  1269. We can still need to call a conversion routine,
  1270. like the routine to convert a stringconstnode }
  1271. if convtype in [tc_equal,tc_not_possible] then
  1272. begin
  1273. left.resulttype:=resulttype;
  1274. result:=left;
  1275. left:=nil;
  1276. exit;
  1277. end;
  1278. end;
  1279. end;
  1280. te_convert_l1,
  1281. te_convert_l2,
  1282. te_convert_l3 :
  1283. begin
  1284. { nothing to do }
  1285. end;
  1286. te_convert_operator :
  1287. begin
  1288. include(current_procinfo.flags,pi_do_call);
  1289. inc(aprocdef.procsym.refs);
  1290. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1291. { tell explicitly which def we must use !! (PM) }
  1292. tcallnode(hp).procdefinition:=aprocdef;
  1293. left:=nil;
  1294. result:=hp;
  1295. exit;
  1296. end;
  1297. te_incompatible :
  1298. begin
  1299. { Procedures have a resulttype.def of voiddef and functions of their
  1300. own resulttype.def. They will therefore always be incompatible with
  1301. a procvar. Because isconvertable cannot check for procedures we
  1302. use an extra check for them.}
  1303. if (left.nodetype=calln) and
  1304. (tcallnode(left).para_count=0) and
  1305. (resulttype.def.deftype=procvardef) and
  1306. (
  1307. (m_tp_procvar in aktmodeswitches) or
  1308. (m_mac_procvar in aktmodeswitches)
  1309. ) then
  1310. begin
  1311. if assigned(tcallnode(left).right) then
  1312. begin
  1313. { this is already a procvar, if it is really equal
  1314. is checked below }
  1315. convtype:=tc_equal;
  1316. hp:=tcallnode(left).right.getcopy;
  1317. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1318. end
  1319. else
  1320. begin
  1321. convtype:=tc_proc_2_procvar;
  1322. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1323. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1324. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1325. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1326. begin
  1327. if assigned(tcallnode(left).methodpointer) then
  1328. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1329. else
  1330. tloadnode(hp).set_mp(load_self_node);
  1331. end;
  1332. resulttypepass(hp);
  1333. end;
  1334. left.free;
  1335. left:=hp;
  1336. { Now check if the procedure we are going to assign to
  1337. the procvar, is compatible with the procvar's type }
  1338. if not(nf_explicit in flags) and
  1339. (proc_to_procvar_equal(currprocdef,
  1340. tprocvardef(resulttype.def),true)=te_incompatible) then
  1341. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1342. exit;
  1343. end;
  1344. { Handle explicit type conversions }
  1345. if nf_explicit in flags then
  1346. begin
  1347. { do common tc_equal cast }
  1348. convtype:=tc_equal;
  1349. { ordinal constants can be resized to 1,2,4,8 bytes }
  1350. if (left.nodetype=ordconstn) then
  1351. begin
  1352. { Insert typeconv for ordinal to the correct size first on left, after
  1353. that the other conversion can be done }
  1354. htype.reset;
  1355. case longint(resulttype.def.size) of
  1356. 1 :
  1357. htype:=s8inttype;
  1358. 2 :
  1359. htype:=s16inttype;
  1360. 4 :
  1361. htype:=s32inttype;
  1362. 8 :
  1363. htype:=s64inttype;
  1364. end;
  1365. { we need explicit, because it can also be an enum }
  1366. if assigned(htype.def) then
  1367. inserttypeconv_internal(left,htype)
  1368. else
  1369. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1370. end;
  1371. { check if the result could be in a register }
  1372. if (not(tstoreddef(resulttype.def).is_intregable) and
  1373. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1374. ((left.resulttype.def.deftype = floatdef) and
  1375. (resulttype.def.deftype <> floatdef)) then
  1376. make_not_regable(left);
  1377. { class/interface to class/interface, with checkobject support }
  1378. if is_class_or_interface(resulttype.def) and
  1379. is_class_or_interface(left.resulttype.def) then
  1380. begin
  1381. { check if the types are related }
  1382. if not(nf_internal in flags) and
  1383. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1384. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1385. begin
  1386. { Give an error when typecasting class to interface, this is compatible
  1387. with delphi }
  1388. if is_interface(resulttype.def) and
  1389. not is_interface(left.resulttype.def) then
  1390. CGMessage2(type_e_classes_not_related,
  1391. FullTypeName(left.resulttype.def,resulttype.def),
  1392. FullTypeName(resulttype.def,left.resulttype.def))
  1393. else
  1394. CGMessage2(type_w_classes_not_related,
  1395. FullTypeName(left.resulttype.def,resulttype.def),
  1396. FullTypeName(resulttype.def,left.resulttype.def))
  1397. end;
  1398. { Add runtime check? }
  1399. if (cs_check_object in aktlocalswitches) then
  1400. begin
  1401. { we can translate the typeconvnode to 'as' when
  1402. typecasting to a class or interface }
  1403. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1404. left:=nil;
  1405. result:=hp;
  1406. exit;
  1407. end;
  1408. end
  1409. else
  1410. begin
  1411. { only if the same size or formal def }
  1412. if not(
  1413. (left.resulttype.def.deftype=formaldef) or
  1414. (
  1415. not(is_open_array(left.resulttype.def)) and
  1416. (left.resulttype.def.size=resulttype.def.size)
  1417. ) or
  1418. (
  1419. is_void(left.resulttype.def) and
  1420. (left.nodetype=derefn)
  1421. )
  1422. ) then
  1423. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1424. end;
  1425. end
  1426. else
  1427. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1428. end;
  1429. else
  1430. internalerror(200211231);
  1431. end;
  1432. end;
  1433. { Give hint or warning for unportable code, exceptions are
  1434. - typecasts from constants
  1435. - void }
  1436. if not(nf_internal in flags) and
  1437. (left.nodetype<>ordconstn) and
  1438. not(is_void(left.resulttype.def)) and
  1439. (((left.resulttype.def.deftype=orddef) and
  1440. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1441. ((resulttype.def.deftype=orddef) and
  1442. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1443. begin
  1444. { Give a warning when sizes don't match, because then info will be lost }
  1445. if left.resulttype.def.size=resulttype.def.size then
  1446. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1447. else
  1448. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1449. end;
  1450. { Constant folding and other node transitions to
  1451. remove the typeconv node }
  1452. case left.nodetype of
  1453. niln :
  1454. begin
  1455. { nil to ordinal node }
  1456. if (resulttype.def.deftype=orddef) then
  1457. begin
  1458. hp:=cordconstnode.create(0,resulttype,true);
  1459. result:=hp;
  1460. exit;
  1461. end
  1462. else
  1463. { fold nil to any pointer type }
  1464. if (resulttype.def.deftype=pointerdef) then
  1465. begin
  1466. hp:=cnilnode.create;
  1467. hp.resulttype:=resulttype;
  1468. result:=hp;
  1469. exit;
  1470. end
  1471. else
  1472. { remove typeconv after niln, but not when the result is a
  1473. methodpointer. The typeconv of the methodpointer will then
  1474. take care of updateing size of niln to OS_64 }
  1475. if not((resulttype.def.deftype=procvardef) and
  1476. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1477. begin
  1478. left.resulttype:=resulttype;
  1479. result:=left;
  1480. left:=nil;
  1481. exit;
  1482. end;
  1483. end;
  1484. ordconstn :
  1485. begin
  1486. { ordinal contants can be directly converted }
  1487. { but not char to char because it is a widechar to char or via versa }
  1488. { which needs extra code to do the code page transistion }
  1489. { constant ordinal to pointer }
  1490. if (resulttype.def.deftype=pointerdef) and
  1491. (convtype<>tc_cchar_2_pchar) then
  1492. begin
  1493. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1494. result:=hp;
  1495. exit;
  1496. end
  1497. else if is_ordinal(resulttype.def) and
  1498. not(convtype=tc_char_2_char) then
  1499. begin
  1500. { replace the resulttype and recheck the range }
  1501. left.resulttype:=resulttype;
  1502. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1503. result:=left;
  1504. left:=nil;
  1505. exit;
  1506. end;
  1507. end;
  1508. pointerconstn :
  1509. begin
  1510. { pointerconstn to any pointer is folded too }
  1511. if (resulttype.def.deftype=pointerdef) then
  1512. begin
  1513. left.resulttype:=resulttype;
  1514. result:=left;
  1515. left:=nil;
  1516. exit;
  1517. end
  1518. { constant pointer to ordinal }
  1519. else if is_ordinal(resulttype.def) then
  1520. begin
  1521. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1522. resulttype,not(nf_explicit in flags));
  1523. result:=hp;
  1524. exit;
  1525. end;
  1526. end;
  1527. end;
  1528. { now call the resulttype helper to do constant folding }
  1529. result:=resulttype_call_helper(convtype);
  1530. end;
  1531. procedure Ttypeconvnode.mark_write;
  1532. begin
  1533. left.mark_write;
  1534. end;
  1535. function ttypeconvnode.first_cord_to_pointer : tnode;
  1536. begin
  1537. result:=nil;
  1538. internalerror(200104043);
  1539. end;
  1540. function ttypeconvnode.first_int_to_int : tnode;
  1541. begin
  1542. first_int_to_int:=nil;
  1543. expectloc:=left.expectloc;
  1544. if not is_void(left.resulttype.def) then
  1545. begin
  1546. if (left.expectloc<>LOC_REGISTER) and
  1547. (resulttype.def.size>left.resulttype.def.size) then
  1548. expectloc:=LOC_REGISTER
  1549. else
  1550. if (left.expectloc=LOC_CREGISTER) and
  1551. (resulttype.def.size<left.resulttype.def.size) then
  1552. expectloc:=LOC_REGISTER;
  1553. end;
  1554. {$ifndef cpu64bit}
  1555. if is_64bit(resulttype.def) then
  1556. registersint:=max(registersint,2)
  1557. else
  1558. {$endif cpu64bit}
  1559. registersint:=max(registersint,1);
  1560. end;
  1561. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1562. begin
  1563. result:=nil;
  1564. registersint:=1;
  1565. expectloc:=LOC_REGISTER;
  1566. end;
  1567. function ttypeconvnode.first_cstring_to_int : tnode;
  1568. begin
  1569. result:=nil;
  1570. internalerror(200510014);
  1571. end;
  1572. function ttypeconvnode.first_string_to_chararray : tnode;
  1573. begin
  1574. first_string_to_chararray:=nil;
  1575. expectloc:=left.expectloc;
  1576. end;
  1577. function ttypeconvnode.first_char_to_string : tnode;
  1578. begin
  1579. first_char_to_string:=nil;
  1580. expectloc:=LOC_REFERENCE;
  1581. end;
  1582. function ttypeconvnode.first_nothing : tnode;
  1583. begin
  1584. first_nothing:=nil;
  1585. end;
  1586. function ttypeconvnode.first_array_to_pointer : tnode;
  1587. begin
  1588. first_array_to_pointer:=nil;
  1589. if registersint<1 then
  1590. registersint:=1;
  1591. expectloc:=LOC_REGISTER;
  1592. end;
  1593. function ttypeconvnode.first_int_to_real: tnode;
  1594. var
  1595. fname: string[32];
  1596. typname : string[12];
  1597. begin
  1598. { Get the type name }
  1599. { Normally the typename should be one of the following:
  1600. single, double - carl
  1601. }
  1602. typname := lower(pbestrealtype^.def.gettypename);
  1603. { converting a 64bit integer to a float requires a helper }
  1604. if is_64bit(left.resulttype.def) then
  1605. begin
  1606. if is_signed(left.resulttype.def) then
  1607. fname := 'fpc_int64_to_'+typname
  1608. else
  1609. {$warning generic conversion from int to float does not support unsigned integers}
  1610. fname := 'fpc_int64_to_'+typname;
  1611. result := ccallnode.createintern(fname,ccallparanode.create(
  1612. left,nil));
  1613. left:=nil;
  1614. firstpass(result);
  1615. exit;
  1616. end
  1617. else
  1618. { other integers are supposed to be 32 bit }
  1619. begin
  1620. {$warning generic conversion from int to float does not support unsigned integers}
  1621. if is_signed(left.resulttype.def) then
  1622. fname := 'fpc_longint_to_'+typname
  1623. else
  1624. fname := 'fpc_longint_to_'+typname;
  1625. result := ccallnode.createintern(fname,ccallparanode.create(
  1626. left,nil));
  1627. left:=nil;
  1628. firstpass(result);
  1629. exit;
  1630. end;
  1631. end;
  1632. function ttypeconvnode.first_real_to_real : tnode;
  1633. begin
  1634. {$ifdef cpufpemu}
  1635. if cs_fp_emulation in aktmoduleswitches then
  1636. begin
  1637. if target_info.system in system_wince then
  1638. begin
  1639. case tfloatdef(left.resulttype.def).typ of
  1640. s32real:
  1641. case tfloatdef(resulttype.def).typ of
  1642. s64real:
  1643. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1644. else
  1645. internalerror(2005082704);
  1646. end;
  1647. s64real:
  1648. case tfloatdef(resulttype.def).typ of
  1649. s32real:
  1650. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1651. else
  1652. internalerror(2005082703);
  1653. end;
  1654. else
  1655. internalerror(2005082702);
  1656. end;
  1657. left:=nil;
  1658. firstpass(result);
  1659. exit;
  1660. end
  1661. else
  1662. begin
  1663. {!! FIXME }
  1664. internalerror(2005082701);
  1665. end;
  1666. end
  1667. else
  1668. {$endif cpufpemu}
  1669. begin
  1670. first_real_to_real:=nil;
  1671. { comp isn't a floating type }
  1672. if registersfpu<1 then
  1673. registersfpu:=1;
  1674. expectloc:=LOC_FPUREGISTER;
  1675. end;
  1676. end;
  1677. function ttypeconvnode.first_pointer_to_array : tnode;
  1678. begin
  1679. first_pointer_to_array:=nil;
  1680. if registersint<1 then
  1681. registersint:=1;
  1682. expectloc:=LOC_REFERENCE;
  1683. end;
  1684. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1685. begin
  1686. first_cchar_to_pchar:=nil;
  1687. internalerror(200104021);
  1688. end;
  1689. function ttypeconvnode.first_bool_to_int : tnode;
  1690. begin
  1691. first_bool_to_int:=nil;
  1692. { byte(boolean) or word(wordbool) or longint(longbool) must
  1693. be accepted for var parameters }
  1694. if (nf_explicit in flags) and
  1695. (left.resulttype.def.size=resulttype.def.size) and
  1696. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1697. exit;
  1698. { when converting to 64bit, first convert to a 32bit int and then }
  1699. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1700. if resulttype.def.size > sizeof(aint) then
  1701. begin
  1702. result := ctypeconvnode.create_internal(left,u32inttype);
  1703. result := ctypeconvnode.create(result,resulttype);
  1704. left := nil;
  1705. firstpass(result);
  1706. exit;
  1707. end;
  1708. expectloc:=LOC_REGISTER;
  1709. if registersint<1 then
  1710. registersint:=1;
  1711. end;
  1712. function ttypeconvnode.first_int_to_bool : tnode;
  1713. begin
  1714. first_int_to_bool:=nil;
  1715. { byte(boolean) or word(wordbool) or longint(longbool) must
  1716. be accepted for var parameters }
  1717. if (nf_explicit in flags) and
  1718. (left.resulttype.def.size=resulttype.def.size) and
  1719. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1720. exit;
  1721. expectloc:=LOC_REGISTER;
  1722. { need if bool to bool !!
  1723. not very nice !!
  1724. insertypeconv(left,s32inttype);
  1725. left.explizit:=true;
  1726. firstpass(left); }
  1727. if registersint<1 then
  1728. registersint:=1;
  1729. end;
  1730. function ttypeconvnode.first_bool_to_bool : tnode;
  1731. begin
  1732. first_bool_to_bool:=nil;
  1733. expectloc:=LOC_REGISTER;
  1734. if registersint<1 then
  1735. registersint:=1;
  1736. end;
  1737. function ttypeconvnode.first_char_to_char : tnode;
  1738. begin
  1739. first_char_to_char:=first_int_to_int;
  1740. end;
  1741. function ttypeconvnode.first_proc_to_procvar : tnode;
  1742. begin
  1743. first_proc_to_procvar:=nil;
  1744. if tabstractprocdef(resulttype.def).is_addressonly then
  1745. begin
  1746. registersint:=left.registersint;
  1747. if registersint<1 then
  1748. registersint:=1;
  1749. expectloc:=LOC_REGISTER;
  1750. end
  1751. else
  1752. begin
  1753. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1754. CGMessage(parser_e_illegal_expression);
  1755. registersint:=left.registersint;
  1756. expectloc:=left.expectloc
  1757. end
  1758. end;
  1759. function ttypeconvnode.first_load_smallset : tnode;
  1760. var
  1761. srsym: ttypesym;
  1762. p: tcallparanode;
  1763. begin
  1764. if not searchsystype('FPC_SMALL_SET',srsym) then
  1765. internalerror(200108313);
  1766. p := ccallparanode.create(left,nil);
  1767. { reused }
  1768. left := nil;
  1769. { convert parameter explicitely to fpc_small_set }
  1770. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1771. { create call, adjust resulttype }
  1772. result :=
  1773. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1774. firstpass(result);
  1775. end;
  1776. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1777. begin
  1778. first_ansistring_to_pchar:=nil;
  1779. expectloc:=LOC_REGISTER;
  1780. if registersint<1 then
  1781. registersint:=1;
  1782. end;
  1783. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1784. begin
  1785. first_arrayconstructor_to_set:=nil;
  1786. internalerror(200104022);
  1787. end;
  1788. function ttypeconvnode.first_class_to_intf : tnode;
  1789. begin
  1790. first_class_to_intf:=nil;
  1791. expectloc:=LOC_REGISTER;
  1792. if registersint<1 then
  1793. registersint:=1;
  1794. end;
  1795. function ttypeconvnode._first_int_to_int : tnode;
  1796. begin
  1797. result:=first_int_to_int;
  1798. end;
  1799. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1800. begin
  1801. result:=first_cstring_to_pchar;
  1802. end;
  1803. function ttypeconvnode._first_cstring_to_int : tnode;
  1804. begin
  1805. result:=first_cstring_to_int;
  1806. end;
  1807. function ttypeconvnode._first_string_to_chararray : tnode;
  1808. begin
  1809. result:=first_string_to_chararray;
  1810. end;
  1811. function ttypeconvnode._first_char_to_string : tnode;
  1812. begin
  1813. result:=first_char_to_string;
  1814. end;
  1815. function ttypeconvnode._first_nothing : tnode;
  1816. begin
  1817. result:=first_nothing;
  1818. end;
  1819. function ttypeconvnode._first_array_to_pointer : tnode;
  1820. begin
  1821. result:=first_array_to_pointer;
  1822. end;
  1823. function ttypeconvnode._first_int_to_real : tnode;
  1824. begin
  1825. result:=first_int_to_real;
  1826. end;
  1827. function ttypeconvnode._first_real_to_real : tnode;
  1828. begin
  1829. result:=first_real_to_real;
  1830. end;
  1831. function ttypeconvnode._first_pointer_to_array : tnode;
  1832. begin
  1833. result:=first_pointer_to_array;
  1834. end;
  1835. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1836. begin
  1837. result:=first_cchar_to_pchar;
  1838. end;
  1839. function ttypeconvnode._first_bool_to_int : tnode;
  1840. begin
  1841. result:=first_bool_to_int;
  1842. end;
  1843. function ttypeconvnode._first_int_to_bool : tnode;
  1844. begin
  1845. result:=first_int_to_bool;
  1846. end;
  1847. function ttypeconvnode._first_bool_to_bool : tnode;
  1848. begin
  1849. result:=first_bool_to_bool;
  1850. end;
  1851. function ttypeconvnode._first_proc_to_procvar : tnode;
  1852. begin
  1853. result:=first_proc_to_procvar;
  1854. end;
  1855. function ttypeconvnode._first_load_smallset : tnode;
  1856. begin
  1857. result:=first_load_smallset;
  1858. end;
  1859. function ttypeconvnode._first_cord_to_pointer : tnode;
  1860. begin
  1861. result:=first_cord_to_pointer;
  1862. end;
  1863. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1864. begin
  1865. result:=first_ansistring_to_pchar;
  1866. end;
  1867. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1868. begin
  1869. result:=first_arrayconstructor_to_set;
  1870. end;
  1871. function ttypeconvnode._first_class_to_intf : tnode;
  1872. begin
  1873. result:=first_class_to_intf;
  1874. end;
  1875. function ttypeconvnode._first_char_to_char : tnode;
  1876. begin
  1877. result:=first_char_to_char;
  1878. end;
  1879. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1880. const
  1881. firstconvert : array[tconverttype] of pointer = (
  1882. nil, { none }
  1883. @ttypeconvnode._first_nothing, {equal}
  1884. @ttypeconvnode._first_nothing, {not_possible}
  1885. nil, { removed in resulttype_string_to_string }
  1886. @ttypeconvnode._first_char_to_string,
  1887. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1888. nil, { removed in resulttype_chararray_to_string }
  1889. @ttypeconvnode._first_cchar_to_pchar,
  1890. @ttypeconvnode._first_cstring_to_pchar,
  1891. @ttypeconvnode._first_cstring_to_int,
  1892. @ttypeconvnode._first_ansistring_to_pchar,
  1893. @ttypeconvnode._first_string_to_chararray,
  1894. nil, { removed in resulttype_chararray_to_string }
  1895. @ttypeconvnode._first_array_to_pointer,
  1896. @ttypeconvnode._first_pointer_to_array,
  1897. @ttypeconvnode._first_int_to_int,
  1898. @ttypeconvnode._first_int_to_bool,
  1899. @ttypeconvnode._first_bool_to_bool,
  1900. @ttypeconvnode._first_bool_to_int,
  1901. @ttypeconvnode._first_real_to_real,
  1902. @ttypeconvnode._first_int_to_real,
  1903. nil, { removed in resulttype_real_to_currency }
  1904. @ttypeconvnode._first_proc_to_procvar,
  1905. @ttypeconvnode._first_arrayconstructor_to_set,
  1906. @ttypeconvnode._first_load_smallset,
  1907. @ttypeconvnode._first_cord_to_pointer,
  1908. @ttypeconvnode._first_nothing,
  1909. @ttypeconvnode._first_nothing,
  1910. @ttypeconvnode._first_class_to_intf,
  1911. @ttypeconvnode._first_char_to_char,
  1912. @ttypeconvnode._first_nothing,
  1913. @ttypeconvnode._first_nothing,
  1914. nil,
  1915. nil,
  1916. nil,
  1917. nil,
  1918. nil,
  1919. nil,
  1920. nil,
  1921. nil
  1922. );
  1923. type
  1924. tprocedureofobject = function : tnode of object;
  1925. var
  1926. r : packed record
  1927. proc : pointer;
  1928. obj : pointer;
  1929. end;
  1930. begin
  1931. { this is a little bit dirty but it works }
  1932. { and should be quite portable too }
  1933. r.proc:=firstconvert[c];
  1934. r.obj:=self;
  1935. if not assigned(r.proc) then
  1936. internalerror(200312081);
  1937. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1938. end;
  1939. function ttypeconvnode.pass_1 : tnode;
  1940. begin
  1941. result:=nil;
  1942. firstpass(left);
  1943. if codegenerror then
  1944. exit;
  1945. { load the value_str from the left part }
  1946. registersint:=left.registersint;
  1947. registersfpu:=left.registersfpu;
  1948. {$ifdef SUPPORT_MMX}
  1949. registersmmx:=left.registersmmx;
  1950. {$endif}
  1951. expectloc:=left.expectloc;
  1952. result:=first_call_helper(convtype);
  1953. end;
  1954. function ttypeconvnode.assign_allowed:boolean;
  1955. begin
  1956. result:=(convtype=tc_equal) or
  1957. { typecasting from void is always allowed }
  1958. is_void(left.resulttype.def) or
  1959. (left.resulttype.def.deftype=formaldef) or
  1960. { int 2 int with same size reuses same location, or for
  1961. tp7 mode also allow size < orignal size }
  1962. (
  1963. (convtype=tc_int_2_int) and
  1964. (
  1965. (resulttype.def.size=left.resulttype.def.size) or
  1966. ((m_tp7 in aktmodeswitches) and
  1967. (resulttype.def.size<left.resulttype.def.size))
  1968. )
  1969. ) or
  1970. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1971. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1972. (nf_explicit in flags) and
  1973. (resulttype.def.size=left.resulttype.def.size));
  1974. { When using only a part of the value it can't be in a register since
  1975. that will load the value in a new register first }
  1976. if (resulttype.def.size<left.resulttype.def.size) then
  1977. make_not_regable(left);
  1978. end;
  1979. function ttypeconvnode.docompare(p: tnode) : boolean;
  1980. begin
  1981. docompare :=
  1982. inherited docompare(p) and
  1983. (convtype = ttypeconvnode(p).convtype);
  1984. end;
  1985. procedure ttypeconvnode._second_int_to_int;
  1986. begin
  1987. second_int_to_int;
  1988. end;
  1989. procedure ttypeconvnode._second_string_to_string;
  1990. begin
  1991. second_string_to_string;
  1992. end;
  1993. procedure ttypeconvnode._second_cstring_to_pchar;
  1994. begin
  1995. second_cstring_to_pchar;
  1996. end;
  1997. procedure ttypeconvnode._second_cstring_to_int;
  1998. begin
  1999. second_cstring_to_int;
  2000. end;
  2001. procedure ttypeconvnode._second_string_to_chararray;
  2002. begin
  2003. second_string_to_chararray;
  2004. end;
  2005. procedure ttypeconvnode._second_array_to_pointer;
  2006. begin
  2007. second_array_to_pointer;
  2008. end;
  2009. procedure ttypeconvnode._second_pointer_to_array;
  2010. begin
  2011. second_pointer_to_array;
  2012. end;
  2013. procedure ttypeconvnode._second_chararray_to_string;
  2014. begin
  2015. second_chararray_to_string;
  2016. end;
  2017. procedure ttypeconvnode._second_char_to_string;
  2018. begin
  2019. second_char_to_string;
  2020. end;
  2021. procedure ttypeconvnode._second_int_to_real;
  2022. begin
  2023. second_int_to_real;
  2024. end;
  2025. procedure ttypeconvnode._second_real_to_real;
  2026. begin
  2027. second_real_to_real;
  2028. end;
  2029. procedure ttypeconvnode._second_cord_to_pointer;
  2030. begin
  2031. second_cord_to_pointer;
  2032. end;
  2033. procedure ttypeconvnode._second_proc_to_procvar;
  2034. begin
  2035. second_proc_to_procvar;
  2036. end;
  2037. procedure ttypeconvnode._second_bool_to_int;
  2038. begin
  2039. second_bool_to_int;
  2040. end;
  2041. procedure ttypeconvnode._second_int_to_bool;
  2042. begin
  2043. second_int_to_bool;
  2044. end;
  2045. procedure ttypeconvnode._second_bool_to_bool;
  2046. begin
  2047. second_bool_to_bool;
  2048. end;
  2049. procedure ttypeconvnode._second_load_smallset;
  2050. begin
  2051. second_load_smallset;
  2052. end;
  2053. procedure ttypeconvnode._second_ansistring_to_pchar;
  2054. begin
  2055. second_ansistring_to_pchar;
  2056. end;
  2057. procedure ttypeconvnode._second_class_to_intf;
  2058. begin
  2059. second_class_to_intf;
  2060. end;
  2061. procedure ttypeconvnode._second_char_to_char;
  2062. begin
  2063. second_char_to_char;
  2064. end;
  2065. procedure ttypeconvnode._second_nothing;
  2066. begin
  2067. second_nothing;
  2068. end;
  2069. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2070. const
  2071. secondconvert : array[tconverttype] of pointer = (
  2072. @ttypeconvnode._second_nothing, {none}
  2073. @ttypeconvnode._second_nothing, {equal}
  2074. @ttypeconvnode._second_nothing, {not_possible}
  2075. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2076. @ttypeconvnode._second_char_to_string,
  2077. @ttypeconvnode._second_nothing, {char_to_charray}
  2078. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2079. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2080. @ttypeconvnode._second_cstring_to_pchar,
  2081. @ttypeconvnode._second_cstring_to_int,
  2082. @ttypeconvnode._second_ansistring_to_pchar,
  2083. @ttypeconvnode._second_string_to_chararray,
  2084. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2085. @ttypeconvnode._second_array_to_pointer,
  2086. @ttypeconvnode._second_pointer_to_array,
  2087. @ttypeconvnode._second_int_to_int,
  2088. @ttypeconvnode._second_int_to_bool,
  2089. @ttypeconvnode._second_bool_to_bool,
  2090. @ttypeconvnode._second_bool_to_int,
  2091. @ttypeconvnode._second_real_to_real,
  2092. @ttypeconvnode._second_int_to_real,
  2093. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2094. @ttypeconvnode._second_proc_to_procvar,
  2095. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2096. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2097. @ttypeconvnode._second_cord_to_pointer,
  2098. @ttypeconvnode._second_nothing, { interface 2 string }
  2099. @ttypeconvnode._second_nothing, { interface 2 guid }
  2100. @ttypeconvnode._second_class_to_intf,
  2101. @ttypeconvnode._second_char_to_char,
  2102. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2103. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2104. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2105. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2106. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2107. @ttypeconvnode._second_nothing, { variant_2_enum }
  2108. @ttypeconvnode._second_nothing, { enum_2_variant }
  2109. @ttypeconvnode._second_nothing, { variant_2_interface }
  2110. @ttypeconvnode._second_nothing, { interface_2_variant }
  2111. @ttypeconvnode._second_nothing { array_2_dynarray }
  2112. );
  2113. type
  2114. tprocedureofobject = procedure of object;
  2115. var
  2116. r : packed record
  2117. proc : pointer;
  2118. obj : pointer;
  2119. end;
  2120. begin
  2121. { this is a little bit dirty but it works }
  2122. { and should be quite portable too }
  2123. r.proc:=secondconvert[c];
  2124. r.obj:=self;
  2125. tprocedureofobject(r)();
  2126. end;
  2127. {*****************************************************************************
  2128. TISNODE
  2129. *****************************************************************************}
  2130. constructor tisnode.create(l,r : tnode);
  2131. begin
  2132. inherited create(isn,l,r);
  2133. end;
  2134. function tisnode.det_resulttype:tnode;
  2135. var
  2136. paras: tcallparanode;
  2137. begin
  2138. result:=nil;
  2139. resulttypepass(left);
  2140. resulttypepass(right);
  2141. set_varstate(left,vs_used,[vsf_must_be_valid]);
  2142. set_varstate(right,vs_used,[vsf_must_be_valid]);
  2143. if codegenerror then
  2144. exit;
  2145. if (right.resulttype.def.deftype=classrefdef) then
  2146. begin
  2147. { left must be a class }
  2148. if is_class(left.resulttype.def) then
  2149. begin
  2150. { the operands must be related }
  2151. if (not(tobjectdef(left.resulttype.def).is_related(
  2152. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2153. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2154. tobjectdef(left.resulttype.def)))) then
  2155. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2156. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  2157. end
  2158. else
  2159. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2160. { call fpc_do_is helper }
  2161. paras := ccallparanode.create(
  2162. left,
  2163. ccallparanode.create(
  2164. right,nil));
  2165. result := ccallnode.createintern('fpc_do_is',paras);
  2166. left := nil;
  2167. right := nil;
  2168. end
  2169. else if is_interface(right.resulttype.def) then
  2170. begin
  2171. { left is a class }
  2172. if is_class(left.resulttype.def) then
  2173. begin
  2174. { the operands must be related }
  2175. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2176. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2177. CGMessage2(type_e_classes_not_related,
  2178. FullTypeName(left.resulttype.def,right.resulttype.def),
  2179. FullTypeName(right.resulttype.def,left.resulttype.def))
  2180. end
  2181. { left is an interface }
  2182. else if is_interface(left.resulttype.def) then
  2183. begin
  2184. { the operands must be related }
  2185. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2186. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2187. CGMessage2(type_e_classes_not_related,
  2188. FullTypeName(left.resulttype.def,right.resulttype.def),
  2189. FullTypeName(right.resulttype.def,left.resulttype.def));
  2190. end
  2191. else
  2192. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2193. { call fpc_do_is helper }
  2194. paras := ccallparanode.create(
  2195. left,
  2196. ccallparanode.create(
  2197. right,nil));
  2198. result := ccallnode.createintern('fpc_do_is',paras);
  2199. left := nil;
  2200. right := nil;
  2201. end
  2202. else
  2203. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2204. resulttype:=booltype;
  2205. end;
  2206. function tisnode.pass_1 : tnode;
  2207. begin
  2208. internalerror(200204254);
  2209. result:=nil;
  2210. end;
  2211. { dummy pass_2, it will never be called, but we need one since }
  2212. { you can't instantiate an abstract class }
  2213. procedure tisnode.pass_2;
  2214. begin
  2215. end;
  2216. {*****************************************************************************
  2217. TASNODE
  2218. *****************************************************************************}
  2219. constructor tasnode.create(l,r : tnode);
  2220. begin
  2221. inherited create(asn,l,r);
  2222. call := nil;
  2223. end;
  2224. destructor tasnode.destroy;
  2225. begin
  2226. call.free;
  2227. inherited destroy;
  2228. end;
  2229. function tasnode.det_resulttype:tnode;
  2230. var
  2231. hp : tnode;
  2232. begin
  2233. result:=nil;
  2234. resulttypepass(right);
  2235. resulttypepass(left);
  2236. set_varstate(right,vs_used,[vsf_must_be_valid]);
  2237. set_varstate(left,vs_used,[vsf_must_be_valid]);
  2238. if codegenerror then
  2239. exit;
  2240. if (right.resulttype.def.deftype=classrefdef) then
  2241. begin
  2242. { left must be a class }
  2243. if is_class(left.resulttype.def) then
  2244. begin
  2245. { the operands must be related }
  2246. if (not(tobjectdef(left.resulttype.def).is_related(
  2247. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2248. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2249. tobjectdef(left.resulttype.def)))) then
  2250. CGMessage2(type_e_classes_not_related,
  2251. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2252. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2253. end
  2254. else
  2255. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2256. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2257. end
  2258. else if is_interface(right.resulttype.def) then
  2259. begin
  2260. { left is a class }
  2261. if not(is_class(left.resulttype.def) or
  2262. is_interface(left.resulttype.def)) then
  2263. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2264. resulttype:=right.resulttype;
  2265. { load the GUID of the interface }
  2266. if (right.nodetype=typen) then
  2267. begin
  2268. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2269. begin
  2270. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2271. right.free;
  2272. right:=hp;
  2273. end
  2274. else
  2275. internalerror(200206282);
  2276. resulttypepass(right);
  2277. end;
  2278. end
  2279. else
  2280. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2281. end;
  2282. function tasnode._getcopy: tnode;
  2283. begin
  2284. result := inherited _getcopy;
  2285. if assigned(call) then
  2286. tasnode(result).call := call.getcopy
  2287. else
  2288. tasnode(result).call := nil;
  2289. end;
  2290. function tasnode.pass_1 : tnode;
  2291. var
  2292. procname: string;
  2293. begin
  2294. result:=nil;
  2295. if not assigned(call) then
  2296. begin
  2297. if is_class(left.resulttype.def) and
  2298. (right.resulttype.def.deftype=classrefdef) then
  2299. call := ccallnode.createinternres('fpc_do_as',
  2300. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2301. resulttype)
  2302. else
  2303. begin
  2304. if is_class(left.resulttype.def) then
  2305. procname := 'fpc_class_as_intf'
  2306. else
  2307. procname := 'fpc_intf_as';
  2308. call := ccallnode.createinternres(procname,
  2309. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2310. resulttype);
  2311. end;
  2312. left := nil;
  2313. right := nil;
  2314. firstpass(call);
  2315. if codegenerror then
  2316. exit;
  2317. expectloc:=call.expectloc;
  2318. registersint:=call.registersint;
  2319. registersfpu:=call.registersfpu;
  2320. {$ifdef SUPPORT_MMX}
  2321. registersmmx:=call.registersmmx;
  2322. {$endif SUPPORT_MMX}
  2323. end;
  2324. end;
  2325. begin
  2326. ctypeconvnode:=ttypeconvnode;
  2327. casnode:=tasnode;
  2328. cisnode:=tisnode;
  2329. end.