ncnv.pas 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156
  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. newblock : tblocknode;
  886. newstat : tstatementnode;
  887. restemp : ttempcreatenode;
  888. begin
  889. result:=nil;
  890. { we can't do widechar to ansichar conversions at compile time, since }
  891. { this maps all non-ascii chars to '?' -> loses information }
  892. if (left.nodetype=ordconstn) and
  893. ((tstringdef(resultdef).stringtype=st_widestring) or
  894. (torddef(left.resultdef).ordtype=uchar) or
  895. { >=128 is destroyed }
  896. (tordconstnode(left).value.uvalue<128)) then
  897. begin
  898. if tstringdef(resultdef).stringtype=st_widestring then
  899. begin
  900. initwidestring(ws);
  901. if torddef(left.resultdef).ordtype=uwidechar then
  902. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value.uvalue))
  903. else
  904. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value.uvalue)));
  905. hp:=cstringconstnode.createwstr(ws);
  906. donewidestring(ws);
  907. end
  908. else
  909. begin
  910. if torddef(left.resultdef).ordtype=uwidechar then
  911. hp:=cstringconstnode.createstr(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue)))
  912. else
  913. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value.uvalue));
  914. tstringconstnode(hp).changestringtype(resultdef);
  915. end;
  916. result:=hp;
  917. end
  918. else
  919. { shortstrings are handled 'inline' (except for widechars) }
  920. if (tstringdef(resultdef).stringtype <> st_shortstring) or
  921. (torddef(left.resultdef).ordtype = uwidechar) then
  922. begin
  923. if (tstringdef(resultdef).stringtype <> st_shortstring) then
  924. begin
  925. { create the procname }
  926. if torddef(left.resultdef).ordtype<>uwidechar then
  927. procname := 'fpc_char_to_'
  928. else
  929. procname := 'fpc_wchar_to_';
  930. procname:=procname+tstringdef(resultdef).stringtypname;
  931. { and the parameter }
  932. para := ccallparanode.create(left,nil);
  933. { and finally the call }
  934. result := ccallnode.createinternres(procname,para,resultdef);
  935. end
  936. else
  937. begin
  938. newblock:=internalstatements(newstat);
  939. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  940. addstatement(newstat,restemp);
  941. addstatement(newstat,ccallnode.createintern('fpc_wchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  942. ctemprefnode.create(restemp),nil))));
  943. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  944. addstatement(newstat,ctemprefnode.create(restemp));
  945. result:=newblock;
  946. end;
  947. left := nil;
  948. end
  949. else
  950. begin
  951. { create word(byte(char) shl 8 or 1) for litte endian machines }
  952. { and word(byte(char) or 256) for big endian machines }
  953. left := ctypeconvnode.create_internal(left,u8inttype);
  954. if (target_info.endian = endian_little) then
  955. left := caddnode.create(orn,
  956. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  957. cordconstnode.create(1,s32inttype,false))
  958. else
  959. left := caddnode.create(orn,left,
  960. cordconstnode.create(1 shl 8,s32inttype,false));
  961. left := ctypeconvnode.create_internal(left,u16inttype);
  962. typecheckpass(left);
  963. end;
  964. end;
  965. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  966. begin
  967. if resultdef.size <> 1 then
  968. begin
  969. { convert first to string, then to chararray }
  970. inserttypeconv(left,cshortstringtype);
  971. inserttypeconv(left,resultdef);
  972. result:=left;
  973. left := nil;
  974. exit;
  975. end;
  976. result := nil;
  977. end;
  978. function ttypeconvnode.typecheck_char_to_char : tnode;
  979. var
  980. hp : tordconstnode;
  981. begin
  982. result:=nil;
  983. if (left.nodetype=ordconstn) and
  984. ((torddef(resultdef).ordtype<>uchar) or
  985. (torddef(left.resultdef).ordtype<>uwidechar) or
  986. { >= 128 is replaced by '?' currently -> loses information }
  987. (tordconstnode(left).value.uvalue<128)) then
  988. begin
  989. if (torddef(resultdef).ordtype=uchar) and
  990. (torddef(left.resultdef).ordtype=uwidechar) then
  991. begin
  992. hp:=cordconstnode.create(
  993. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value.uvalue))),
  994. cchartype,true);
  995. result:=hp;
  996. end
  997. else if (torddef(resultdef).ordtype=uwidechar) and
  998. (torddef(left.resultdef).ordtype=uchar) then
  999. begin
  1000. hp:=cordconstnode.create(
  1001. asciichar2unicode(chr(tordconstnode(left).value.uvalue)),
  1002. cwidechartype,true);
  1003. result:=hp;
  1004. end
  1005. else
  1006. internalerror(200105131);
  1007. exit;
  1008. end;
  1009. end;
  1010. function ttypeconvnode.typecheck_int_to_int : tnode;
  1011. var
  1012. v : TConstExprInt;
  1013. begin
  1014. result:=nil;
  1015. if left.nodetype=ordconstn then
  1016. begin
  1017. v:=tordconstnode(left).value;
  1018. if is_currency(resultdef) then
  1019. v:=v*10000;
  1020. if (resultdef.typ=pointerdef) then
  1021. result:=cpointerconstnode.create(TConstPtrUInt(v.uvalue),resultdef)
  1022. else
  1023. begin
  1024. if is_currency(left.resultdef) then
  1025. v:=v div 10000;
  1026. result:=cordconstnode.create(v,resultdef,false);
  1027. end;
  1028. end
  1029. else if left.nodetype=pointerconstn then
  1030. begin
  1031. v:=tpointerconstnode(left).value;
  1032. if (resultdef.typ=pointerdef) then
  1033. result:=cpointerconstnode.create(v.uvalue,resultdef)
  1034. else
  1035. begin
  1036. if is_currency(resultdef) then
  1037. v:=v*10000;
  1038. result:=cordconstnode.create(v,resultdef,false);
  1039. end;
  1040. end
  1041. else
  1042. begin
  1043. { multiply by 10000 for currency. We need to use getcopy to pass
  1044. the argument because the current node is always disposed. Only
  1045. inserting the multiply in the left node is not possible because
  1046. it'll get in an infinite loop to convert int->currency }
  1047. if is_currency(resultdef) then
  1048. begin
  1049. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  1050. include(result.flags,nf_is_currency);
  1051. end
  1052. else if is_currency(left.resultdef) then
  1053. begin
  1054. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  1055. include(result.flags,nf_is_currency);
  1056. end;
  1057. end;
  1058. end;
  1059. function ttypeconvnode.typecheck_int_to_real : tnode;
  1060. var
  1061. rv : bestreal;
  1062. begin
  1063. result:=nil;
  1064. if left.nodetype=ordconstn then
  1065. begin
  1066. rv:=tordconstnode(left).value;
  1067. if is_currency(resultdef) then
  1068. rv:=rv*10000.0
  1069. else if is_currency(left.resultdef) then
  1070. rv:=rv/10000.0;
  1071. result:=crealconstnode.create(rv,resultdef);
  1072. end
  1073. else
  1074. begin
  1075. { multiply by 10000 for currency. We need to use getcopy to pass
  1076. the argument because the current node is always disposed. Only
  1077. inserting the multiply in the left node is not possible because
  1078. it'll get in an infinite loop to convert int->currency }
  1079. if is_currency(resultdef) then
  1080. begin
  1081. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  1082. include(result.flags,nf_is_currency);
  1083. end
  1084. else if is_currency(left.resultdef) then
  1085. begin
  1086. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  1087. include(result.flags,nf_is_currency);
  1088. end;
  1089. end;
  1090. end;
  1091. function ttypeconvnode.typecheck_real_to_currency : tnode;
  1092. begin
  1093. if not is_currency(resultdef) then
  1094. internalerror(200304221);
  1095. result:=nil;
  1096. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1097. include(left.flags,nf_is_currency);
  1098. typecheckpass(left);
  1099. { Convert constants directly, else call Round() }
  1100. if left.nodetype=realconstn then
  1101. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  1102. else
  1103. begin
  1104. result:=ccallnode.createinternres('fpc_round_real',
  1105. ccallparanode.create(left,nil),resultdef);
  1106. left:=nil;
  1107. end;
  1108. end;
  1109. function ttypeconvnode.typecheck_real_to_real : tnode;
  1110. begin
  1111. result:=nil;
  1112. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  1113. begin
  1114. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  1115. include(left.flags,nf_is_currency);
  1116. typecheckpass(left);
  1117. end
  1118. else
  1119. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  1120. begin
  1121. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  1122. include(left.flags,nf_is_currency);
  1123. typecheckpass(left);
  1124. end;
  1125. end;
  1126. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  1127. begin
  1128. result:=nil;
  1129. if is_pwidechar(resultdef) then
  1130. inserttypeconv(left,cwidestringtype)
  1131. else
  1132. inserttypeconv(left,cshortstringtype);
  1133. { evaluate again, reset resultdef so the convert_typ
  1134. will be calculated again and cstring_to_pchar will
  1135. be used for futher conversion }
  1136. convtype:=tc_none;
  1137. result:=pass_typecheck;
  1138. end;
  1139. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  1140. begin
  1141. result:=nil;
  1142. if is_pwidechar(resultdef) then
  1143. inserttypeconv(left,cwidestringtype)
  1144. else
  1145. if is_pchar(resultdef) and
  1146. is_widestring(left.resultdef) then
  1147. begin
  1148. inserttypeconv(left,cansistringtype);
  1149. { the second pass of second_cstring_to_pchar expects a }
  1150. { strinconstn, but this may become a call to the }
  1151. { widestring manager in case left contains "high ascii" }
  1152. if (left.nodetype<>stringconstn) then
  1153. begin
  1154. result:=left;
  1155. left:=nil;
  1156. end;
  1157. end;
  1158. end;
  1159. function ttypeconvnode.typecheck_cstring_to_int : tnode;
  1160. var
  1161. fcc : cardinal;
  1162. pb : pbyte;
  1163. begin
  1164. result:=nil;
  1165. if left.nodetype<>stringconstn then
  1166. internalerror(200510012);
  1167. if tstringconstnode(left).len=4 then
  1168. begin
  1169. pb:=pbyte(tstringconstnode(left).value_str);
  1170. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1171. result:=cordconstnode.create(fcc,u32inttype,false);
  1172. end
  1173. else
  1174. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1175. end;
  1176. function ttypeconvnode.typecheck_arrayconstructor_to_set : tnode;
  1177. var
  1178. hp : tnode;
  1179. begin
  1180. result:=nil;
  1181. if left.nodetype<>arrayconstructorn then
  1182. internalerror(5546);
  1183. { remove typeconv node }
  1184. hp:=left;
  1185. left:=nil;
  1186. { create a set constructor tree }
  1187. arrayconstructor_to_set(hp);
  1188. result:=hp;
  1189. end;
  1190. function ttypeconvnode.typecheck_set_to_set : tnode;
  1191. begin
  1192. result:=nil;
  1193. { constant sets can be converted by changing the type only }
  1194. if (left.nodetype=setconstn) then
  1195. begin
  1196. left.resultdef:=resultdef;
  1197. result:=left;
  1198. left:=nil;
  1199. exit;
  1200. end;
  1201. end;
  1202. function ttypeconvnode.typecheck_pchar_to_string : tnode;
  1203. var
  1204. newblock : tblocknode;
  1205. newstat : tstatementnode;
  1206. restemp : ttempcreatenode;
  1207. begin
  1208. if tstringdef(resultdef).stringtype=st_shortstring then
  1209. begin
  1210. newblock:=internalstatements(newstat);
  1211. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1212. addstatement(newstat,restemp);
  1213. addstatement(newstat,ccallnode.createintern('fpc_pchar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1214. ctemprefnode.create(restemp),nil))));
  1215. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1216. addstatement(newstat,ctemprefnode.create(restemp));
  1217. result:=newblock;
  1218. end
  1219. else
  1220. result := ccallnode.createinternres(
  1221. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1222. ccallparanode.create(left,nil),resultdef);
  1223. left:=nil;
  1224. end;
  1225. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1226. begin
  1227. if assigned(tobjectdef(left.resultdef).iidguid) then
  1228. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1229. end;
  1230. function ttypeconvnode.typecheck_dynarray_to_openarray : tnode;
  1231. begin
  1232. { a dynamic array is a pointer to an array, so to convert it to }
  1233. { an open array, we have to dereference it (JM) }
  1234. result := ctypeconvnode.create_internal(left,voidpointertype);
  1235. typecheckpass(result);
  1236. { left is reused }
  1237. left := nil;
  1238. result := cderefnode.create(result);
  1239. include(result.flags,nf_no_checkpointer);
  1240. result.resultdef := resultdef;
  1241. end;
  1242. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1243. var
  1244. newblock : tblocknode;
  1245. newstat : tstatementnode;
  1246. restemp : ttempcreatenode;
  1247. begin
  1248. if tstringdef(resultdef).stringtype=st_shortstring then
  1249. begin
  1250. newblock:=internalstatements(newstat);
  1251. restemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1252. addstatement(newstat,restemp);
  1253. addstatement(newstat,ccallnode.createintern('fpc_pwidechar_to_shortstr',ccallparanode.create(left,ccallparanode.create(
  1254. ctemprefnode.create(restemp),nil))));
  1255. addstatement(newstat,ctempdeletenode.create_normal_temp(restemp));
  1256. addstatement(newstat,ctemprefnode.create(restemp));
  1257. result:=newblock;
  1258. end
  1259. else
  1260. result := ccallnode.createinternres(
  1261. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1262. ccallparanode.create(left,nil),resultdef);
  1263. left:=nil;
  1264. end;
  1265. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1266. begin
  1267. result := ccallnode.createinternres(
  1268. 'fpc_variant_to_dynarray',
  1269. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1270. ccallparanode.create(left,nil)
  1271. ),resultdef);
  1272. typecheckpass(result);
  1273. left:=nil;
  1274. end;
  1275. function ttypeconvnode.typecheck_dynarray_to_variant : tnode;
  1276. begin
  1277. result := ccallnode.createinternres(
  1278. 'fpc_dynarray_to_variant',
  1279. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resultdef),initrtti,rdt_normal)),
  1280. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1281. ),resultdef);
  1282. typecheckpass(result);
  1283. left:=nil;
  1284. end;
  1285. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1286. begin
  1287. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1288. result := ccallnode.createinternres(
  1289. 'fpc_variant_to_idispatch',
  1290. ccallparanode.create(left,nil)
  1291. ,resultdef)
  1292. else
  1293. result := ccallnode.createinternres(
  1294. 'fpc_variant_to_interface',
  1295. ccallparanode.create(left,nil)
  1296. ,resultdef);
  1297. typecheckpass(result);
  1298. left:=nil;
  1299. end;
  1300. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1301. begin
  1302. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1303. result := ccallnode.createinternres(
  1304. 'fpc_idispatch_to_variant',
  1305. ccallparanode.create(left,nil)
  1306. ,resultdef)
  1307. else
  1308. result := ccallnode.createinternres(
  1309. 'fpc_interface_to_variant',
  1310. ccallparanode.create(left,nil)
  1311. ,resultdef);
  1312. typecheckpass(result);
  1313. left:=nil;
  1314. end;
  1315. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1316. begin
  1317. result := ctypeconvnode.create_internal(left,sinttype);
  1318. result := ctypeconvnode.create_internal(result,resultdef);
  1319. typecheckpass(result);
  1320. { left is reused }
  1321. left := nil;
  1322. end;
  1323. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1324. begin
  1325. result := ctypeconvnode.create_internal(left,sinttype);
  1326. result := ctypeconvnode.create_internal(result,cvarianttype);
  1327. typecheckpass(result);
  1328. { left is reused }
  1329. left := nil;
  1330. end;
  1331. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1332. var
  1333. newstatement : tstatementnode;
  1334. temp : ttempcreatenode;
  1335. temp2 : ttempcreatenode;
  1336. begin
  1337. { create statements with call to getmem+initialize }
  1338. result:=internalstatements(newstatement);
  1339. { create temp for result }
  1340. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1341. addstatement(newstatement,temp);
  1342. { get temp for array of lengths }
  1343. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1344. addstatement(newstatement,temp2);
  1345. { one dimensional }
  1346. addstatement(newstatement,cassignmentnode.create(
  1347. ctemprefnode.create_offset(temp2,0),
  1348. cordconstnode.create
  1349. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1350. { create call to fpc_dynarr_setlength }
  1351. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1352. ccallparanode.create(caddrnode.create_internal
  1353. (ctemprefnode.create(temp2)),
  1354. ccallparanode.create(cordconstnode.create
  1355. (1,s32inttype,true),
  1356. ccallparanode.create(caddrnode.create_internal
  1357. (crttinode.create(tstoreddef(resultdef),initrtti,rdt_normal)),
  1358. ccallparanode.create(
  1359. ctypeconvnode.create_internal(
  1360. ctemprefnode.create(temp),voidpointertype),
  1361. nil))))
  1362. ));
  1363. addstatement(newstatement,ctempdeletenode.create(temp2));
  1364. { copy ... }
  1365. addstatement(newstatement,cassignmentnode.create(
  1366. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1367. left
  1368. ));
  1369. { left is reused }
  1370. left:=nil;
  1371. { the last statement should return the value as
  1372. location and type, this is done be referencing the
  1373. temp and converting it first from a persistent temp to
  1374. normal temp }
  1375. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1376. addstatement(newstatement,ctemprefnode.create(temp));
  1377. end;
  1378. procedure copyparasym(p:TObject;arg:pointer);
  1379. var
  1380. newparast : TSymtable absolute arg;
  1381. vs : tparavarsym;
  1382. begin
  1383. if tsym(p).typ<>paravarsym then
  1384. exit;
  1385. with tparavarsym(p) do
  1386. begin
  1387. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1388. vs.defaultconstsym:=defaultconstsym;
  1389. newparast.insert(vs);
  1390. end;
  1391. end;
  1392. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1393. var
  1394. pd : tabstractprocdef;
  1395. begin
  1396. result:=nil;
  1397. pd:=tabstractprocdef(left.resultdef);
  1398. { create procvardef }
  1399. resultdef:=tprocvardef.create(pd.parast.symtablelevel);
  1400. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1401. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1402. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1403. tprocvardef(resultdef).returndef:=pd.returndef;
  1404. { method ? then set the methodpointer flag }
  1405. if (pd.owner.symtabletype=ObjectSymtable) then
  1406. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1407. { was it a local procedure? }
  1408. if (pd.owner.symtabletype=localsymtable) then
  1409. include(tprocvardef(resultdef).procoptions,po_local);
  1410. { only need the address of the method? this is needed
  1411. for @tobject.create. In this case there will be a loadn without
  1412. a methodpointer. }
  1413. if (left.nodetype=loadn) and
  1414. not assigned(tloadnode(left).left) then
  1415. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1416. { Add parameters use only references, we don't need to keep the
  1417. parast. We use the parast from the original function to calculate
  1418. our parameter data and reset it afterwards }
  1419. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1420. tprocvardef(resultdef).calcparas;
  1421. end;
  1422. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1423. const
  1424. resultdefconvert : array[tconverttype] of pointer = (
  1425. {none} nil,
  1426. {equal} nil,
  1427. {not_possible} nil,
  1428. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1429. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1430. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1431. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1432. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1433. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1434. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1435. { ansistring_2_pchar } nil,
  1436. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1437. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1438. { array_2_pointer } nil,
  1439. { pointer_2_array } nil,
  1440. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1441. { int_2_bool } nil,
  1442. { bool_2_bool } nil,
  1443. { bool_2_int } nil,
  1444. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1445. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1446. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1447. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1448. { nil_2_methodprocvar } nil,
  1449. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1450. { set_to_set } @ttypeconvnode.typecheck_set_to_set,
  1451. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1452. { intf_2_string } nil,
  1453. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1454. { class_2_intf } nil,
  1455. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1456. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1457. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1458. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1459. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1460. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1461. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1462. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1463. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1464. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1465. );
  1466. type
  1467. tprocedureofobject = function : tnode of object;
  1468. var
  1469. r : packed record
  1470. proc : pointer;
  1471. obj : pointer;
  1472. end;
  1473. begin
  1474. result:=nil;
  1475. { this is a little bit dirty but it works }
  1476. { and should be quite portable too }
  1477. r.proc:=resultdefconvert[c];
  1478. r.obj:=self;
  1479. if assigned(r.proc) then
  1480. result:=tprocedureofobject(r)();
  1481. end;
  1482. function ttypeconvnode.pass_typecheck:tnode;
  1483. var
  1484. hdef : tdef;
  1485. hp : tnode;
  1486. currprocdef : tabstractprocdef;
  1487. aprocdef : tprocdef;
  1488. eq : tequaltype;
  1489. cdoptions : tcompare_defs_options;
  1490. newblock: tblocknode;
  1491. newstatement: tstatementnode;
  1492. tempnode: ttempcreatenode;
  1493. begin
  1494. result:=nil;
  1495. resultdef:=totypedef;
  1496. typecheckpass(left);
  1497. if codegenerror then
  1498. exit;
  1499. { When absolute force tc_equal }
  1500. if (nf_absolute in flags) then
  1501. begin
  1502. convtype:=tc_equal;
  1503. if not(tstoreddef(resultdef).is_intregable) and
  1504. not(tstoreddef(resultdef).is_fpuregable) then
  1505. make_not_regable(left,[ra_addr_regable]);
  1506. exit;
  1507. end;
  1508. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1509. convert on the procvar value. This is used to access the
  1510. fields of a methodpointer }
  1511. if not(nf_load_procvar in flags) and
  1512. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1513. maybe_call_procvar(left,true);
  1514. { convert array constructors to sets, because there is no conversion
  1515. possible for array constructors }
  1516. if (resultdef.typ<>arraydef) and
  1517. is_array_constructor(left.resultdef) then
  1518. begin
  1519. arrayconstructor_to_set(left);
  1520. typecheckpass(left);
  1521. end;
  1522. if convtype=tc_none then
  1523. begin
  1524. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1525. if nf_explicit in flags then
  1526. include(cdoptions,cdo_explicit);
  1527. if nf_internal in flags then
  1528. include(cdoptions,cdo_internal);
  1529. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1530. case eq of
  1531. te_exact,
  1532. te_equal :
  1533. begin
  1534. result := simplify;
  1535. if assigned(result) then
  1536. exit;
  1537. { Only leave when there is no conversion to do.
  1538. We can still need to call a conversion routine,
  1539. like the routine to convert a stringconstnode }
  1540. if convtype in [tc_equal,tc_not_possible] then
  1541. begin
  1542. left.resultdef:=resultdef;
  1543. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1544. include(left.flags, nf_typedaddr);
  1545. result:=left;
  1546. left:=nil;
  1547. exit;
  1548. end;
  1549. end;
  1550. te_convert_l1,
  1551. te_convert_l2,
  1552. te_convert_l3,
  1553. te_convert_l4:
  1554. begin
  1555. result := simplify;
  1556. if assigned(result) then
  1557. exit;
  1558. { nothing to do }
  1559. end;
  1560. te_convert_operator :
  1561. begin
  1562. include(current_procinfo.flags,pi_do_call);
  1563. aprocdef.procsym.IncRefCountBy(1);
  1564. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1565. { tell explicitly which def we must use !! (PM) }
  1566. tcallnode(hp).procdefinition:=aprocdef;
  1567. left:=nil;
  1568. result:=hp;
  1569. exit;
  1570. end;
  1571. te_incompatible :
  1572. begin
  1573. { Procedures have a resultdef of voiddef and functions of their
  1574. own resultdef. They will therefore always be incompatible with
  1575. a procvar. Because isconvertable cannot check for procedures we
  1576. use an extra check for them.}
  1577. if (left.nodetype=calln) and
  1578. (tcallnode(left).para_count=0) and
  1579. (resultdef.typ=procvardef) and
  1580. (
  1581. (m_tp_procvar in current_settings.modeswitches) or
  1582. (m_mac_procvar in current_settings.modeswitches)
  1583. ) then
  1584. begin
  1585. if assigned(tcallnode(left).right) then
  1586. begin
  1587. { this is already a procvar, if it is really equal
  1588. is checked below }
  1589. convtype:=tc_equal;
  1590. hp:=tcallnode(left).right.getcopy;
  1591. currprocdef:=tabstractprocdef(hp.resultdef);
  1592. end
  1593. else
  1594. begin
  1595. convtype:=tc_proc_2_procvar;
  1596. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1597. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1598. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1599. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1600. begin
  1601. if assigned(tcallnode(left).methodpointer) then
  1602. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  1603. else
  1604. tloadnode(hp).set_mp(load_self_node);
  1605. end;
  1606. typecheckpass(hp);
  1607. end;
  1608. left.free;
  1609. left:=hp;
  1610. { Now check if the procedure we are going to assign to
  1611. the procvar, is compatible with the procvar's type }
  1612. if not(nf_explicit in flags) and
  1613. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef))=te_incompatible) then
  1614. IncompatibleTypes(left.resultdef,resultdef);
  1615. exit;
  1616. end;
  1617. { Handle explicit type conversions }
  1618. if nf_explicit in flags then
  1619. begin
  1620. { do common tc_equal cast }
  1621. convtype:=tc_equal;
  1622. { ordinal constants can be resized to 1,2,4,8 bytes }
  1623. if (left.nodetype=ordconstn) then
  1624. begin
  1625. { Insert typeconv for ordinal to the correct size first on left, after
  1626. that the other conversion can be done }
  1627. hdef:=nil;
  1628. case longint(resultdef.size) of
  1629. 1 :
  1630. hdef:=s8inttype;
  1631. 2 :
  1632. hdef:=s16inttype;
  1633. 4 :
  1634. hdef:=s32inttype;
  1635. 8 :
  1636. hdef:=s64inttype;
  1637. end;
  1638. { we need explicit, because it can also be an enum }
  1639. if assigned(hdef) then
  1640. inserttypeconv_internal(left,hdef)
  1641. else
  1642. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1643. end;
  1644. { check if the result could be in a register }
  1645. if (not(tstoreddef(resultdef).is_intregable) and
  1646. not(tstoreddef(resultdef).is_fpuregable)) or
  1647. ((left.resultdef.typ = floatdef) and
  1648. (resultdef.typ <> floatdef)) then
  1649. make_not_regable(left,[ra_addr_regable]);
  1650. { class/interface to class/interface, with checkobject support }
  1651. if is_class_or_interface(resultdef) and
  1652. is_class_or_interface(left.resultdef) then
  1653. begin
  1654. { check if the types are related }
  1655. if not(nf_internal in flags) and
  1656. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1657. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1658. begin
  1659. { Give an error when typecasting class to interface, this is compatible
  1660. with delphi }
  1661. if is_interface(resultdef) and
  1662. not is_interface(left.resultdef) then
  1663. CGMessage2(type_e_classes_not_related,
  1664. FullTypeName(left.resultdef,resultdef),
  1665. FullTypeName(resultdef,left.resultdef))
  1666. else
  1667. CGMessage2(type_w_classes_not_related,
  1668. FullTypeName(left.resultdef,resultdef),
  1669. FullTypeName(resultdef,left.resultdef))
  1670. end;
  1671. { Add runtime check? }
  1672. if (cs_check_object in current_settings.localswitches) and
  1673. not(nf_internal in flags) then
  1674. begin
  1675. { we can translate the typeconvnode to 'as' when
  1676. typecasting to a class or interface }
  1677. { we need to make sure the result can still be
  1678. passed as a var parameter }
  1679. newblock:=internalstatements(newstatement);
  1680. if (valid_for_var(left,false)) then
  1681. begin
  1682. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1683. addstatement(newstatement,tempnode);
  1684. addstatement(newstatement,cassignmentnode.create(
  1685. ctemprefnode.create(tempnode),
  1686. caddrnode.create_internal(left)));
  1687. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1688. end
  1689. else
  1690. begin
  1691. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1692. addstatement(newstatement,tempnode);
  1693. addstatement(newstatement,cassignmentnode.create(
  1694. ctemprefnode.create(tempnode),
  1695. left));
  1696. left:=ctemprefnode.create(tempnode);
  1697. end;
  1698. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1699. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1700. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1701. left:=nil;
  1702. result:=newblock;
  1703. exit;
  1704. end;
  1705. end
  1706. else
  1707. begin
  1708. { only if the same size or formal def, and }
  1709. { don't allow type casting of constants to }
  1710. { structured types }
  1711. if not(
  1712. (left.resultdef.typ=formaldef) or
  1713. (
  1714. not(is_open_array(left.resultdef)) and
  1715. not(is_array_constructor(left.resultdef)) and
  1716. (left.resultdef.size=resultdef.size) and
  1717. { disallow casts of const nodes }
  1718. (not is_constnode(left) or
  1719. { however, there are some exceptions }
  1720. (not(resultdef.typ in [arraydef,recorddef,setdef,stringdef,
  1721. filedef,variantdef,objectdef]) or
  1722. is_class_or_interface(resultdef) or
  1723. { the softfloat code generates casts <const. float> to record }
  1724. (nf_internal in flags)
  1725. ))
  1726. ) or
  1727. (
  1728. is_void(left.resultdef) and
  1729. (left.nodetype=derefn)
  1730. )
  1731. ) then
  1732. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1733. end;
  1734. end
  1735. else
  1736. IncompatibleTypes(left.resultdef,resultdef);
  1737. end;
  1738. else
  1739. internalerror(200211231);
  1740. end;
  1741. end;
  1742. { Give hint or warning for unportable code, exceptions are
  1743. - typecasts from constants
  1744. - void }
  1745. if not(nf_internal in flags) and
  1746. (left.nodetype<>ordconstn) and
  1747. not(is_void(left.resultdef)) and
  1748. (((left.resultdef.typ=orddef) and
  1749. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1750. ((resultdef.typ=orddef) and
  1751. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1752. begin
  1753. { Give a warning when sizes don't match, because then info will be lost }
  1754. if left.resultdef.size=resultdef.size then
  1755. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1756. else
  1757. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1758. end;
  1759. result := simplify;
  1760. if assigned(result) then
  1761. exit;
  1762. { now call the resultdef helper to do constant folding }
  1763. result:=typecheck_call_helper(convtype);
  1764. end;
  1765. function ttypeconvnode.simplify: tnode;
  1766. var
  1767. hp: tnode;
  1768. begin
  1769. result := nil;
  1770. { Constant folding and other node transitions to
  1771. remove the typeconv node }
  1772. case left.nodetype of
  1773. realconstn :
  1774. begin
  1775. if (convtype = tc_real_2_currency) then
  1776. result := typecheck_real_to_currency
  1777. else if (convtype = tc_real_2_real) then
  1778. result := typecheck_real_to_real
  1779. else
  1780. exit;
  1781. if not(assigned(result)) then
  1782. begin
  1783. result := left;
  1784. left := nil;
  1785. end;
  1786. if (result.nodetype = realconstn) then
  1787. begin
  1788. hp:=result;
  1789. result:=crealconstnode.create(trealconstnode(hp).value_real,resultdef);
  1790. if ([nf_explicit,nf_internal] * flags <> []) then
  1791. include(result.flags, nf_explicit);
  1792. hp.free;
  1793. end;
  1794. end;
  1795. niln :
  1796. begin
  1797. { nil to ordinal node }
  1798. if (resultdef.typ=orddef) then
  1799. begin
  1800. hp:=cordconstnode.create(0,resultdef,true);
  1801. if ([nf_explicit,nf_internal] * flags <> []) then
  1802. include(hp.flags, nf_explicit);
  1803. result:=hp;
  1804. exit;
  1805. end
  1806. else
  1807. { fold nil to any pointer type }
  1808. if (resultdef.typ=pointerdef) then
  1809. begin
  1810. hp:=cnilnode.create;
  1811. hp.resultdef:=resultdef;
  1812. if ([nf_explicit,nf_internal] * flags <> []) then
  1813. include(hp.flags, nf_explicit);
  1814. result:=hp;
  1815. exit;
  1816. end
  1817. else
  1818. { remove typeconv after niln, but not when the result is a
  1819. methodpointer. The typeconv of the methodpointer will then
  1820. take care of updateing size of niln to OS_64 }
  1821. if not((resultdef.typ=procvardef) and
  1822. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1823. begin
  1824. left.resultdef:=resultdef;
  1825. if ([nf_explicit,nf_internal] * flags <> []) then
  1826. include(left.flags, nf_explicit);
  1827. result:=left;
  1828. left:=nil;
  1829. exit;
  1830. end;
  1831. end;
  1832. ordconstn :
  1833. begin
  1834. { ordinal contants can be directly converted }
  1835. { but not char to char because it is a widechar to char or via versa }
  1836. { which needs extra code to do the code page transistion }
  1837. { constant ordinal to pointer }
  1838. if (resultdef.typ=pointerdef) and
  1839. (convtype<>tc_cchar_2_pchar) then
  1840. begin
  1841. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value.uvalue),resultdef);
  1842. if ([nf_explicit,nf_internal] * flags <> []) then
  1843. include(hp.flags, nf_explicit);
  1844. result:=hp;
  1845. exit;
  1846. end
  1847. else if is_ordinal(resultdef) and
  1848. not(convtype=tc_char_2_char) then
  1849. begin
  1850. { replace the resultdef and recheck the range }
  1851. if ([nf_explicit,nf_internal] * flags <> []) then
  1852. include(left.flags, nf_explicit);
  1853. testrange(resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1854. left.resultdef:=resultdef;
  1855. result:=left;
  1856. left:=nil;
  1857. exit;
  1858. end;
  1859. end;
  1860. pointerconstn :
  1861. begin
  1862. { pointerconstn to any pointer is folded too }
  1863. if (resultdef.typ=pointerdef) then
  1864. begin
  1865. left.resultdef:=resultdef;
  1866. if ([nf_explicit,nf_internal] * flags <> []) then
  1867. include(left.flags, nf_explicit);
  1868. result:=left;
  1869. left:=nil;
  1870. exit;
  1871. end
  1872. { constant pointer to ordinal }
  1873. else if is_ordinal(resultdef) then
  1874. begin
  1875. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1876. resultdef,not(nf_explicit in flags));
  1877. if ([nf_explicit,nf_internal] * flags <> []) then
  1878. include(hp.flags, nf_explicit);
  1879. result:=hp;
  1880. exit;
  1881. end;
  1882. end;
  1883. end;
  1884. end;
  1885. procedure Ttypeconvnode.mark_write;
  1886. begin
  1887. left.mark_write;
  1888. end;
  1889. function ttypeconvnode.first_cord_to_pointer : tnode;
  1890. begin
  1891. result:=nil;
  1892. internalerror(200104043);
  1893. end;
  1894. function ttypeconvnode.first_int_to_int : tnode;
  1895. begin
  1896. first_int_to_int:=nil;
  1897. expectloc:=left.expectloc;
  1898. if not is_void(left.resultdef) then
  1899. begin
  1900. if (left.expectloc<>LOC_REGISTER) and
  1901. (resultdef.size>left.resultdef.size) then
  1902. expectloc:=LOC_REGISTER
  1903. else
  1904. if (left.expectloc=LOC_CREGISTER) and
  1905. (resultdef.size<left.resultdef.size) then
  1906. expectloc:=LOC_REGISTER;
  1907. end;
  1908. end;
  1909. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1910. begin
  1911. result:=nil;
  1912. expectloc:=LOC_REGISTER;
  1913. end;
  1914. function ttypeconvnode.first_cstring_to_int : tnode;
  1915. begin
  1916. result:=nil;
  1917. internalerror(200510014);
  1918. end;
  1919. function ttypeconvnode.first_string_to_chararray : tnode;
  1920. begin
  1921. first_string_to_chararray:=nil;
  1922. expectloc:=left.expectloc;
  1923. end;
  1924. function ttypeconvnode.first_char_to_string : tnode;
  1925. begin
  1926. first_char_to_string:=nil;
  1927. expectloc:=LOC_REFERENCE;
  1928. end;
  1929. function ttypeconvnode.first_nothing : tnode;
  1930. begin
  1931. first_nothing:=nil;
  1932. end;
  1933. function ttypeconvnode.first_array_to_pointer : tnode;
  1934. begin
  1935. first_array_to_pointer:=nil;
  1936. expectloc:=LOC_REGISTER;
  1937. end;
  1938. function ttypeconvnode.first_int_to_real: tnode;
  1939. var
  1940. fname: string[32];
  1941. begin
  1942. if target_info.system in system_wince then
  1943. begin
  1944. { converting a 64bit integer to a float requires a helper }
  1945. if is_64bitint(left.resultdef) or
  1946. is_currency(left.resultdef) then
  1947. begin
  1948. { hack to avoid double division by 10000, as it's
  1949. already done by typecheckpass.resultdef_int_to_real }
  1950. if is_currency(left.resultdef) then
  1951. left.resultdef := s64inttype;
  1952. if is_signed(left.resultdef) then
  1953. fname:='I64TO'
  1954. else
  1955. fname:='UI64TO';
  1956. end
  1957. else
  1958. { other integers are supposed to be 32 bit }
  1959. begin
  1960. if is_signed(left.resultdef) then
  1961. fname:='ITO'
  1962. else
  1963. fname:='UTO';
  1964. firstpass(left);
  1965. end;
  1966. if tfloatdef(resultdef).floattype=s64real then
  1967. fname:=fname+'D'
  1968. else
  1969. fname:=fname+'S';
  1970. result:=ccallnode.createintern(fname,ccallparanode.create(
  1971. left,nil));
  1972. left:=nil;
  1973. firstpass(result);
  1974. exit;
  1975. end
  1976. else
  1977. begin
  1978. { converting a 64bit integer to a float requires a helper }
  1979. if is_64bitint(left.resultdef) or
  1980. is_currency(left.resultdef) then
  1981. begin
  1982. { hack to avoid double division by 10000, as it's
  1983. already done by typecheckpass.resultdef_int_to_real }
  1984. if is_currency(left.resultdef) then
  1985. left.resultdef := s64inttype;
  1986. if is_signed(left.resultdef) then
  1987. fname:='int64_to_'
  1988. else
  1989. { we can't do better currently }
  1990. fname:='int64_to_';
  1991. end
  1992. else
  1993. { other integers are supposed to be 32 bit }
  1994. begin
  1995. if is_signed(left.resultdef) then
  1996. fname:='int32_to_'
  1997. else
  1998. { we can't do better currently }
  1999. fname:='int32_to_';
  2000. firstpass(left);
  2001. end;
  2002. if tfloatdef(resultdef).floattype=s64real then
  2003. fname:=fname+'float64'
  2004. else
  2005. fname:=fname+'float32';
  2006. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  2007. left,nil)),resultdef);
  2008. left:=nil;
  2009. firstpass(result);
  2010. exit;
  2011. end;
  2012. end;
  2013. function ttypeconvnode.first_real_to_real : tnode;
  2014. begin
  2015. {$ifdef cpufpemu}
  2016. if cs_fp_emulation in current_settings.moduleswitches then
  2017. begin
  2018. if target_info.system in system_wince then
  2019. begin
  2020. case tfloatdef(left.resultdef).floattype of
  2021. s32real:
  2022. case tfloatdef(resultdef).floattype of
  2023. s64real:
  2024. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  2025. s32real:
  2026. begin
  2027. result:=left;
  2028. left:=nil;
  2029. end;
  2030. else
  2031. internalerror(2005082704);
  2032. end;
  2033. s64real:
  2034. case tfloatdef(resultdef).floattype of
  2035. s32real:
  2036. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  2037. s64real:
  2038. begin
  2039. result:=left;
  2040. left:=nil;
  2041. end;
  2042. else
  2043. internalerror(2005082703);
  2044. end;
  2045. else
  2046. internalerror(2005082702);
  2047. end;
  2048. left:=nil;
  2049. firstpass(result);
  2050. exit;
  2051. end
  2052. else
  2053. begin
  2054. case tfloatdef(left.resultdef).floattype of
  2055. s32real:
  2056. case tfloatdef(resultdef).floattype of
  2057. s64real:
  2058. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  2059. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  2060. s32real:
  2061. begin
  2062. result:=left;
  2063. left:=nil;
  2064. end;
  2065. else
  2066. internalerror(200610151);
  2067. end;
  2068. s64real:
  2069. case tfloatdef(resultdef).floattype of
  2070. s32real:
  2071. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  2072. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  2073. s64real:
  2074. begin
  2075. result:=left;
  2076. left:=nil;
  2077. end;
  2078. else
  2079. internalerror(200610152);
  2080. end;
  2081. else
  2082. internalerror(200610153);
  2083. end;
  2084. left:=nil;
  2085. firstpass(result);
  2086. exit;
  2087. end;
  2088. end
  2089. else
  2090. {$endif cpufpemu}
  2091. begin
  2092. first_real_to_real:=nil;
  2093. expectloc:=LOC_FPUREGISTER;
  2094. end;
  2095. end;
  2096. function ttypeconvnode.first_pointer_to_array : tnode;
  2097. begin
  2098. first_pointer_to_array:=nil;
  2099. expectloc:=LOC_REFERENCE;
  2100. end;
  2101. function ttypeconvnode.first_cchar_to_pchar : tnode;
  2102. begin
  2103. first_cchar_to_pchar:=nil;
  2104. internalerror(200104021);
  2105. end;
  2106. function ttypeconvnode.first_bool_to_int : tnode;
  2107. begin
  2108. first_bool_to_int:=nil;
  2109. { byte(boolean) or word(wordbool) or longint(longbool) must
  2110. be accepted for var parameters }
  2111. if (nf_explicit in flags) and
  2112. (left.resultdef.size=resultdef.size) and
  2113. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2114. exit;
  2115. { when converting to 64bit, first convert to a 32bit int and then }
  2116. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  2117. if resultdef.size > sizeof(aint) then
  2118. begin
  2119. result := ctypeconvnode.create_internal(left,u32inttype);
  2120. result := ctypeconvnode.create(result,resultdef);
  2121. left := nil;
  2122. firstpass(result);
  2123. exit;
  2124. end;
  2125. expectloc:=LOC_REGISTER;
  2126. end;
  2127. function ttypeconvnode.first_int_to_bool : tnode;
  2128. begin
  2129. first_int_to_bool:=nil;
  2130. { byte(boolean) or word(wordbool) or longint(longbool) must
  2131. be accepted for var parameters }
  2132. if (nf_explicit in flags) and
  2133. (left.resultdef.size=resultdef.size) and
  2134. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  2135. exit;
  2136. expectloc:=LOC_REGISTER;
  2137. end;
  2138. function ttypeconvnode.first_bool_to_bool : tnode;
  2139. begin
  2140. first_bool_to_bool:=nil;
  2141. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  2142. expectloc := left.expectloc
  2143. else
  2144. expectloc:=LOC_REGISTER;
  2145. end;
  2146. function ttypeconvnode.first_char_to_char : tnode;
  2147. var
  2148. fname: string[18];
  2149. begin
  2150. if (torddef(resultdef).ordtype=uchar) and
  2151. (torddef(left.resultdef).ordtype=uwidechar) then
  2152. fname := 'fpc_wchar_to_char'
  2153. else if (torddef(resultdef).ordtype=uwidechar) and
  2154. (torddef(left.resultdef).ordtype=uchar) then
  2155. fname := 'fpc_char_to_wchar'
  2156. else
  2157. internalerror(2007081201);
  2158. result := ccallnode.createintern(fname,ccallparanode.create(left,nil));
  2159. left:=nil;
  2160. firstpass(result);
  2161. end;
  2162. function ttypeconvnode.first_proc_to_procvar : tnode;
  2163. begin
  2164. first_proc_to_procvar:=nil;
  2165. { if we take the address of a nested function, it'll }
  2166. { probably be used in a foreach() construct and then }
  2167. { the parent needs a stackframe }
  2168. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  2169. include(current_procinfo.flags,pi_needs_stackframe);
  2170. if tabstractprocdef(resultdef).is_addressonly then
  2171. expectloc:=LOC_REGISTER
  2172. else
  2173. begin
  2174. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2175. CGMessage(parser_e_illegal_expression);
  2176. expectloc:=left.expectloc;
  2177. end;
  2178. end;
  2179. function ttypeconvnode.first_nil_to_methodprocvar : tnode;
  2180. begin
  2181. first_nil_to_methodprocvar:=nil;
  2182. expectloc:=LOC_REFERENCE;
  2183. end;
  2184. function ttypeconvnode.first_set_to_set : tnode;
  2185. var
  2186. newstatement : tstatementnode;
  2187. temp : ttempcreatenode;
  2188. begin
  2189. { in theory, we should do range checking here,
  2190. but Delphi doesn't do it either (FK) }
  2191. if left.nodetype=setconstn then
  2192. begin
  2193. left.resultdef:=resultdef;
  2194. result:=left;
  2195. end
  2196. { equal sets for the code generator? }
  2197. else if (left.resultdef.size=resultdef.size) and
  2198. (tsetdef(left.resultdef).setbase=tsetdef(resultdef).setbase) then
  2199. {$warning This causes wrong (but Delphi-compatible) results for disjoint subsets}
  2200. { e.g., this prints true because of this:
  2201. var
  2202. sa: set of 1..2;
  2203. sb: set of 5..6;
  2204. b: byte;
  2205. begin
  2206. b:=1;
  2207. sa:=[1..2];
  2208. sb:=sa;
  2209. writeln(b in sb);
  2210. end.
  2211. }
  2212. result:=left
  2213. else
  2214. // if is_varset(resultdef) then
  2215. begin
  2216. result:=internalstatements(newstatement);
  2217. { in case left is a smallset expression, it can be an addn or so. }
  2218. { fpc_varset_load expects a formal const parameter, which doesn't }
  2219. { accept set addn's -> assign to a temp first and pass the temp }
  2220. if not(left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2221. begin
  2222. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,false);
  2223. addstatement(newstatement,temp);
  2224. { temp := left }
  2225. addstatement(newstatement,cassignmentnode.create(
  2226. ctemprefnode.create(temp),left));
  2227. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2228. addstatement(newstatement,ctemprefnode.create(temp));
  2229. left:=result;
  2230. firstpass(left);
  2231. { recreate the result's internalstatements list }
  2232. result:=internalstatements(newstatement);
  2233. end;
  2234. { create temp for result }
  2235. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2236. addstatement(newstatement,temp);
  2237. addstatement(newstatement,ccallnode.createintern('fpc_varset_load',
  2238. ccallparanode.create(cordconstnode.create(tsetdef(left.resultdef).setbase div 8 - tsetdef(resultdef).setbase div 8,sinttype,false),
  2239. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2240. ccallparanode.create(ctemprefnode.create(temp),
  2241. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2242. ccallparanode.create(left,nil))))))
  2243. );
  2244. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2245. addstatement(newstatement,ctemprefnode.create(temp));
  2246. end;
  2247. {
  2248. else
  2249. begin
  2250. srsym:=search_system_type('FPC_SMALL_SET');
  2251. result :=
  2252. ccallnode.createinternres('fpc_set_load_small',
  2253. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.typedef),nil),resultdef);
  2254. end;
  2255. }
  2256. { reused }
  2257. left:=nil;
  2258. end;
  2259. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2260. begin
  2261. first_ansistring_to_pchar:=nil;
  2262. expectloc:=LOC_REGISTER;
  2263. end;
  2264. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2265. begin
  2266. first_arrayconstructor_to_set:=nil;
  2267. internalerror(200104022);
  2268. end;
  2269. function ttypeconvnode.first_class_to_intf : tnode;
  2270. begin
  2271. first_class_to_intf:=nil;
  2272. expectloc:=LOC_REGISTER;
  2273. end;
  2274. function ttypeconvnode._first_int_to_int : tnode;
  2275. begin
  2276. result:=first_int_to_int;
  2277. end;
  2278. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2279. begin
  2280. result:=first_cstring_to_pchar;
  2281. end;
  2282. function ttypeconvnode._first_cstring_to_int : tnode;
  2283. begin
  2284. result:=first_cstring_to_int;
  2285. end;
  2286. function ttypeconvnode._first_string_to_chararray : tnode;
  2287. begin
  2288. result:=first_string_to_chararray;
  2289. end;
  2290. function ttypeconvnode._first_char_to_string : tnode;
  2291. begin
  2292. result:=first_char_to_string;
  2293. end;
  2294. function ttypeconvnode._first_nothing : tnode;
  2295. begin
  2296. result:=first_nothing;
  2297. end;
  2298. function ttypeconvnode._first_array_to_pointer : tnode;
  2299. begin
  2300. result:=first_array_to_pointer;
  2301. end;
  2302. function ttypeconvnode._first_int_to_real : tnode;
  2303. begin
  2304. result:=first_int_to_real;
  2305. end;
  2306. function ttypeconvnode._first_real_to_real : tnode;
  2307. begin
  2308. result:=first_real_to_real;
  2309. end;
  2310. function ttypeconvnode._first_pointer_to_array : tnode;
  2311. begin
  2312. result:=first_pointer_to_array;
  2313. end;
  2314. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2315. begin
  2316. result:=first_cchar_to_pchar;
  2317. end;
  2318. function ttypeconvnode._first_bool_to_int : tnode;
  2319. begin
  2320. result:=first_bool_to_int;
  2321. end;
  2322. function ttypeconvnode._first_int_to_bool : tnode;
  2323. begin
  2324. result:=first_int_to_bool;
  2325. end;
  2326. function ttypeconvnode._first_bool_to_bool : tnode;
  2327. begin
  2328. result:=first_bool_to_bool;
  2329. end;
  2330. function ttypeconvnode._first_proc_to_procvar : tnode;
  2331. begin
  2332. result:=first_proc_to_procvar;
  2333. end;
  2334. function ttypeconvnode._first_nil_to_methodprocvar : tnode;
  2335. begin
  2336. result:=first_nil_to_methodprocvar;
  2337. end;
  2338. function ttypeconvnode._first_set_to_set : tnode;
  2339. begin
  2340. result:=first_set_to_set;
  2341. end;
  2342. function ttypeconvnode._first_cord_to_pointer : tnode;
  2343. begin
  2344. result:=first_cord_to_pointer;
  2345. end;
  2346. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2347. begin
  2348. result:=first_ansistring_to_pchar;
  2349. end;
  2350. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2351. begin
  2352. result:=first_arrayconstructor_to_set;
  2353. end;
  2354. function ttypeconvnode._first_class_to_intf : tnode;
  2355. begin
  2356. result:=first_class_to_intf;
  2357. end;
  2358. function ttypeconvnode._first_char_to_char : tnode;
  2359. begin
  2360. result:=first_char_to_char;
  2361. end;
  2362. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2363. const
  2364. firstconvert : array[tconverttype] of pointer = (
  2365. nil, { none }
  2366. @ttypeconvnode._first_nothing, {equal}
  2367. @ttypeconvnode._first_nothing, {not_possible}
  2368. nil, { removed in typecheck_string_to_string }
  2369. @ttypeconvnode._first_char_to_string,
  2370. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2371. nil, { removed in typecheck_chararray_to_string }
  2372. @ttypeconvnode._first_cchar_to_pchar,
  2373. @ttypeconvnode._first_cstring_to_pchar,
  2374. @ttypeconvnode._first_cstring_to_int,
  2375. @ttypeconvnode._first_ansistring_to_pchar,
  2376. @ttypeconvnode._first_string_to_chararray,
  2377. nil, { removed in typecheck_chararray_to_string }
  2378. @ttypeconvnode._first_array_to_pointer,
  2379. @ttypeconvnode._first_pointer_to_array,
  2380. @ttypeconvnode._first_int_to_int,
  2381. @ttypeconvnode._first_int_to_bool,
  2382. @ttypeconvnode._first_bool_to_bool,
  2383. @ttypeconvnode._first_bool_to_int,
  2384. @ttypeconvnode._first_real_to_real,
  2385. @ttypeconvnode._first_int_to_real,
  2386. nil, { removed in typecheck_real_to_currency }
  2387. @ttypeconvnode._first_proc_to_procvar,
  2388. @ttypeconvnode._first_nil_to_methodprocvar,
  2389. @ttypeconvnode._first_arrayconstructor_to_set,
  2390. @ttypeconvnode._first_set_to_set,
  2391. @ttypeconvnode._first_cord_to_pointer,
  2392. @ttypeconvnode._first_nothing,
  2393. @ttypeconvnode._first_nothing,
  2394. @ttypeconvnode._first_class_to_intf,
  2395. @ttypeconvnode._first_char_to_char,
  2396. @ttypeconvnode._first_nothing,
  2397. @ttypeconvnode._first_nothing,
  2398. nil,
  2399. nil,
  2400. nil,
  2401. nil,
  2402. nil,
  2403. nil,
  2404. nil
  2405. );
  2406. type
  2407. tprocedureofobject = function : tnode of object;
  2408. var
  2409. r : packed record
  2410. proc : pointer;
  2411. obj : pointer;
  2412. end;
  2413. begin
  2414. { this is a little bit dirty but it works }
  2415. { and should be quite portable too }
  2416. r.proc:=firstconvert[c];
  2417. r.obj:=self;
  2418. if not assigned(r.proc) then
  2419. internalerror(200312081);
  2420. first_call_helper:=tprocedureofobject(r)()
  2421. end;
  2422. function ttypeconvnode.pass_1 : tnode;
  2423. begin
  2424. result:=nil;
  2425. firstpass(left);
  2426. if codegenerror then
  2427. exit;
  2428. expectloc:=left.expectloc;
  2429. result:=first_call_helper(convtype);
  2430. end;
  2431. function ttypeconvnode.assign_allowed:boolean;
  2432. begin
  2433. result:=(convtype=tc_equal) or
  2434. { typecasting from void is always allowed }
  2435. is_void(left.resultdef) or
  2436. (left.resultdef.typ=formaldef) or
  2437. { int 2 int with same size reuses same location, or for
  2438. tp7 mode also allow size < orignal size }
  2439. (
  2440. (convtype=tc_int_2_int) and
  2441. (
  2442. (resultdef.size=left.resultdef.size) or
  2443. ((m_tp7 in current_settings.modeswitches) and
  2444. (resultdef.size<left.resultdef.size))
  2445. )
  2446. ) or
  2447. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2448. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  2449. (nf_explicit in flags) and
  2450. (resultdef.size=left.resultdef.size));
  2451. { When using only a part of the value it can't be in a register since
  2452. that will load the value in a new register first }
  2453. if (resultdef.size<left.resultdef.size) then
  2454. make_not_regable(left,[ra_addr_regable]);
  2455. end;
  2456. function ttypeconvnode.docompare(p: tnode) : boolean;
  2457. begin
  2458. docompare :=
  2459. inherited docompare(p) and
  2460. (convtype = ttypeconvnode(p).convtype);
  2461. end;
  2462. procedure ttypeconvnode._second_int_to_int;
  2463. begin
  2464. second_int_to_int;
  2465. end;
  2466. procedure ttypeconvnode._second_string_to_string;
  2467. begin
  2468. second_string_to_string;
  2469. end;
  2470. procedure ttypeconvnode._second_cstring_to_pchar;
  2471. begin
  2472. second_cstring_to_pchar;
  2473. end;
  2474. procedure ttypeconvnode._second_cstring_to_int;
  2475. begin
  2476. second_cstring_to_int;
  2477. end;
  2478. procedure ttypeconvnode._second_string_to_chararray;
  2479. begin
  2480. second_string_to_chararray;
  2481. end;
  2482. procedure ttypeconvnode._second_array_to_pointer;
  2483. begin
  2484. second_array_to_pointer;
  2485. end;
  2486. procedure ttypeconvnode._second_pointer_to_array;
  2487. begin
  2488. second_pointer_to_array;
  2489. end;
  2490. procedure ttypeconvnode._second_chararray_to_string;
  2491. begin
  2492. second_chararray_to_string;
  2493. end;
  2494. procedure ttypeconvnode._second_char_to_string;
  2495. begin
  2496. second_char_to_string;
  2497. end;
  2498. procedure ttypeconvnode._second_int_to_real;
  2499. begin
  2500. second_int_to_real;
  2501. end;
  2502. procedure ttypeconvnode._second_real_to_real;
  2503. begin
  2504. second_real_to_real;
  2505. end;
  2506. procedure ttypeconvnode._second_cord_to_pointer;
  2507. begin
  2508. second_cord_to_pointer;
  2509. end;
  2510. procedure ttypeconvnode._second_proc_to_procvar;
  2511. begin
  2512. second_proc_to_procvar;
  2513. end;
  2514. procedure ttypeconvnode._second_nil_to_methodprocvar;
  2515. begin
  2516. second_nil_to_methodprocvar;
  2517. end;
  2518. procedure ttypeconvnode._second_bool_to_int;
  2519. begin
  2520. second_bool_to_int;
  2521. end;
  2522. procedure ttypeconvnode._second_int_to_bool;
  2523. begin
  2524. second_int_to_bool;
  2525. end;
  2526. procedure ttypeconvnode._second_bool_to_bool;
  2527. begin
  2528. second_bool_to_bool;
  2529. end;
  2530. procedure ttypeconvnode._second_set_to_set;
  2531. begin
  2532. second_set_to_set;
  2533. end;
  2534. procedure ttypeconvnode._second_ansistring_to_pchar;
  2535. begin
  2536. second_ansistring_to_pchar;
  2537. end;
  2538. procedure ttypeconvnode._second_class_to_intf;
  2539. begin
  2540. second_class_to_intf;
  2541. end;
  2542. procedure ttypeconvnode._second_char_to_char;
  2543. begin
  2544. second_char_to_char;
  2545. end;
  2546. procedure ttypeconvnode._second_nothing;
  2547. begin
  2548. second_nothing;
  2549. end;
  2550. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2551. const
  2552. secondconvert : array[tconverttype] of pointer = (
  2553. @ttypeconvnode._second_nothing, {none}
  2554. @ttypeconvnode._second_nothing, {equal}
  2555. @ttypeconvnode._second_nothing, {not_possible}
  2556. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2557. @ttypeconvnode._second_char_to_string,
  2558. @ttypeconvnode._second_nothing, {char_to_charray}
  2559. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2560. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2561. @ttypeconvnode._second_cstring_to_pchar,
  2562. @ttypeconvnode._second_cstring_to_int,
  2563. @ttypeconvnode._second_ansistring_to_pchar,
  2564. @ttypeconvnode._second_string_to_chararray,
  2565. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2566. @ttypeconvnode._second_array_to_pointer,
  2567. @ttypeconvnode._second_pointer_to_array,
  2568. @ttypeconvnode._second_int_to_int,
  2569. @ttypeconvnode._second_int_to_bool,
  2570. @ttypeconvnode._second_bool_to_bool,
  2571. @ttypeconvnode._second_bool_to_int,
  2572. @ttypeconvnode._second_real_to_real,
  2573. @ttypeconvnode._second_int_to_real,
  2574. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2575. @ttypeconvnode._second_proc_to_procvar,
  2576. @ttypeconvnode._second_nil_to_methodprocvar,
  2577. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2578. @ttypeconvnode._second_nothing, { second_set_to_set, handled in first pass }
  2579. @ttypeconvnode._second_cord_to_pointer,
  2580. @ttypeconvnode._second_nothing, { interface 2 string }
  2581. @ttypeconvnode._second_nothing, { interface 2 guid }
  2582. @ttypeconvnode._second_class_to_intf,
  2583. @ttypeconvnode._second_char_to_char,
  2584. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2585. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2586. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2587. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2588. @ttypeconvnode._second_nothing, { variant_2_enum }
  2589. @ttypeconvnode._second_nothing, { enum_2_variant }
  2590. @ttypeconvnode._second_nothing, { variant_2_interface }
  2591. @ttypeconvnode._second_nothing, { interface_2_variant }
  2592. @ttypeconvnode._second_nothing { array_2_dynarray }
  2593. );
  2594. type
  2595. tprocedureofobject = procedure of object;
  2596. var
  2597. r : packed record
  2598. proc : pointer;
  2599. obj : pointer;
  2600. end;
  2601. begin
  2602. { this is a little bit dirty but it works }
  2603. { and should be quite portable too }
  2604. r.proc:=secondconvert[c];
  2605. r.obj:=self;
  2606. tprocedureofobject(r)();
  2607. end;
  2608. {*****************************************************************************
  2609. TISNODE
  2610. *****************************************************************************}
  2611. constructor tisnode.create(l,r : tnode);
  2612. begin
  2613. inherited create(isn,l,r);
  2614. end;
  2615. function tisnode.pass_typecheck:tnode;
  2616. var
  2617. paras: tcallparanode;
  2618. begin
  2619. result:=nil;
  2620. typecheckpass(left);
  2621. typecheckpass(right);
  2622. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2623. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2624. if codegenerror then
  2625. exit;
  2626. if (right.resultdef.typ=classrefdef) then
  2627. begin
  2628. { left must be a class }
  2629. if is_class(left.resultdef) then
  2630. begin
  2631. { the operands must be related }
  2632. if (not(tobjectdef(left.resultdef).is_related(
  2633. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2634. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2635. tobjectdef(left.resultdef)))) then
  2636. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2637. tclassrefdef(right.resultdef).pointeddef.typename);
  2638. end
  2639. else
  2640. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2641. { call fpc_do_is helper }
  2642. paras := ccallparanode.create(
  2643. left,
  2644. ccallparanode.create(
  2645. right,nil));
  2646. result := ccallnode.createintern('fpc_do_is',paras);
  2647. left := nil;
  2648. right := nil;
  2649. end
  2650. else if is_interface(right.resultdef) then
  2651. begin
  2652. { left is a class }
  2653. if is_class(left.resultdef) then
  2654. begin
  2655. { the class must implement the interface }
  2656. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2657. CGMessage2(type_e_classes_not_related,
  2658. FullTypeName(left.resultdef,right.resultdef),
  2659. FullTypeName(right.resultdef,left.resultdef))
  2660. end
  2661. { left is an interface }
  2662. else if is_interface(left.resultdef) then
  2663. begin
  2664. { the operands must be related }
  2665. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2666. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2667. CGMessage2(type_e_classes_not_related,
  2668. FullTypeName(left.resultdef,right.resultdef),
  2669. FullTypeName(right.resultdef,left.resultdef));
  2670. end
  2671. else
  2672. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2673. { call fpc_do_is helper }
  2674. paras := ccallparanode.create(
  2675. left,
  2676. ccallparanode.create(
  2677. right,nil));
  2678. result := ccallnode.createintern('fpc_do_is',paras);
  2679. left := nil;
  2680. right := nil;
  2681. end
  2682. else
  2683. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2684. resultdef:=booltype;
  2685. end;
  2686. function tisnode.pass_1 : tnode;
  2687. begin
  2688. internalerror(200204254);
  2689. result:=nil;
  2690. end;
  2691. { dummy pass_2, it will never be called, but we need one since }
  2692. { you can't instantiate an abstract class }
  2693. procedure tisnode.pass_generate_code;
  2694. begin
  2695. end;
  2696. {*****************************************************************************
  2697. TASNODE
  2698. *****************************************************************************}
  2699. constructor tasnode.create(l,r : tnode);
  2700. begin
  2701. inherited create(asn,l,r);
  2702. call := nil;
  2703. end;
  2704. destructor tasnode.destroy;
  2705. begin
  2706. call.free;
  2707. inherited destroy;
  2708. end;
  2709. function tasnode.pass_typecheck:tnode;
  2710. var
  2711. hp : tnode;
  2712. begin
  2713. result:=nil;
  2714. typecheckpass(right);
  2715. typecheckpass(left);
  2716. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2717. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2718. if codegenerror then
  2719. exit;
  2720. if (right.resultdef.typ=classrefdef) then
  2721. begin
  2722. { left must be a class }
  2723. if is_class(left.resultdef) then
  2724. begin
  2725. { the operands must be related }
  2726. if (not(tobjectdef(left.resultdef).is_related(
  2727. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2728. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2729. tobjectdef(left.resultdef)))) then
  2730. CGMessage2(type_e_classes_not_related,
  2731. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2732. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2733. end
  2734. else
  2735. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2736. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2737. end
  2738. else if is_interface(right.resultdef) then
  2739. begin
  2740. { left is a class }
  2741. if not(is_class(left.resultdef) or
  2742. is_interfacecom(left.resultdef)) then
  2743. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2744. resultdef:=right.resultdef;
  2745. { load the GUID of the interface }
  2746. if (right.nodetype=typen) then
  2747. begin
  2748. if assigned(tobjectdef(right.resultdef).iidguid) then
  2749. begin
  2750. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  2751. right.free;
  2752. right:=hp;
  2753. end
  2754. else
  2755. internalerror(200206282);
  2756. typecheckpass(right);
  2757. end;
  2758. end
  2759. else
  2760. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2761. end;
  2762. function tasnode.dogetcopy: tnode;
  2763. begin
  2764. result := inherited dogetcopy;
  2765. if assigned(call) then
  2766. tasnode(result).call := call.getcopy
  2767. else
  2768. tasnode(result).call := nil;
  2769. end;
  2770. function tasnode.pass_1 : tnode;
  2771. var
  2772. procname: string;
  2773. begin
  2774. result:=nil;
  2775. if not assigned(call) then
  2776. begin
  2777. if is_class(left.resultdef) and
  2778. (right.resultdef.typ=classrefdef) then
  2779. call := ccallnode.createinternres('fpc_do_as',
  2780. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2781. resultdef)
  2782. else
  2783. begin
  2784. if is_class(left.resultdef) then
  2785. procname := 'fpc_class_as_intf'
  2786. else
  2787. procname := 'fpc_intf_as';
  2788. call := ccallnode.createintern(procname,
  2789. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2790. call := ctypeconvnode.create_internal(call,resultdef);
  2791. end;
  2792. left := nil;
  2793. right := nil;
  2794. firstpass(call);
  2795. if codegenerror then
  2796. exit;
  2797. expectloc:=call.expectloc;
  2798. end;
  2799. end;
  2800. begin
  2801. ctypeconvnode:=ttypeconvnode;
  2802. casnode:=tasnode;
  2803. cisnode:=tisnode;
  2804. end.