pexpr.pas 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Does parsing of expression for Free Pascal
  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 pexpr;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symtype,symdef,symbase,
  22. node,ncal,
  23. tokens,globtype,globals,constexp,
  24. pgentype;
  25. { reads a whole expression }
  26. function expr(dotypecheck:boolean) : tnode;
  27. { reads an expression without assignements and .. }
  28. function comp_expr(accept_equal,typeonly:boolean):tnode;
  29. { reads a single factor }
  30. function factor(getaddr,typeonly:boolean) : tnode;
  31. procedure string_dec(var def: tdef; allowtypedef: boolean);
  32. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  33. { the ID token has to be consumed before calling this function }
  34. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  35. function get_intconst:TConstExprInt;
  36. function get_stringconst:string;
  37. { Does some postprocessing for a generic type (especially when nested types
  38. of the specialization are used) }
  39. procedure post_comp_expr_gendef(var def: tdef);
  40. implementation
  41. uses
  42. { common }
  43. cutils,
  44. { global }
  45. verbose,
  46. systems,widestr,
  47. { symtable }
  48. symconst,symtable,symsym,symcpu,defutil,defcmp,
  49. { module }
  50. fmodule,ppu,
  51. { pass 1 }
  52. pass_1,htypechk,
  53. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  54. { parser }
  55. scanner,
  56. pbase,pinline,ptype,pgenutil,procinfo,cpuinfo
  57. ;
  58. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;forward;
  59. const
  60. { true, if the inherited call is anonymous }
  61. anon_inherited : boolean = false;
  62. { last def found, only used by anon. inherited calls to insert proper type casts }
  63. srdef : tdef = nil;
  64. procedure string_dec(var def:tdef; allowtypedef: boolean);
  65. { reads a string type with optional length }
  66. { and returns a pointer to the string }
  67. { definition }
  68. var
  69. p : tnode;
  70. begin
  71. def:=cshortstringtype;
  72. consume(_STRING);
  73. if token=_LECKKLAMMER then
  74. begin
  75. if not(allowtypedef) then
  76. Message(parser_e_no_local_para_def);
  77. consume(_LECKKLAMMER);
  78. p:=comp_expr(true,false);
  79. if not is_constintnode(p) then
  80. begin
  81. Message(parser_e_illegal_expression);
  82. { error recovery }
  83. consume(_RECKKLAMMER);
  84. end
  85. else
  86. begin
  87. if (tordconstnode(p).value<=0) then
  88. begin
  89. Message(parser_e_invalid_string_size);
  90. tordconstnode(p).value:=255;
  91. end;
  92. if tordconstnode(p).value>255 then
  93. begin
  94. { longstring is currently unsupported (CEC)! }
  95. { t:=cstringdef.createlong(tordconstnode(p).value))}
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. def:=cstringdef.createshort(int64(tordconstnode(p).value));
  99. end
  100. else
  101. if tordconstnode(p).value<>255 then
  102. def:=cstringdef.createshort(int64(tordconstnode(p).value));
  103. consume(_RECKKLAMMER);
  104. end;
  105. p.free;
  106. end
  107. else
  108. begin
  109. if cs_refcountedstrings in current_settings.localswitches then
  110. begin
  111. if m_default_unicodestring in current_settings.modeswitches then
  112. def:=cunicodestringtype
  113. else
  114. def:=getansistringdef
  115. end
  116. else
  117. def:=cshortstringtype;
  118. end;
  119. end;
  120. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  121. var
  122. p1,p2,argname : tnode;
  123. prev_in_args,
  124. old_named_args_allowed,
  125. old_allow_array_constructor : boolean;
  126. begin
  127. if token=end_of_paras then
  128. begin
  129. parse_paras:=nil;
  130. exit;
  131. end;
  132. { save old values }
  133. prev_in_args:=in_args;
  134. old_allow_array_constructor:=allow_array_constructor;
  135. old_named_args_allowed:=named_args_allowed;
  136. { set para parsing values }
  137. in_args:=true;
  138. named_args_allowed:=false;
  139. allow_array_constructor:=true;
  140. p2:=nil;
  141. repeat
  142. if __namedpara then
  143. begin
  144. if token=_COMMA then
  145. begin
  146. { empty parameter }
  147. p2:=ccallparanode.create(cnothingnode.create,p2);
  148. end
  149. else
  150. begin
  151. named_args_allowed:=true;
  152. p1:=comp_expr(true,false);
  153. named_args_allowed:=false;
  154. if found_arg_name then
  155. begin
  156. argname:=p1;
  157. p1:=comp_expr(true,false);
  158. p2:=ccallparanode.create(p1,p2);
  159. tcallparanode(p2).parametername:=argname;
  160. end
  161. else
  162. p2:=ccallparanode.create(p1,p2);
  163. found_arg_name:=false;
  164. end;
  165. end
  166. else
  167. begin
  168. p1:=comp_expr(true,false);
  169. p2:=ccallparanode.create(p1,p2);
  170. end;
  171. { it's for the str(l:5,s); }
  172. if __colon and (token=_COLON) then
  173. begin
  174. consume(_COLON);
  175. p1:=comp_expr(true,false);
  176. p2:=ccallparanode.create(p1,p2);
  177. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  178. if try_to_consume(_COLON) then
  179. begin
  180. p1:=comp_expr(true,false);
  181. p2:=ccallparanode.create(p1,p2);
  182. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  183. end
  184. end;
  185. until not try_to_consume(_COMMA);
  186. allow_array_constructor:=old_allow_array_constructor;
  187. in_args:=prev_in_args;
  188. named_args_allowed:=old_named_args_allowed;
  189. parse_paras:=p2;
  190. end;
  191. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  192. var
  193. hp : tnode;
  194. hdef : tdef;
  195. temp : ttempcreatenode;
  196. newstatement : tstatementnode;
  197. begin
  198. { Properties are not allowed, because the write can
  199. be different from the read }
  200. if (nf_isproperty in p1.flags) then
  201. begin
  202. Message(type_e_variable_id_expected);
  203. { We can continue with the loading,
  204. it'll not create errors. Only the expected
  205. result can be wrong }
  206. end;
  207. hp:=p1;
  208. while assigned(hp) and
  209. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  210. hp:=tunarynode(hp).left;
  211. if not assigned(hp) then
  212. internalerror(200410121);
  213. if (hp.nodetype=calln) then
  214. begin
  215. typecheckpass(p1);
  216. result:=internalstatements(newstatement);
  217. hdef:=cpointerdef.getreusable(p1.resultdef);
  218. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  219. addstatement(newstatement,temp);
  220. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  221. addstatement(newstatement,cassignmentnode.create(
  222. cderefnode.create(ctemprefnode.create(temp)),
  223. caddnode.create(ntyp,
  224. cderefnode.create(ctemprefnode.create(temp)),
  225. p2)));
  226. addstatement(newstatement,ctempdeletenode.create(temp));
  227. end
  228. else
  229. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  230. end;
  231. function statement_syssym(l : byte) : tnode;
  232. var
  233. p1,p2,paras : tnode;
  234. err,
  235. prev_in_args : boolean;
  236. def : tdef;
  237. exit_procinfo: tprocinfo;
  238. begin
  239. prev_in_args:=in_args;
  240. case l of
  241. in_new_x :
  242. begin
  243. if afterassignment or in_args then
  244. statement_syssym:=new_function
  245. else
  246. statement_syssym:=new_dispose_statement(true);
  247. end;
  248. in_dispose_x :
  249. begin
  250. statement_syssym:=new_dispose_statement(false);
  251. end;
  252. in_ord_x :
  253. begin
  254. consume(_LKLAMMER);
  255. in_args:=true;
  256. p1:=comp_expr(true,false);
  257. consume(_RKLAMMER);
  258. p1:=geninlinenode(in_ord_x,false,p1);
  259. statement_syssym := p1;
  260. end;
  261. in_exit :
  262. begin
  263. statement_syssym:=nil;
  264. if try_to_consume(_LKLAMMER) then
  265. begin
  266. if not (m_mac in current_settings.modeswitches) then
  267. begin
  268. if not(try_to_consume(_RKLAMMER)) then
  269. begin
  270. p1:=comp_expr(true,false);
  271. consume(_RKLAMMER);
  272. if not assigned(current_procinfo) or
  273. (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
  274. is_void(current_procinfo.procdef.returndef) then
  275. begin
  276. Message(parser_e_void_function);
  277. { recovery }
  278. p1.free;
  279. p1:=nil;
  280. end;
  281. end
  282. else
  283. p1:=nil;
  284. end
  285. else
  286. begin
  287. { non local exit ? }
  288. if current_procinfo.procdef.procsym.name<>pattern then
  289. begin
  290. exit_procinfo:=current_procinfo.parent;
  291. while assigned(exit_procinfo) do
  292. begin
  293. if exit_procinfo.procdef.procsym.name=pattern then
  294. break;
  295. exit_procinfo:=exit_procinfo.parent;
  296. end;
  297. if assigned(exit_procinfo) then
  298. begin
  299. if not(assigned(exit_procinfo.nestedexitlabel)) then
  300. begin
  301. include(current_procinfo.flags,pi_has_nested_exit);
  302. exclude(current_procinfo.procdef.procoptions,po_inline);
  303. exit_procinfo.nestedexitlabel:=clabelsym.create('$nestedexit');
  304. { the compiler is responsible to define this label }
  305. exit_procinfo.nestedexitlabel.defined:=true;
  306. exit_procinfo.nestedexitlabel.used:=true;
  307. exit_procinfo.nestedexitlabel.jumpbuf:=clocalvarsym.create('LABEL$_'+exit_procinfo.nestedexitlabel.name,vs_value,rec_jmp_buf,[]);
  308. exit_procinfo.procdef.localst.insert(exit_procinfo.nestedexitlabel);
  309. exit_procinfo.procdef.localst.insert(exit_procinfo.nestedexitlabel.jumpbuf);
  310. end;
  311. statement_syssym:=cgotonode.create(exit_procinfo.nestedexitlabel);
  312. tgotonode(statement_syssym).labelsym:=exit_procinfo.nestedexitlabel;
  313. end
  314. else
  315. Message(parser_e_macpas_exit_wrong_param);
  316. end;
  317. consume(_ID);
  318. consume(_RKLAMMER);
  319. p1:=nil;
  320. end
  321. end
  322. else
  323. p1:=nil;
  324. if not assigned(statement_syssym) then
  325. statement_syssym:=cexitnode.create(p1);
  326. end;
  327. in_break :
  328. begin
  329. statement_syssym:=cbreaknode.create
  330. end;
  331. in_continue :
  332. begin
  333. statement_syssym:=ccontinuenode.create
  334. end;
  335. in_leave :
  336. begin
  337. if m_mac in current_settings.modeswitches then
  338. statement_syssym:=cbreaknode.create
  339. else
  340. begin
  341. Message1(sym_e_id_not_found, orgpattern);
  342. statement_syssym:=cerrornode.create;
  343. end;
  344. end;
  345. in_cycle :
  346. begin
  347. if m_mac in current_settings.modeswitches then
  348. statement_syssym:=ccontinuenode.create
  349. else
  350. begin
  351. Message1(sym_e_id_not_found, orgpattern);
  352. statement_syssym:=cerrornode.create;
  353. end;
  354. end;
  355. in_typeof_x :
  356. begin
  357. consume(_LKLAMMER);
  358. in_args:=true;
  359. p1:=comp_expr(true,false);
  360. consume(_RKLAMMER);
  361. if p1.nodetype=typen then
  362. ttypenode(p1).allowed:=true;
  363. { Allow classrefdef, which is required for
  364. Typeof(self) in static class methods }
  365. if not(is_objc_class_or_protocol(p1.resultdef)) and
  366. not(is_java_class_or_interface(p1.resultdef)) and
  367. ((p1.resultdef.typ = objectdef) or
  368. (assigned(current_procinfo) and
  369. ((po_classmethod in current_procinfo.procdef.procoptions) or
  370. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  371. (p1.resultdef.typ=classrefdef))) then
  372. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  373. else
  374. begin
  375. Message(parser_e_class_id_expected);
  376. p1.destroy;
  377. statement_syssym:=cerrornode.create;
  378. end;
  379. end;
  380. in_sizeof_x,
  381. in_bitsizeof_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. p1:=comp_expr(true,false);
  386. consume(_RKLAMMER);
  387. if ((p1.nodetype<>typen) and
  388. (
  389. (is_object(p1.resultdef) and
  390. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  391. is_open_array(p1.resultdef) or
  392. is_array_of_const(p1.resultdef) or
  393. is_open_string(p1.resultdef)
  394. )) or
  395. { keep the function call if it is a type parameter to avoid arithmetic errors due to constant folding }
  396. (p1.resultdef.typ=undefineddef) then
  397. begin
  398. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  399. { no packed bit support for these things }
  400. if l=in_bitsizeof_x then
  401. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  402. end
  403. else
  404. begin
  405. { allow helpers for SizeOf and BitSizeOf }
  406. if p1.nodetype=typen then
  407. ttypenode(p1).helperallowed:=true;
  408. if (p1.resultdef.typ=forwarddef) then
  409. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  410. if (l = in_sizeof_x) or
  411. (not((p1.nodetype = vecn) and
  412. is_packed_array(tvecnode(p1).left.resultdef)) and
  413. not((p1.nodetype = subscriptn) and
  414. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  415. begin
  416. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  417. if (l = in_bitsizeof_x) then
  418. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  419. end
  420. else
  421. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  422. { p1 not needed !}
  423. p1.destroy;
  424. end;
  425. end;
  426. in_typeinfo_x,
  427. in_objc_encode_x :
  428. begin
  429. if (l=in_typeinfo_x) or
  430. (m_objectivec1 in current_settings.modeswitches) then
  431. begin
  432. consume(_LKLAMMER);
  433. in_args:=true;
  434. p1:=comp_expr(true,false);
  435. { When reading a class type it is parsed as loadvmtaddrn,
  436. typeinfo only needs the type so we remove the loadvmtaddrn }
  437. if p1.nodetype=loadvmtaddrn then
  438. begin
  439. p2:=tloadvmtaddrnode(p1).left;
  440. tloadvmtaddrnode(p1).left:=nil;
  441. p1.free;
  442. p1:=p2;
  443. end;
  444. if p1.nodetype=typen then
  445. begin
  446. ttypenode(p1).allowed:=true;
  447. { allow helpers for TypeInfo }
  448. if l=in_typeinfo_x then
  449. ttypenode(p1).helperallowed:=true;
  450. end;
  451. { else
  452. begin
  453. p1.destroy;
  454. p1:=cerrornode.create;
  455. Message(parser_e_illegal_parameter_list);
  456. end;}
  457. consume(_RKLAMMER);
  458. p2:=geninlinenode(l,false,p1);
  459. statement_syssym:=p2;
  460. end
  461. else
  462. begin
  463. Message1(sym_e_id_not_found, orgpattern);
  464. statement_syssym:=cerrornode.create;
  465. end;
  466. end;
  467. in_aligned_x,
  468. in_unaligned_x :
  469. begin
  470. err:=false;
  471. consume(_LKLAMMER);
  472. in_args:=true;
  473. p1:=comp_expr(true,false);
  474. p2:=ccallparanode.create(p1,nil);
  475. p2:=geninlinenode(l,false,p2);
  476. consume(_RKLAMMER);
  477. statement_syssym:=p2;
  478. end;
  479. in_assigned_x :
  480. begin
  481. err:=false;
  482. consume(_LKLAMMER);
  483. in_args:=true;
  484. p1:=comp_expr(true,false);
  485. { When reading a class type it is parsed as loadvmtaddrn,
  486. typeinfo only needs the type so we remove the loadvmtaddrn }
  487. if p1.nodetype=loadvmtaddrn then
  488. begin
  489. p2:=tloadvmtaddrnode(p1).left;
  490. tloadvmtaddrnode(p1).left:=nil;
  491. p1.free;
  492. p1:=p2;
  493. end;
  494. if not codegenerror then
  495. begin
  496. case p1.resultdef.typ of
  497. procdef, { procvar }
  498. pointerdef,
  499. procvardef,
  500. classrefdef : ;
  501. objectdef :
  502. if not is_implicit_pointer_object_type(p1.resultdef) then
  503. begin
  504. Message(parser_e_illegal_parameter_list);
  505. err:=true;
  506. end;
  507. arraydef :
  508. if not is_dynamic_array(p1.resultdef) then
  509. begin
  510. Message(parser_e_illegal_parameter_list);
  511. err:=true;
  512. end;
  513. else
  514. if p1.resultdef.typ<>undefineddef then
  515. begin
  516. Message(parser_e_illegal_parameter_list);
  517. err:=true;
  518. end;
  519. end;
  520. end
  521. else
  522. err:=true;
  523. if not err then
  524. begin
  525. p2:=ccallparanode.create(p1,nil);
  526. p2:=geninlinenode(in_assigned_x,false,p2);
  527. end
  528. else
  529. begin
  530. p1.free;
  531. p2:=cerrornode.create;
  532. end;
  533. consume(_RKLAMMER);
  534. statement_syssym:=p2;
  535. end;
  536. in_addr_x :
  537. begin
  538. consume(_LKLAMMER);
  539. in_args:=true;
  540. p1:=comp_expr(true,false);
  541. p1:=caddrnode.create(p1);
  542. consume(_RKLAMMER);
  543. statement_syssym:=p1;
  544. end;
  545. in_ofs_x :
  546. begin
  547. if target_info.system in systems_managed_vm then
  548. message(parser_e_feature_unsupported_for_vm);
  549. consume(_LKLAMMER);
  550. in_args:=true;
  551. p1:=comp_expr(true,false);
  552. p1:=caddrnode.create(p1);
  553. do_typecheckpass(p1);
  554. { Ofs() returns a cardinal/qword, not a pointer }
  555. p1.resultdef:=uinttype;
  556. consume(_RKLAMMER);
  557. statement_syssym:=p1;
  558. end;
  559. in_seg_x :
  560. begin
  561. consume(_LKLAMMER);
  562. in_args:=true;
  563. p1:=comp_expr(true,false);
  564. p1:=geninlinenode(in_seg_x,false,p1);
  565. consume(_RKLAMMER);
  566. statement_syssym:=p1;
  567. end;
  568. in_high_x,
  569. in_low_x :
  570. begin
  571. consume(_LKLAMMER);
  572. in_args:=true;
  573. p1:=comp_expr(true,false);
  574. p2:=geninlinenode(l,false,p1);
  575. consume(_RKLAMMER);
  576. statement_syssym:=p2;
  577. end;
  578. in_succ_x,
  579. in_pred_x :
  580. begin
  581. consume(_LKLAMMER);
  582. in_args:=true;
  583. p1:=comp_expr(true,false);
  584. p2:=geninlinenode(l,false,p1);
  585. consume(_RKLAMMER);
  586. statement_syssym:=p2;
  587. end;
  588. in_inc_x,
  589. in_dec_x :
  590. begin
  591. consume(_LKLAMMER);
  592. in_args:=true;
  593. p1:=comp_expr(true,false);
  594. if try_to_consume(_COMMA) then
  595. p2:=ccallparanode.create(comp_expr(true,false),nil)
  596. else
  597. p2:=nil;
  598. p2:=ccallparanode.create(p1,p2);
  599. statement_syssym:=geninlinenode(l,false,p2);
  600. consume(_RKLAMMER);
  601. end;
  602. in_slice_x:
  603. begin
  604. if not(in_args) then
  605. begin
  606. message(parser_e_illegal_slice);
  607. consume(_LKLAMMER);
  608. in_args:=true;
  609. comp_expr(true,false).free;
  610. if try_to_consume(_COMMA) then
  611. comp_expr(true,false).free;
  612. statement_syssym:=cerrornode.create;
  613. consume(_RKLAMMER);
  614. end
  615. else
  616. begin
  617. consume(_LKLAMMER);
  618. in_args:=true;
  619. p1:=comp_expr(true,false);
  620. Consume(_COMMA);
  621. if not(codegenerror) then
  622. p2:=ccallparanode.create(comp_expr(true,false),nil)
  623. else
  624. p2:=cerrornode.create;
  625. p2:=ccallparanode.create(p1,p2);
  626. statement_syssym:=geninlinenode(l,false,p2);
  627. consume(_RKLAMMER);
  628. end;
  629. end;
  630. in_initialize_x:
  631. begin
  632. statement_syssym:=inline_initialize;
  633. end;
  634. in_finalize_x:
  635. begin
  636. statement_syssym:=inline_finalize;
  637. end;
  638. in_copy_x:
  639. begin
  640. statement_syssym:=inline_copy;
  641. end;
  642. in_concat_x :
  643. begin
  644. consume(_LKLAMMER);
  645. in_args:=true;
  646. { Translate to x:=x+y[+z]. The addnode will do the
  647. type checking }
  648. p2:=nil;
  649. repeat
  650. p1:=comp_expr(true,false);
  651. if p2<>nil then
  652. p2:=caddnode.create(addn,p2,p1)
  653. else
  654. begin
  655. { Force string type if it isn't yet }
  656. if not(
  657. (p1.resultdef.typ=stringdef) or
  658. is_chararray(p1.resultdef) or
  659. is_char(p1.resultdef)
  660. ) then
  661. inserttypeconv(p1,cshortstringtype);
  662. p2:=p1;
  663. end;
  664. until not try_to_consume(_COMMA);
  665. consume(_RKLAMMER);
  666. statement_syssym:=p2;
  667. end;
  668. in_read_x,
  669. in_readln_x,
  670. in_readstr_x:
  671. begin
  672. if try_to_consume(_LKLAMMER) then
  673. begin
  674. paras:=parse_paras(false,false,_RKLAMMER);
  675. consume(_RKLAMMER);
  676. end
  677. else
  678. paras:=nil;
  679. p1:=geninlinenode(l,false,paras);
  680. statement_syssym := p1;
  681. end;
  682. in_setlength_x:
  683. begin
  684. statement_syssym := inline_setlength;
  685. end;
  686. in_objc_selector_x:
  687. begin
  688. if (m_objectivec1 in current_settings.modeswitches) then
  689. begin
  690. consume(_LKLAMMER);
  691. in_args:=true;
  692. { don't turn procsyms into calls (getaddr = true) }
  693. p1:=factor(true,false);
  694. p2:=geninlinenode(l,false,p1);
  695. consume(_RKLAMMER);
  696. statement_syssym:=p2;
  697. end
  698. else
  699. begin
  700. Message1(sym_e_id_not_found, orgpattern);
  701. statement_syssym:=cerrornode.create;
  702. end;
  703. end;
  704. in_length_x:
  705. begin
  706. consume(_LKLAMMER);
  707. in_args:=true;
  708. p1:=comp_expr(true,false);
  709. p2:=geninlinenode(l,false,p1);
  710. consume(_RKLAMMER);
  711. statement_syssym:=p2;
  712. end;
  713. in_write_x,
  714. in_writeln_x,
  715. in_writestr_x :
  716. begin
  717. if try_to_consume(_LKLAMMER) then
  718. begin
  719. paras:=parse_paras(true,false,_RKLAMMER);
  720. consume(_RKLAMMER);
  721. end
  722. else
  723. paras:=nil;
  724. p1 := geninlinenode(l,false,paras);
  725. statement_syssym := p1;
  726. end;
  727. in_str_x_string :
  728. begin
  729. consume(_LKLAMMER);
  730. paras:=parse_paras(true,false,_RKLAMMER);
  731. consume(_RKLAMMER);
  732. p1 := geninlinenode(l,false,paras);
  733. statement_syssym := p1;
  734. end;
  735. in_val_x:
  736. Begin
  737. consume(_LKLAMMER);
  738. in_args := true;
  739. p1:= ccallparanode.create(comp_expr(true,false), nil);
  740. consume(_COMMA);
  741. p2 := ccallparanode.create(comp_expr(true,false),p1);
  742. if try_to_consume(_COMMA) then
  743. p2 := ccallparanode.create(comp_expr(true,false),p2);
  744. consume(_RKLAMMER);
  745. p2 := geninlinenode(l,false,p2);
  746. statement_syssym := p2;
  747. End;
  748. in_include_x_y,
  749. in_exclude_x_y :
  750. begin
  751. consume(_LKLAMMER);
  752. in_args:=true;
  753. p1:=comp_expr(true,false);
  754. consume(_COMMA);
  755. p2:=comp_expr(true,false);
  756. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  757. consume(_RKLAMMER);
  758. end;
  759. in_pack_x_y_z,
  760. in_unpack_x_y_z :
  761. begin
  762. consume(_LKLAMMER);
  763. in_args:=true;
  764. p1:=comp_expr(true,false);
  765. consume(_COMMA);
  766. p2:=comp_expr(true,false);
  767. consume(_COMMA);
  768. paras:=comp_expr(true,false);
  769. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  770. consume(_RKLAMMER);
  771. end;
  772. in_assert_x_y :
  773. begin
  774. consume(_LKLAMMER);
  775. in_args:=true;
  776. p1:=comp_expr(true,false);
  777. if try_to_consume(_COMMA) then
  778. p2:=comp_expr(true,false)
  779. else
  780. begin
  781. { then insert an empty string }
  782. p2:=cstringconstnode.createstr('');
  783. end;
  784. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  785. consume(_RKLAMMER);
  786. end;
  787. in_get_frame:
  788. begin
  789. statement_syssym:=geninlinenode(l,false,nil);
  790. end;
  791. (*
  792. in_get_caller_frame:
  793. begin
  794. if try_to_consume(_LKLAMMER) then
  795. begin
  796. {You used to call get_caller_frame as get_caller_frame(get_frame),
  797. however, as a stack frame may not exist, it does more harm than
  798. good, so ignore it.}
  799. in_args:=true;
  800. p1:=comp_expr(true,false);
  801. p1.destroy;
  802. consume(_RKLAMMER);
  803. end;
  804. statement_syssym:=geninlinenode(l,false,nil);
  805. end;
  806. *)
  807. in_default_x:
  808. begin
  809. consume(_LKLAMMER);
  810. in_args:=true;
  811. def:=nil;
  812. single_type(def,[stoAllowSpecialization]);
  813. statement_syssym:=cerrornode.create;
  814. if def<>generrordef then
  815. { "type expected" error is already done by single_type }
  816. if def.typ=forwarddef then
  817. Message1(type_e_type_is_not_completly_defined,tforwarddef(def).tosymname^)
  818. else
  819. begin
  820. statement_syssym.free;
  821. statement_syssym:=geninlinenode(in_default_x,false,ctypenode.create(def));
  822. end;
  823. { consume the right bracket here for a nicer error position }
  824. consume(_RKLAMMER);
  825. end;
  826. in_setstring_x_y_z:
  827. begin
  828. statement_syssym := inline_setstring;
  829. end;
  830. else
  831. internalerror(15);
  832. end;
  833. in_args:=prev_in_args;
  834. end;
  835. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  836. var
  837. pd: tprocdef;
  838. begin
  839. maybe_load_methodpointer:=false;
  840. if not assigned(p1) then
  841. begin
  842. case st.symtabletype of
  843. withsymtable :
  844. begin
  845. if (st.defowner.typ=objectdef) then
  846. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  847. end;
  848. ObjectSymtable,
  849. recordsymtable:
  850. begin
  851. { Escape nested procedures }
  852. if assigned(current_procinfo) then
  853. begin
  854. pd:=current_procinfo.get_normal_proc.procdef;
  855. { We are calling from the static class method which has no self node }
  856. if assigned(pd) and pd.no_self_node then
  857. if st.symtabletype=recordsymtable then
  858. p1:=ctypenode.create(pd.struct)
  859. else
  860. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  861. else
  862. p1:=load_self_node;
  863. end
  864. else
  865. p1:=load_self_node;
  866. { We are calling a member }
  867. maybe_load_methodpointer:=true;
  868. end;
  869. end;
  870. end;
  871. end;
  872. { reads the parameter for a subroutine call }
  873. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  874. var
  875. membercall,
  876. prevafterassn : boolean;
  877. i : integer;
  878. para,p2 : tnode;
  879. currpara : tparavarsym;
  880. aprocdef : tprocdef;
  881. begin
  882. prevafterassn:=afterassignment;
  883. afterassignment:=false;
  884. membercall:=false;
  885. aprocdef:=nil;
  886. { when it is a call to a member we need to load the
  887. methodpointer first
  888. }
  889. membercall:=maybe_load_methodpointer(st,p1);
  890. { When we are expecting a procvar we also need
  891. to get the address in some cases }
  892. if assigned(getprocvardef) then
  893. begin
  894. if (block_type=bt_const) or
  895. getaddr then
  896. begin
  897. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  898. getaddr:=true;
  899. end
  900. else
  901. if ((m_tp_procvar in current_settings.modeswitches) or
  902. (m_mac_procvar in current_settings.modeswitches)) and
  903. not(token in [_CARET,_POINT,_LKLAMMER]) then
  904. begin
  905. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  906. if assigned(aprocdef) then
  907. getaddr:=true;
  908. end;
  909. end;
  910. { only need to get the address of the procedure? }
  911. if getaddr then
  912. begin
  913. { for now we don't support pointers to generic functions, but since
  914. this is only temporary we use a non translated message }
  915. if assigned(spezcontext) then
  916. begin
  917. comment(v_error, 'Pointers to generics functions not implemented');
  918. p1:=cerrornode.create;
  919. spezcontext.free;
  920. exit;
  921. end;
  922. { Retrieve info which procvar to call. For tp_procvar the
  923. aprocdef is already loaded above so we can reuse it }
  924. if not assigned(aprocdef) and
  925. assigned(getprocvardef) then
  926. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  927. { generate a methodcallnode or proccallnode }
  928. { we shouldn't convert things like @tcollection.load }
  929. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  930. if assigned(p1) then
  931. begin
  932. { for loading methodpointer of an inherited function
  933. we use self as instance and load the address of
  934. the function directly and not through the vmt (PFV) }
  935. if (cnf_inherited in callflags) then
  936. begin
  937. include(tloadnode(p2).loadnodeflags,loadnf_inherited);
  938. p1.free;
  939. p1:=load_self_node;
  940. end;
  941. if (p1.nodetype<>typen) then
  942. tloadnode(p2).set_mp(p1)
  943. else
  944. p1.free;
  945. end;
  946. p1:=p2;
  947. { no postfix operators }
  948. again:=false;
  949. end
  950. else
  951. begin
  952. para:=nil;
  953. if anon_inherited then
  954. begin
  955. if not assigned(current_procinfo) then
  956. internalerror(200305054);
  957. for i:=0 to current_procinfo.procdef.paras.count-1 do
  958. begin
  959. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  960. if not(vo_is_hidden_para in currpara.varoptions) then
  961. begin
  962. { inheritance by msgint? }
  963. if assigned(srdef) then
  964. { anonymous inherited via msgid calls only require a var parameter for
  965. both methods, so we need some type casting here }
  966. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  967. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  968. para)
  969. else
  970. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  971. end;
  972. end;
  973. end
  974. else
  975. begin
  976. if try_to_consume(_LKLAMMER) then
  977. begin
  978. para:=parse_paras(false,false,_RKLAMMER);
  979. consume(_RKLAMMER);
  980. end;
  981. end;
  982. { indicate if this call was generated by a member and
  983. no explicit self is used, this is needed to determine
  984. how to handle a destructor call (PFV) }
  985. if membercall then
  986. include(callflags,cnf_member_call);
  987. if assigned(obj) then
  988. begin
  989. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  990. internalerror(200310031);
  991. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags,spezcontext);
  992. end
  993. else
  994. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags,spezcontext);
  995. end;
  996. afterassignment:=prevafterassn;
  997. end;
  998. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  999. var
  1000. hp,hp2 : tnode;
  1001. hpp : ^tnode;
  1002. currprocdef : tprocdef;
  1003. begin
  1004. if not assigned(pv) then
  1005. internalerror(200301121);
  1006. if (m_tp_procvar in current_settings.modeswitches) or
  1007. (m_mac_procvar in current_settings.modeswitches) then
  1008. begin
  1009. hp:=p2;
  1010. hpp:=@p2;
  1011. while assigned(hp) and
  1012. (hp.nodetype=typeconvn) do
  1013. begin
  1014. hp:=ttypeconvnode(hp).left;
  1015. { save orignal address of the old tree so we can replace the node }
  1016. hpp:=@hp;
  1017. end;
  1018. if (hp.nodetype=calln) and
  1019. { a procvar can't have parameters! }
  1020. not assigned(tcallnode(hp).left) then
  1021. begin
  1022. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  1023. if assigned(currprocdef) then
  1024. begin
  1025. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  1026. if (po_methodpointer in pv.procoptions) then
  1027. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  1028. hp.destroy;
  1029. { replace the old callnode with the new loadnode }
  1030. hpp^:=hp2;
  1031. end;
  1032. end;
  1033. end;
  1034. end;
  1035. { the following procedure handles the access to a property symbol }
  1036. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  1037. var
  1038. paras : tnode;
  1039. p2 : tnode;
  1040. membercall : boolean;
  1041. callflags : tcallnodeflags;
  1042. propaccesslist : tpropaccesslist;
  1043. sym: tsym;
  1044. begin
  1045. { property parameters? read them only if the property really }
  1046. { has parameters }
  1047. paras:=nil;
  1048. if (ppo_hasparameters in propsym.propoptions) then
  1049. begin
  1050. if try_to_consume(_LECKKLAMMER) then
  1051. begin
  1052. paras:=parse_paras(false,false,_RECKKLAMMER);
  1053. consume(_RECKKLAMMER);
  1054. end;
  1055. end;
  1056. { indexed property }
  1057. if (ppo_indexed in propsym.propoptions) then
  1058. begin
  1059. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1060. paras:=ccallparanode.create(p2,paras);
  1061. end;
  1062. { we need only a write property if a := follows }
  1063. { if not(afterassignment) and not(in_args) then }
  1064. if token=_ASSIGNMENT then
  1065. begin
  1066. if propsym.getpropaccesslist(palt_write,propaccesslist) then
  1067. begin
  1068. sym:=propaccesslist.firstsym^.sym;
  1069. case sym.typ of
  1070. procsym :
  1071. begin
  1072. callflags:=[];
  1073. { generate the method call }
  1074. membercall:=maybe_load_methodpointer(st,p1);
  1075. if membercall then
  1076. include(callflags,cnf_member_call);
  1077. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags,nil);
  1078. addsymref(sym);
  1079. paras:=nil;
  1080. consume(_ASSIGNMENT);
  1081. { read the expression }
  1082. if propsym.propdef.typ=procvardef then
  1083. getprocvardef:=tprocvardef(propsym.propdef);
  1084. p2:=comp_expr(true,false);
  1085. if assigned(getprocvardef) then
  1086. handle_procvar(getprocvardef,p2);
  1087. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1088. { mark as property, both the tcallnode and the real call block }
  1089. include(p1.flags,nf_isproperty);
  1090. getprocvardef:=nil;
  1091. end;
  1092. fieldvarsym :
  1093. begin
  1094. { generate access code }
  1095. if not handle_staticfield_access(sym,false,p1) then
  1096. propaccesslist_to_node(p1,st,propaccesslist);
  1097. include(p1.flags,nf_isproperty);
  1098. consume(_ASSIGNMENT);
  1099. { read the expression }
  1100. p2:=comp_expr(true,false);
  1101. p1:=cassignmentnode.create(p1,p2);
  1102. end
  1103. else
  1104. begin
  1105. p1:=cerrornode.create;
  1106. Message(parser_e_no_procedure_to_access_property);
  1107. end;
  1108. end;
  1109. end
  1110. else
  1111. begin
  1112. p1:=cerrornode.create;
  1113. Message(parser_e_no_procedure_to_access_property);
  1114. end;
  1115. end
  1116. else
  1117. begin
  1118. if propsym.getpropaccesslist(palt_read,propaccesslist) then
  1119. begin
  1120. sym := propaccesslist.firstsym^.sym;
  1121. case sym.typ of
  1122. fieldvarsym :
  1123. begin
  1124. { generate access code }
  1125. if not handle_staticfield_access(sym,false,p1) then
  1126. propaccesslist_to_node(p1,st,propaccesslist);
  1127. include(p1.flags,nf_isproperty);
  1128. { catch expressions like "(propx):=1;" }
  1129. include(p1.flags,nf_no_lvalue);
  1130. end;
  1131. procsym :
  1132. begin
  1133. callflags:=[];
  1134. { generate the method call }
  1135. membercall:=maybe_load_methodpointer(st,p1);
  1136. if membercall then
  1137. include(callflags,cnf_member_call);
  1138. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags,nil);
  1139. paras:=nil;
  1140. include(p1.flags,nf_isproperty);
  1141. include(p1.flags,nf_no_lvalue);
  1142. end
  1143. else
  1144. begin
  1145. p1:=cerrornode.create;
  1146. Message(type_e_mismatch);
  1147. end;
  1148. end;
  1149. end
  1150. else
  1151. begin
  1152. { error, no function to read property }
  1153. p1:=cerrornode.create;
  1154. Message(parser_e_no_procedure_to_access_property);
  1155. end;
  1156. end;
  1157. { release paras if not used }
  1158. if assigned(paras) then
  1159. paras.free;
  1160. end;
  1161. { the ID token has to be consumed before calling this function }
  1162. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  1163. var
  1164. isclassref:boolean;
  1165. begin
  1166. if sym=nil then
  1167. begin
  1168. { pattern is still valid unless
  1169. there is another ID just after the ID of sym }
  1170. Message1(sym_e_id_no_member,orgpattern);
  1171. p1.free;
  1172. p1:=cerrornode.create;
  1173. { try to clean up }
  1174. spezcontext.free;
  1175. again:=false;
  1176. end
  1177. else
  1178. begin
  1179. if assigned(p1) then
  1180. begin
  1181. if not assigned(p1.resultdef) then
  1182. do_typecheckpass(p1);
  1183. isclassref:=(p1.resultdef.typ=classrefdef);
  1184. end
  1185. else
  1186. isclassref:=false;
  1187. if assigned(spezcontext) and not (sym.typ=procsym) then
  1188. internalerror(2015091801);
  1189. { we assume, that only procsyms and varsyms are in an object }
  1190. { symbol table, for classes, properties are allowed }
  1191. case sym.typ of
  1192. procsym:
  1193. begin
  1194. do_proc_call(sym,sym.owner,structh,
  1195. (getaddr and not(token in [_CARET,_POINT])),
  1196. again,p1,callflags,spezcontext);
  1197. { we need to know which procedure is called }
  1198. do_typecheckpass(p1);
  1199. { calling using classref? }
  1200. if isclassref and
  1201. (p1.nodetype=calln) and
  1202. assigned(tcallnode(p1).procdefinition) then
  1203. begin
  1204. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1205. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1206. Message(parser_e_only_class_members_via_class_ref);
  1207. { in Java, constructors are not automatically inherited
  1208. -> calling a constructor from a parent type will create
  1209. an instance of that parent type! }
  1210. if is_javaclass(structh) and
  1211. (tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1212. (tcallnode(p1).procdefinition.owner.defowner<>find_real_class_definition(tobjectdef(structh),false)) then
  1213. Message(parser_e_java_no_inherited_constructor);
  1214. { Provide a warning if we try to create an instance of a
  1215. abstract class using the type name of that class. We
  1216. must not provide a warning if we use a "class of"
  1217. variable of that type though as we don't know the
  1218. type of the class }
  1219. if (tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1220. (oo_is_abstract in structh.objectoptions) and
  1221. assigned(tcallnode(p1).methodpointer) and
  1222. (tcallnode(p1).methodpointer.nodetype=loadvmtaddrn) then
  1223. Message1(type_w_instance_abstract_class,structh.RttiName);
  1224. end;
  1225. end;
  1226. fieldvarsym:
  1227. begin
  1228. if not handle_staticfield_access(sym,true,p1) then
  1229. begin
  1230. if isclassref then
  1231. if assigned(p1) and
  1232. (
  1233. is_self_node(p1) or
  1234. (assigned(current_procinfo) and (current_procinfo.get_normal_proc.procdef.no_self_node) and
  1235. (current_procinfo.procdef.struct=structh))) then
  1236. Message(parser_e_only_class_members)
  1237. else
  1238. Message(parser_e_only_class_members_via_class_ref);
  1239. p1:=csubscriptnode.create(sym,p1);
  1240. end;
  1241. end;
  1242. propertysym:
  1243. begin
  1244. if isclassref and not (sp_static in sym.symoptions) then
  1245. Message(parser_e_only_class_members_via_class_ref);
  1246. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1247. end;
  1248. typesym:
  1249. begin
  1250. p1.free;
  1251. if try_to_consume(_LKLAMMER) then
  1252. begin
  1253. p1:=comp_expr(true,false);
  1254. consume(_RKLAMMER);
  1255. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1256. end
  1257. else
  1258. begin
  1259. p1:=ctypenode.create(ttypesym(sym).typedef);
  1260. if (is_class(ttypesym(sym).typedef) or
  1261. is_objcclass(ttypesym(sym).typedef) or
  1262. is_javaclass(ttypesym(sym).typedef)) and
  1263. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1264. p1:=cloadvmtaddrnode.create(p1);
  1265. end;
  1266. end;
  1267. constsym:
  1268. begin
  1269. p1.free;
  1270. p1:=genconstsymtree(tconstsym(sym));
  1271. end;
  1272. staticvarsym:
  1273. begin
  1274. { typed constant is a staticvarsym
  1275. now they are absolutevarsym }
  1276. p1.free;
  1277. p1:=cloadnode.create(sym,sym.Owner);
  1278. end;
  1279. absolutevarsym:
  1280. begin
  1281. p1.free;
  1282. p1:=nil;
  1283. { typed constants are absolutebarsyms now to handle storage properly }
  1284. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1285. end
  1286. else
  1287. internalerror(16);
  1288. end;
  1289. end;
  1290. end;
  1291. function handle_specialize_inline_specialization(var srsym:tsym;out srsymtable:tsymtable):boolean;
  1292. var
  1293. spezdef : tdef;
  1294. begin
  1295. result:=false;
  1296. if not assigned(srsym) then
  1297. message1(sym_e_id_no_member,orgpattern)
  1298. else
  1299. if srsym.typ<>typesym then
  1300. message(type_e_type_id_expected)
  1301. else
  1302. begin
  1303. spezdef:=ttypesym(srsym).typedef;
  1304. generate_specialization(spezdef,false,'');
  1305. if spezdef<>generrordef then
  1306. begin
  1307. srsym:=spezdef.typesym;
  1308. srsymtable:=srsym.owner;
  1309. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1310. result:=true;
  1311. end
  1312. end;
  1313. end;
  1314. function handle_factor_typenode(hdef:tdef;getaddr:boolean;var again:boolean;sym:tsym;typeonly:boolean):tnode;
  1315. var
  1316. srsym : tsym;
  1317. srsymtable : tsymtable;
  1318. isspecialize : boolean;
  1319. begin
  1320. if sym=nil then
  1321. sym:=hdef.typesym;
  1322. { allow Ordinal(Value) for type declarations since it
  1323. can be an enummeration declaration or a set lke:
  1324. (OrdinalType(const1)..OrdinalType(const2) }
  1325. if (not typeonly or is_ordinal(hdef)) and
  1326. try_to_consume(_LKLAMMER) then
  1327. begin
  1328. result:=comp_expr(true,false);
  1329. consume(_RKLAMMER);
  1330. { type casts to class helpers aren't allowed }
  1331. if is_objectpascal_helper(hdef) then
  1332. Message(parser_e_no_category_as_types)
  1333. { recovery by not creating a conversion node }
  1334. else
  1335. result:=ctypeconvnode.create_explicit(result,hdef);
  1336. end
  1337. { not LKLAMMER }
  1338. else if (token=_POINT) and
  1339. (is_object(hdef) or is_record(hdef)) then
  1340. begin
  1341. consume(_POINT);
  1342. { handles calling methods declared in parent objects
  1343. using "parentobject.methodname()" }
  1344. if assigned(current_structdef) and
  1345. not(getaddr) and
  1346. def_is_related(current_structdef,hdef) then
  1347. begin
  1348. result:=ctypenode.create(hdef);
  1349. ttypenode(result).typesym:=sym;
  1350. if not (m_delphi in current_settings.modeswitches) and
  1351. (block_type in [bt_type,bt_var_type,bt_const_type]) and
  1352. (token=_ID) and
  1353. (idtoken=_SPECIALIZE) then
  1354. begin
  1355. consume(_ID);
  1356. if token<>_ID then
  1357. message(type_e_type_id_expected);
  1358. isspecialize:=true;
  1359. end
  1360. else
  1361. isspecialize:=false;
  1362. { search also in inherited methods }
  1363. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,[ssf_search_helper]);
  1364. if isspecialize then
  1365. begin
  1366. consume(_ID);
  1367. if not handle_specialize_inline_specialization(srsym,srsymtable) then
  1368. begin
  1369. result.free;
  1370. result:=cerrornode.create;
  1371. end;
  1372. end
  1373. else
  1374. begin
  1375. if assigned(srsym) then
  1376. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1377. consume(_ID);
  1378. end;
  1379. if result.nodetype<>errorn then
  1380. do_member_read(tabstractrecorddef(hdef),false,srsym,result,again,[],nil);
  1381. end
  1382. else
  1383. begin
  1384. { handles:
  1385. * @TObject.Load
  1386. * static methods and variables }
  1387. result:=ctypenode.create(hdef);
  1388. ttypenode(result).typesym:=sym;
  1389. if not (m_delphi in current_settings.modeswitches) and
  1390. (block_type in [bt_type,bt_var_type,bt_const_type]) and
  1391. (token=_ID) and
  1392. (idtoken=_SPECIALIZE) then
  1393. begin
  1394. consume(_ID);
  1395. if token<>_ID then
  1396. message(type_e_type_id_expected);
  1397. isspecialize:=true;
  1398. end
  1399. else
  1400. isspecialize:=false;
  1401. { TP allows also @TMenu.Load if Load is only }
  1402. { defined in an anchestor class }
  1403. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1404. if isspecialize then
  1405. begin
  1406. consume(_ID);
  1407. if not handle_specialize_inline_specialization(srsym,srsymtable) then
  1408. begin
  1409. result.free;
  1410. result:=cerrornode.create;
  1411. end;
  1412. end
  1413. else
  1414. begin
  1415. if assigned(srsym) then
  1416. begin
  1417. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1418. consume(_ID);
  1419. end
  1420. else
  1421. Message1(sym_e_id_no_member,orgpattern);
  1422. end;
  1423. if (result.nodetype<>errorn) and assigned(srsym) then
  1424. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],nil);
  1425. end;
  1426. end
  1427. else
  1428. begin
  1429. { Normally here would be the check against the usage
  1430. of "TClassHelper.Something", but as that might be
  1431. used inside of system symbols like sizeof and
  1432. typeinfo this check is put into ttypenode.pass_1
  1433. (for "TClassHelper" alone) and tcallnode.pass_1
  1434. (for "TClassHelper.Something") }
  1435. { class reference ? }
  1436. if is_class(hdef) or
  1437. is_objcclass(hdef) or
  1438. { Java interfaces also can have loadvmtaddrnodes,
  1439. e.g. for expressions such as JLClass(intftype) }
  1440. is_java_class_or_interface(hdef) then
  1441. begin
  1442. if getaddr and (token=_POINT) and
  1443. not is_javainterface(hdef) then
  1444. begin
  1445. consume(_POINT);
  1446. { allows @Object.Method }
  1447. { also allows static methods and variables }
  1448. result:=ctypenode.create(hdef);
  1449. ttypenode(result).typesym:=sym;
  1450. { TP allows also @TMenu.Load if Load is only }
  1451. { defined in an anchestor class }
  1452. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1453. if assigned(srsym) then
  1454. begin
  1455. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1456. consume(_ID);
  1457. { in case of @Object.Method1.Method2, we have to call
  1458. Method1 -> create a loadvmtaddr node as self instead of
  1459. a typen (the typenode would be changed to self of the
  1460. current method in case Method1 is a constructor, see
  1461. mantis #24844) }
  1462. if not(block_type in [bt_type,bt_const_type,bt_var_type]) and
  1463. (srsym.typ=procsym) and
  1464. (token in [_CARET,_POINT]) then
  1465. result:=cloadvmtaddrnode.create(result);
  1466. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],nil);
  1467. end
  1468. else
  1469. begin
  1470. Message1(sym_e_id_no_member,orgpattern);
  1471. consume(_ID);
  1472. end;
  1473. end
  1474. else
  1475. begin
  1476. result:=ctypenode.create(hdef);
  1477. ttypenode(result).typesym:=sym;
  1478. { For a type block we simply return only
  1479. the type. For all other blocks we return
  1480. a loadvmt node }
  1481. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1482. result:=cloadvmtaddrnode.create(result);
  1483. end;
  1484. end
  1485. else
  1486. begin
  1487. result:=ctypenode.create(hdef);
  1488. ttypenode(result).typesym:=sym;
  1489. end;
  1490. end;
  1491. end;
  1492. {****************************************************************************
  1493. Factor
  1494. ****************************************************************************}
  1495. function real_const_node_from_pattern(s:string):tnode;
  1496. var
  1497. d : bestreal;
  1498. code : integer;
  1499. cur : currency;
  1500. begin
  1501. val(s,d,code);
  1502. if code<>0 then
  1503. begin
  1504. Message(parser_e_error_in_real);
  1505. d:=1.0;
  1506. end;
  1507. if current_settings.fputype=fpu_none then
  1508. Message(parser_e_unsupported_real);
  1509. if (current_settings.minfpconstprec=s32real) and
  1510. (d = single(d)) then
  1511. result:=crealconstnode.create(d,s32floattype)
  1512. else if (current_settings.minfpconstprec=s64real) and
  1513. (d = double(d)) then
  1514. result:=crealconstnode.create(d,s64floattype)
  1515. else
  1516. result:=crealconstnode.create(d,pbestrealtype^);
  1517. val(pattern,cur,code);
  1518. if code=0 then
  1519. trealconstnode(result).value_currency:=cur;
  1520. end;
  1521. {---------------------------------------------
  1522. PostFixOperators
  1523. ---------------------------------------------}
  1524. { returns whether or not p1 has been changed }
  1525. function postfixoperators(var p1:tnode;var again:boolean;getaddr:boolean): boolean;
  1526. { tries to avoid syntax errors after invalid qualifiers }
  1527. procedure recoverconsume_postfixops;
  1528. begin
  1529. repeat
  1530. if not try_to_consume(_CARET) then
  1531. if try_to_consume(_POINT) then
  1532. try_to_consume(_ID)
  1533. else if try_to_consume(_LECKKLAMMER) then
  1534. begin
  1535. repeat
  1536. comp_expr(true,false);
  1537. until not try_to_consume(_COMMA);
  1538. consume(_RECKKLAMMER);
  1539. end
  1540. else if try_to_consume(_LKLAMMER) then
  1541. begin
  1542. repeat
  1543. comp_expr(true,false);
  1544. until not try_to_consume(_COMMA);
  1545. consume(_RKLAMMER);
  1546. end
  1547. else
  1548. break;
  1549. until false;
  1550. end;
  1551. procedure handle_variantarray;
  1552. var
  1553. p4 : tnode;
  1554. newstatement : tstatementnode;
  1555. tempresultvariant,
  1556. temp : ttempcreatenode;
  1557. paras : tcallparanode;
  1558. newblock : tnode;
  1559. countindices : aint;
  1560. begin
  1561. { create statements with call initialize the arguments and
  1562. call fpc_dynarr_setlength }
  1563. newblock:=internalstatements(newstatement);
  1564. { get temp for array of indicies,
  1565. we set the real size later }
  1566. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1567. addstatement(newstatement,temp);
  1568. countindices:=0;
  1569. repeat
  1570. p4:=comp_expr(true,false);
  1571. addstatement(newstatement,cassignmentnode.create(
  1572. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1573. inc(countindices);
  1574. until not try_to_consume(_COMMA);
  1575. { set real size }
  1576. temp.size:=countindices*s32inttype.size;
  1577. consume(_RECKKLAMMER);
  1578. { we need only a write access if a := follows }
  1579. if token=_ASSIGNMENT then
  1580. begin
  1581. consume(_ASSIGNMENT);
  1582. p4:=comp_expr(true,false);
  1583. { create call to fpc_vararray_put }
  1584. paras:=ccallparanode.create(cordconstnode.create
  1585. (countindices,s32inttype,true),
  1586. ccallparanode.create(caddrnode.create_internal
  1587. (ctemprefnode.create(temp)),
  1588. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1589. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1590. ,nil))));
  1591. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1592. addstatement(newstatement,ctempdeletenode.create(temp));
  1593. end
  1594. else
  1595. begin
  1596. { create temp for result }
  1597. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1598. addstatement(newstatement,tempresultvariant);
  1599. { create call to fpc_vararray_get }
  1600. paras:=ccallparanode.create(cordconstnode.create
  1601. (countindices,s32inttype,true),
  1602. ccallparanode.create(caddrnode.create_internal
  1603. (ctemprefnode.create(temp)),
  1604. ccallparanode.create(p1,
  1605. ccallparanode.create(
  1606. ctemprefnode.create(tempresultvariant)
  1607. ,nil))));
  1608. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1609. addstatement(newstatement,ctempdeletenode.create(temp));
  1610. { the last statement should return the value as
  1611. location and type, this is done be referencing the
  1612. temp and converting it first from a persistent temp to
  1613. normal temp }
  1614. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1615. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1616. end;
  1617. p1:=newblock;
  1618. end;
  1619. function parse_array_constructor(arrdef:tarraydef): tnode;
  1620. var
  1621. newstatement,assstatement:tstatementnode;
  1622. arrnode:ttempcreatenode;
  1623. temp2:ttempcreatenode;
  1624. assnode:tnode;
  1625. paracount:integer;
  1626. begin
  1627. result:=internalstatements(newstatement);
  1628. { create temp for result }
  1629. arrnode:=ctempcreatenode.create(arrdef,arrdef.size,tt_persistent,true);
  1630. addstatement(newstatement,arrnode);
  1631. paracount:=0;
  1632. { check arguments and create an assignment calls }
  1633. if try_to_consume(_LKLAMMER) then
  1634. begin
  1635. assnode:=internalstatements(assstatement);
  1636. repeat
  1637. { arr[i] := param_i }
  1638. addstatement(assstatement,
  1639. cassignmentnode.create(
  1640. cvecnode.create(
  1641. ctemprefnode.create(arrnode),
  1642. cordconstnode.create(paracount,arrdef.rangedef,false)),
  1643. comp_expr(true,false)));
  1644. inc(paracount);
  1645. until not try_to_consume(_COMMA);
  1646. consume(_RKLAMMER);
  1647. end
  1648. else
  1649. assnode:=nil;
  1650. { get temp for array of lengths }
  1651. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1652. addstatement(newstatement,temp2);
  1653. { one dimensional }
  1654. addstatement(newstatement,cassignmentnode.create(
  1655. ctemprefnode.create_offset(temp2,0),
  1656. cordconstnode.create
  1657. (paracount,s32inttype,true)));
  1658. { create call to fpc_dynarr_setlength }
  1659. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1660. ccallparanode.create(caddrnode.create_internal
  1661. (ctemprefnode.create(temp2)),
  1662. ccallparanode.create(cordconstnode.create
  1663. (1,s32inttype,true),
  1664. ccallparanode.create(caddrnode.create_internal
  1665. (crttinode.create(tstoreddef(arrdef),initrtti,rdt_normal)),
  1666. ccallparanode.create(
  1667. ctypeconvnode.create_internal(
  1668. ctemprefnode.create(arrnode),voidpointertype),
  1669. nil))))
  1670. ));
  1671. { add assignment statememnts }
  1672. addstatement(newstatement,ctempdeletenode.create(temp2));
  1673. if assigned(assnode) then
  1674. addstatement(newstatement,assnode);
  1675. { the last statement should return the value as
  1676. location and type, this is done be referencing the
  1677. temp and converting it first from a persistent temp to
  1678. normal temp }
  1679. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1680. addstatement(newstatement,ctemprefnode.create(arrnode));
  1681. end;
  1682. function try_type_helper(var node:tnode;def:tdef):boolean;
  1683. var
  1684. srsym : tsym;
  1685. srsymtable : tsymtable;
  1686. n : tnode;
  1687. newstatement : tstatementnode;
  1688. temp : ttempcreatenode;
  1689. extdef : tdef;
  1690. begin
  1691. result:=false;
  1692. if (token=_ID) and (block_type in [bt_body,bt_general,bt_except,bt_const]) then
  1693. begin
  1694. if not assigned(def) then
  1695. if node.nodetype=addrn then
  1696. { always use the pointer type for addr nodes as otherwise
  1697. we'll have an anonymous pointertype with no name }
  1698. def:=voidpointertype
  1699. else
  1700. def:=node.resultdef;
  1701. result:=search_objectpascal_helper(def,nil,pattern,srsym,srsymtable);
  1702. if result then
  1703. begin
  1704. if not (srsymtable.symtabletype=objectsymtable) or
  1705. not is_objectpascal_helper(tdef(srsymtable.defowner)) then
  1706. internalerror(2013011401);
  1707. { convert const node to temp node of the extended type }
  1708. if node.nodetype in (nodetype_const+[niln,addrn]) then
  1709. begin
  1710. extdef:=tobjectdef(srsymtable.defowner).extendeddef;
  1711. newstatement:=nil;
  1712. n:=internalstatements(newstatement);
  1713. temp:=ctempcreatenode.create(extdef,extdef.size,tt_persistent,false);
  1714. addstatement(newstatement,temp);
  1715. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),node));
  1716. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1717. addstatement(newstatement,ctemprefnode.create(temp));
  1718. node:=n;
  1719. do_typecheckpass(node)
  1720. end;
  1721. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1722. consume(_ID);
  1723. do_member_read(nil,getaddr,srsym,node,again,[],nil);
  1724. end;
  1725. end;
  1726. end;
  1727. var
  1728. protsym : tpropertysym;
  1729. p2,p3 : tnode;
  1730. srsym : tsym;
  1731. srsymtable : TSymtable;
  1732. structh : tabstractrecorddef;
  1733. { shouldn't be used that often, so the extra overhead is ok to save
  1734. stack space }
  1735. dispatchstring : ansistring;
  1736. erroroutp1,
  1737. allowspecialize,
  1738. isspecialize,
  1739. found,
  1740. haderror,
  1741. nodechanged : boolean;
  1742. calltype: tdispcalltype;
  1743. valstr,expstr : string;
  1744. intval : qword;
  1745. code : integer;
  1746. strdef : tdef;
  1747. label
  1748. skipreckklammercheck,
  1749. skippointdefcheck;
  1750. begin
  1751. result:=false;
  1752. again:=true;
  1753. while again do
  1754. begin
  1755. { we need the resultdef }
  1756. do_typecheckpass_changed(p1,nodechanged);
  1757. result:=result or nodechanged;
  1758. if codegenerror then
  1759. begin
  1760. recoverconsume_postfixops;
  1761. exit;
  1762. end;
  1763. { handle token }
  1764. case token of
  1765. _CARET:
  1766. begin
  1767. consume(_CARET);
  1768. { support tp/mac procvar^ if the procvar returns a
  1769. pointer type }
  1770. if ((m_tp_procvar in current_settings.modeswitches) or
  1771. (m_mac_procvar in current_settings.modeswitches)) and
  1772. (p1.resultdef.typ=procvardef) and
  1773. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1774. begin
  1775. p1:=ccallnode.create_procvar(nil,p1);
  1776. typecheckpass(p1);
  1777. end;
  1778. { iso file buf access? }
  1779. if (m_iso in current_settings.modeswitches) and
  1780. (p1.resultdef.typ=filedef) then
  1781. begin
  1782. case tfiledef(p1.resultdef).filetyp of
  1783. ft_text:
  1784. begin
  1785. p1:=cderefnode.create(ccallnode.createintern('fpc_getbuf_text',ccallparanode.create(p1,nil)));
  1786. typecheckpass(p1);
  1787. end;
  1788. ft_typed:
  1789. begin
  1790. p1:=cderefnode.create(ctypeconvnode.create_internal(ccallnode.createintern('fpc_getbuf_typedfile',ccallparanode.create(p1,nil)),
  1791. cpointerdef.getreusable(tfiledef(p1.resultdef).typedfiledef)));
  1792. typecheckpass(p1);
  1793. end;
  1794. end;
  1795. end
  1796. else if (p1.resultdef.typ<>pointerdef) then
  1797. begin
  1798. { ^ as binary operator is a problem!!!! (FK) }
  1799. again:=false;
  1800. Message(parser_e_invalid_qualifier);
  1801. recoverconsume_postfixops;
  1802. p1.destroy;
  1803. p1:=cerrornode.create;
  1804. end
  1805. else
  1806. p1:=cderefnode.create(p1);
  1807. end;
  1808. _LECKKLAMMER:
  1809. begin
  1810. if is_class_or_interface_or_object(p1.resultdef) or
  1811. is_dispinterface(p1.resultdef) or
  1812. is_record(p1.resultdef) or
  1813. is_javaclass(p1.resultdef) then
  1814. begin
  1815. { default property }
  1816. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1817. if not(assigned(protsym)) then
  1818. begin
  1819. p1.destroy;
  1820. p1:=cerrornode.create;
  1821. again:=false;
  1822. message(parser_e_no_default_property_available);
  1823. end
  1824. else
  1825. begin
  1826. { The property symbol is referenced indirect }
  1827. protsym.IncRefCount;
  1828. handle_propertysym(protsym,protsym.owner,p1);
  1829. end;
  1830. end
  1831. else
  1832. begin
  1833. consume(_LECKKLAMMER);
  1834. repeat
  1835. { in all of the cases below, p1 is changed }
  1836. case p1.resultdef.typ of
  1837. pointerdef:
  1838. begin
  1839. { support delphi autoderef }
  1840. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1841. (m_autoderef in current_settings.modeswitches) then
  1842. p1:=cderefnode.create(p1);
  1843. p2:=comp_expr(true,false);
  1844. { Support Pbytevar[0..9] which returns array [0..9].}
  1845. if try_to_consume(_POINTPOINT) then
  1846. p2:=crangenode.create(p2,comp_expr(true,false));
  1847. p1:=cvecnode.create(p1,p2);
  1848. end;
  1849. variantdef:
  1850. begin
  1851. handle_variantarray;
  1852. { the RECKKLAMMER is already read }
  1853. goto skipreckklammercheck;
  1854. end;
  1855. stringdef :
  1856. begin
  1857. p2:=comp_expr(true,false);
  1858. { Support string[0..9] which returns array [0..9] of char.}
  1859. if try_to_consume(_POINTPOINT) then
  1860. p2:=crangenode.create(p2,comp_expr(true,false));
  1861. p1:=cvecnode.create(p1,p2);
  1862. end;
  1863. arraydef:
  1864. begin
  1865. p2:=comp_expr(true,false);
  1866. { support SEG:OFS for go32v2/msdos Mem[] }
  1867. if (target_info.system in [system_i386_go32v2,system_i386_watcom,system_i8086_msdos,system_i8086_win16]) and
  1868. (p1.nodetype=loadn) and
  1869. assigned(tloadnode(p1).symtableentry) and
  1870. assigned(tloadnode(p1).symtableentry.owner.name) and
  1871. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1872. ((tloadnode(p1).symtableentry.name='MEM') or
  1873. (tloadnode(p1).symtableentry.name='MEMW') or
  1874. (tloadnode(p1).symtableentry.name='MEML')) then
  1875. begin
  1876. {$if defined(i8086)}
  1877. consume(_COLON);
  1878. inserttypeconv(p2,u16inttype);
  1879. inserttypeconv_internal(p2,u32inttype);
  1880. p3:=cshlshrnode.create(shln,p2,cordconstnode.create($10,s16inttype,false));
  1881. p2:=comp_expr(true,false);
  1882. inserttypeconv(p2,u16inttype);
  1883. inserttypeconv_internal(p2,u32inttype);
  1884. p2:=caddnode.create(addn,p2,p3);
  1885. case tloadnode(p1).symtableentry.name of
  1886. 'MEM': p2:=ctypeconvnode.create_internal(p2,bytefarpointertype);
  1887. 'MEMW': p2:=ctypeconvnode.create_internal(p2,wordfarpointertype);
  1888. 'MEML': p2:=ctypeconvnode.create_internal(p2,longintfarpointertype);
  1889. else
  1890. internalerror(2013053102);
  1891. end;
  1892. p1:=cderefnode.create(p2);
  1893. {$elseif defined(i386)}
  1894. if try_to_consume(_COLON) then
  1895. begin
  1896. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1897. p2:=comp_expr(true,false);
  1898. p2:=caddnode.create(addn,p2,p3);
  1899. if try_to_consume(_POINTPOINT) then
  1900. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1901. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1902. p1:=cvecnode.create(p1,p2);
  1903. include(tvecnode(p1).flags,nf_memseg);
  1904. include(tvecnode(p1).flags,nf_memindex);
  1905. end
  1906. else
  1907. begin
  1908. if try_to_consume(_POINTPOINT) then
  1909. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1910. p2:=crangenode.create(p2,comp_expr(true,false));
  1911. p1:=cvecnode.create(p1,p2);
  1912. include(tvecnode(p1).flags,nf_memindex);
  1913. end;
  1914. {$else}
  1915. internalerror(2013053101);
  1916. {$endif}
  1917. end
  1918. else
  1919. begin
  1920. if try_to_consume(_POINTPOINT) then
  1921. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1922. p2:=crangenode.create(p2,comp_expr(true,false));
  1923. p1:=cvecnode.create(p1,p2);
  1924. end;
  1925. end;
  1926. else
  1927. begin
  1928. if p1.resultdef.typ<>undefineddef then
  1929. Message(parser_e_invalid_qualifier);
  1930. p1.destroy;
  1931. p1:=cerrornode.create;
  1932. comp_expr(true,false);
  1933. again:=false;
  1934. end;
  1935. end;
  1936. do_typecheckpass(p1);
  1937. until not try_to_consume(_COMMA);
  1938. consume(_RECKKLAMMER);
  1939. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1940. skipreckklammercheck:
  1941. end;
  1942. end;
  1943. _POINT :
  1944. begin
  1945. consume(_POINT);
  1946. allowspecialize:=not (m_delphi in current_settings.modeswitches) and (block_type in [bt_type,bt_var_type,bt_const_type]);
  1947. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  1948. begin
  1949. //consume(_ID);
  1950. isspecialize:=true;
  1951. end
  1952. else
  1953. isspecialize:=false;
  1954. if (p1.resultdef.typ=pointerdef) and
  1955. (m_autoderef in current_settings.modeswitches) and
  1956. { don't auto-deref objc.id, because then the code
  1957. below for supporting id.anyobjcmethod isn't triggered }
  1958. (p1.resultdef<>objc_idtype) then
  1959. begin
  1960. p1:=cderefnode.create(p1);
  1961. do_typecheckpass(p1);
  1962. end;
  1963. { procvar.<something> can never mean anything so always
  1964. try to call it in case it returns a record/object/... }
  1965. maybe_call_procvar(p1,false);
  1966. if (p1.nodetype=ordconstn) and
  1967. not is_boolean(p1.resultdef) and
  1968. not is_enum(p1.resultdef) then
  1969. begin
  1970. { type helpers are checked first }
  1971. if (token=_ID) and try_type_helper(p1,nil) then
  1972. goto skippointdefcheck;
  1973. { only an "e" or "E" can follow an intconst with a ".", the
  1974. other case (another intconst) is handled by the scanner }
  1975. if (token=_ID) and (pattern[1]='E') then
  1976. begin
  1977. haderror:=false;
  1978. if length(pattern)>1 then
  1979. begin
  1980. expstr:=copy(pattern,2,length(pattern)-1);
  1981. val(expstr,intval,code);
  1982. if code<>0 then
  1983. begin
  1984. haderror:=true;
  1985. intval:=intval; // Hackfix the "var assigned but never used" note.
  1986. end;
  1987. end
  1988. else
  1989. expstr:='';
  1990. consume(token);
  1991. if tordconstnode(p1).value.signed then
  1992. str(tordconstnode(p1).value.svalue,valstr)
  1993. else
  1994. str(tordconstnode(p1).value.uvalue,valstr);
  1995. valstr:=valstr+'.0E';
  1996. if expstr='' then
  1997. case token of
  1998. _MINUS:
  1999. begin
  2000. consume(token);
  2001. if token=_INTCONST then
  2002. begin
  2003. valstr:=valstr+'-'+pattern;
  2004. consume(token);
  2005. end
  2006. else
  2007. haderror:=true;
  2008. end;
  2009. _PLUS:
  2010. begin
  2011. consume(token);
  2012. if token=_INTCONST then
  2013. begin
  2014. valstr:=valstr+pattern;
  2015. consume(token);
  2016. end
  2017. else
  2018. haderror:=true;
  2019. end;
  2020. _INTCONST:
  2021. begin
  2022. valstr:=valstr+pattern;
  2023. consume(_INTCONST);
  2024. end;
  2025. else
  2026. haderror:=true;
  2027. end
  2028. else
  2029. valstr:=valstr+expstr;
  2030. if haderror then
  2031. begin
  2032. Message(parser_e_error_in_real);
  2033. p2:=cerrornode.create;
  2034. end
  2035. else
  2036. p2:=real_const_node_from_pattern(valstr);
  2037. p1.free;
  2038. p1:=p2;
  2039. again:=false;
  2040. goto skippointdefcheck;
  2041. end
  2042. else
  2043. begin
  2044. { just convert the ordconst to a realconst }
  2045. p2:=crealconstnode.create(tordconstnode(p1).value,pbestrealtype^);
  2046. p1.free;
  2047. p1:=p2;
  2048. again:=false;
  2049. goto skippointdefcheck;
  2050. end;
  2051. end;
  2052. if (p1.nodetype=stringconstn) and (token=_ID) then
  2053. begin
  2054. { the def of a string const is an array }
  2055. case tstringconstnode(p1).cst_type of
  2056. cst_conststring:
  2057. if cs_refcountedstrings in current_settings.localswitches then
  2058. if m_default_unicodestring in current_settings.modeswitches then
  2059. strdef:=cunicodestringtype
  2060. else
  2061. strdef:=cansistringtype
  2062. else
  2063. strdef:=cshortstringtype;
  2064. cst_shortstring:
  2065. strdef:=cshortstringtype;
  2066. cst_ansistring:
  2067. { use getansistringdef? }
  2068. strdef:=cansistringtype;
  2069. cst_widestring:
  2070. strdef:=cwidestringtype;
  2071. cst_unicodestring:
  2072. strdef:=cunicodestringtype;
  2073. cst_longstring:
  2074. { let's see when someone stumbles upon this...}
  2075. internalerror(201301111);
  2076. else
  2077. internalerror(2013112903);
  2078. end;
  2079. if try_type_helper(p1,strdef) then
  2080. goto skippointdefcheck;
  2081. end;
  2082. { this is skipped if label skippointdefcheck is used }
  2083. case p1.resultdef.typ of
  2084. recorddef:
  2085. begin
  2086. if isspecialize or (token=_ID) then
  2087. begin
  2088. erroroutp1:=true;
  2089. srsym:=nil;
  2090. structh:=tabstractrecorddef(p1.resultdef);
  2091. if isspecialize then
  2092. begin
  2093. { consume the specialize }
  2094. consume(_ID);
  2095. if token<>_ID then
  2096. consume(_ID)
  2097. else
  2098. begin
  2099. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2100. consume(_ID);
  2101. if handle_specialize_inline_specialization(srsym,srsymtable) then
  2102. erroroutp1:=false;
  2103. end;
  2104. end
  2105. else
  2106. begin
  2107. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2108. if assigned(srsym) then
  2109. begin
  2110. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2111. consume(_ID);
  2112. erroroutp1:=false;
  2113. end
  2114. else
  2115. begin
  2116. Message1(sym_e_id_no_member,orgpattern);
  2117. { try to clean up }
  2118. consume(_ID);
  2119. end;
  2120. end;
  2121. if erroroutp1 then
  2122. begin
  2123. p1.free;
  2124. p1:=cerrornode.create;
  2125. end
  2126. else
  2127. do_member_read(structh,getaddr,srsym,p1,again,[],nil);
  2128. end
  2129. else
  2130. consume(_ID);
  2131. end;
  2132. enumdef:
  2133. begin
  2134. if token=_ID then
  2135. begin
  2136. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  2137. if assigned(srsym) and (srsym.typ=enumsym) and (p1.nodetype=typen) then
  2138. begin
  2139. p1.destroy;
  2140. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2141. p1:=genenumnode(tenumsym(srsym));
  2142. consume(_ID);
  2143. end
  2144. else
  2145. if not try_type_helper(p1,nil) then
  2146. begin
  2147. p1.destroy;
  2148. Message1(sym_e_id_no_member,orgpattern);
  2149. p1:=cerrornode.create;
  2150. consume(_ID);
  2151. end;
  2152. end;
  2153. end;
  2154. arraydef:
  2155. begin
  2156. if is_dynamic_array(p1.resultdef) then
  2157. begin
  2158. if (token=_ID) and not try_type_helper(p1,nil) then
  2159. begin
  2160. if pattern='CREATE' then
  2161. begin
  2162. consume(_ID);
  2163. p2:=parse_array_constructor(tarraydef(p1.resultdef));
  2164. p1.destroy;
  2165. p1:=p2;
  2166. end
  2167. else
  2168. begin
  2169. Message2(scan_f_syn_expected,'CREATE',pattern);
  2170. p1.destroy;
  2171. p1:=cerrornode.create;
  2172. consume(_ID);
  2173. end;
  2174. end;
  2175. end
  2176. else
  2177. begin
  2178. Message(parser_e_invalid_qualifier);
  2179. p1.destroy;
  2180. p1:=cerrornode.create;
  2181. consume(_ID);
  2182. end;
  2183. end;
  2184. variantdef:
  2185. begin
  2186. { dispatch call? }
  2187. { lhs := v.ident[parameters] -> property get
  2188. lhs := v.ident(parameters) -> method call
  2189. v.ident[parameters] := rhs -> property put
  2190. v.ident(parameters) := rhs -> also property put }
  2191. if token=_ID then
  2192. begin
  2193. if not try_type_helper(p1,nil) then
  2194. begin
  2195. dispatchstring:=orgpattern;
  2196. consume(_ID);
  2197. calltype:=dct_method;
  2198. if try_to_consume(_LKLAMMER) then
  2199. begin
  2200. p2:=parse_paras(false,true,_RKLAMMER);
  2201. consume(_RKLAMMER);
  2202. end
  2203. else if try_to_consume(_LECKKLAMMER) then
  2204. begin
  2205. p2:=parse_paras(false,true,_RECKKLAMMER);
  2206. consume(_RECKKLAMMER);
  2207. calltype:=dct_propget;
  2208. end
  2209. else
  2210. p2:=nil;
  2211. { property setter? }
  2212. if (token=_ASSIGNMENT) and not(afterassignment) then
  2213. begin
  2214. consume(_ASSIGNMENT);
  2215. { read the expression }
  2216. p3:=comp_expr(true,false);
  2217. { concat value parameter too }
  2218. p2:=ccallparanode.create(p3,p2);
  2219. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  2220. end
  2221. else
  2222. { this is only an approximation
  2223. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  2224. if afterassignment or in_args or (token<>_SEMICOLON) then
  2225. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  2226. else
  2227. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  2228. end;
  2229. end
  2230. else { Error }
  2231. Consume(_ID);
  2232. end;
  2233. classrefdef:
  2234. begin
  2235. if token=_ID then
  2236. begin
  2237. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  2238. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2239. if assigned(srsym) then
  2240. begin
  2241. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2242. consume(_ID);
  2243. do_member_read(structh,getaddr,srsym,p1,again,[],nil);
  2244. end
  2245. else
  2246. begin
  2247. Message1(sym_e_id_no_member,orgpattern);
  2248. p1.destroy;
  2249. p1:=cerrornode.create;
  2250. { try to clean up }
  2251. consume(_ID);
  2252. end;
  2253. end
  2254. else { Error }
  2255. Consume(_ID);
  2256. end;
  2257. objectdef:
  2258. begin
  2259. if isspecialize or (token=_ID) then
  2260. begin
  2261. erroroutp1:=true;
  2262. srsym:=nil;
  2263. structh:=tobjectdef(p1.resultdef);
  2264. if isspecialize then
  2265. begin
  2266. { consume the "specialize" }
  2267. consume(_ID);
  2268. if token<>_ID then
  2269. consume(_ID)
  2270. else
  2271. begin
  2272. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2273. consume(_ID);
  2274. if handle_specialize_inline_specialization(srsym,srsymtable) then
  2275. erroroutp1:=false;
  2276. end;
  2277. end
  2278. else
  2279. begin
  2280. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2281. if assigned(srsym) then
  2282. begin
  2283. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2284. consume(_ID);
  2285. erroroutp1:=false;
  2286. end
  2287. else
  2288. begin
  2289. Message1(sym_e_id_no_member,orgpattern);
  2290. { try to clean up }
  2291. consume(_ID);
  2292. end;
  2293. end;
  2294. if erroroutp1 then
  2295. begin
  2296. p1.free;
  2297. p1:=cerrornode.create;
  2298. end
  2299. else
  2300. do_member_read(structh,getaddr,srsym,p1,again,[],nil);
  2301. end
  2302. else { Error }
  2303. Consume(_ID);
  2304. end;
  2305. pointerdef:
  2306. begin
  2307. if (p1.resultdef=objc_idtype) then
  2308. begin
  2309. { objc's id type can be used to call any
  2310. Objective-C method of any Objective-C class
  2311. type that's currently in scope }
  2312. if search_objc_method(pattern,srsym,srsymtable) then
  2313. begin
  2314. consume(_ID);
  2315. do_proc_call(srsym,srsymtable,nil,
  2316. (getaddr and not(token in [_CARET,_POINT])),
  2317. again,p1,[cnf_objc_id_call],nil);
  2318. { we need to know which procedure is called }
  2319. do_typecheckpass(p1);
  2320. end
  2321. else
  2322. begin
  2323. consume(_ID);
  2324. Message(parser_e_methode_id_expected);
  2325. end;
  2326. end
  2327. else
  2328. begin
  2329. if not try_type_helper(p1,nil) then
  2330. begin
  2331. Message(parser_e_invalid_qualifier);
  2332. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2333. Message(parser_h_maybe_deref_caret_missing);
  2334. end;
  2335. end
  2336. end;
  2337. else
  2338. begin
  2339. found:=try_type_helper(p1,nil);
  2340. if not found then
  2341. begin
  2342. if p1.resultdef.typ<>undefineddef then
  2343. Message(parser_e_invalid_qualifier);
  2344. p1.destroy;
  2345. p1:=cerrornode.create;
  2346. { Error }
  2347. consume(_ID);
  2348. end;
  2349. end;
  2350. end;
  2351. { processing an ordconstnode avoids the resultdef check }
  2352. skippointdefcheck:
  2353. end;
  2354. else
  2355. begin
  2356. { is this a procedure variable ? }
  2357. if assigned(p1.resultdef) and
  2358. (p1.resultdef.typ=procvardef) then
  2359. begin
  2360. { Typenode for typecasting or expecting a procvar }
  2361. if (p1.nodetype=typen) or
  2362. (
  2363. assigned(getprocvardef) and
  2364. equal_defs(p1.resultdef,getprocvardef)
  2365. ) then
  2366. begin
  2367. if try_to_consume(_LKLAMMER) then
  2368. begin
  2369. p1:=comp_expr(true,false);
  2370. consume(_RKLAMMER);
  2371. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2372. end
  2373. else
  2374. again:=false
  2375. end
  2376. else
  2377. begin
  2378. if try_to_consume(_LKLAMMER) then
  2379. begin
  2380. p2:=parse_paras(false,false,_RKLAMMER);
  2381. consume(_RKLAMMER);
  2382. p1:=ccallnode.create_procvar(p2,p1);
  2383. { proc():= is never possible }
  2384. if token=_ASSIGNMENT then
  2385. begin
  2386. Message(parser_e_illegal_expression);
  2387. p1.free;
  2388. p1:=cerrornode.create;
  2389. again:=false;
  2390. end;
  2391. end
  2392. else
  2393. again:=false;
  2394. end;
  2395. end
  2396. else
  2397. again:=false;
  2398. end;
  2399. end;
  2400. { we only try again if p1 was changed }
  2401. if again or
  2402. (p1.nodetype=errorn) then
  2403. result:=true;
  2404. end; { while again }
  2405. end;
  2406. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  2407. out memberparentdef: tdef): boolean;
  2408. var
  2409. hdef : tdef;
  2410. begin
  2411. result:=true;
  2412. memberparentdef:=nil;
  2413. case st.symtabletype of
  2414. ObjectSymtable,
  2415. recordsymtable:
  2416. begin
  2417. memberparentdef:=tdef(st.defowner);
  2418. exit;
  2419. end;
  2420. WithSymtable:
  2421. begin
  2422. if assigned(p1) then
  2423. internalerror(2007012002);
  2424. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  2425. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  2426. if not(hdef.typ in [objectdef,classrefdef]) then
  2427. exit;
  2428. if (hdef.typ=classrefdef) then
  2429. hdef:=tclassrefdef(hdef).pointeddef;
  2430. memberparentdef:=hdef;
  2431. end;
  2432. else
  2433. result:=false;
  2434. end;
  2435. end;
  2436. {$maxfpuregisters 0}
  2437. function factor(getaddr,typeonly:boolean) : tnode;
  2438. {---------------------------------------------
  2439. Factor_read_id
  2440. ---------------------------------------------}
  2441. procedure factor_read_id(out p1:tnode;var again:boolean);
  2442. function findwithsymtable : boolean;
  2443. var
  2444. hp : psymtablestackitem;
  2445. begin
  2446. result:=true;
  2447. hp:=symtablestack.stack;
  2448. while assigned(hp) do
  2449. begin
  2450. if hp^.symtable.symtabletype=withsymtable then
  2451. exit;
  2452. hp:=hp^.next;
  2453. end;
  2454. result:=false;
  2455. end;
  2456. var
  2457. srsym: tsym;
  2458. srsymtable: TSymtable;
  2459. hdef: tdef;
  2460. pd: tprocdef;
  2461. orgstoredpattern,
  2462. storedpattern: string;
  2463. callflags: tcallnodeflags;
  2464. t : ttoken;
  2465. allowspecialize,
  2466. isspecialize,
  2467. unit_found : boolean;
  2468. tokenpos: tfileposinfo;
  2469. begin
  2470. { allow post fix operators }
  2471. again:=true;
  2472. { preinitalize tokenpos }
  2473. tokenpos:=current_filepos;
  2474. p1:=nil;
  2475. allowspecialize:=not (m_delphi in current_settings.modeswitches) and (block_type in [bt_type,bt_var_type,bt_const_type]);
  2476. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  2477. begin
  2478. consume(_ID);
  2479. isspecialize:=true;
  2480. end
  2481. else
  2482. isspecialize:=false;
  2483. { first check for identifier }
  2484. if token<>_ID then
  2485. begin
  2486. srsym:=generrorsym;
  2487. srsymtable:=nil;
  2488. consume(_ID);
  2489. unit_found:=false;
  2490. end
  2491. else
  2492. begin
  2493. if typeonly then
  2494. searchsym_type(pattern,srsym,srsymtable)
  2495. else
  2496. searchsym(pattern,srsym,srsymtable);
  2497. { handle unit specification like System.Writeln }
  2498. if not isspecialize then
  2499. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true,allowspecialize,isspecialize)
  2500. else
  2501. begin
  2502. unit_found:=false;
  2503. t:=_ID;
  2504. end;
  2505. storedpattern:=pattern;
  2506. orgstoredpattern:=orgpattern;
  2507. { store the position of the token before consuming it }
  2508. tokenpos:=current_filepos;
  2509. consume(t);
  2510. { named parameter support }
  2511. found_arg_name:=false;
  2512. if not(unit_found) and
  2513. not isspecialize and
  2514. named_args_allowed and
  2515. (token=_ASSIGNMENT) then
  2516. begin
  2517. found_arg_name:=true;
  2518. p1:=cstringconstnode.createstr(storedpattern);
  2519. consume(_ASSIGNMENT);
  2520. exit;
  2521. end;
  2522. if isspecialize then
  2523. begin
  2524. if not assigned(srsym) or
  2525. (srsym.typ<>typesym) then
  2526. begin
  2527. identifier_not_found(orgstoredpattern,tokenpos);
  2528. srsym:=generrorsym;
  2529. srsymtable:=nil;
  2530. end
  2531. else
  2532. begin
  2533. hdef:=ttypesym(srsym).typedef;
  2534. generate_specialization(hdef,false,'');
  2535. if hdef=generrordef then
  2536. begin
  2537. srsym:=generrorsym;
  2538. srsymtable:=nil;
  2539. end
  2540. else
  2541. begin
  2542. srsym:=hdef.typesym;
  2543. srsymtable:=srsym.owner;
  2544. end;
  2545. end;
  2546. end;
  2547. { check hints, but only if it isn't a potential generic symbol;
  2548. that is checked in sub_expr if it isn't a generic }
  2549. if assigned(srsym) and
  2550. not (
  2551. (srsym.typ=typesym) and
  2552. (ttypesym(srsym).typedef.typ in [recorddef,objectdef,arraydef,procvardef,undefineddef]) and
  2553. not (sp_generic_para in srsym.symoptions) and
  2554. (token in [_LT, _LSHARPBRACKET])
  2555. ) then
  2556. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2557. { if nothing found give error and return errorsym }
  2558. if not assigned(srsym) or
  2559. { is this a generic dummy symbol? }
  2560. ((srsym.typ=typesym) and
  2561. assigned(ttypesym(srsym).typedef) and
  2562. (ttypesym(srsym).typedef.typ=undefineddef) and
  2563. not (sp_generic_para in srsym.symoptions) and
  2564. not (token in [_LT, _LSHARPBRACKET]) and
  2565. not (
  2566. { in non-Delphi modes the generic class' name without a
  2567. "specialization" or "<T>" may be used to identify the
  2568. current class }
  2569. (sp_generic_dummy in srsym.symoptions) and
  2570. assigned(current_structdef) and
  2571. (df_generic in current_structdef.defoptions) and
  2572. not (m_delphi in current_settings.modeswitches) and
  2573. assigned(get_generic_in_hierarchy_by_name(srsym,current_structdef))
  2574. )) and
  2575. { it could be a rename of a generic para }
  2576. { Note: if this generates false positives we'll need to
  2577. include a "basesym" to tsym to track the original
  2578. symbol }
  2579. not (sp_explicitrename in srsym.symoptions) then
  2580. begin
  2581. { if a generic is parsed and when we are inside an with block,
  2582. a symbol might not be defined }
  2583. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) and
  2584. findwithsymtable then
  2585. begin
  2586. { create dummy symbol, it will be freed later on }
  2587. srsym:=tsym.create(undefinedsym,'$undefinedsym');
  2588. srsymtable:=nil;
  2589. end
  2590. else
  2591. begin
  2592. identifier_not_found(orgstoredpattern,tokenpos);
  2593. srsym:=generrorsym;
  2594. srsymtable:=nil;
  2595. end;
  2596. end;
  2597. end;
  2598. { Access to funcret or need to call the function? }
  2599. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  2600. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  2601. { result(x) is not allowed }
  2602. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  2603. (
  2604. (token=_LKLAMMER) or
  2605. (
  2606. (([m_tp7,m_delphi,m_mac,m_iso] * current_settings.modeswitches) <> []) and
  2607. (afterassignment or in_args)
  2608. )
  2609. ) then
  2610. begin
  2611. hdef:=tdef(srsym.owner.defowner);
  2612. if assigned(hdef) and
  2613. (hdef.typ=procdef) then
  2614. srsym:=tprocdef(hdef).procsym
  2615. else
  2616. begin
  2617. Message(parser_e_illegal_expression);
  2618. srsym:=generrorsym;
  2619. end;
  2620. srsymtable:=srsym.owner;
  2621. end;
  2622. begin
  2623. case srsym.typ of
  2624. absolutevarsym :
  2625. begin
  2626. if (tabsolutevarsym(srsym).abstyp=tovar) then
  2627. begin
  2628. p1:=nil;
  2629. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  2630. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  2631. include(p1.flags,nf_absolute);
  2632. end
  2633. else
  2634. p1:=cloadnode.create(srsym,srsymtable);
  2635. end;
  2636. staticvarsym,
  2637. localvarsym,
  2638. paravarsym,
  2639. fieldvarsym :
  2640. begin
  2641. { check if we are reading a field of an object/class/ }
  2642. { record. is_member_read() will deal with withsymtables }
  2643. { if needed. }
  2644. p1:=nil;
  2645. if is_member_read(srsym,srsymtable,p1,hdef) then
  2646. begin
  2647. { if the field was originally found in an }
  2648. { objectsymtable, it means it's part of self }
  2649. { if only method from which it was called is }
  2650. { not class static }
  2651. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2652. { if we are accessing a owner procsym from the nested }
  2653. { class we need to call it as a class member }
  2654. if assigned(current_structdef) and
  2655. (((current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2656. (sp_static in srsym.symoptions)) then
  2657. if srsymtable.symtabletype=recordsymtable then
  2658. p1:=ctypenode.create(hdef)
  2659. else
  2660. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2661. else
  2662. begin
  2663. if assigned(current_procinfo) then
  2664. begin
  2665. pd:=current_procinfo.get_normal_proc.procdef;
  2666. if assigned(pd) and pd.no_self_node then
  2667. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  2668. else
  2669. p1:=load_self_node;
  2670. end
  2671. else
  2672. p1:=load_self_node;
  2673. end;
  2674. { now, if the field itself is part of an objectsymtab }
  2675. { (it can be even if it was found in a withsymtable, }
  2676. { e.g., "with classinstance do field := 5"), then }
  2677. { let do_member_read handle it }
  2678. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2679. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2680. else
  2681. { otherwise it's a regular record subscript }
  2682. p1:=csubscriptnode.create(srsym,p1);
  2683. end
  2684. else
  2685. { regular non-field load }
  2686. p1:=cloadnode.create(srsym,srsymtable);
  2687. end;
  2688. syssym :
  2689. begin
  2690. p1:=statement_syssym(tsyssym(srsym).number);
  2691. end;
  2692. typesym :
  2693. begin
  2694. hdef:=ttypesym(srsym).typedef;
  2695. if not assigned(hdef) then
  2696. begin
  2697. again:=false;
  2698. end
  2699. else
  2700. begin
  2701. { We need to know if this unit uses Variants }
  2702. if ((hdef=cvarianttype) or (hdef=colevarianttype)) and
  2703. not(cs_compilesystem in current_settings.moduleswitches) then
  2704. current_module.flags:=current_module.flags or uf_uses_variants;
  2705. p1:=handle_factor_typenode(hdef,getaddr,again,srsym,typeonly);
  2706. end;
  2707. end;
  2708. enumsym :
  2709. begin
  2710. p1:=genenumnode(tenumsym(srsym));
  2711. end;
  2712. constsym :
  2713. begin
  2714. if tconstsym(srsym).consttyp=constresourcestring then
  2715. begin
  2716. p1:=cloadnode.create(srsym,srsymtable);
  2717. do_typecheckpass(p1);
  2718. p1.resultdef:=getansistringdef;
  2719. end
  2720. else
  2721. p1:=genconstsymtree(tconstsym(srsym));
  2722. end;
  2723. procsym :
  2724. begin
  2725. p1:=nil;
  2726. { check if it's a method/class method }
  2727. if is_member_read(srsym,srsymtable,p1,hdef) then
  2728. begin
  2729. { if we are accessing a owner procsym from the nested }
  2730. { class we need to call it as a class member }
  2731. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) and
  2732. assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2733. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef));
  2734. { not srsymtable.symtabletype since that can be }
  2735. { withsymtable as well }
  2736. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2737. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2738. else
  2739. { no procsyms in records (yet) }
  2740. internalerror(2007012006);
  2741. end
  2742. else
  2743. begin
  2744. { regular procedure/function call }
  2745. if not unit_found then
  2746. callflags:=[]
  2747. else
  2748. callflags:=[cnf_unit_specified];
  2749. do_proc_call(srsym,srsymtable,nil,
  2750. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  2751. again,p1,callflags,nil);
  2752. end;
  2753. end;
  2754. propertysym :
  2755. begin
  2756. p1:=nil;
  2757. { property of a class/object? }
  2758. if is_member_read(srsym,srsymtable,p1,hdef) then
  2759. begin
  2760. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2761. { if we are accessing a owner procsym from the nested }
  2762. { class or from a static class method we need to call }
  2763. { it as a class member }
  2764. if (assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2765. (assigned(current_procinfo) and current_procinfo.get_normal_proc.procdef.no_self_node) then
  2766. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2767. else
  2768. p1:=load_self_node;
  2769. { not srsymtable.symtabletype since that can be }
  2770. { withsymtable as well }
  2771. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2772. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2773. else
  2774. { no propertysyms in records (yet) }
  2775. internalerror(2009111510);
  2776. end
  2777. else
  2778. { no method pointer }
  2779. begin
  2780. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  2781. end;
  2782. end;
  2783. labelsym :
  2784. begin
  2785. { Support @label }
  2786. if getaddr then
  2787. begin
  2788. if srsym.owner<>current_procinfo.procdef.localst then
  2789. CGMessage(parser_e_label_outside_proc);
  2790. p1:=cloadnode.create(srsym,srsym.owner)
  2791. end
  2792. else
  2793. begin
  2794. consume(_COLON);
  2795. if tlabelsym(srsym).defined then
  2796. Message(sym_e_label_already_defined);
  2797. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  2798. begin
  2799. tlabelsym(srsym).nonlocal:=true;
  2800. exclude(current_procinfo.procdef.procoptions,po_inline);
  2801. end;
  2802. if tlabelsym(srsym).nonlocal and
  2803. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  2804. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  2805. tlabelsym(srsym).defined:=true;
  2806. p1:=clabelnode.create(nil,tlabelsym(srsym));
  2807. tlabelsym(srsym).code:=p1;
  2808. end;
  2809. end;
  2810. undefinedsym :
  2811. begin
  2812. p1:=cnothingnode.Create;
  2813. p1.resultdef:=cundefineddef.create(true);
  2814. { clean up previously created dummy symbol }
  2815. srsym.free;
  2816. end;
  2817. errorsym :
  2818. begin
  2819. p1:=cerrornode.create;
  2820. if try_to_consume(_LKLAMMER) then
  2821. begin
  2822. parse_paras(false,false,_RKLAMMER);
  2823. consume(_RKLAMMER);
  2824. end;
  2825. end;
  2826. else
  2827. begin
  2828. p1:=cerrornode.create;
  2829. Message(parser_e_illegal_expression);
  2830. end;
  2831. end; { end case }
  2832. if assigned(p1) and (p1.nodetype<>errorn) then
  2833. p1.fileinfo:=tokenpos;
  2834. end;
  2835. end;
  2836. {---------------------------------------------
  2837. Factor_Read_Set
  2838. ---------------------------------------------}
  2839. { Read a set between [] }
  2840. function factor_read_set:tnode;
  2841. var
  2842. p1,p2 : tnode;
  2843. lastp,
  2844. buildp : tarrayconstructornode;
  2845. old_allow_array_constructor : boolean;
  2846. begin
  2847. buildp:=nil;
  2848. lastp:=nil;
  2849. { be sure that a least one arrayconstructn is used, also for an
  2850. empty [] }
  2851. if token=_RECKKLAMMER then
  2852. buildp:=carrayconstructornode.create(nil,buildp)
  2853. else
  2854. repeat
  2855. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  2856. old_allow_array_constructor:=allow_array_constructor;
  2857. allow_array_constructor:=false;
  2858. p1:=comp_expr(true,false);
  2859. if try_to_consume(_POINTPOINT) then
  2860. begin
  2861. p2:=comp_expr(true,false);
  2862. p1:=carrayconstructorrangenode.create(p1,p2);
  2863. end;
  2864. { insert at the end of the tree, to get the correct order }
  2865. if not assigned(buildp) then
  2866. begin
  2867. buildp:=carrayconstructornode.create(p1,nil);
  2868. lastp:=buildp;
  2869. end
  2870. else
  2871. begin
  2872. lastp.right:=carrayconstructornode.create(p1,nil);
  2873. lastp:=tarrayconstructornode(lastp.right);
  2874. end;
  2875. allow_array_constructor:=old_allow_array_constructor;
  2876. { there could be more elements }
  2877. until not try_to_consume(_COMMA);
  2878. factor_read_set:=buildp;
  2879. end;
  2880. function can_load_self_node: boolean;
  2881. begin
  2882. result:=false;
  2883. if (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  2884. not assigned(current_structdef) or
  2885. not assigned(current_procinfo) then
  2886. exit;
  2887. result:=not current_procinfo.get_normal_proc.procdef.no_self_node;
  2888. end;
  2889. {---------------------------------------------
  2890. Factor (Main)
  2891. ---------------------------------------------}
  2892. var
  2893. l : longint;
  2894. ic : int64;
  2895. qc : qword;
  2896. p1 : tnode;
  2897. code : integer;
  2898. srsym : tsym;
  2899. srsymtable : TSymtable;
  2900. pd : tprocdef;
  2901. hclassdef : tobjectdef;
  2902. d : bestreal;
  2903. hs,hsorg : string;
  2904. hdef : tdef;
  2905. filepos : tfileposinfo;
  2906. callflags : tcallnodeflags;
  2907. idstr : tidstring;
  2908. dopostfix,
  2909. again,
  2910. updatefpos,
  2911. nodechanged : boolean;
  2912. begin
  2913. { can't keep a copy of p1 and compare pointers afterwards, because
  2914. p1 may be freed and reallocated in the same place! }
  2915. dopostfix:=true;
  2916. updatefpos:=false;
  2917. p1:=nil;
  2918. filepos:=current_tokenpos;
  2919. again:=false;
  2920. pd:=nil;
  2921. if token=_ID then
  2922. begin
  2923. again:=true;
  2924. { Handle references to self }
  2925. if (idtoken=_SELF) and can_load_self_node then
  2926. begin
  2927. p1:=load_self_node;
  2928. consume(_ID);
  2929. again:=true;
  2930. end
  2931. else
  2932. factor_read_id(p1,again);
  2933. if assigned(p1) then
  2934. begin
  2935. { factor_read_id will set the filepos to after the id,
  2936. and in case of _SELF the filepos will already be the
  2937. same as filepos (so setting it again doesn't hurt). }
  2938. p1.fileinfo:=filepos;
  2939. filepos:=current_tokenpos;
  2940. end;
  2941. { handle post fix operators }
  2942. if (m_delphi in current_settings.modeswitches) and
  2943. (block_type=bt_body) and
  2944. (token in [_LT,_LSHARPBRACKET]) then
  2945. begin
  2946. if p1.nodetype=typen then
  2947. idstr:=ttypenode(p1).typesym.name
  2948. else
  2949. if (p1.nodetype=loadvmtaddrn) and
  2950. (tloadvmtaddrnode(p1).left.nodetype=typen) then
  2951. idstr:=ttypenode(tloadvmtaddrnode(p1).left).typesym.name
  2952. else
  2953. if (p1.nodetype=loadn) then
  2954. idstr:=tloadnode(p1).symtableentry.name
  2955. else
  2956. idstr:='';
  2957. { if this is the case then the postfix handling is done in
  2958. sub_expr if necessary }
  2959. dopostfix:=not could_be_generic(idstr);
  2960. end;
  2961. if dopostfix then
  2962. updatefpos:=postfixoperators(p1,again,getaddr);
  2963. end
  2964. else
  2965. begin
  2966. updatefpos:=true;
  2967. case token of
  2968. _RETURN :
  2969. begin
  2970. consume(_RETURN);
  2971. p1:=nil;
  2972. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2973. begin
  2974. p1:=comp_expr(true,false);
  2975. if not assigned(current_procinfo) or
  2976. (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
  2977. is_void(current_procinfo.procdef.returndef) then
  2978. begin
  2979. Message(parser_e_void_function);
  2980. { recovery }
  2981. p1.free;
  2982. p1:=nil;
  2983. end;
  2984. end;
  2985. p1 := cexitnode.create(p1);
  2986. end;
  2987. _INHERITED :
  2988. begin
  2989. again:=true;
  2990. consume(_INHERITED);
  2991. if assigned(current_procinfo) and
  2992. assigned(current_structdef) and
  2993. ((current_structdef.typ=objectdef) or
  2994. ((target_info.system in systems_jvm) and
  2995. (current_structdef.typ=recorddef)))then
  2996. begin
  2997. { for record helpers in mode Delphi "inherited" is not
  2998. allowed }
  2999. if is_objectpascal_helper(current_structdef) and
  3000. (m_delphi in current_settings.modeswitches) and
  3001. is_record(tobjectdef(current_structdef).extendeddef) then
  3002. Message(parser_e_inherited_not_in_record);
  3003. if (current_structdef.typ=objectdef) then
  3004. begin
  3005. hclassdef:=tobjectdef(current_structdef).childof;
  3006. { Objective-C categories *replace* methods in the class
  3007. they extend, or add methods to it. So calling an
  3008. inherited method always calls the method inherited from
  3009. the parent of the extended class }
  3010. if is_objccategory(current_structdef) then
  3011. hclassdef:=hclassdef.childof;
  3012. end
  3013. else if target_info.system in systems_jvm then
  3014. hclassdef:=java_fpcbaserecordtype
  3015. else
  3016. internalerror(2012012401);
  3017. { if inherited; only then we need the method with
  3018. the same name }
  3019. if token <> _ID then
  3020. begin
  3021. hs:=current_procinfo.procdef.procsym.name;
  3022. hsorg:=current_procinfo.procdef.procsym.realname;
  3023. anon_inherited:=true;
  3024. { For message methods we need to search using the message
  3025. number or string }
  3026. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  3027. srdef:=nil;
  3028. if (po_msgint in pd.procoptions) then
  3029. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  3030. else
  3031. if (po_msgstr in pd.procoptions) then
  3032. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  3033. else
  3034. { helpers have their own ways of dealing with inherited }
  3035. if is_objectpascal_helper(current_structdef) then
  3036. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3037. else
  3038. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3039. end
  3040. else
  3041. begin
  3042. hs:=pattern;
  3043. hsorg:=orgpattern;
  3044. consume(_ID);
  3045. anon_inherited:=false;
  3046. { helpers have their own ways of dealing with inherited }
  3047. if is_objectpascal_helper(current_structdef) then
  3048. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3049. else
  3050. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3051. end;
  3052. if assigned(srsym) then
  3053. begin
  3054. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  3055. { load the procdef from the inherited class and
  3056. not from self }
  3057. case srsym.typ of
  3058. procsym:
  3059. begin
  3060. if is_objectpascal_helper(current_structdef) then
  3061. begin
  3062. { for a helper load the procdef either from the
  3063. extended type, from the parent helper or from
  3064. the extended type of the parent helper
  3065. depending on the def the found symbol belongs
  3066. to }
  3067. if (srsym.Owner.defowner.typ=objectdef) and
  3068. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  3069. if def_is_related(current_structdef,tdef(srsym.Owner.defowner)) and
  3070. assigned(tobjectdef(current_structdef).childof) then
  3071. hdef:=tobjectdef(current_structdef).childof
  3072. else
  3073. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  3074. else
  3075. hdef:=tdef(srsym.Owner.defowner);
  3076. end
  3077. else
  3078. hdef:=hclassdef;
  3079. if (po_classmethod in current_procinfo.procdef.procoptions) or
  3080. (po_staticmethod in current_procinfo.procdef.procoptions) then
  3081. hdef:=cclassrefdef.create(hdef);
  3082. p1:=ctypenode.create(hdef);
  3083. { we need to allow helpers here }
  3084. ttypenode(p1).helperallowed:=true;
  3085. end;
  3086. propertysym:
  3087. ;
  3088. else
  3089. begin
  3090. Message(parser_e_methode_id_expected);
  3091. p1:=cerrornode.create;
  3092. end;
  3093. end;
  3094. callflags:=[cnf_inherited];
  3095. include(current_procinfo.flags,pi_has_inherited);
  3096. if anon_inherited then
  3097. include(callflags,cnf_anon_inherited);
  3098. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags,nil);
  3099. end
  3100. else
  3101. begin
  3102. if anon_inherited then
  3103. begin
  3104. { For message methods we need to call DefaultHandler }
  3105. if (po_msgint in pd.procoptions) or
  3106. (po_msgstr in pd.procoptions) then
  3107. begin
  3108. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,[ssf_search_helper]);
  3109. if not assigned(srsym) or
  3110. (srsym.typ<>procsym) then
  3111. internalerror(200303171);
  3112. p1:=nil;
  3113. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[],nil);
  3114. end
  3115. else
  3116. begin
  3117. { we need to ignore the inherited; }
  3118. p1:=cnothingnode.create;
  3119. end;
  3120. end
  3121. else
  3122. begin
  3123. Message1(sym_e_id_no_member,hsorg);
  3124. p1:=cerrornode.create;
  3125. end;
  3126. again:=false;
  3127. end;
  3128. { turn auto inheriting off }
  3129. anon_inherited:=false;
  3130. end
  3131. else
  3132. begin
  3133. { in case of records we use a more clear error message }
  3134. if assigned(current_structdef) and
  3135. (current_structdef.typ=recorddef) then
  3136. Message(parser_e_inherited_not_in_record)
  3137. else
  3138. Message(parser_e_generic_methods_only_in_methods);
  3139. again:=false;
  3140. p1:=cerrornode.create;
  3141. end;
  3142. postfixoperators(p1,again,getaddr);
  3143. end;
  3144. _INTCONST :
  3145. begin
  3146. {Try first wether the value fits in an int64.}
  3147. val(pattern,ic,code);
  3148. if code=0 then
  3149. begin
  3150. consume(_INTCONST);
  3151. int_to_type(ic,hdef);
  3152. p1:=cordconstnode.create(ic,hdef,true);
  3153. end
  3154. else
  3155. begin
  3156. { try qword next }
  3157. val(pattern,qc,code);
  3158. if code=0 then
  3159. begin
  3160. consume(_INTCONST);
  3161. int_to_type(qc,hdef);
  3162. p1:=cordconstnode.create(qc,hdef,true);
  3163. end;
  3164. end;
  3165. if code<>0 then
  3166. begin
  3167. { finally float }
  3168. val(pattern,d,code);
  3169. if code<>0 then
  3170. begin
  3171. Message(parser_e_invalid_integer);
  3172. consume(_INTCONST);
  3173. l:=1;
  3174. p1:=cordconstnode.create(l,sinttype,true);
  3175. end
  3176. else
  3177. begin
  3178. consume(_INTCONST);
  3179. p1:=crealconstnode.create(d,pbestrealtype^);
  3180. end;
  3181. end
  3182. else
  3183. { the necessary range checking has already been done by val }
  3184. tordconstnode(p1).rangecheck:=false;
  3185. if token=_POINT then
  3186. begin
  3187. again:=true;
  3188. postfixoperators(p1,again,getaddr);
  3189. end;
  3190. end;
  3191. _REALNUMBER :
  3192. begin
  3193. p1:=real_const_node_from_pattern(pattern);
  3194. consume(_REALNUMBER);
  3195. if token=_POINT then
  3196. begin
  3197. again:=true;
  3198. postfixoperators(p1,again,getaddr);
  3199. end;
  3200. end;
  3201. _STRING :
  3202. begin
  3203. string_dec(hdef,true);
  3204. { STRING can be also a type cast }
  3205. if try_to_consume(_LKLAMMER) then
  3206. begin
  3207. p1:=comp_expr(true,false);
  3208. consume(_RKLAMMER);
  3209. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3210. { handle postfix operators here e.g. string(a)[10] }
  3211. again:=true;
  3212. postfixoperators(p1,again,getaddr);
  3213. end
  3214. else
  3215. p1:=ctypenode.create(hdef);
  3216. end;
  3217. _FILE :
  3218. begin
  3219. hdef:=cfiletype;
  3220. consume(_FILE);
  3221. { FILE can be also a type cast }
  3222. if try_to_consume(_LKLAMMER) then
  3223. begin
  3224. p1:=comp_expr(true,false);
  3225. consume(_RKLAMMER);
  3226. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3227. { handle postfix operators here e.g. string(a)[10] }
  3228. again:=true;
  3229. postfixoperators(p1,again,getaddr);
  3230. end
  3231. else
  3232. begin
  3233. p1:=ctypenode.create(hdef);
  3234. end;
  3235. end;
  3236. _CSTRING :
  3237. begin
  3238. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern),nil);
  3239. consume(_CSTRING);
  3240. if token in postfixoperator_tokens then
  3241. begin
  3242. again:=true;
  3243. postfixoperators(p1,again,getaddr);
  3244. end;
  3245. end;
  3246. _CCHAR :
  3247. begin
  3248. p1:=cordconstnode.create(ord(pattern[1]),cansichartype,true);
  3249. consume(_CCHAR);
  3250. if token=_POINT then
  3251. begin
  3252. again:=true;
  3253. postfixoperators(p1,again,getaddr);
  3254. end;
  3255. end;
  3256. _CWSTRING:
  3257. begin
  3258. p1:=cstringconstnode.createunistr(patternw);
  3259. consume(_CWSTRING);
  3260. if token in postfixoperator_tokens then
  3261. begin
  3262. again:=true;
  3263. postfixoperators(p1,again,getaddr);
  3264. end;
  3265. end;
  3266. _CWCHAR:
  3267. begin
  3268. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  3269. consume(_CWCHAR);
  3270. if token=_POINT then
  3271. begin
  3272. again:=true;
  3273. postfixoperators(p1,again,getaddr);
  3274. end;
  3275. end;
  3276. _KLAMMERAFFE :
  3277. begin
  3278. consume(_KLAMMERAFFE);
  3279. got_addrn:=true;
  3280. { support both @<x> and @(<x>) }
  3281. if try_to_consume(_LKLAMMER) then
  3282. begin
  3283. p1:=factor(true,false);
  3284. { inside parentheses a full expression is allowed, see also tests\webtbs\tb27517.pp }
  3285. if token<>_RKLAMMER then
  3286. p1:=sub_expr(opcompare,true,false,p1);
  3287. consume(_RKLAMMER);
  3288. end
  3289. else
  3290. p1:=factor(true,false);
  3291. if token in postfixoperator_tokens then
  3292. begin
  3293. again:=true;
  3294. postfixoperators(p1,again,getaddr);
  3295. end;
  3296. got_addrn:=false;
  3297. p1:=caddrnode.create(p1);
  3298. p1.fileinfo:=filepos;
  3299. if cs_typed_addresses in current_settings.localswitches then
  3300. include(p1.flags,nf_typedaddr);
  3301. { Store the procvar that we are expecting, the
  3302. addrn will use the information to find the correct
  3303. procdef or it will return an error }
  3304. if assigned(getprocvardef) and
  3305. (taddrnode(p1).left.nodetype = loadn) then
  3306. taddrnode(p1).getprocvardef:=getprocvardef;
  3307. end;
  3308. _LKLAMMER :
  3309. begin
  3310. consume(_LKLAMMER);
  3311. p1:=comp_expr(true,false);
  3312. consume(_RKLAMMER);
  3313. { it's not a good solution
  3314. but (a+b)^ makes some problems }
  3315. if token in postfixoperator_tokens then
  3316. begin
  3317. again:=true;
  3318. postfixoperators(p1,again,getaddr);
  3319. end;
  3320. end;
  3321. _LECKKLAMMER :
  3322. begin
  3323. consume(_LECKKLAMMER);
  3324. p1:=factor_read_set;
  3325. consume(_RECKKLAMMER);
  3326. end;
  3327. _PLUS :
  3328. begin
  3329. consume(_PLUS);
  3330. p1:=factor(false,false);
  3331. p1:=cunaryplusnode.create(p1);
  3332. end;
  3333. _MINUS :
  3334. begin
  3335. consume(_MINUS);
  3336. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  3337. begin
  3338. { ugly hack, but necessary to be able to parse }
  3339. { -9223372036854775808 as int64 (JM) }
  3340. pattern := '-'+pattern;
  3341. p1:=sub_expr(oppower,false,false,nil);
  3342. { -1 ** 4 should be - (1 ** 4) and not
  3343. (-1) ** 4
  3344. This was the reason of tw0869.pp test failure PM }
  3345. if p1.nodetype=starstarn then
  3346. begin
  3347. if tbinarynode(p1).left.nodetype=ordconstn then
  3348. begin
  3349. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  3350. p1:=cunaryminusnode.create(p1);
  3351. end
  3352. else if tbinarynode(p1).left.nodetype=realconstn then
  3353. begin
  3354. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  3355. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  3356. p1:=cunaryminusnode.create(p1);
  3357. end
  3358. else
  3359. internalerror(20021029);
  3360. end;
  3361. end
  3362. else
  3363. begin
  3364. if m_isolike_unary_minus in current_settings.modeswitches then
  3365. p1:=sub_expr(opmultiply,false,false,nil)
  3366. else
  3367. p1:=sub_expr(oppower,false,false,nil);
  3368. p1:=cunaryminusnode.create(p1);
  3369. end;
  3370. end;
  3371. _OP_NOT :
  3372. begin
  3373. consume(_OP_NOT);
  3374. p1:=factor(false,false);
  3375. p1:=cnotnode.create(p1);
  3376. end;
  3377. _NIL :
  3378. begin
  3379. consume(_NIL);
  3380. p1:=cnilnode.create;
  3381. { It's really ugly code nil^, but delphi allows it }
  3382. if token in [_CARET,_POINT] then
  3383. begin
  3384. again:=true;
  3385. postfixoperators(p1,again,getaddr);
  3386. end;
  3387. end;
  3388. _OBJCPROTOCOL:
  3389. begin
  3390. { The @protocol keyword is used in two ways in Objective-C:
  3391. 1) to declare protocols (~ Object Pascal interfaces)
  3392. 2) to obtain the metaclass (~ Object Pascal) "class of")
  3393. of a declared protocol
  3394. This code is for handling the second case. Because of 1),
  3395. we cannot simply use a system unit symbol.
  3396. }
  3397. consume(_OBJCPROTOCOL);
  3398. consume(_LKLAMMER);
  3399. p1:=factor(false,false);
  3400. consume(_RKLAMMER);
  3401. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  3402. end;
  3403. else
  3404. begin
  3405. Message(parser_e_illegal_expression);
  3406. p1:=cerrornode.create;
  3407. { recover }
  3408. consume(token);
  3409. end;
  3410. end;
  3411. end;
  3412. { generate error node if no node is created }
  3413. if not assigned(p1) then
  3414. begin
  3415. {$ifdef EXTDEBUG}
  3416. Comment(V_Warning,'factor: p1=nil');
  3417. {$endif}
  3418. p1:=cerrornode.create;
  3419. updatefpos:=true;
  3420. end;
  3421. { get the resultdef for the node if nothing stops us }
  3422. if (not assigned(p1.resultdef)) and dopostfix then
  3423. begin
  3424. do_typecheckpass_changed(p1,nodechanged);
  3425. updatefpos:=updatefpos or nodechanged;
  3426. end;
  3427. if assigned(p1) and
  3428. updatefpos then
  3429. p1.fileinfo:=filepos;
  3430. factor:=p1;
  3431. end;
  3432. {$maxfpuregisters default}
  3433. procedure post_comp_expr_gendef(var def: tdef);
  3434. var
  3435. p1 : tnode;
  3436. again : boolean;
  3437. begin
  3438. if not assigned(def) then
  3439. internalerror(2011053001);
  3440. again:=false;
  3441. { handle potential typecasts, etc }
  3442. p1:=handle_factor_typenode(def,false,again,nil,false);
  3443. { parse postfix operators }
  3444. postfixoperators(p1,again,false);
  3445. if assigned(p1) and (p1.nodetype=typen) then
  3446. def:=ttypenode(p1).typedef
  3447. else
  3448. def:=generrordef;
  3449. end;
  3450. {****************************************************************************
  3451. Sub_Expr
  3452. ****************************************************************************}
  3453. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;
  3454. {Reads a subexpression while the operators are of the current precedence
  3455. level, or any higher level. Replaces the old term, simpl_expr and
  3456. simpl2_expr.}
  3457. function istypenode(n:tnode):boolean;inline;
  3458. { Checks whether the given node is a type node or a VMT node containing a
  3459. typenode. This is used in the code for inline specializations in the
  3460. _LT branch below }
  3461. begin
  3462. result:=assigned(n) and
  3463. (
  3464. (n.nodetype=typen) or
  3465. (
  3466. (n.nodetype=loadvmtaddrn) and
  3467. (tloadvmtaddrnode(n).left.nodetype=typen)
  3468. )
  3469. );
  3470. end;
  3471. function gettypedef(n:tnode):tdef;inline;
  3472. { This returns the typedef that belongs to the given typenode or
  3473. loadvmtaddrnode. n must not be Nil! }
  3474. begin
  3475. if n.nodetype=typen then
  3476. result:=ttypenode(n).typedef
  3477. else
  3478. result:=ttypenode(tloadvmtaddrnode(n).left).typedef;
  3479. end;
  3480. function getgenericsym(n:tnode;out srsym:tsym):boolean;
  3481. var
  3482. srsymtable : tsymtable;
  3483. begin
  3484. srsym:=nil;
  3485. case n.nodetype of
  3486. typen:
  3487. srsym:=ttypenode(n).typedef.typesym;
  3488. loadvmtaddrn:
  3489. srsym:=ttypenode(tloadvmtaddrnode(n).left).typedef.typesym;
  3490. loadn:
  3491. if not searchsym_with_symoption(tloadnode(n).symtableentry.Name,srsym,srsymtable,sp_generic_dummy) then
  3492. srsym:=nil;
  3493. { TODO : handle const nodes }
  3494. end;
  3495. result:=assigned(srsym);
  3496. end;
  3497. label
  3498. SubExprStart;
  3499. var
  3500. p1,p2 : tnode;
  3501. oldt : Ttoken;
  3502. filepos : tfileposinfo;
  3503. again : boolean;
  3504. gendef,parseddef : tdef;
  3505. gensym : tsym;
  3506. begin
  3507. SubExprStart:
  3508. if pred_level=highest_precedence then
  3509. begin
  3510. if factornode=nil then
  3511. p1:=factor(false,typeonly)
  3512. else
  3513. p1:=factornode;
  3514. end
  3515. else
  3516. p1:=sub_expr(succ(pred_level),true,typeonly,factornode);
  3517. repeat
  3518. if (token in [NOTOKEN..last_operator]) and
  3519. (token in operator_levels[pred_level]) and
  3520. ((token<>_EQ) or accept_equal) then
  3521. begin
  3522. oldt:=token;
  3523. filepos:=current_tokenpos;
  3524. consume(token);
  3525. if pred_level=highest_precedence then
  3526. p2:=factor(false,false)
  3527. else
  3528. p2:=sub_expr(succ(pred_level),true,typeonly,nil);
  3529. case oldt of
  3530. _PLUS :
  3531. p1:=caddnode.create(addn,p1,p2);
  3532. _MINUS :
  3533. p1:=caddnode.create(subn,p1,p2);
  3534. _STAR :
  3535. p1:=caddnode.create(muln,p1,p2);
  3536. _SLASH :
  3537. p1:=caddnode.create(slashn,p1,p2);
  3538. _EQ:
  3539. p1:=caddnode.create(equaln,p1,p2);
  3540. _GT :
  3541. p1:=caddnode.create(gtn,p1,p2);
  3542. _LT :
  3543. begin
  3544. { we need to decice whether we have an inline specialization
  3545. (type nodes to the left and right of "<", mode Delphi and
  3546. ">" or "," following) or a normal "<" comparison }
  3547. { TODO : p1 could be a non type if e.g. a variable with the
  3548. same name is defined in the same unit where the
  3549. generic is defined (though "same unit" is not
  3550. necessarily needed) }
  3551. if getgenericsym(p1,gensym) and
  3552. { Attention: when nested specializations are supported
  3553. p2 could be a loadn if a "<" follows }
  3554. istypenode(p2) and
  3555. (m_delphi in current_settings.modeswitches) and
  3556. { TODO : add _LT, _LSHARPBRACKET for nested specializations }
  3557. (token in [_GT,_RSHARPBRACKET,_COMMA]) then
  3558. begin
  3559. { this is an inline specialization }
  3560. { retrieve the defs of two nodes }
  3561. gendef:=nil;
  3562. parseddef:=gettypedef(p2);
  3563. if parseddef.typesym.typ<>typesym then
  3564. Internalerror(2011051001);
  3565. { check the hints for parseddef }
  3566. check_hints(parseddef.typesym,parseddef.typesym.symoptions,parseddef.typesym.deprecatedmsg);
  3567. { generate the specialization }
  3568. generate_specialization(gendef,false,'',parseddef,gensym.RealName,p2.fileinfo);
  3569. { we don't need the old left and right nodes anymore }
  3570. p1.Free;
  3571. p2.Free;
  3572. { in case of a class or a record the specialized generic
  3573. is always a classrefdef }
  3574. again:=false;
  3575. { handle potential typecasts, etc }
  3576. p1:=handle_factor_typenode(gendef,false,again,nil,false);
  3577. { parse postfix operators }
  3578. if postfixoperators(p1,again,false) then
  3579. if assigned(p1) then
  3580. p1.fileinfo:=filepos
  3581. else
  3582. p1:=cerrornode.create;
  3583. { with p1 now set we are in reality directly behind the
  3584. call to "factor" thus we need to call down to that
  3585. again }
  3586. { This is disabled until specializations on the right
  3587. hand side work as well, because
  3588. "not working expressions" is better than "half working
  3589. expressions" }
  3590. {factornode:=p1;
  3591. goto SubExprStart;}
  3592. end
  3593. else
  3594. begin
  3595. { this is a normal "<" comparison }
  3596. { potential generic types that are followed by a "<": }
  3597. { a) might not have their resultdef set }
  3598. if not assigned(p1.resultdef) then
  3599. do_typecheckpass(p1);
  3600. { b) are not checked whether they are an undefined def,
  3601. but not a generic parameter }
  3602. if (p1.nodetype=typen) and
  3603. (ttypenode(p1).typedef.typ=undefineddef) and
  3604. assigned(ttypenode(p1).typedef.typesym) and
  3605. not (sp_generic_para in ttypenode(p1).typedef.typesym.symoptions) then
  3606. begin
  3607. identifier_not_found(ttypenode(p1).typedef.typesym.RealName);
  3608. p1.Free;
  3609. p1:=cerrornode.create;
  3610. end;
  3611. { c) don't have their hints checked }
  3612. if istypenode(p1) then
  3613. begin
  3614. gendef:=gettypedef(p1);
  3615. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3616. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3617. end;
  3618. { Note: the second part of the expression will be needed
  3619. for nested specializations }
  3620. if istypenode(p2) {and
  3621. not (token in [_LT, _LSHARPBRACKET])} then
  3622. begin
  3623. gendef:=gettypedef(p2);
  3624. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3625. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3626. end;
  3627. { create the comparison node for "<" }
  3628. p1:=caddnode.create(ltn,p1,p2)
  3629. end;
  3630. end;
  3631. _GTE :
  3632. p1:=caddnode.create(gten,p1,p2);
  3633. _LTE :
  3634. p1:=caddnode.create(lten,p1,p2);
  3635. _SYMDIF :
  3636. p1:=caddnode.create(symdifn,p1,p2);
  3637. _STARSTAR :
  3638. p1:=caddnode.create(starstarn,p1,p2);
  3639. _OP_AS,
  3640. _OP_IS :
  3641. begin
  3642. if token in [_LT, _LSHARPBRACKET] then
  3643. begin
  3644. { for now we're handling this as a generic declaration;
  3645. there could be cases though (because of operator
  3646. overloading) where this is the wrong decision... }
  3647. { TODO : here the same note as in _LT applies as p2 could
  3648. point to a variable, etc }
  3649. gendef:=gettypedef(p2);
  3650. if gendef.typesym.typ<>typesym then
  3651. Internalerror(2011071401);
  3652. { generate the specialization }
  3653. generate_specialization(gendef,false,'');
  3654. { we don't need the old p2 anymore }
  3655. p2.Free;
  3656. again:=false;
  3657. { handle potential typecasts, etc }
  3658. p2:=handle_factor_typenode(gendef,false,again,nil,false);
  3659. { parse postfix operators }
  3660. if postfixoperators(p2,again,false) then
  3661. if assigned(p2) then
  3662. p2.fileinfo:=filepos
  3663. else
  3664. p2:=cerrornode.create;
  3665. { here we don't need to call back down to "factor", thus
  3666. no "goto" }
  3667. end;
  3668. { now generate the "is" or "as" node }
  3669. case oldt of
  3670. _OP_AS:
  3671. p1:=casnode.create(p1,p2);
  3672. _OP_IS:
  3673. p1:=cisnode.create(p1,p2);
  3674. end;
  3675. end;
  3676. _OP_IN :
  3677. p1:=cinnode.create(p1,p2);
  3678. _OP_OR,
  3679. _PIPE {macpas only} :
  3680. begin
  3681. p1:=caddnode.create(orn,p1,p2);
  3682. if (oldt = _PIPE) then
  3683. include(p1.flags,nf_short_bool);
  3684. end;
  3685. _OP_AND,
  3686. _AMPERSAND {macpas only} :
  3687. begin
  3688. p1:=caddnode.create(andn,p1,p2);
  3689. if (oldt = _AMPERSAND) then
  3690. include(p1.flags,nf_short_bool);
  3691. end;
  3692. _OP_DIV :
  3693. p1:=cmoddivnode.create(divn,p1,p2);
  3694. _OP_NOT :
  3695. p1:=cnotnode.create(p1);
  3696. _OP_MOD :
  3697. begin
  3698. p1:=cmoddivnode.create(modn,p1,p2);
  3699. if m_iso in current_settings.modeswitches then
  3700. include(p1.flags,nf_isomod);
  3701. end;
  3702. _OP_SHL :
  3703. p1:=cshlshrnode.create(shln,p1,p2);
  3704. _OP_SHR :
  3705. p1:=cshlshrnode.create(shrn,p1,p2);
  3706. _OP_XOR :
  3707. p1:=caddnode.create(xorn,p1,p2);
  3708. _ASSIGNMENT :
  3709. p1:=cassignmentnode.create(p1,p2);
  3710. _NE :
  3711. p1:=caddnode.create(unequaln,p1,p2);
  3712. end;
  3713. p1.fileinfo:=filepos;
  3714. end
  3715. else
  3716. break;
  3717. until false;
  3718. sub_expr:=p1;
  3719. end;
  3720. function comp_expr(accept_equal,typeonly:boolean):tnode;
  3721. var
  3722. oldafterassignment : boolean;
  3723. p1 : tnode;
  3724. begin
  3725. oldafterassignment:=afterassignment;
  3726. afterassignment:=true;
  3727. p1:=sub_expr(opcompare,accept_equal,typeonly,nil);
  3728. { get the resultdef for this expression }
  3729. if not assigned(p1.resultdef) then
  3730. do_typecheckpass(p1);
  3731. afterassignment:=oldafterassignment;
  3732. comp_expr:=p1;
  3733. end;
  3734. function expr(dotypecheck : boolean) : tnode;
  3735. var
  3736. p1,p2 : tnode;
  3737. filepos : tfileposinfo;
  3738. oldafterassignment,
  3739. updatefpos : boolean;
  3740. begin
  3741. oldafterassignment:=afterassignment;
  3742. p1:=sub_expr(opcompare,true,false,nil);
  3743. { get the resultdef for this expression }
  3744. if not assigned(p1.resultdef) and
  3745. dotypecheck then
  3746. do_typecheckpass(p1);
  3747. filepos:=current_tokenpos;
  3748. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  3749. afterassignment:=true;
  3750. updatefpos:=true;
  3751. case token of
  3752. _POINTPOINT :
  3753. begin
  3754. consume(_POINTPOINT);
  3755. p2:=sub_expr(opcompare,true,false,nil);
  3756. p1:=crangenode.create(p1,p2);
  3757. end;
  3758. _ASSIGNMENT :
  3759. begin
  3760. consume(_ASSIGNMENT);
  3761. if assigned(p1.resultdef) and (p1.resultdef.typ=procvardef) then
  3762. getprocvardef:=tprocvardef(p1.resultdef);
  3763. p2:=sub_expr(opcompare,true,false,nil);
  3764. if assigned(getprocvardef) then
  3765. handle_procvar(getprocvardef,p2);
  3766. getprocvardef:=nil;
  3767. p1:=cassignmentnode.create(p1,p2);
  3768. end;
  3769. _PLUSASN :
  3770. begin
  3771. consume(_PLUSASN);
  3772. p2:=sub_expr(opcompare,true,false,nil);
  3773. p1:=gen_c_style_operator(addn,p1,p2);
  3774. end;
  3775. _MINUSASN :
  3776. begin
  3777. consume(_MINUSASN);
  3778. p2:=sub_expr(opcompare,true,false,nil);
  3779. p1:=gen_c_style_operator(subn,p1,p2);
  3780. end;
  3781. _STARASN :
  3782. begin
  3783. consume(_STARASN );
  3784. p2:=sub_expr(opcompare,true,false,nil);
  3785. p1:=gen_c_style_operator(muln,p1,p2);
  3786. end;
  3787. _SLASHASN :
  3788. begin
  3789. consume(_SLASHASN );
  3790. p2:=sub_expr(opcompare,true,false,nil);
  3791. p1:=gen_c_style_operator(slashn,p1,p2);
  3792. end;
  3793. else
  3794. updatefpos:=false;
  3795. end;
  3796. { get the resultdef for this expression }
  3797. if not assigned(p1.resultdef) and
  3798. dotypecheck then
  3799. do_typecheckpass(p1);
  3800. afterassignment:=oldafterassignment;
  3801. if updatefpos then
  3802. p1.fileinfo:=filepos;
  3803. expr:=p1;
  3804. end;
  3805. function get_intconst:TConstExprInt;
  3806. {Reads an expression, tries to evalute it and check if it is an integer
  3807. constant. Then the constant is returned.}
  3808. var
  3809. p:tnode;
  3810. begin
  3811. result:=0;
  3812. p:=comp_expr(true,false);
  3813. if not codegenerror then
  3814. begin
  3815. if (p.nodetype<>ordconstn) or
  3816. not(is_integer(p.resultdef)) then
  3817. Message(parser_e_illegal_expression)
  3818. else
  3819. result:=tordconstnode(p).value;
  3820. end;
  3821. p.free;
  3822. end;
  3823. function get_stringconst:string;
  3824. {Reads an expression, tries to evaluate it and checks if it is a string
  3825. constant. Then the constant is returned.}
  3826. var
  3827. p:tnode;
  3828. begin
  3829. get_stringconst:='';
  3830. p:=comp_expr(true,false);
  3831. if p.nodetype<>stringconstn then
  3832. begin
  3833. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  3834. get_stringconst:=char(int64(tordconstnode(p).value))
  3835. else
  3836. Message(parser_e_illegal_expression);
  3837. end
  3838. else
  3839. get_stringconst:=strpas(tstringconstnode(p).value_str);
  3840. p.free;
  3841. end;
  3842. end.