ncnv.pas 96 KB

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