ncnv.pas 125 KB

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