ncnv.pas 126 KB

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