ncnv.pas 133 KB

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