ncnv.pas 124 KB

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