ncnv.pas 114 KB

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