pexpr.pas 170 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276
  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,hadspecialize: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),true);
  99. end
  100. else
  101. if tordconstnode(p).value<>255 then
  102. def:=cstringdef.createshort(int64(tordconstnode(p).value),true);
  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:=cansistringtype
  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,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;out spezcontext:tspecializationcontext):boolean;
  1292. var
  1293. spezdef : tdef;
  1294. begin
  1295. result:=false;
  1296. spezcontext:=nil;
  1297. srsymtable:=nil;
  1298. if not assigned(srsym) then
  1299. message1(sym_e_id_no_member,orgpattern)
  1300. else
  1301. if not (srsym.typ in [typesym,procsym]) then
  1302. message(type_e_type_id_expected)
  1303. else
  1304. begin
  1305. if srsym.typ=typesym then
  1306. spezdef:=ttypesym(srsym).typedef
  1307. else
  1308. spezdef:=tdef(tprocsym(srsym).procdeflist[0]);
  1309. spezdef:=generate_specialization_phase1(spezcontext,spezdef);
  1310. case spezdef.typ of
  1311. errordef:
  1312. begin
  1313. spezcontext.free;
  1314. spezcontext:=nil;
  1315. srsym:=generrorsym;
  1316. end;
  1317. procdef:
  1318. begin
  1319. if block_type<>bt_body then
  1320. begin
  1321. message(parser_e_illegal_expression);
  1322. spezcontext.free;
  1323. spezcontext:=nil;
  1324. srsym:=generrorsym;
  1325. end
  1326. else
  1327. begin
  1328. srsym:=tprocdef(spezdef).procsym;
  1329. srsymtable:=srsym.owner;
  1330. result:=true;
  1331. end;
  1332. end;
  1333. objectdef,
  1334. recorddef,
  1335. arraydef,
  1336. procvardef:
  1337. begin
  1338. spezdef:=generate_specialization_phase2(spezcontext,tstoreddef(spezdef),false,'');
  1339. spezcontext.free;
  1340. spezcontext:=nil;
  1341. srsym:=spezdef.typesym;
  1342. srsymtable:=srsym.owner;
  1343. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1344. result:=true;
  1345. end;
  1346. else
  1347. internalerror(2015070302);
  1348. end;
  1349. end;
  1350. end;
  1351. function handle_factor_typenode(hdef:tdef;getaddr:boolean;var again:boolean;sym:tsym;typeonly:boolean):tnode;
  1352. var
  1353. srsym : tsym;
  1354. srsymtable : tsymtable;
  1355. isspecialize : boolean;
  1356. spezcontext : tspecializationcontext;
  1357. begin
  1358. spezcontext:=nil;
  1359. if sym=nil then
  1360. sym:=hdef.typesym;
  1361. { allow Ordinal(Value) for type declarations since it
  1362. can be an enummeration declaration or a set lke:
  1363. (OrdinalType(const1)..OrdinalType(const2) }
  1364. if (not typeonly or is_ordinal(hdef)) and
  1365. try_to_consume(_LKLAMMER) then
  1366. begin
  1367. result:=comp_expr(true,false);
  1368. consume(_RKLAMMER);
  1369. { type casts to class helpers aren't allowed }
  1370. if is_objectpascal_helper(hdef) then
  1371. Message(parser_e_no_category_as_types)
  1372. { recovery by not creating a conversion node }
  1373. else
  1374. result:=ctypeconvnode.create_explicit(result,hdef);
  1375. end
  1376. { not LKLAMMER }
  1377. else if (token=_POINT) and
  1378. (is_object(hdef) or is_record(hdef)) then
  1379. begin
  1380. consume(_POINT);
  1381. { handles calling methods declared in parent objects
  1382. using "parentobject.methodname()" }
  1383. if assigned(current_structdef) and
  1384. not(getaddr) and
  1385. def_is_related(current_structdef,hdef) then
  1386. begin
  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. { search also in inherited methods }
  1402. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,[ssf_search_helper]);
  1403. if isspecialize then
  1404. begin
  1405. consume(_ID);
  1406. if not handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  1407. begin
  1408. result.free;
  1409. result:=cerrornode.create;
  1410. end;
  1411. end
  1412. else
  1413. begin
  1414. if assigned(srsym) then
  1415. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1416. consume(_ID);
  1417. end;
  1418. if result.nodetype<>errorn then
  1419. do_member_read(tabstractrecorddef(hdef),false,srsym,result,again,[],spezcontext)
  1420. else
  1421. spezcontext.free;
  1422. end
  1423. else
  1424. begin
  1425. { handles:
  1426. * @TObject.Load
  1427. * static methods and variables }
  1428. result:=ctypenode.create(hdef);
  1429. ttypenode(result).typesym:=sym;
  1430. if not (m_delphi in current_settings.modeswitches) and
  1431. (block_type in [bt_type,bt_var_type,bt_const_type]) and
  1432. (token=_ID) and
  1433. (idtoken=_SPECIALIZE) then
  1434. begin
  1435. consume(_ID);
  1436. if token<>_ID then
  1437. message(type_e_type_id_expected);
  1438. isspecialize:=true;
  1439. end
  1440. else
  1441. isspecialize:=false;
  1442. { TP allows also @TMenu.Load if Load is only }
  1443. { defined in an anchestor class }
  1444. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1445. if isspecialize then
  1446. begin
  1447. consume(_ID);
  1448. if not handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  1449. begin
  1450. result.free;
  1451. result:=cerrornode.create;
  1452. end;
  1453. end
  1454. else
  1455. begin
  1456. if assigned(srsym) then
  1457. begin
  1458. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1459. consume(_ID);
  1460. end
  1461. else
  1462. Message1(sym_e_id_no_member,orgpattern);
  1463. end;
  1464. if (result.nodetype<>errorn) and assigned(srsym) then
  1465. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],spezcontext)
  1466. else
  1467. spezcontext.free;
  1468. end;
  1469. end
  1470. else
  1471. begin
  1472. { Normally here would be the check against the usage
  1473. of "TClassHelper.Something", but as that might be
  1474. used inside of system symbols like sizeof and
  1475. typeinfo this check is put into ttypenode.pass_1
  1476. (for "TClassHelper" alone) and tcallnode.pass_1
  1477. (for "TClassHelper.Something") }
  1478. { class reference ? }
  1479. if is_class(hdef) or
  1480. is_objcclass(hdef) or
  1481. { Java interfaces also can have loadvmtaddrnodes,
  1482. e.g. for expressions such as JLClass(intftype) }
  1483. is_java_class_or_interface(hdef) then
  1484. begin
  1485. if getaddr and (token=_POINT) and
  1486. not is_javainterface(hdef) then
  1487. begin
  1488. consume(_POINT);
  1489. { allows @Object.Method }
  1490. { also allows static methods and variables }
  1491. result:=ctypenode.create(hdef);
  1492. ttypenode(result).typesym:=sym;
  1493. { TP allows also @TMenu.Load if Load is only }
  1494. { defined in an anchestor class }
  1495. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1496. if assigned(srsym) then
  1497. begin
  1498. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1499. consume(_ID);
  1500. { in case of @Object.Method1.Method2, we have to call
  1501. Method1 -> create a loadvmtaddr node as self instead of
  1502. a typen (the typenode would be changed to self of the
  1503. current method in case Method1 is a constructor, see
  1504. mantis #24844) }
  1505. if not(block_type in [bt_type,bt_const_type,bt_var_type]) and
  1506. (srsym.typ=procsym) and
  1507. (token in [_CARET,_POINT]) then
  1508. result:=cloadvmtaddrnode.create(result);
  1509. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],nil);
  1510. end
  1511. else
  1512. begin
  1513. Message1(sym_e_id_no_member,orgpattern);
  1514. consume(_ID);
  1515. end;
  1516. end
  1517. else
  1518. begin
  1519. result:=ctypenode.create(hdef);
  1520. ttypenode(result).typesym:=sym;
  1521. { For a type block we simply return only
  1522. the type. For all other blocks we return
  1523. a loadvmt node }
  1524. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1525. result:=cloadvmtaddrnode.create(result);
  1526. end;
  1527. end
  1528. else
  1529. begin
  1530. result:=ctypenode.create(hdef);
  1531. ttypenode(result).typesym:=sym;
  1532. end;
  1533. end;
  1534. end;
  1535. {****************************************************************************
  1536. Factor
  1537. ****************************************************************************}
  1538. function real_const_node_from_pattern(s:string):tnode;
  1539. var
  1540. d : bestreal;
  1541. code : integer;
  1542. cur : currency;
  1543. begin
  1544. val(s,d,code);
  1545. if code<>0 then
  1546. begin
  1547. Message(parser_e_error_in_real);
  1548. d:=1.0;
  1549. end;
  1550. if current_settings.fputype=fpu_none then
  1551. Message(parser_e_unsupported_real);
  1552. if (current_settings.minfpconstprec=s32real) and
  1553. (d = single(d)) then
  1554. result:=crealconstnode.create(d,s32floattype)
  1555. else if (current_settings.minfpconstprec=s64real) and
  1556. (d = double(d)) then
  1557. result:=crealconstnode.create(d,s64floattype)
  1558. else
  1559. result:=crealconstnode.create(d,pbestrealtype^);
  1560. val(pattern,cur,code);
  1561. if code=0 then
  1562. trealconstnode(result).value_currency:=cur;
  1563. end;
  1564. {---------------------------------------------
  1565. PostFixOperators
  1566. ---------------------------------------------}
  1567. { returns whether or not p1 has been changed }
  1568. function postfixoperators(var p1:tnode;var again:boolean;getaddr:boolean): boolean;
  1569. { tries to avoid syntax errors after invalid qualifiers }
  1570. procedure recoverconsume_postfixops;
  1571. begin
  1572. repeat
  1573. if not try_to_consume(_CARET) then
  1574. if try_to_consume(_POINT) then
  1575. try_to_consume(_ID)
  1576. else if try_to_consume(_LECKKLAMMER) then
  1577. begin
  1578. repeat
  1579. comp_expr(true,false);
  1580. until not try_to_consume(_COMMA);
  1581. consume(_RECKKLAMMER);
  1582. end
  1583. else if try_to_consume(_LKLAMMER) then
  1584. begin
  1585. repeat
  1586. comp_expr(true,false);
  1587. until not try_to_consume(_COMMA);
  1588. consume(_RKLAMMER);
  1589. end
  1590. else
  1591. break;
  1592. until false;
  1593. end;
  1594. procedure handle_variantarray;
  1595. var
  1596. p4 : tnode;
  1597. newstatement : tstatementnode;
  1598. tempresultvariant,
  1599. temp : ttempcreatenode;
  1600. paras : tcallparanode;
  1601. newblock : tnode;
  1602. countindices : aint;
  1603. begin
  1604. { create statements with call initialize the arguments and
  1605. call fpc_dynarr_setlength }
  1606. newblock:=internalstatements(newstatement);
  1607. { get temp for array of indicies,
  1608. we set the real size later }
  1609. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1610. addstatement(newstatement,temp);
  1611. countindices:=0;
  1612. repeat
  1613. p4:=comp_expr(true,false);
  1614. addstatement(newstatement,cassignmentnode.create(
  1615. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1616. inc(countindices);
  1617. until not try_to_consume(_COMMA);
  1618. { set real size }
  1619. temp.size:=countindices*s32inttype.size;
  1620. consume(_RECKKLAMMER);
  1621. { we need only a write access if a := follows }
  1622. if token=_ASSIGNMENT then
  1623. begin
  1624. consume(_ASSIGNMENT);
  1625. p4:=comp_expr(true,false);
  1626. { create call to fpc_vararray_put }
  1627. paras:=ccallparanode.create(cordconstnode.create
  1628. (countindices,s32inttype,true),
  1629. ccallparanode.create(caddrnode.create_internal
  1630. (ctemprefnode.create(temp)),
  1631. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1632. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1633. ,nil))));
  1634. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1635. addstatement(newstatement,ctempdeletenode.create(temp));
  1636. end
  1637. else
  1638. begin
  1639. { create temp for result }
  1640. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1641. addstatement(newstatement,tempresultvariant);
  1642. { create call to fpc_vararray_get }
  1643. paras:=ccallparanode.create(cordconstnode.create
  1644. (countindices,s32inttype,true),
  1645. ccallparanode.create(caddrnode.create_internal
  1646. (ctemprefnode.create(temp)),
  1647. ccallparanode.create(p1,
  1648. ccallparanode.create(
  1649. ctemprefnode.create(tempresultvariant)
  1650. ,nil))));
  1651. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1652. addstatement(newstatement,ctempdeletenode.create(temp));
  1653. { the last statement should return the value as
  1654. location and type, this is done be referencing the
  1655. temp and converting it first from a persistent temp to
  1656. normal temp }
  1657. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1658. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1659. end;
  1660. p1:=newblock;
  1661. end;
  1662. function parse_array_constructor(arrdef:tarraydef): tnode;
  1663. var
  1664. newstatement,assstatement:tstatementnode;
  1665. arrnode:ttempcreatenode;
  1666. temp2:ttempcreatenode;
  1667. assnode:tnode;
  1668. paracount:integer;
  1669. begin
  1670. result:=internalstatements(newstatement);
  1671. { create temp for result }
  1672. arrnode:=ctempcreatenode.create(arrdef,arrdef.size,tt_persistent,true);
  1673. addstatement(newstatement,arrnode);
  1674. paracount:=0;
  1675. { check arguments and create an assignment calls }
  1676. if try_to_consume(_LKLAMMER) then
  1677. begin
  1678. assnode:=internalstatements(assstatement);
  1679. repeat
  1680. { arr[i] := param_i }
  1681. addstatement(assstatement,
  1682. cassignmentnode.create(
  1683. cvecnode.create(
  1684. ctemprefnode.create(arrnode),
  1685. cordconstnode.create(paracount,arrdef.rangedef,false)),
  1686. comp_expr(true,false)));
  1687. inc(paracount);
  1688. until not try_to_consume(_COMMA);
  1689. consume(_RKLAMMER);
  1690. end
  1691. else
  1692. assnode:=nil;
  1693. { get temp for array of lengths }
  1694. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1695. addstatement(newstatement,temp2);
  1696. { one dimensional }
  1697. addstatement(newstatement,cassignmentnode.create(
  1698. ctemprefnode.create_offset(temp2,0),
  1699. cordconstnode.create
  1700. (paracount,s32inttype,true)));
  1701. { create call to fpc_dynarr_setlength }
  1702. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1703. ccallparanode.create(caddrnode.create_internal
  1704. (ctemprefnode.create(temp2)),
  1705. ccallparanode.create(cordconstnode.create
  1706. (1,s32inttype,true),
  1707. ccallparanode.create(caddrnode.create_internal
  1708. (crttinode.create(tstoreddef(arrdef),initrtti,rdt_normal)),
  1709. ccallparanode.create(
  1710. ctypeconvnode.create_internal(
  1711. ctemprefnode.create(arrnode),voidpointertype),
  1712. nil))))
  1713. ));
  1714. { add assignment statememnts }
  1715. addstatement(newstatement,ctempdeletenode.create(temp2));
  1716. if assigned(assnode) then
  1717. addstatement(newstatement,assnode);
  1718. { the last statement should return the value as
  1719. location and type, this is done be referencing the
  1720. temp and converting it first from a persistent temp to
  1721. normal temp }
  1722. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1723. addstatement(newstatement,ctemprefnode.create(arrnode));
  1724. end;
  1725. function try_type_helper(var node:tnode;def:tdef):boolean;
  1726. var
  1727. srsym : tsym;
  1728. srsymtable : tsymtable;
  1729. n : tnode;
  1730. newstatement : tstatementnode;
  1731. temp : ttempcreatenode;
  1732. extdef : tdef;
  1733. begin
  1734. result:=false;
  1735. if (token=_ID) and (block_type in [bt_body,bt_general,bt_except,bt_const]) then
  1736. begin
  1737. if not assigned(def) then
  1738. if node.nodetype=addrn then
  1739. { always use the pointer type for addr nodes as otherwise
  1740. we'll have an anonymous pointertype with no name }
  1741. def:=voidpointertype
  1742. else
  1743. def:=node.resultdef;
  1744. result:=search_objectpascal_helper(def,nil,pattern,srsym,srsymtable);
  1745. if result then
  1746. begin
  1747. if not (srsymtable.symtabletype=objectsymtable) or
  1748. not is_objectpascal_helper(tdef(srsymtable.defowner)) then
  1749. internalerror(2013011401);
  1750. { convert const node to temp node of the extended type }
  1751. if node.nodetype in (nodetype_const+[niln,addrn]) then
  1752. begin
  1753. extdef:=tobjectdef(srsymtable.defowner).extendeddef;
  1754. newstatement:=nil;
  1755. n:=internalstatements(newstatement);
  1756. temp:=ctempcreatenode.create(extdef,extdef.size,tt_persistent,false);
  1757. addstatement(newstatement,temp);
  1758. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),node));
  1759. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1760. addstatement(newstatement,ctemprefnode.create(temp));
  1761. node:=n;
  1762. do_typecheckpass(node)
  1763. end;
  1764. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1765. consume(_ID);
  1766. do_member_read(nil,getaddr,srsym,node,again,[],nil);
  1767. end;
  1768. end;
  1769. end;
  1770. var
  1771. protsym : tpropertysym;
  1772. p2,p3 : tnode;
  1773. srsym : tsym;
  1774. srsymtable : TSymtable;
  1775. structh : tabstractrecorddef;
  1776. { shouldn't be used that often, so the extra overhead is ok to save
  1777. stack space }
  1778. dispatchstring : ansistring;
  1779. erroroutp1,
  1780. allowspecialize,
  1781. isspecialize,
  1782. found,
  1783. haderror,
  1784. nodechanged : boolean;
  1785. calltype: tdispcalltype;
  1786. valstr,expstr : string;
  1787. intval : qword;
  1788. code : integer;
  1789. strdef : tdef;
  1790. spezcontext : tspecializationcontext;
  1791. old_current_filepos : tfileposinfo;
  1792. label
  1793. skipreckklammercheck,
  1794. skippointdefcheck;
  1795. begin
  1796. result:=false;
  1797. again:=true;
  1798. while again do
  1799. begin
  1800. spezcontext:=nil;
  1801. { we need the resultdef }
  1802. do_typecheckpass_changed(p1,nodechanged);
  1803. result:=result or nodechanged;
  1804. if codegenerror then
  1805. begin
  1806. recoverconsume_postfixops;
  1807. exit;
  1808. end;
  1809. { handle token }
  1810. case token of
  1811. _CARET:
  1812. begin
  1813. consume(_CARET);
  1814. { support tp/mac procvar^ if the procvar returns a
  1815. pointer type }
  1816. if ((m_tp_procvar in current_settings.modeswitches) or
  1817. (m_mac_procvar in current_settings.modeswitches)) and
  1818. (p1.resultdef.typ=procvardef) and
  1819. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1820. begin
  1821. p1:=ccallnode.create_procvar(nil,p1);
  1822. typecheckpass(p1);
  1823. end;
  1824. { iso file buf access? }
  1825. if (m_iso in current_settings.modeswitches) and
  1826. (p1.resultdef.typ=filedef) then
  1827. begin
  1828. case tfiledef(p1.resultdef).filetyp of
  1829. ft_text:
  1830. begin
  1831. p1:=cderefnode.create(ccallnode.createintern('fpc_getbuf_text',ccallparanode.create(p1,nil)));
  1832. typecheckpass(p1);
  1833. end;
  1834. ft_typed:
  1835. begin
  1836. p1:=cderefnode.create(ctypeconvnode.create_internal(ccallnode.createintern('fpc_getbuf_typedfile',ccallparanode.create(p1,nil)),
  1837. cpointerdef.getreusable(tfiledef(p1.resultdef).typedfiledef)));
  1838. typecheckpass(p1);
  1839. end;
  1840. end;
  1841. end
  1842. else if (p1.resultdef.typ<>pointerdef) then
  1843. begin
  1844. { ^ as binary operator is a problem!!!! (FK) }
  1845. again:=false;
  1846. Message(parser_e_invalid_qualifier);
  1847. recoverconsume_postfixops;
  1848. p1.destroy;
  1849. p1:=cerrornode.create;
  1850. end
  1851. else
  1852. p1:=cderefnode.create(p1);
  1853. end;
  1854. _LECKKLAMMER:
  1855. begin
  1856. if is_class_or_interface_or_object(p1.resultdef) or
  1857. is_dispinterface(p1.resultdef) or
  1858. is_record(p1.resultdef) or
  1859. is_javaclass(p1.resultdef) then
  1860. begin
  1861. { default property }
  1862. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1863. if not(assigned(protsym)) then
  1864. begin
  1865. p1.destroy;
  1866. p1:=cerrornode.create;
  1867. again:=false;
  1868. message(parser_e_no_default_property_available);
  1869. end
  1870. else
  1871. begin
  1872. { The property symbol is referenced indirect }
  1873. protsym.IncRefCount;
  1874. handle_propertysym(protsym,protsym.owner,p1);
  1875. end;
  1876. end
  1877. else
  1878. begin
  1879. consume(_LECKKLAMMER);
  1880. repeat
  1881. { in all of the cases below, p1 is changed }
  1882. case p1.resultdef.typ of
  1883. pointerdef:
  1884. begin
  1885. { support delphi autoderef }
  1886. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1887. (m_autoderef in current_settings.modeswitches) then
  1888. p1:=cderefnode.create(p1);
  1889. p2:=comp_expr(true,false);
  1890. { Support Pbytevar[0..9] which returns array [0..9].}
  1891. if try_to_consume(_POINTPOINT) then
  1892. p2:=crangenode.create(p2,comp_expr(true,false));
  1893. p1:=cvecnode.create(p1,p2);
  1894. end;
  1895. variantdef:
  1896. begin
  1897. handle_variantarray;
  1898. { the RECKKLAMMER is already read }
  1899. goto skipreckklammercheck;
  1900. end;
  1901. stringdef :
  1902. begin
  1903. p2:=comp_expr(true,false);
  1904. { Support string[0..9] which returns array [0..9] of char.}
  1905. if try_to_consume(_POINTPOINT) then
  1906. p2:=crangenode.create(p2,comp_expr(true,false));
  1907. p1:=cvecnode.create(p1,p2);
  1908. end;
  1909. arraydef:
  1910. begin
  1911. p2:=comp_expr(true,false);
  1912. { support SEG:OFS for go32v2/msdos Mem[] }
  1913. if (target_info.system in [system_i386_go32v2,system_i386_watcom,system_i8086_msdos,system_i8086_win16]) and
  1914. (p1.nodetype=loadn) and
  1915. assigned(tloadnode(p1).symtableentry) and
  1916. assigned(tloadnode(p1).symtableentry.owner.name) and
  1917. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1918. ((tloadnode(p1).symtableentry.name='MEM') or
  1919. (tloadnode(p1).symtableentry.name='MEMW') or
  1920. (tloadnode(p1).symtableentry.name='MEML')) then
  1921. begin
  1922. {$if defined(i8086)}
  1923. consume(_COLON);
  1924. inserttypeconv(p2,u16inttype);
  1925. inserttypeconv_internal(p2,u32inttype);
  1926. p3:=cshlshrnode.create(shln,p2,cordconstnode.create($10,s16inttype,false));
  1927. p2:=comp_expr(true,false);
  1928. inserttypeconv(p2,u16inttype);
  1929. inserttypeconv_internal(p2,u32inttype);
  1930. p2:=caddnode.create(addn,p2,p3);
  1931. case tloadnode(p1).symtableentry.name of
  1932. 'MEM': p2:=ctypeconvnode.create_internal(p2,bytefarpointertype);
  1933. 'MEMW': p2:=ctypeconvnode.create_internal(p2,wordfarpointertype);
  1934. 'MEML': p2:=ctypeconvnode.create_internal(p2,longintfarpointertype);
  1935. else
  1936. internalerror(2013053102);
  1937. end;
  1938. p1:=cderefnode.create(p2);
  1939. {$elseif defined(i386)}
  1940. if try_to_consume(_COLON) then
  1941. begin
  1942. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1943. p2:=comp_expr(true,false);
  1944. p2:=caddnode.create(addn,p2,p3);
  1945. if try_to_consume(_POINTPOINT) then
  1946. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1947. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1948. p1:=cvecnode.create(p1,p2);
  1949. include(tvecnode(p1).flags,nf_memseg);
  1950. include(tvecnode(p1).flags,nf_memindex);
  1951. end
  1952. else
  1953. begin
  1954. if try_to_consume(_POINTPOINT) then
  1955. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1956. p2:=crangenode.create(p2,comp_expr(true,false));
  1957. p1:=cvecnode.create(p1,p2);
  1958. include(tvecnode(p1).flags,nf_memindex);
  1959. end;
  1960. {$else}
  1961. internalerror(2013053101);
  1962. {$endif}
  1963. end
  1964. else
  1965. begin
  1966. if try_to_consume(_POINTPOINT) then
  1967. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1968. p2:=crangenode.create(p2,comp_expr(true,false));
  1969. p1:=cvecnode.create(p1,p2);
  1970. end;
  1971. end;
  1972. else
  1973. begin
  1974. if p1.resultdef.typ<>undefineddef then
  1975. Message(parser_e_invalid_qualifier);
  1976. p1.destroy;
  1977. p1:=cerrornode.create;
  1978. comp_expr(true,false);
  1979. again:=false;
  1980. end;
  1981. end;
  1982. do_typecheckpass(p1);
  1983. until not try_to_consume(_COMMA);
  1984. consume(_RECKKLAMMER);
  1985. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1986. skipreckklammercheck:
  1987. end;
  1988. end;
  1989. _POINT :
  1990. begin
  1991. consume(_POINT);
  1992. allowspecialize:=not (m_delphi in current_settings.modeswitches) and (block_type in [bt_type,bt_var_type,bt_const_type]);
  1993. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  1994. begin
  1995. //consume(_ID);
  1996. isspecialize:=true;
  1997. end
  1998. else
  1999. isspecialize:=false;
  2000. if (p1.resultdef.typ=pointerdef) and
  2001. (m_autoderef in current_settings.modeswitches) and
  2002. { don't auto-deref objc.id, because then the code
  2003. below for supporting id.anyobjcmethod isn't triggered }
  2004. (p1.resultdef<>objc_idtype) then
  2005. begin
  2006. p1:=cderefnode.create(p1);
  2007. do_typecheckpass(p1);
  2008. end;
  2009. { procvar.<something> can never mean anything so always
  2010. try to call it in case it returns a record/object/... }
  2011. maybe_call_procvar(p1,false);
  2012. if (p1.nodetype=ordconstn) and
  2013. not is_boolean(p1.resultdef) and
  2014. not is_enum(p1.resultdef) then
  2015. begin
  2016. { type helpers are checked first }
  2017. if (token=_ID) and try_type_helper(p1,nil) then
  2018. goto skippointdefcheck;
  2019. { only an "e" or "E" can follow an intconst with a ".", the
  2020. other case (another intconst) is handled by the scanner }
  2021. if (token=_ID) and (pattern[1]='E') then
  2022. begin
  2023. haderror:=false;
  2024. if length(pattern)>1 then
  2025. begin
  2026. expstr:=copy(pattern,2,length(pattern)-1);
  2027. val(expstr,intval,code);
  2028. if code<>0 then
  2029. begin
  2030. haderror:=true;
  2031. intval:=intval; // Hackfix the "var assigned but never used" note.
  2032. end;
  2033. end
  2034. else
  2035. expstr:='';
  2036. consume(token);
  2037. if tordconstnode(p1).value.signed then
  2038. str(tordconstnode(p1).value.svalue,valstr)
  2039. else
  2040. str(tordconstnode(p1).value.uvalue,valstr);
  2041. valstr:=valstr+'.0E';
  2042. if expstr='' then
  2043. case token of
  2044. _MINUS:
  2045. begin
  2046. consume(token);
  2047. if token=_INTCONST then
  2048. begin
  2049. valstr:=valstr+'-'+pattern;
  2050. consume(token);
  2051. end
  2052. else
  2053. haderror:=true;
  2054. end;
  2055. _PLUS:
  2056. begin
  2057. consume(token);
  2058. if token=_INTCONST then
  2059. begin
  2060. valstr:=valstr+pattern;
  2061. consume(token);
  2062. end
  2063. else
  2064. haderror:=true;
  2065. end;
  2066. _INTCONST:
  2067. begin
  2068. valstr:=valstr+pattern;
  2069. consume(_INTCONST);
  2070. end;
  2071. else
  2072. haderror:=true;
  2073. end
  2074. else
  2075. valstr:=valstr+expstr;
  2076. if haderror then
  2077. begin
  2078. Message(parser_e_error_in_real);
  2079. p2:=cerrornode.create;
  2080. end
  2081. else
  2082. p2:=real_const_node_from_pattern(valstr);
  2083. p1.free;
  2084. p1:=p2;
  2085. again:=false;
  2086. goto skippointdefcheck;
  2087. end
  2088. else
  2089. begin
  2090. { just convert the ordconst to a realconst }
  2091. p2:=crealconstnode.create(tordconstnode(p1).value,pbestrealtype^);
  2092. p1.free;
  2093. p1:=p2;
  2094. again:=false;
  2095. goto skippointdefcheck;
  2096. end;
  2097. end;
  2098. if (p1.nodetype=stringconstn) and (token=_ID) then
  2099. begin
  2100. { the def of a string const is an array }
  2101. case tstringconstnode(p1).cst_type of
  2102. cst_conststring:
  2103. if cs_refcountedstrings in current_settings.localswitches then
  2104. if m_default_unicodestring in current_settings.modeswitches then
  2105. strdef:=cunicodestringtype
  2106. else
  2107. strdef:=cansistringtype
  2108. else
  2109. strdef:=cshortstringtype;
  2110. cst_shortstring:
  2111. strdef:=cshortstringtype;
  2112. cst_ansistring:
  2113. { use getansistringdef? }
  2114. strdef:=cansistringtype;
  2115. cst_widestring:
  2116. strdef:=cwidestringtype;
  2117. cst_unicodestring:
  2118. strdef:=cunicodestringtype;
  2119. cst_longstring:
  2120. { let's see when someone stumbles upon this...}
  2121. internalerror(201301111);
  2122. else
  2123. internalerror(2013112903);
  2124. end;
  2125. if try_type_helper(p1,strdef) then
  2126. goto skippointdefcheck;
  2127. end;
  2128. { this is skipped if label skippointdefcheck is used }
  2129. case p1.resultdef.typ of
  2130. recorddef:
  2131. begin
  2132. if isspecialize or (token=_ID) then
  2133. begin
  2134. erroroutp1:=true;
  2135. srsym:=nil;
  2136. structh:=tabstractrecorddef(p1.resultdef);
  2137. if isspecialize then
  2138. begin
  2139. { consume the specialize }
  2140. consume(_ID);
  2141. if token<>_ID then
  2142. consume(_ID)
  2143. else
  2144. begin
  2145. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2146. consume(_ID);
  2147. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2148. erroroutp1:=false;
  2149. end;
  2150. end
  2151. else
  2152. begin
  2153. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2154. if assigned(srsym) then
  2155. begin
  2156. old_current_filepos:=current_filepos;
  2157. consume(_ID);
  2158. if not (sp_generic_dummy in srsym.symoptions) or
  2159. not (token in [_LT,_LSHARPBRACKET]) then
  2160. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2161. else
  2162. p1:=cspecializenode.create(p1,getaddr,srsym);
  2163. erroroutp1:=false;
  2164. end
  2165. else
  2166. begin
  2167. Message1(sym_e_id_no_member,orgpattern);
  2168. { try to clean up }
  2169. consume(_ID);
  2170. end;
  2171. end;
  2172. if erroroutp1 then
  2173. begin
  2174. p1.free;
  2175. p1:=cerrornode.create;
  2176. end
  2177. else
  2178. if p1.nodetype<>specializen then
  2179. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2180. end
  2181. else
  2182. consume(_ID);
  2183. end;
  2184. enumdef:
  2185. begin
  2186. if token=_ID then
  2187. begin
  2188. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  2189. if assigned(srsym) and (srsym.typ=enumsym) and (p1.nodetype=typen) then
  2190. begin
  2191. p1.destroy;
  2192. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2193. p1:=genenumnode(tenumsym(srsym));
  2194. consume(_ID);
  2195. end
  2196. else
  2197. if not try_type_helper(p1,nil) then
  2198. begin
  2199. p1.destroy;
  2200. Message1(sym_e_id_no_member,orgpattern);
  2201. p1:=cerrornode.create;
  2202. consume(_ID);
  2203. end;
  2204. end;
  2205. end;
  2206. arraydef:
  2207. begin
  2208. if is_dynamic_array(p1.resultdef) then
  2209. begin
  2210. if (token=_ID) and not try_type_helper(p1,nil) then
  2211. begin
  2212. if pattern='CREATE' then
  2213. begin
  2214. consume(_ID);
  2215. p2:=parse_array_constructor(tarraydef(p1.resultdef));
  2216. p1.destroy;
  2217. p1:=p2;
  2218. end
  2219. else
  2220. begin
  2221. Message2(scan_f_syn_expected,'CREATE',pattern);
  2222. p1.destroy;
  2223. p1:=cerrornode.create;
  2224. consume(_ID);
  2225. end;
  2226. end;
  2227. end
  2228. else
  2229. begin
  2230. Message(parser_e_invalid_qualifier);
  2231. p1.destroy;
  2232. p1:=cerrornode.create;
  2233. consume(_ID);
  2234. end;
  2235. end;
  2236. variantdef:
  2237. begin
  2238. { dispatch call? }
  2239. { lhs := v.ident[parameters] -> property get
  2240. lhs := v.ident(parameters) -> method call
  2241. v.ident[parameters] := rhs -> property put
  2242. v.ident(parameters) := rhs -> also property put }
  2243. if token=_ID then
  2244. begin
  2245. if not try_type_helper(p1,nil) then
  2246. begin
  2247. dispatchstring:=orgpattern;
  2248. consume(_ID);
  2249. calltype:=dct_method;
  2250. if try_to_consume(_LKLAMMER) then
  2251. begin
  2252. p2:=parse_paras(false,true,_RKLAMMER);
  2253. consume(_RKLAMMER);
  2254. end
  2255. else if try_to_consume(_LECKKLAMMER) then
  2256. begin
  2257. p2:=parse_paras(false,true,_RECKKLAMMER);
  2258. consume(_RECKKLAMMER);
  2259. calltype:=dct_propget;
  2260. end
  2261. else
  2262. p2:=nil;
  2263. { property setter? }
  2264. if (token=_ASSIGNMENT) and not(afterassignment) then
  2265. begin
  2266. consume(_ASSIGNMENT);
  2267. { read the expression }
  2268. p3:=comp_expr(true,false);
  2269. { concat value parameter too }
  2270. p2:=ccallparanode.create(p3,p2);
  2271. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  2272. end
  2273. else
  2274. { this is only an approximation
  2275. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  2276. if afterassignment or in_args or (token<>_SEMICOLON) then
  2277. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  2278. else
  2279. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  2280. end;
  2281. end
  2282. else { Error }
  2283. Consume(_ID);
  2284. end;
  2285. classrefdef:
  2286. begin
  2287. erroroutp1:=true;
  2288. if token=_ID then
  2289. begin
  2290. srsym:=nil;
  2291. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  2292. if isspecialize then
  2293. begin
  2294. { consume the specialize }
  2295. consume(_ID);
  2296. if token<>_ID then
  2297. consume(_ID)
  2298. else
  2299. begin
  2300. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2301. consume(_ID);
  2302. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2303. erroroutp1:=false;
  2304. end;
  2305. end
  2306. else
  2307. begin
  2308. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2309. if assigned(srsym) then
  2310. begin
  2311. old_current_filepos:=current_filepos;
  2312. consume(_ID);
  2313. if not (sp_generic_dummy in srsym.symoptions) or
  2314. not (token in [_LT,_LSHARPBRACKET]) then
  2315. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2316. else
  2317. p1:=cspecializenode.create(p1,getaddr,srsym);
  2318. erroroutp1:=false;
  2319. end
  2320. else
  2321. begin
  2322. Message1(sym_e_id_no_member,orgpattern);
  2323. { try to clean up }
  2324. consume(_ID);
  2325. end;
  2326. end;
  2327. if erroroutp1 then
  2328. begin
  2329. p1.free;
  2330. p1:=cerrornode.create;
  2331. end
  2332. else
  2333. if p1.nodetype<>specializen then
  2334. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2335. end
  2336. else { Error }
  2337. Consume(_ID);
  2338. end;
  2339. objectdef:
  2340. begin
  2341. if isspecialize or (token=_ID) then
  2342. begin
  2343. erroroutp1:=true;
  2344. srsym:=nil;
  2345. structh:=tobjectdef(p1.resultdef);
  2346. if isspecialize then
  2347. begin
  2348. { consume the "specialize" }
  2349. consume(_ID);
  2350. if token<>_ID then
  2351. consume(_ID)
  2352. else
  2353. begin
  2354. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2355. consume(_ID);
  2356. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2357. erroroutp1:=false;
  2358. end;
  2359. end
  2360. else
  2361. begin
  2362. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2363. if assigned(srsym) then
  2364. begin
  2365. old_current_filepos:=current_filepos;
  2366. consume(_ID);
  2367. if not (sp_generic_dummy in srsym.symoptions) or
  2368. not (token in [_LT,_LSHARPBRACKET]) then
  2369. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2370. else
  2371. p1:=cspecializenode.create(p1,getaddr,srsym);
  2372. erroroutp1:=false;
  2373. end
  2374. else
  2375. begin
  2376. Message1(sym_e_id_no_member,orgpattern);
  2377. { try to clean up }
  2378. consume(_ID);
  2379. end;
  2380. end;
  2381. if erroroutp1 then
  2382. begin
  2383. p1.free;
  2384. p1:=cerrornode.create;
  2385. end
  2386. else
  2387. if p1.nodetype<>specializen then
  2388. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2389. end
  2390. else { Error }
  2391. Consume(_ID);
  2392. end;
  2393. pointerdef:
  2394. begin
  2395. if (p1.resultdef=objc_idtype) then
  2396. begin
  2397. { objc's id type can be used to call any
  2398. Objective-C method of any Objective-C class
  2399. type that's currently in scope }
  2400. if search_objc_method(pattern,srsym,srsymtable) then
  2401. begin
  2402. consume(_ID);
  2403. do_proc_call(srsym,srsymtable,nil,
  2404. (getaddr and not(token in [_CARET,_POINT])),
  2405. again,p1,[cnf_objc_id_call],nil);
  2406. { we need to know which procedure is called }
  2407. do_typecheckpass(p1);
  2408. end
  2409. else
  2410. begin
  2411. consume(_ID);
  2412. Message(parser_e_methode_id_expected);
  2413. end;
  2414. end
  2415. else
  2416. begin
  2417. if not try_type_helper(p1,nil) then
  2418. begin
  2419. Message(parser_e_invalid_qualifier);
  2420. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2421. Message(parser_h_maybe_deref_caret_missing);
  2422. end;
  2423. end
  2424. end;
  2425. else
  2426. begin
  2427. found:=try_type_helper(p1,nil);
  2428. if not found then
  2429. begin
  2430. if p1.resultdef.typ<>undefineddef then
  2431. Message(parser_e_invalid_qualifier);
  2432. p1.destroy;
  2433. p1:=cerrornode.create;
  2434. { Error }
  2435. consume(_ID);
  2436. end;
  2437. end;
  2438. end;
  2439. { processing an ordconstnode avoids the resultdef check }
  2440. skippointdefcheck:
  2441. end;
  2442. else
  2443. begin
  2444. { is this a procedure variable ? }
  2445. if assigned(p1.resultdef) and
  2446. (p1.resultdef.typ=procvardef) then
  2447. begin
  2448. { Typenode for typecasting or expecting a procvar }
  2449. if (p1.nodetype=typen) or
  2450. (
  2451. assigned(getprocvardef) and
  2452. equal_defs(p1.resultdef,getprocvardef)
  2453. ) then
  2454. begin
  2455. if try_to_consume(_LKLAMMER) then
  2456. begin
  2457. p1:=comp_expr(true,false);
  2458. consume(_RKLAMMER);
  2459. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2460. end
  2461. else
  2462. again:=false
  2463. end
  2464. else
  2465. begin
  2466. if try_to_consume(_LKLAMMER) then
  2467. begin
  2468. p2:=parse_paras(false,false,_RKLAMMER);
  2469. consume(_RKLAMMER);
  2470. p1:=ccallnode.create_procvar(p2,p1);
  2471. { proc():= is never possible }
  2472. if token=_ASSIGNMENT then
  2473. begin
  2474. Message(parser_e_illegal_expression);
  2475. p1.free;
  2476. p1:=cerrornode.create;
  2477. again:=false;
  2478. end;
  2479. end
  2480. else
  2481. again:=false;
  2482. end;
  2483. end
  2484. else
  2485. again:=false;
  2486. end;
  2487. end;
  2488. { we only try again if p1 was changed }
  2489. if again or
  2490. (p1.nodetype=errorn) then
  2491. result:=true;
  2492. end; { while again }
  2493. end;
  2494. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  2495. out memberparentdef: tdef): boolean;
  2496. var
  2497. hdef : tdef;
  2498. begin
  2499. result:=true;
  2500. memberparentdef:=nil;
  2501. case st.symtabletype of
  2502. ObjectSymtable,
  2503. recordsymtable:
  2504. begin
  2505. memberparentdef:=tdef(st.defowner);
  2506. exit;
  2507. end;
  2508. WithSymtable:
  2509. begin
  2510. if assigned(p1) then
  2511. internalerror(2007012002);
  2512. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  2513. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  2514. if not(hdef.typ in [objectdef,classrefdef]) then
  2515. exit;
  2516. if (hdef.typ=classrefdef) then
  2517. hdef:=tclassrefdef(hdef).pointeddef;
  2518. memberparentdef:=hdef;
  2519. end;
  2520. else
  2521. result:=false;
  2522. end;
  2523. end;
  2524. {$maxfpuregisters 0}
  2525. function factor(getaddr,typeonly,hadspecialize:boolean) : tnode;
  2526. {---------------------------------------------
  2527. Factor_read_id
  2528. ---------------------------------------------}
  2529. procedure factor_read_id(out p1:tnode;var again:boolean);
  2530. function findwithsymtable : boolean;
  2531. var
  2532. hp : psymtablestackitem;
  2533. begin
  2534. result:=true;
  2535. hp:=symtablestack.stack;
  2536. while assigned(hp) do
  2537. begin
  2538. if hp^.symtable.symtabletype=withsymtable then
  2539. exit;
  2540. hp:=hp^.next;
  2541. end;
  2542. result:=false;
  2543. end;
  2544. var
  2545. srsym: tsym;
  2546. srsymtable: TSymtable;
  2547. hdef: tdef;
  2548. pd: tprocdef;
  2549. orgstoredpattern,
  2550. storedpattern: string;
  2551. callflags: tcallnodeflags;
  2552. t : ttoken;
  2553. allowspecialize,
  2554. isspecialize,
  2555. unit_found : boolean;
  2556. tokenpos: tfileposinfo;
  2557. begin
  2558. { allow post fix operators }
  2559. again:=true;
  2560. { preinitalize tokenpos }
  2561. tokenpos:=current_filepos;
  2562. p1:=nil;
  2563. allowspecialize:=not (m_delphi in current_settings.modeswitches) and
  2564. not hadspecialize and
  2565. (block_type in [bt_type,bt_var_type,bt_const_type]);
  2566. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  2567. begin
  2568. consume(_ID);
  2569. isspecialize:=true;
  2570. end
  2571. else
  2572. isspecialize:=hadspecialize;
  2573. { first check for identifier }
  2574. if token<>_ID then
  2575. begin
  2576. srsym:=generrorsym;
  2577. srsymtable:=nil;
  2578. consume(_ID);
  2579. unit_found:=false;
  2580. end
  2581. else
  2582. begin
  2583. if typeonly then
  2584. searchsym_type(pattern,srsym,srsymtable)
  2585. else
  2586. searchsym(pattern,srsym,srsymtable);
  2587. { handle unit specification like System.Writeln }
  2588. if not isspecialize then
  2589. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true,allowspecialize,isspecialize)
  2590. else
  2591. begin
  2592. unit_found:=false;
  2593. t:=_ID;
  2594. end;
  2595. storedpattern:=pattern;
  2596. orgstoredpattern:=orgpattern;
  2597. { store the position of the token before consuming it }
  2598. tokenpos:=current_filepos;
  2599. consume(t);
  2600. { named parameter support }
  2601. found_arg_name:=false;
  2602. if not(unit_found) and
  2603. not isspecialize and
  2604. named_args_allowed and
  2605. (token=_ASSIGNMENT) then
  2606. begin
  2607. found_arg_name:=true;
  2608. p1:=cstringconstnode.createstr(storedpattern);
  2609. consume(_ASSIGNMENT);
  2610. exit;
  2611. end;
  2612. if isspecialize then
  2613. begin
  2614. if not assigned(srsym) or
  2615. (srsym.typ<>typesym) then
  2616. begin
  2617. identifier_not_found(orgstoredpattern,tokenpos);
  2618. srsym:=generrorsym;
  2619. srsymtable:=nil;
  2620. end
  2621. else
  2622. begin
  2623. hdef:=ttypesym(srsym).typedef;
  2624. generate_specialization(hdef,false,'');
  2625. if hdef=generrordef then
  2626. begin
  2627. srsym:=generrorsym;
  2628. srsymtable:=nil;
  2629. end
  2630. else
  2631. begin
  2632. srsym:=hdef.typesym;
  2633. srsymtable:=srsym.owner;
  2634. end;
  2635. end;
  2636. end;
  2637. { check hints, but only if it isn't a potential generic symbol;
  2638. that is checked in sub_expr if it isn't a generic }
  2639. if assigned(srsym) and
  2640. not (
  2641. (srsym.typ=typesym) and
  2642. (ttypesym(srsym).typedef.typ in [recorddef,objectdef,arraydef,procvardef,undefineddef]) and
  2643. not (sp_generic_para in srsym.symoptions) and
  2644. (token in [_LT, _LSHARPBRACKET])
  2645. ) then
  2646. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2647. { if nothing found give error and return errorsym }
  2648. if not assigned(srsym) or
  2649. { is this a generic dummy symbol? }
  2650. ((srsym.typ=typesym) and
  2651. assigned(ttypesym(srsym).typedef) and
  2652. (ttypesym(srsym).typedef.typ=undefineddef) and
  2653. not (sp_generic_para in srsym.symoptions) and
  2654. not (token in [_LT, _LSHARPBRACKET]) and
  2655. not (
  2656. { in non-Delphi modes the generic class' name without a
  2657. "specialization" or "<T>" may be used to identify the
  2658. current class }
  2659. (sp_generic_dummy in srsym.symoptions) and
  2660. assigned(current_structdef) and
  2661. (df_generic in current_structdef.defoptions) and
  2662. not (m_delphi in current_settings.modeswitches) and
  2663. assigned(get_generic_in_hierarchy_by_name(srsym,current_structdef))
  2664. )) and
  2665. { it could be a rename of a generic para }
  2666. { Note: if this generates false positives we'll need to
  2667. include a "basesym" to tsym to track the original
  2668. symbol }
  2669. not (sp_explicitrename in srsym.symoptions) then
  2670. begin
  2671. { if a generic is parsed and when we are inside an with block,
  2672. a symbol might not be defined }
  2673. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) and
  2674. findwithsymtable then
  2675. begin
  2676. { create dummy symbol, it will be freed later on }
  2677. srsym:=tstoredsym.create(undefinedsym,'$undefinedsym',false);
  2678. srsymtable:=nil;
  2679. end
  2680. else
  2681. begin
  2682. identifier_not_found(orgstoredpattern,tokenpos);
  2683. srsym:=generrorsym;
  2684. srsymtable:=nil;
  2685. end;
  2686. end;
  2687. end;
  2688. { Access to funcret or need to call the function? }
  2689. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  2690. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  2691. { result(x) is not allowed }
  2692. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  2693. (
  2694. (token=_LKLAMMER) or
  2695. (
  2696. (([m_tp7,m_delphi,m_mac,m_iso] * current_settings.modeswitches) <> []) and
  2697. (afterassignment or in_args)
  2698. )
  2699. ) then
  2700. begin
  2701. hdef:=tdef(srsym.owner.defowner);
  2702. if assigned(hdef) and
  2703. (hdef.typ=procdef) then
  2704. srsym:=tprocdef(hdef).procsym
  2705. else
  2706. begin
  2707. Message(parser_e_illegal_expression);
  2708. srsym:=generrorsym;
  2709. end;
  2710. srsymtable:=srsym.owner;
  2711. end;
  2712. begin
  2713. case srsym.typ of
  2714. absolutevarsym :
  2715. begin
  2716. if (tabsolutevarsym(srsym).abstyp=tovar) then
  2717. begin
  2718. p1:=nil;
  2719. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  2720. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  2721. include(p1.flags,nf_absolute);
  2722. end
  2723. else
  2724. p1:=cloadnode.create(srsym,srsymtable);
  2725. end;
  2726. staticvarsym,
  2727. localvarsym,
  2728. paravarsym,
  2729. fieldvarsym :
  2730. begin
  2731. { check if we are reading a field of an object/class/ }
  2732. { record. is_member_read() will deal with withsymtables }
  2733. { if needed. }
  2734. p1:=nil;
  2735. if is_member_read(srsym,srsymtable,p1,hdef) then
  2736. begin
  2737. { if the field was originally found in an }
  2738. { objectsymtable, it means it's part of self }
  2739. { if only method from which it was called is }
  2740. { not class static }
  2741. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2742. { if we are accessing a owner procsym from the nested }
  2743. { class we need to call it as a class member }
  2744. if assigned(current_structdef) and
  2745. (((current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2746. (sp_static in srsym.symoptions)) then
  2747. if srsymtable.symtabletype=recordsymtable then
  2748. p1:=ctypenode.create(hdef)
  2749. else
  2750. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2751. else
  2752. begin
  2753. if assigned(current_procinfo) then
  2754. begin
  2755. pd:=current_procinfo.get_normal_proc.procdef;
  2756. if assigned(pd) and pd.no_self_node then
  2757. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  2758. else
  2759. p1:=load_self_node;
  2760. end
  2761. else
  2762. p1:=load_self_node;
  2763. end;
  2764. { now, if the field itself is part of an objectsymtab }
  2765. { (it can be even if it was found in a withsymtable, }
  2766. { e.g., "with classinstance do field := 5"), then }
  2767. { let do_member_read handle it }
  2768. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2769. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2770. else
  2771. { otherwise it's a regular record subscript }
  2772. p1:=csubscriptnode.create(srsym,p1);
  2773. end
  2774. else
  2775. { regular non-field load }
  2776. p1:=cloadnode.create(srsym,srsymtable);
  2777. end;
  2778. syssym :
  2779. begin
  2780. p1:=statement_syssym(tsyssym(srsym).number);
  2781. end;
  2782. typesym :
  2783. begin
  2784. hdef:=ttypesym(srsym).typedef;
  2785. if not assigned(hdef) then
  2786. begin
  2787. again:=false;
  2788. end
  2789. else
  2790. begin
  2791. { We need to know if this unit uses Variants }
  2792. if ((hdef=cvarianttype) or (hdef=colevarianttype)) and
  2793. not(cs_compilesystem in current_settings.moduleswitches) then
  2794. current_module.flags:=current_module.flags or uf_uses_variants;
  2795. p1:=handle_factor_typenode(hdef,getaddr,again,srsym,typeonly);
  2796. end;
  2797. end;
  2798. enumsym :
  2799. begin
  2800. p1:=genenumnode(tenumsym(srsym));
  2801. end;
  2802. constsym :
  2803. begin
  2804. if tconstsym(srsym).consttyp=constresourcestring then
  2805. begin
  2806. p1:=cloadnode.create(srsym,srsymtable);
  2807. do_typecheckpass(p1);
  2808. p1.resultdef:=getansistringdef;
  2809. end
  2810. else
  2811. p1:=genconstsymtree(tconstsym(srsym));
  2812. end;
  2813. procsym :
  2814. begin
  2815. p1:=nil;
  2816. { check if it's a method/class method }
  2817. if is_member_read(srsym,srsymtable,p1,hdef) then
  2818. begin
  2819. { if we are accessing a owner procsym from the nested }
  2820. { class we need to call it as a class member }
  2821. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) and
  2822. assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2823. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef));
  2824. { not srsymtable.symtabletype since that can be }
  2825. { withsymtable as well }
  2826. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2827. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2828. else
  2829. { no procsyms in records (yet) }
  2830. internalerror(2007012006);
  2831. end
  2832. else
  2833. begin
  2834. { regular procedure/function call }
  2835. if not unit_found then
  2836. callflags:=[]
  2837. else
  2838. callflags:=[cnf_unit_specified];
  2839. do_proc_call(srsym,srsymtable,nil,
  2840. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  2841. again,p1,callflags,nil);
  2842. end;
  2843. end;
  2844. propertysym :
  2845. begin
  2846. p1:=nil;
  2847. { property of a class/object? }
  2848. if is_member_read(srsym,srsymtable,p1,hdef) then
  2849. begin
  2850. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2851. { if we are accessing a owner procsym from the nested }
  2852. { class or from a static class method we need to call }
  2853. { it as a class member }
  2854. if (assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2855. (assigned(current_procinfo) and current_procinfo.get_normal_proc.procdef.no_self_node) then
  2856. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2857. else
  2858. p1:=load_self_node;
  2859. { not srsymtable.symtabletype since that can be }
  2860. { withsymtable as well }
  2861. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2862. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2863. else
  2864. { no propertysyms in records (yet) }
  2865. internalerror(2009111510);
  2866. end
  2867. else
  2868. { no method pointer }
  2869. begin
  2870. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  2871. end;
  2872. end;
  2873. labelsym :
  2874. begin
  2875. { Support @label }
  2876. if getaddr then
  2877. begin
  2878. if srsym.owner<>current_procinfo.procdef.localst then
  2879. CGMessage(parser_e_label_outside_proc);
  2880. p1:=cloadnode.create(srsym,srsym.owner)
  2881. end
  2882. else
  2883. begin
  2884. consume(_COLON);
  2885. if tlabelsym(srsym).defined then
  2886. Message(sym_e_label_already_defined);
  2887. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  2888. begin
  2889. tlabelsym(srsym).nonlocal:=true;
  2890. exclude(current_procinfo.procdef.procoptions,po_inline);
  2891. end;
  2892. if tlabelsym(srsym).nonlocal and
  2893. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  2894. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  2895. tlabelsym(srsym).defined:=true;
  2896. p1:=clabelnode.create(nil,tlabelsym(srsym));
  2897. tlabelsym(srsym).code:=p1;
  2898. end;
  2899. end;
  2900. undefinedsym :
  2901. begin
  2902. p1:=cnothingnode.Create;
  2903. p1.resultdef:=cundefineddef.create(true);
  2904. { clean up previously created dummy symbol }
  2905. srsym.free;
  2906. end;
  2907. errorsym :
  2908. begin
  2909. p1:=cerrornode.create;
  2910. if try_to_consume(_LKLAMMER) then
  2911. begin
  2912. parse_paras(false,false,_RKLAMMER);
  2913. consume(_RKLAMMER);
  2914. end;
  2915. end;
  2916. else
  2917. begin
  2918. p1:=cerrornode.create;
  2919. Message(parser_e_illegal_expression);
  2920. end;
  2921. end; { end case }
  2922. if assigned(p1) and (p1.nodetype<>errorn) then
  2923. p1.fileinfo:=tokenpos;
  2924. end;
  2925. end;
  2926. {---------------------------------------------
  2927. Factor_Read_Set
  2928. ---------------------------------------------}
  2929. { Read a set between [] }
  2930. function factor_read_set:tnode;
  2931. var
  2932. p1,p2 : tnode;
  2933. lastp,
  2934. buildp : tarrayconstructornode;
  2935. old_allow_array_constructor : boolean;
  2936. begin
  2937. buildp:=nil;
  2938. lastp:=nil;
  2939. { be sure that a least one arrayconstructn is used, also for an
  2940. empty [] }
  2941. if token=_RECKKLAMMER then
  2942. buildp:=carrayconstructornode.create(nil,buildp)
  2943. else
  2944. repeat
  2945. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  2946. old_allow_array_constructor:=allow_array_constructor;
  2947. allow_array_constructor:=false;
  2948. p1:=comp_expr(true,false);
  2949. if try_to_consume(_POINTPOINT) then
  2950. begin
  2951. p2:=comp_expr(true,false);
  2952. p1:=carrayconstructorrangenode.create(p1,p2);
  2953. end;
  2954. { insert at the end of the tree, to get the correct order }
  2955. if not assigned(buildp) then
  2956. begin
  2957. buildp:=carrayconstructornode.create(p1,nil);
  2958. lastp:=buildp;
  2959. end
  2960. else
  2961. begin
  2962. lastp.right:=carrayconstructornode.create(p1,nil);
  2963. lastp:=tarrayconstructornode(lastp.right);
  2964. end;
  2965. allow_array_constructor:=old_allow_array_constructor;
  2966. { there could be more elements }
  2967. until not try_to_consume(_COMMA);
  2968. factor_read_set:=buildp;
  2969. end;
  2970. function can_load_self_node: boolean;
  2971. begin
  2972. result:=false;
  2973. if (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  2974. not assigned(current_structdef) or
  2975. not assigned(current_procinfo) then
  2976. exit;
  2977. result:=not current_procinfo.get_normal_proc.procdef.no_self_node;
  2978. end;
  2979. {---------------------------------------------
  2980. Factor (Main)
  2981. ---------------------------------------------}
  2982. var
  2983. l : longint;
  2984. ic : int64;
  2985. qc : qword;
  2986. p1 : tnode;
  2987. code : integer;
  2988. srsym : tsym;
  2989. srsymtable : TSymtable;
  2990. pd : tprocdef;
  2991. hclassdef : tobjectdef;
  2992. d : bestreal;
  2993. hs,hsorg : string;
  2994. hdef : tdef;
  2995. filepos : tfileposinfo;
  2996. callflags : tcallnodeflags;
  2997. idstr : tidstring;
  2998. dopostfix,
  2999. again,
  3000. updatefpos,
  3001. nodechanged : boolean;
  3002. begin
  3003. { can't keep a copy of p1 and compare pointers afterwards, because
  3004. p1 may be freed and reallocated in the same place! }
  3005. dopostfix:=true;
  3006. updatefpos:=false;
  3007. p1:=nil;
  3008. filepos:=current_tokenpos;
  3009. again:=false;
  3010. pd:=nil;
  3011. if token=_ID then
  3012. begin
  3013. again:=true;
  3014. { Handle references to self }
  3015. if (idtoken=_SELF) and can_load_self_node then
  3016. begin
  3017. p1:=load_self_node;
  3018. consume(_ID);
  3019. again:=true;
  3020. end
  3021. else
  3022. factor_read_id(p1,again);
  3023. if assigned(p1) then
  3024. begin
  3025. { factor_read_id will set the filepos to after the id,
  3026. and in case of _SELF the filepos will already be the
  3027. same as filepos (so setting it again doesn't hurt). }
  3028. p1.fileinfo:=filepos;
  3029. filepos:=current_tokenpos;
  3030. end;
  3031. { handle post fix operators }
  3032. if (p1.nodetype=specializen) then
  3033. { post fix operators are handled after specialization }
  3034. dopostfix:=false
  3035. else
  3036. if (m_delphi in current_settings.modeswitches) and
  3037. (block_type=bt_body) and
  3038. (token in [_LT,_LSHARPBRACKET]) then
  3039. begin
  3040. if p1.nodetype=typen then
  3041. idstr:=ttypenode(p1).typesym.name
  3042. else
  3043. if (p1.nodetype=loadvmtaddrn) and
  3044. (tloadvmtaddrnode(p1).left.nodetype=typen) then
  3045. idstr:=ttypenode(tloadvmtaddrnode(p1).left).typesym.name
  3046. else
  3047. if (p1.nodetype=loadn) then
  3048. idstr:=tloadnode(p1).symtableentry.name
  3049. else
  3050. idstr:='';
  3051. { if this is the case then the postfix handling is done in
  3052. sub_expr if necessary }
  3053. dopostfix:=not could_be_generic(idstr);
  3054. end;
  3055. { maybe an additional parameter instead of misusing hadspezialize? }
  3056. if dopostfix and not hadspecialize then
  3057. updatefpos:=postfixoperators(p1,again,getaddr);
  3058. end
  3059. else
  3060. begin
  3061. updatefpos:=true;
  3062. case token of
  3063. _RETURN :
  3064. begin
  3065. consume(_RETURN);
  3066. p1:=nil;
  3067. if not(token in [_SEMICOLON,_ELSE,_END]) then
  3068. begin
  3069. p1:=comp_expr(true,false);
  3070. if not assigned(current_procinfo) or
  3071. (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
  3072. is_void(current_procinfo.procdef.returndef) then
  3073. begin
  3074. Message(parser_e_void_function);
  3075. { recovery }
  3076. p1.free;
  3077. p1:=nil;
  3078. end;
  3079. end;
  3080. p1 := cexitnode.create(p1);
  3081. end;
  3082. _INHERITED :
  3083. begin
  3084. again:=true;
  3085. consume(_INHERITED);
  3086. if assigned(current_procinfo) and
  3087. assigned(current_structdef) and
  3088. ((current_structdef.typ=objectdef) or
  3089. ((target_info.system in systems_jvm) and
  3090. (current_structdef.typ=recorddef)))then
  3091. begin
  3092. { for record helpers in mode Delphi "inherited" is not
  3093. allowed }
  3094. if is_objectpascal_helper(current_structdef) and
  3095. (m_delphi in current_settings.modeswitches) and
  3096. is_record(tobjectdef(current_structdef).extendeddef) then
  3097. Message(parser_e_inherited_not_in_record);
  3098. if (current_structdef.typ=objectdef) then
  3099. begin
  3100. hclassdef:=tobjectdef(current_structdef).childof;
  3101. { Objective-C categories *replace* methods in the class
  3102. they extend, or add methods to it. So calling an
  3103. inherited method always calls the method inherited from
  3104. the parent of the extended class }
  3105. if is_objccategory(current_structdef) then
  3106. hclassdef:=hclassdef.childof;
  3107. end
  3108. else if target_info.system in systems_jvm then
  3109. hclassdef:=java_fpcbaserecordtype
  3110. else
  3111. internalerror(2012012401);
  3112. { if inherited; only then we need the method with
  3113. the same name }
  3114. if token <> _ID then
  3115. begin
  3116. hs:=current_procinfo.procdef.procsym.name;
  3117. hsorg:=current_procinfo.procdef.procsym.realname;
  3118. anon_inherited:=true;
  3119. { For message methods we need to search using the message
  3120. number or string }
  3121. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  3122. srdef:=nil;
  3123. if (po_msgint in pd.procoptions) then
  3124. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  3125. else
  3126. if (po_msgstr in pd.procoptions) then
  3127. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  3128. else
  3129. { helpers have their own ways of dealing with inherited }
  3130. if is_objectpascal_helper(current_structdef) then
  3131. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3132. else
  3133. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3134. end
  3135. else
  3136. begin
  3137. hs:=pattern;
  3138. hsorg:=orgpattern;
  3139. consume(_ID);
  3140. anon_inherited:=false;
  3141. { helpers have their own ways of dealing with inherited }
  3142. if is_objectpascal_helper(current_structdef) then
  3143. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3144. else
  3145. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3146. end;
  3147. if assigned(srsym) then
  3148. begin
  3149. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  3150. { load the procdef from the inherited class and
  3151. not from self }
  3152. case srsym.typ of
  3153. procsym:
  3154. begin
  3155. if is_objectpascal_helper(current_structdef) then
  3156. begin
  3157. { for a helper load the procdef either from the
  3158. extended type, from the parent helper or from
  3159. the extended type of the parent helper
  3160. depending on the def the found symbol belongs
  3161. to }
  3162. if (srsym.Owner.defowner.typ=objectdef) and
  3163. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  3164. if def_is_related(current_structdef,tdef(srsym.Owner.defowner)) and
  3165. assigned(tobjectdef(current_structdef).childof) then
  3166. hdef:=tobjectdef(current_structdef).childof
  3167. else
  3168. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  3169. else
  3170. hdef:=tdef(srsym.Owner.defowner);
  3171. end
  3172. else
  3173. hdef:=hclassdef;
  3174. if (po_classmethod in current_procinfo.procdef.procoptions) or
  3175. (po_staticmethod in current_procinfo.procdef.procoptions) then
  3176. hdef:=cclassrefdef.create(hdef);
  3177. p1:=ctypenode.create(hdef);
  3178. { we need to allow helpers here }
  3179. ttypenode(p1).helperallowed:=true;
  3180. end;
  3181. propertysym:
  3182. ;
  3183. else
  3184. begin
  3185. Message(parser_e_methode_id_expected);
  3186. p1:=cerrornode.create;
  3187. end;
  3188. end;
  3189. callflags:=[cnf_inherited];
  3190. include(current_procinfo.flags,pi_has_inherited);
  3191. if anon_inherited then
  3192. include(callflags,cnf_anon_inherited);
  3193. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags,nil);
  3194. end
  3195. else
  3196. begin
  3197. if anon_inherited then
  3198. begin
  3199. { For message methods we need to call DefaultHandler }
  3200. if (po_msgint in pd.procoptions) or
  3201. (po_msgstr in pd.procoptions) then
  3202. begin
  3203. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,[ssf_search_helper]);
  3204. if not assigned(srsym) or
  3205. (srsym.typ<>procsym) then
  3206. internalerror(200303171);
  3207. p1:=nil;
  3208. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[],nil);
  3209. end
  3210. else
  3211. begin
  3212. { we need to ignore the inherited; }
  3213. p1:=cnothingnode.create;
  3214. end;
  3215. end
  3216. else
  3217. begin
  3218. Message1(sym_e_id_no_member,hsorg);
  3219. p1:=cerrornode.create;
  3220. end;
  3221. again:=false;
  3222. end;
  3223. { turn auto inheriting off }
  3224. anon_inherited:=false;
  3225. end
  3226. else
  3227. begin
  3228. { in case of records we use a more clear error message }
  3229. if assigned(current_structdef) and
  3230. (current_structdef.typ=recorddef) then
  3231. Message(parser_e_inherited_not_in_record)
  3232. else
  3233. Message(parser_e_generic_methods_only_in_methods);
  3234. again:=false;
  3235. p1:=cerrornode.create;
  3236. end;
  3237. postfixoperators(p1,again,getaddr);
  3238. end;
  3239. _INTCONST :
  3240. begin
  3241. {Try first wether the value fits in an int64.}
  3242. val(pattern,ic,code);
  3243. if code=0 then
  3244. begin
  3245. consume(_INTCONST);
  3246. int_to_type(ic,hdef);
  3247. p1:=cordconstnode.create(ic,hdef,true);
  3248. end
  3249. else
  3250. begin
  3251. { try qword next }
  3252. val(pattern,qc,code);
  3253. if code=0 then
  3254. begin
  3255. consume(_INTCONST);
  3256. int_to_type(qc,hdef);
  3257. p1:=cordconstnode.create(qc,hdef,true);
  3258. end;
  3259. end;
  3260. if code<>0 then
  3261. begin
  3262. { finally float }
  3263. val(pattern,d,code);
  3264. if code<>0 then
  3265. begin
  3266. Message(parser_e_invalid_integer);
  3267. consume(_INTCONST);
  3268. l:=1;
  3269. p1:=cordconstnode.create(l,sinttype,true);
  3270. end
  3271. else
  3272. begin
  3273. consume(_INTCONST);
  3274. p1:=crealconstnode.create(d,pbestrealtype^);
  3275. end;
  3276. end
  3277. else
  3278. { the necessary range checking has already been done by val }
  3279. tordconstnode(p1).rangecheck:=false;
  3280. if token=_POINT then
  3281. begin
  3282. again:=true;
  3283. postfixoperators(p1,again,getaddr);
  3284. end;
  3285. end;
  3286. _REALNUMBER :
  3287. begin
  3288. p1:=real_const_node_from_pattern(pattern);
  3289. consume(_REALNUMBER);
  3290. if token=_POINT then
  3291. begin
  3292. again:=true;
  3293. postfixoperators(p1,again,getaddr);
  3294. end;
  3295. end;
  3296. _STRING :
  3297. begin
  3298. string_dec(hdef,true);
  3299. { STRING can be also a type cast }
  3300. if try_to_consume(_LKLAMMER) then
  3301. begin
  3302. p1:=comp_expr(true,false);
  3303. consume(_RKLAMMER);
  3304. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3305. { handle postfix operators here e.g. string(a)[10] }
  3306. again:=true;
  3307. postfixoperators(p1,again,getaddr);
  3308. end
  3309. else
  3310. p1:=ctypenode.create(hdef);
  3311. end;
  3312. _FILE :
  3313. begin
  3314. hdef:=cfiletype;
  3315. consume(_FILE);
  3316. { FILE can be also a type cast }
  3317. if try_to_consume(_LKLAMMER) then
  3318. begin
  3319. p1:=comp_expr(true,false);
  3320. consume(_RKLAMMER);
  3321. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3322. { handle postfix operators here e.g. string(a)[10] }
  3323. again:=true;
  3324. postfixoperators(p1,again,getaddr);
  3325. end
  3326. else
  3327. begin
  3328. p1:=ctypenode.create(hdef);
  3329. end;
  3330. end;
  3331. _CSTRING :
  3332. begin
  3333. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern),nil);
  3334. consume(_CSTRING);
  3335. if token in postfixoperator_tokens then
  3336. begin
  3337. again:=true;
  3338. postfixoperators(p1,again,getaddr);
  3339. end;
  3340. end;
  3341. _CCHAR :
  3342. begin
  3343. p1:=cordconstnode.create(ord(pattern[1]),cansichartype,true);
  3344. consume(_CCHAR);
  3345. if token=_POINT then
  3346. begin
  3347. again:=true;
  3348. postfixoperators(p1,again,getaddr);
  3349. end;
  3350. end;
  3351. _CWSTRING:
  3352. begin
  3353. p1:=cstringconstnode.createunistr(patternw);
  3354. consume(_CWSTRING);
  3355. if token in postfixoperator_tokens then
  3356. begin
  3357. again:=true;
  3358. postfixoperators(p1,again,getaddr);
  3359. end;
  3360. end;
  3361. _CWCHAR:
  3362. begin
  3363. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  3364. consume(_CWCHAR);
  3365. if token=_POINT then
  3366. begin
  3367. again:=true;
  3368. postfixoperators(p1,again,getaddr);
  3369. end;
  3370. end;
  3371. _KLAMMERAFFE :
  3372. begin
  3373. consume(_KLAMMERAFFE);
  3374. got_addrn:=true;
  3375. { support both @<x> and @(<x>) }
  3376. if try_to_consume(_LKLAMMER) then
  3377. begin
  3378. p1:=factor(true,false,false);
  3379. { inside parentheses a full expression is allowed, see also tests\webtbs\tb27517.pp }
  3380. if token<>_RKLAMMER then
  3381. p1:=sub_expr(opcompare,true,false,p1);
  3382. consume(_RKLAMMER);
  3383. end
  3384. else
  3385. p1:=factor(true,false,false);
  3386. if token in postfixoperator_tokens then
  3387. begin
  3388. again:=true;
  3389. postfixoperators(p1,again,getaddr);
  3390. end;
  3391. got_addrn:=false;
  3392. p1:=caddrnode.create(p1);
  3393. p1.fileinfo:=filepos;
  3394. if cs_typed_addresses in current_settings.localswitches then
  3395. include(p1.flags,nf_typedaddr);
  3396. { Store the procvar that we are expecting, the
  3397. addrn will use the information to find the correct
  3398. procdef or it will return an error }
  3399. if assigned(getprocvardef) and
  3400. (taddrnode(p1).left.nodetype = loadn) then
  3401. taddrnode(p1).getprocvardef:=getprocvardef;
  3402. end;
  3403. _LKLAMMER :
  3404. begin
  3405. consume(_LKLAMMER);
  3406. p1:=comp_expr(true,false);
  3407. consume(_RKLAMMER);
  3408. { it's not a good solution
  3409. but (a+b)^ makes some problems }
  3410. if token in postfixoperator_tokens then
  3411. begin
  3412. again:=true;
  3413. postfixoperators(p1,again,getaddr);
  3414. end;
  3415. end;
  3416. _LECKKLAMMER :
  3417. begin
  3418. consume(_LECKKLAMMER);
  3419. p1:=factor_read_set;
  3420. consume(_RECKKLAMMER);
  3421. end;
  3422. _PLUS :
  3423. begin
  3424. consume(_PLUS);
  3425. p1:=factor(false,false,false);
  3426. p1:=cunaryplusnode.create(p1);
  3427. end;
  3428. _MINUS :
  3429. begin
  3430. consume(_MINUS);
  3431. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  3432. begin
  3433. { ugly hack, but necessary to be able to parse }
  3434. { -9223372036854775808 as int64 (JM) }
  3435. pattern := '-'+pattern;
  3436. p1:=sub_expr(oppower,false,false,nil);
  3437. { -1 ** 4 should be - (1 ** 4) and not
  3438. (-1) ** 4
  3439. This was the reason of tw0869.pp test failure PM }
  3440. if p1.nodetype=starstarn then
  3441. begin
  3442. if tbinarynode(p1).left.nodetype=ordconstn then
  3443. begin
  3444. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  3445. p1:=cunaryminusnode.create(p1);
  3446. end
  3447. else if tbinarynode(p1).left.nodetype=realconstn then
  3448. begin
  3449. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  3450. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  3451. p1:=cunaryminusnode.create(p1);
  3452. end
  3453. else
  3454. internalerror(20021029);
  3455. end;
  3456. end
  3457. else
  3458. begin
  3459. if m_isolike_unary_minus in current_settings.modeswitches then
  3460. p1:=sub_expr(opmultiply,false,false,nil)
  3461. else
  3462. p1:=sub_expr(oppower,false,false,nil);
  3463. p1:=cunaryminusnode.create(p1);
  3464. end;
  3465. end;
  3466. _OP_NOT :
  3467. begin
  3468. consume(_OP_NOT);
  3469. p1:=factor(false,false,false);
  3470. p1:=cnotnode.create(p1);
  3471. end;
  3472. _NIL :
  3473. begin
  3474. consume(_NIL);
  3475. p1:=cnilnode.create;
  3476. { It's really ugly code nil^, but delphi allows it }
  3477. if token in [_CARET,_POINT] then
  3478. begin
  3479. again:=true;
  3480. postfixoperators(p1,again,getaddr);
  3481. end;
  3482. end;
  3483. _OBJCPROTOCOL:
  3484. begin
  3485. { The @protocol keyword is used in two ways in Objective-C:
  3486. 1) to declare protocols (~ Object Pascal interfaces)
  3487. 2) to obtain the metaclass (~ Object Pascal) "class of")
  3488. of a declared protocol
  3489. This code is for handling the second case. Because of 1),
  3490. we cannot simply use a system unit symbol.
  3491. }
  3492. consume(_OBJCPROTOCOL);
  3493. consume(_LKLAMMER);
  3494. p1:=factor(false,false,false);
  3495. consume(_RKLAMMER);
  3496. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  3497. end;
  3498. else
  3499. begin
  3500. Message(parser_e_illegal_expression);
  3501. p1:=cerrornode.create;
  3502. { recover }
  3503. consume(token);
  3504. end;
  3505. end;
  3506. end;
  3507. { generate error node if no node is created }
  3508. if not assigned(p1) then
  3509. begin
  3510. {$ifdef EXTDEBUG}
  3511. Comment(V_Warning,'factor: p1=nil');
  3512. {$endif}
  3513. p1:=cerrornode.create;
  3514. updatefpos:=true;
  3515. end;
  3516. { get the resultdef for the node if nothing stops us }
  3517. if (not assigned(p1.resultdef)) and dopostfix then
  3518. begin
  3519. do_typecheckpass_changed(p1,nodechanged);
  3520. updatefpos:=updatefpos or nodechanged;
  3521. end;
  3522. if assigned(p1) and
  3523. updatefpos then
  3524. p1.fileinfo:=filepos;
  3525. factor:=p1;
  3526. end;
  3527. {$maxfpuregisters default}
  3528. procedure post_comp_expr_gendef(var def: tdef);
  3529. var
  3530. p1 : tnode;
  3531. again : boolean;
  3532. begin
  3533. if not assigned(def) then
  3534. internalerror(2011053001);
  3535. again:=false;
  3536. { handle potential typecasts, etc }
  3537. p1:=handle_factor_typenode(def,false,again,nil,false);
  3538. { parse postfix operators }
  3539. postfixoperators(p1,again,false);
  3540. if assigned(p1) and (p1.nodetype=typen) then
  3541. def:=ttypenode(p1).typedef
  3542. else
  3543. def:=generrordef;
  3544. end;
  3545. {****************************************************************************
  3546. Sub_Expr
  3547. ****************************************************************************}
  3548. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;
  3549. {Reads a subexpression while the operators are of the current precedence
  3550. level, or any higher level. Replaces the old term, simpl_expr and
  3551. simpl2_expr.}
  3552. function istypenode(n:tnode):boolean;inline;
  3553. { Checks whether the given node is a type node or a VMT node containing a
  3554. typenode. This is used in the code for inline specializations in the
  3555. _LT branch below }
  3556. begin
  3557. result:=assigned(n) and
  3558. (
  3559. (n.nodetype=typen) or
  3560. (
  3561. (n.nodetype=loadvmtaddrn) and
  3562. (tloadvmtaddrnode(n).left.nodetype=typen)
  3563. )
  3564. );
  3565. end;
  3566. function gettypedef(n:tnode):tdef;inline;
  3567. { This returns the typedef that belongs to the given typenode or
  3568. loadvmtaddrnode. n must not be Nil! }
  3569. begin
  3570. if n.nodetype=typen then
  3571. result:=ttypenode(n).typedef
  3572. else
  3573. result:=ttypenode(tloadvmtaddrnode(n).left).typedef;
  3574. end;
  3575. function gettypedef(sym:tsym):tdef;inline;
  3576. begin
  3577. result:=nil;
  3578. case sym.typ of
  3579. typesym:
  3580. result:=ttypesym(sym).typedef;
  3581. procsym:
  3582. result:=tdef(tprocsym(sym).procdeflist[0]);
  3583. else
  3584. internalerror(2015092701);
  3585. end;
  3586. end;
  3587. function getgenericsym(n:tnode;out srsym:tsym):boolean;
  3588. var
  3589. srsymtable : tsymtable;
  3590. begin
  3591. srsym:=nil;
  3592. case n.nodetype of
  3593. typen:
  3594. srsym:=ttypenode(n).typedef.typesym;
  3595. loadvmtaddrn:
  3596. srsym:=ttypenode(tloadvmtaddrnode(n).left).typedef.typesym;
  3597. loadn:
  3598. if not searchsym_with_symoption(tloadnode(n).symtableentry.Name,srsym,srsymtable,sp_generic_dummy) then
  3599. srsym:=nil;
  3600. specializen:
  3601. srsym:=tspecializenode(n).sym;
  3602. { TODO : handle const nodes }
  3603. end;
  3604. result:=assigned(srsym);
  3605. end;
  3606. function generate_inline_specialization(gendef:tdef;n:tnode;filepos:tfileposinfo;parseddef:tdef;gensym:tsym;p2:tnode):tnode;
  3607. var
  3608. again,
  3609. getaddr : boolean;
  3610. pload : tnode;
  3611. spezcontext : tspecializationcontext;
  3612. structdef : tabstractrecorddef;
  3613. begin
  3614. if n.nodetype=specializen then
  3615. begin
  3616. getaddr:=tspecializenode(n).getaddr;
  3617. pload:=tspecializenode(n).left;
  3618. tspecializenode(n).left:=nil;
  3619. end
  3620. else
  3621. begin
  3622. getaddr:=false;
  3623. pload:=nil;
  3624. end;
  3625. if assigned(parseddef) and assigned(gensym) and assigned(p2) then
  3626. gendef:=generate_specialization_phase1(spezcontext,gendef,parseddef,gensym.realname,p2.fileinfo)
  3627. else
  3628. gendef:=generate_specialization_phase1(spezcontext,gendef);
  3629. case gendef.typ of
  3630. errordef:
  3631. begin
  3632. spezcontext.free;
  3633. spezcontext:=nil;
  3634. gensym:=generrorsym;
  3635. end;
  3636. objectdef,
  3637. recorddef,
  3638. procvardef,
  3639. arraydef:
  3640. begin
  3641. gendef:=generate_specialization_phase2(spezcontext,tstoreddef(gendef),false,'');
  3642. spezcontext.free;
  3643. spezcontext:=nil;
  3644. gensym:=gendef.typesym;
  3645. end;
  3646. procdef:
  3647. begin
  3648. if block_type<>bt_body then
  3649. begin
  3650. message(parser_e_illegal_expression);
  3651. gensym:=generrorsym;
  3652. end
  3653. else
  3654. begin
  3655. gensym:=tprocdef(gendef).procsym;
  3656. end;
  3657. end;
  3658. else
  3659. internalerror(2015092702);
  3660. end;
  3661. { in case of a class or a record the specialized generic
  3662. is always a classrefdef }
  3663. again:=false;
  3664. if assigned(pload) then
  3665. begin
  3666. result:=pload;
  3667. structdef:=nil;
  3668. case result.resultdef.typ of
  3669. objectdef,
  3670. recorddef:
  3671. begin
  3672. structdef:=tabstractrecorddef(result.resultdef);
  3673. end;
  3674. classrefdef:
  3675. begin
  3676. structdef:=tabstractrecorddef(tclassrefdef(result.resultdef).pointeddef);
  3677. end;
  3678. else
  3679. internalerror(2015092703);
  3680. end;
  3681. do_member_read(structdef,getaddr,gensym,result,again,[],spezcontext);
  3682. end
  3683. else
  3684. begin
  3685. result:=nil;
  3686. { check if it's a method/class method }
  3687. if is_member_read(gensym,gensym.owner,result,parseddef) then
  3688. begin
  3689. { if we are accessing a owner procsym from the nested }
  3690. { class we need to call it as a class member }
  3691. if (gensym.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  3692. assigned(current_structdef) and (current_structdef<>parseddef) and is_owned_by(current_structdef,parseddef) then
  3693. begin
  3694. result:=cloadvmtaddrnode.create(ctypenode.create(parseddef));
  3695. { not srsymtable.symtabletype since that can be }
  3696. { withsymtable as well }
  3697. if (gensym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  3698. begin
  3699. do_member_read(tabstractrecorddef(parseddef),getaddr,gensym,result,again,[],spezcontext);
  3700. spezcontext:=nil;
  3701. end
  3702. else
  3703. { no procsyms in records (yet) }
  3704. internalerror(2015092704);
  3705. end
  3706. else
  3707. begin
  3708. { regular procedure/function call }
  3709. do_proc_call(gensym,gensym.owner,nil,
  3710. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  3711. again,result,[],spezcontext);
  3712. spezcontext:=nil;
  3713. end;
  3714. end
  3715. else
  3716. { handle potential typecasts, etc }
  3717. result:=handle_factor_typenode(gendef,false,again,nil,false);
  3718. end;
  3719. { parse postfix operators }
  3720. if postfixoperators(result,again,false) then
  3721. if assigned(result) then
  3722. result.fileinfo:=filepos
  3723. else
  3724. result:=cerrornode.create;
  3725. spezcontext.free;
  3726. end;
  3727. label
  3728. SubExprStart;
  3729. var
  3730. p1,p2,ptmp : tnode;
  3731. oldt : Ttoken;
  3732. filepos : tfileposinfo;
  3733. gendef,parseddef : tdef;
  3734. gensym : tsym;
  3735. begin
  3736. SubExprStart:
  3737. if pred_level=highest_precedence then
  3738. begin
  3739. if factornode=nil then
  3740. p1:=factor(false,typeonly,false)
  3741. else
  3742. p1:=factornode;
  3743. end
  3744. else
  3745. p1:=sub_expr(succ(pred_level),true,typeonly,factornode);
  3746. repeat
  3747. if (token in [NOTOKEN..last_operator]) and
  3748. (token in operator_levels[pred_level]) and
  3749. ((token<>_EQ) or accept_equal) then
  3750. begin
  3751. oldt:=token;
  3752. filepos:=current_tokenpos;
  3753. consume(token);
  3754. if pred_level=highest_precedence then
  3755. p2:=factor(false,false,false)
  3756. else
  3757. p2:=sub_expr(succ(pred_level),true,typeonly,nil);
  3758. case oldt of
  3759. _PLUS :
  3760. p1:=caddnode.create(addn,p1,p2);
  3761. _MINUS :
  3762. p1:=caddnode.create(subn,p1,p2);
  3763. _STAR :
  3764. p1:=caddnode.create(muln,p1,p2);
  3765. _SLASH :
  3766. p1:=caddnode.create(slashn,p1,p2);
  3767. _EQ:
  3768. p1:=caddnode.create(equaln,p1,p2);
  3769. _GT :
  3770. p1:=caddnode.create(gtn,p1,p2);
  3771. _LT :
  3772. begin
  3773. { we need to decice whether we have an inline specialization
  3774. (type nodes to the left and right of "<", mode Delphi and
  3775. ">" or "," following) or a normal "<" comparison }
  3776. { TODO : p1 could be a non type if e.g. a variable with the
  3777. same name is defined in the same unit where the
  3778. generic is defined (though "same unit" is not
  3779. necessarily needed) }
  3780. if getgenericsym(p1,gensym) and
  3781. { Attention: when nested specializations are supported
  3782. p2 could be a loadn if a "<" follows }
  3783. istypenode(p2) and
  3784. (m_delphi in current_settings.modeswitches) and
  3785. { TODO : add _LT, _LSHARPBRACKET for nested specializations }
  3786. (token in [_GT,_RSHARPBRACKET,_COMMA]) then
  3787. begin
  3788. { this is an inline specialization }
  3789. { retrieve the defs of two nodes }
  3790. if p1.nodetype=specializen then
  3791. gendef:=gettypedef(tspecializenode(p1).sym)
  3792. else
  3793. gendef:=nil;
  3794. parseddef:=gettypedef(p2);
  3795. { check the hints for parseddef }
  3796. check_hints(parseddef.typesym,parseddef.typesym.symoptions,parseddef.typesym.deprecatedmsg,p1.fileinfo);
  3797. ptmp:=generate_inline_specialization(gendef,p1,filepos,parseddef,gensym,p2);
  3798. { we don't need these nodes anymore }
  3799. p1.free;
  3800. p2.free;
  3801. p1:=ptmp;
  3802. { with p1 now set we are in reality directly behind the
  3803. call to "factor" thus we need to call down to that
  3804. again }
  3805. { This is disabled until specializations on the right
  3806. hand side work as well, because
  3807. "not working expressions" is better than "half working
  3808. expressions" }
  3809. {factornode:=p1;
  3810. goto SubExprStart;}
  3811. end
  3812. else
  3813. begin
  3814. { this is a normal "<" comparison }
  3815. { potential generic types that are followed by a "<": }
  3816. { a) might not have their resultdef set }
  3817. if not assigned(p1.resultdef) then
  3818. do_typecheckpass(p1);
  3819. { b) are not checked whether they are an undefined def,
  3820. but not a generic parameter }
  3821. if (p1.nodetype=typen) and
  3822. (ttypenode(p1).typedef.typ=undefineddef) and
  3823. assigned(ttypenode(p1).typedef.typesym) and
  3824. not (sp_generic_para in ttypenode(p1).typedef.typesym.symoptions) then
  3825. begin
  3826. identifier_not_found(ttypenode(p1).typedef.typesym.RealName);
  3827. p1.Free;
  3828. p1:=cerrornode.create;
  3829. end;
  3830. { c) don't have their hints checked }
  3831. if istypenode(p1) then
  3832. begin
  3833. gendef:=gettypedef(p1);
  3834. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3835. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3836. end;
  3837. { Note: the second part of the expression will be needed
  3838. for nested specializations }
  3839. if istypenode(p2) {and
  3840. not (token in [_LT, _LSHARPBRACKET])} then
  3841. begin
  3842. gendef:=gettypedef(p2);
  3843. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3844. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3845. end;
  3846. { create the comparison node for "<" }
  3847. p1:=caddnode.create(ltn,p1,p2)
  3848. end;
  3849. end;
  3850. _GTE :
  3851. p1:=caddnode.create(gten,p1,p2);
  3852. _LTE :
  3853. p1:=caddnode.create(lten,p1,p2);
  3854. _SYMDIF :
  3855. p1:=caddnode.create(symdifn,p1,p2);
  3856. _STARSTAR :
  3857. p1:=caddnode.create(starstarn,p1,p2);
  3858. _OP_AS,
  3859. _OP_IS :
  3860. begin
  3861. if (m_delphi in current_settings.modeswitches) and
  3862. (token in [_LT, _LSHARPBRACKET]) and
  3863. getgenericsym(p2,gensym) then
  3864. begin
  3865. { for now we're handling this as a generic declaration;
  3866. there could be cases though (because of operator
  3867. overloading) where this is the wrong decision... }
  3868. { TODO : here the same note as in _LT applies as p2 could
  3869. point to a variable, etc }
  3870. gendef:=gettypedef(p2);
  3871. ptmp:=generate_inline_specialization(gendef,p2,filepos,nil,nil,nil);
  3872. { we don't need the old p2 anymore }
  3873. p2.Free;
  3874. p2:=ptmp;
  3875. { here we don't need to call back down to "factor", thus
  3876. no "goto" }
  3877. end;
  3878. { now generate the "is" or "as" node }
  3879. case oldt of
  3880. _OP_AS:
  3881. p1:=casnode.create(p1,p2);
  3882. _OP_IS:
  3883. p1:=cisnode.create(p1,p2);
  3884. end;
  3885. end;
  3886. _OP_IN :
  3887. p1:=cinnode.create(p1,p2);
  3888. _OP_OR,
  3889. _PIPE {macpas only} :
  3890. begin
  3891. p1:=caddnode.create(orn,p1,p2);
  3892. if (oldt = _PIPE) then
  3893. include(p1.flags,nf_short_bool);
  3894. end;
  3895. _OP_AND,
  3896. _AMPERSAND {macpas only} :
  3897. begin
  3898. p1:=caddnode.create(andn,p1,p2);
  3899. if (oldt = _AMPERSAND) then
  3900. include(p1.flags,nf_short_bool);
  3901. end;
  3902. _OP_DIV :
  3903. p1:=cmoddivnode.create(divn,p1,p2);
  3904. _OP_NOT :
  3905. p1:=cnotnode.create(p1);
  3906. _OP_MOD :
  3907. begin
  3908. p1:=cmoddivnode.create(modn,p1,p2);
  3909. if m_iso in current_settings.modeswitches then
  3910. include(p1.flags,nf_isomod);
  3911. end;
  3912. _OP_SHL :
  3913. p1:=cshlshrnode.create(shln,p1,p2);
  3914. _OP_SHR :
  3915. p1:=cshlshrnode.create(shrn,p1,p2);
  3916. _OP_XOR :
  3917. p1:=caddnode.create(xorn,p1,p2);
  3918. _ASSIGNMENT :
  3919. p1:=cassignmentnode.create(p1,p2);
  3920. _NE :
  3921. p1:=caddnode.create(unequaln,p1,p2);
  3922. end;
  3923. p1.fileinfo:=filepos;
  3924. end
  3925. else
  3926. break;
  3927. until false;
  3928. sub_expr:=p1;
  3929. end;
  3930. function comp_expr(accept_equal,typeonly:boolean):tnode;
  3931. var
  3932. oldafterassignment : boolean;
  3933. p1 : tnode;
  3934. begin
  3935. oldafterassignment:=afterassignment;
  3936. afterassignment:=true;
  3937. p1:=sub_expr(opcompare,accept_equal,typeonly,nil);
  3938. { get the resultdef for this expression }
  3939. if not assigned(p1.resultdef) then
  3940. do_typecheckpass(p1);
  3941. afterassignment:=oldafterassignment;
  3942. comp_expr:=p1;
  3943. end;
  3944. function expr(dotypecheck : boolean) : tnode;
  3945. var
  3946. p1,p2 : tnode;
  3947. filepos : tfileposinfo;
  3948. oldafterassignment,
  3949. updatefpos : boolean;
  3950. begin
  3951. oldafterassignment:=afterassignment;
  3952. p1:=sub_expr(opcompare,true,false,nil);
  3953. { get the resultdef for this expression }
  3954. if not assigned(p1.resultdef) and
  3955. dotypecheck then
  3956. do_typecheckpass(p1);
  3957. filepos:=current_tokenpos;
  3958. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  3959. afterassignment:=true;
  3960. updatefpos:=true;
  3961. case token of
  3962. _POINTPOINT :
  3963. begin
  3964. consume(_POINTPOINT);
  3965. p2:=sub_expr(opcompare,true,false,nil);
  3966. p1:=crangenode.create(p1,p2);
  3967. end;
  3968. _ASSIGNMENT :
  3969. begin
  3970. consume(_ASSIGNMENT);
  3971. if assigned(p1.resultdef) and (p1.resultdef.typ=procvardef) then
  3972. getprocvardef:=tprocvardef(p1.resultdef);
  3973. p2:=sub_expr(opcompare,true,false,nil);
  3974. if assigned(getprocvardef) then
  3975. handle_procvar(getprocvardef,p2);
  3976. getprocvardef:=nil;
  3977. p1:=cassignmentnode.create(p1,p2);
  3978. end;
  3979. _PLUSASN :
  3980. begin
  3981. consume(_PLUSASN);
  3982. p2:=sub_expr(opcompare,true,false,nil);
  3983. p1:=gen_c_style_operator(addn,p1,p2);
  3984. end;
  3985. _MINUSASN :
  3986. begin
  3987. consume(_MINUSASN);
  3988. p2:=sub_expr(opcompare,true,false,nil);
  3989. p1:=gen_c_style_operator(subn,p1,p2);
  3990. end;
  3991. _STARASN :
  3992. begin
  3993. consume(_STARASN );
  3994. p2:=sub_expr(opcompare,true,false,nil);
  3995. p1:=gen_c_style_operator(muln,p1,p2);
  3996. end;
  3997. _SLASHASN :
  3998. begin
  3999. consume(_SLASHASN );
  4000. p2:=sub_expr(opcompare,true,false,nil);
  4001. p1:=gen_c_style_operator(slashn,p1,p2);
  4002. end;
  4003. else
  4004. updatefpos:=false;
  4005. end;
  4006. { get the resultdef for this expression }
  4007. if not assigned(p1.resultdef) and
  4008. dotypecheck then
  4009. do_typecheckpass(p1);
  4010. afterassignment:=oldafterassignment;
  4011. if updatefpos then
  4012. p1.fileinfo:=filepos;
  4013. expr:=p1;
  4014. end;
  4015. function get_intconst:TConstExprInt;
  4016. {Reads an expression, tries to evalute it and check if it is an integer
  4017. constant. Then the constant is returned.}
  4018. var
  4019. p:tnode;
  4020. begin
  4021. result:=0;
  4022. p:=comp_expr(true,false);
  4023. if not codegenerror then
  4024. begin
  4025. if (p.nodetype<>ordconstn) or
  4026. not(is_integer(p.resultdef)) then
  4027. Message(parser_e_illegal_expression)
  4028. else
  4029. result:=tordconstnode(p).value;
  4030. end;
  4031. p.free;
  4032. end;
  4033. function get_stringconst:string;
  4034. {Reads an expression, tries to evaluate it and checks if it is a string
  4035. constant. Then the constant is returned.}
  4036. var
  4037. p:tnode;
  4038. begin
  4039. get_stringconst:='';
  4040. p:=comp_expr(true,false);
  4041. if p.nodetype<>stringconstn then
  4042. begin
  4043. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  4044. get_stringconst:=char(int64(tordconstnode(p).value))
  4045. else
  4046. Message(parser_e_illegal_expression);
  4047. end
  4048. else
  4049. get_stringconst:=strpas(tstringconstnode(p).value_str);
  4050. p.free;
  4051. end;
  4052. end.