pexpr.pas 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163
  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. { reads a whole expression }
  25. function expr(dotypecheck:boolean) : tnode;
  26. { reads an expression without assignements and .. }
  27. function comp_expr(accept_equal,typeonly:boolean):tnode;
  28. { reads a single factor }
  29. function factor(getaddr,typeonly:boolean) : tnode;
  30. procedure string_dec(var def: tdef; allowtypedef: boolean);
  31. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  32. { the ID token has to be consumed before calling this function }
  33. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  34. function get_intconst:TConstExprInt;
  35. function get_stringconst:string;
  36. implementation
  37. uses
  38. { common }
  39. cutils,
  40. { global }
  41. verbose,
  42. systems,widestr,
  43. { symtable }
  44. symconst,symtable,symsym,defutil,defcmp,
  45. { module }
  46. fmodule,ppu,
  47. { pass 1 }
  48. pass_1,htypechk,
  49. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  50. { parser }
  51. scanner,
  52. pbase,pinline,ptype,
  53. { codegen }
  54. cgbase,procinfo,cpuinfo
  55. ;
  56. { sub_expr(opmultiply) is need to get -1 ** 4 to be
  57. read as - (1**4) and not (-1)**4 PM }
  58. type
  59. Toperator_precedence=(opcompare,opaddition,opmultiply,oppower);
  60. const
  61. highest_precedence = oppower;
  62. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;forward;
  63. const
  64. { true, if the inherited call is anonymous }
  65. anon_inherited : boolean = false;
  66. { last def found, only used by anon. inherited calls to insert proper type casts }
  67. srdef : tdef = nil;
  68. procedure string_dec(var def:tdef; allowtypedef: boolean);
  69. { reads a string type with optional length }
  70. { and returns a pointer to the string }
  71. { definition }
  72. var
  73. p : tnode;
  74. begin
  75. def:=cshortstringtype;
  76. consume(_STRING);
  77. if token=_LECKKLAMMER then
  78. begin
  79. if not(allowtypedef) then
  80. Message(parser_e_no_local_para_def);
  81. consume(_LECKKLAMMER);
  82. p:=comp_expr(true,false);
  83. if not is_constintnode(p) then
  84. begin
  85. Message(parser_e_illegal_expression);
  86. { error recovery }
  87. consume(_RECKKLAMMER);
  88. end
  89. else
  90. begin
  91. if (tordconstnode(p).value<=0) then
  92. begin
  93. Message(parser_e_invalid_string_size);
  94. tordconstnode(p).value:=255;
  95. end;
  96. if tordconstnode(p).value>255 then
  97. begin
  98. { longstring is currently unsupported (CEC)! }
  99. { t:=tstringdef.createlong(tordconstnode(p).value))}
  100. Message(parser_e_invalid_string_size);
  101. tordconstnode(p).value:=255;
  102. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  103. end
  104. else
  105. if tordconstnode(p).value<>255 then
  106. def:=tstringdef.createshort(int64(tordconstnode(p).value));
  107. consume(_RECKKLAMMER);
  108. end;
  109. p.free;
  110. end
  111. else
  112. begin
  113. if cs_ansistrings in current_settings.localswitches then
  114. def:=cansistringtype
  115. else
  116. def:=cshortstringtype;
  117. end;
  118. end;
  119. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  120. var
  121. p1,p2,argname : tnode;
  122. prev_in_args,
  123. old_named_args_allowed,
  124. old_allow_array_constructor : boolean;
  125. begin
  126. if token=end_of_paras then
  127. begin
  128. parse_paras:=nil;
  129. exit;
  130. end;
  131. { save old values }
  132. prev_in_args:=in_args;
  133. old_allow_array_constructor:=allow_array_constructor;
  134. old_named_args_allowed:=named_args_allowed;
  135. { set para parsing values }
  136. in_args:=true;
  137. named_args_allowed:=false;
  138. allow_array_constructor:=true;
  139. p2:=nil;
  140. repeat
  141. if __namedpara then
  142. begin
  143. if token=_COMMA then
  144. begin
  145. { empty parameter }
  146. p2:=ccallparanode.create(cnothingnode.create,p2);
  147. end
  148. else
  149. begin
  150. named_args_allowed:=true;
  151. p1:=comp_expr(true,false);
  152. named_args_allowed:=false;
  153. if found_arg_name then
  154. begin
  155. argname:=p1;
  156. p1:=comp_expr(true,false);
  157. p2:=ccallparanode.create(p1,p2);
  158. tcallparanode(p2).parametername:=argname;
  159. end
  160. else
  161. p2:=ccallparanode.create(p1,p2);
  162. found_arg_name:=false;
  163. end;
  164. end
  165. else
  166. begin
  167. p1:=comp_expr(true,false);
  168. p2:=ccallparanode.create(p1,p2);
  169. end;
  170. { it's for the str(l:5,s); }
  171. if __colon and (token=_COLON) then
  172. begin
  173. consume(_COLON);
  174. p1:=comp_expr(true,false);
  175. p2:=ccallparanode.create(p1,p2);
  176. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  177. if try_to_consume(_COLON) then
  178. begin
  179. p1:=comp_expr(true,false);
  180. p2:=ccallparanode.create(p1,p2);
  181. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  182. end
  183. end;
  184. until not try_to_consume(_COMMA);
  185. allow_array_constructor:=old_allow_array_constructor;
  186. in_args:=prev_in_args;
  187. named_args_allowed:=old_named_args_allowed;
  188. parse_paras:=p2;
  189. end;
  190. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  191. var
  192. hp : tnode;
  193. hdef : tdef;
  194. temp : ttempcreatenode;
  195. newstatement : tstatementnode;
  196. begin
  197. { Properties are not allowed, because the write can
  198. be different from the read }
  199. if (nf_isproperty in p1.flags) then
  200. begin
  201. Message(type_e_variable_id_expected);
  202. { We can continue with the loading,
  203. it'll not create errors. Only the expected
  204. result can be wrong }
  205. end;
  206. hp:=p1;
  207. while assigned(hp) and
  208. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  209. hp:=tunarynode(hp).left;
  210. if not assigned(hp) then
  211. internalerror(200410121);
  212. if (hp.nodetype=calln) then
  213. begin
  214. typecheckpass(p1);
  215. result:=internalstatements(newstatement);
  216. hdef:=tpointerdef.create(p1.resultdef);
  217. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  218. addstatement(newstatement,temp);
  219. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  220. addstatement(newstatement,cassignmentnode.create(
  221. cderefnode.create(ctemprefnode.create(temp)),
  222. caddnode.create(ntyp,
  223. cderefnode.create(ctemprefnode.create(temp)),
  224. p2)));
  225. addstatement(newstatement,ctempdeletenode.create(temp));
  226. end
  227. else
  228. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  229. end;
  230. function statement_syssym(l : byte) : tnode;
  231. var
  232. p1,p2,paras : tnode;
  233. err,
  234. prev_in_args : boolean;
  235. begin
  236. prev_in_args:=in_args;
  237. case l of
  238. in_new_x :
  239. begin
  240. if afterassignment or in_args then
  241. statement_syssym:=new_function
  242. else
  243. statement_syssym:=new_dispose_statement(true);
  244. end;
  245. in_dispose_x :
  246. begin
  247. statement_syssym:=new_dispose_statement(false);
  248. end;
  249. in_ord_x :
  250. begin
  251. consume(_LKLAMMER);
  252. in_args:=true;
  253. p1:=comp_expr(true,false);
  254. consume(_RKLAMMER);
  255. p1:=geninlinenode(in_ord_x,false,p1);
  256. statement_syssym := p1;
  257. end;
  258. in_exit :
  259. begin
  260. if try_to_consume(_LKLAMMER) then
  261. begin
  262. if not (m_mac in current_settings.modeswitches) then
  263. begin
  264. if not(try_to_consume(_RKLAMMER)) then
  265. begin
  266. p1:=comp_expr(true,false);
  267. consume(_RKLAMMER);
  268. if (not assigned(current_procinfo) or
  269. is_void(current_procinfo.procdef.returndef)) then
  270. begin
  271. Message(parser_e_void_function);
  272. { recovery }
  273. p1.free;
  274. p1:=nil;
  275. end;
  276. end
  277. else
  278. p1:=nil;
  279. end
  280. else
  281. begin
  282. if not (current_procinfo.procdef.procsym.name = pattern) then
  283. Message(parser_e_macpas_exit_wrong_param);
  284. consume(_ID);
  285. consume(_RKLAMMER);
  286. p1:=nil;
  287. end
  288. end
  289. else
  290. p1:=nil;
  291. statement_syssym:=cexitnode.create(p1);
  292. end;
  293. in_break :
  294. begin
  295. statement_syssym:=cbreaknode.create
  296. end;
  297. in_continue :
  298. begin
  299. statement_syssym:=ccontinuenode.create
  300. end;
  301. in_leave :
  302. begin
  303. if m_mac in current_settings.modeswitches then
  304. statement_syssym:=cbreaknode.create
  305. else
  306. begin
  307. Message1(sym_e_id_not_found, orgpattern);
  308. statement_syssym:=cerrornode.create;
  309. end;
  310. end;
  311. in_cycle :
  312. begin
  313. if m_mac in current_settings.modeswitches then
  314. statement_syssym:=ccontinuenode.create
  315. else
  316. begin
  317. Message1(sym_e_id_not_found, orgpattern);
  318. statement_syssym:=cerrornode.create;
  319. end;
  320. end;
  321. in_typeof_x :
  322. begin
  323. consume(_LKLAMMER);
  324. in_args:=true;
  325. p1:=comp_expr(true,false);
  326. consume(_RKLAMMER);
  327. if p1.nodetype=typen then
  328. ttypenode(p1).allowed:=true;
  329. { Allow classrefdef, which is required for
  330. Typeof(self) in static class methods }
  331. if not(is_objc_class_or_protocol(p1.resultdef)) and
  332. ((p1.resultdef.typ = objectdef) or
  333. (assigned(current_procinfo) and
  334. ((po_classmethod in current_procinfo.procdef.procoptions) or
  335. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  336. (p1.resultdef.typ=classrefdef))) then
  337. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  338. else
  339. begin
  340. Message(parser_e_class_id_expected);
  341. p1.destroy;
  342. statement_syssym:=cerrornode.create;
  343. end;
  344. end;
  345. in_sizeof_x,
  346. in_bitsizeof_x :
  347. begin
  348. consume(_LKLAMMER);
  349. in_args:=true;
  350. p1:=comp_expr(true,false);
  351. consume(_RKLAMMER);
  352. if ((p1.nodetype<>typen) and
  353. (
  354. (is_object(p1.resultdef) and
  355. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  356. is_open_array(p1.resultdef) or
  357. is_array_of_const(p1.resultdef) or
  358. is_open_string(p1.resultdef)
  359. )) or
  360. { keep the function call if it is a type parameter to avoid arithmetic errors due to constant folding }
  361. (p1.resultdef.typ=undefineddef) then
  362. begin
  363. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  364. { no packed bit support for these things }
  365. if (l = in_bitsizeof_x) then
  366. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  367. end
  368. else
  369. begin
  370. { allow helpers for SizeOf and BitSizeOf }
  371. if p1.nodetype=typen then
  372. ttypenode(p1).helperallowed:=true;
  373. if (p1.resultdef.typ=forwarddef) then
  374. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  375. if (l = in_sizeof_x) or
  376. (not((p1.nodetype = vecn) and
  377. is_packed_array(tvecnode(p1).left.resultdef)) and
  378. not((p1.nodetype = subscriptn) and
  379. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  380. begin
  381. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  382. if (l = in_bitsizeof_x) then
  383. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  384. end
  385. else
  386. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  387. { p1 not needed !}
  388. p1.destroy;
  389. end;
  390. end;
  391. in_typeinfo_x,
  392. in_objc_encode_x :
  393. begin
  394. if (l=in_typeinfo_x) or
  395. (m_objectivec1 in current_settings.modeswitches) then
  396. begin
  397. consume(_LKLAMMER);
  398. in_args:=true;
  399. p1:=comp_expr(true,false);
  400. { When reading a class type it is parsed as loadvmtaddrn,
  401. typeinfo only needs the type so we remove the loadvmtaddrn }
  402. if p1.nodetype=loadvmtaddrn then
  403. begin
  404. p2:=tloadvmtaddrnode(p1).left;
  405. tloadvmtaddrnode(p1).left:=nil;
  406. p1.free;
  407. p1:=p2;
  408. end;
  409. if p1.nodetype=typen then
  410. begin
  411. ttypenode(p1).allowed:=true;
  412. { allow helpers for TypeInfo }
  413. if l=in_typeinfo_x then
  414. ttypenode(p1).helperallowed:=true;
  415. end;
  416. { else
  417. begin
  418. p1.destroy;
  419. p1:=cerrornode.create;
  420. Message(parser_e_illegal_parameter_list);
  421. end;}
  422. consume(_RKLAMMER);
  423. p2:=geninlinenode(l,false,p1);
  424. statement_syssym:=p2;
  425. end
  426. else
  427. begin
  428. Message1(sym_e_id_not_found, orgpattern);
  429. statement_syssym:=cerrornode.create;
  430. end;
  431. end;
  432. in_unaligned_x :
  433. begin
  434. err:=false;
  435. consume(_LKLAMMER);
  436. in_args:=true;
  437. p1:=comp_expr(true,false);
  438. p2:=ccallparanode.create(p1,nil);
  439. p2:=geninlinenode(in_unaligned_x,false,p2);
  440. consume(_RKLAMMER);
  441. statement_syssym:=p2;
  442. end;
  443. in_assigned_x :
  444. begin
  445. err:=false;
  446. consume(_LKLAMMER);
  447. in_args:=true;
  448. p1:=comp_expr(true,false);
  449. { When reading a class type it is parsed as loadvmtaddrn,
  450. typeinfo only needs the type so we remove the loadvmtaddrn }
  451. if p1.nodetype=loadvmtaddrn then
  452. begin
  453. p2:=tloadvmtaddrnode(p1).left;
  454. tloadvmtaddrnode(p1).left:=nil;
  455. p1.free;
  456. p1:=p2;
  457. end;
  458. if not codegenerror then
  459. begin
  460. case p1.resultdef.typ of
  461. procdef, { procvar }
  462. pointerdef,
  463. procvardef,
  464. classrefdef : ;
  465. objectdef :
  466. if not is_implicit_pointer_object_type(p1.resultdef) then
  467. begin
  468. Message(parser_e_illegal_parameter_list);
  469. err:=true;
  470. end;
  471. arraydef :
  472. if not is_dynamic_array(p1.resultdef) then
  473. begin
  474. Message(parser_e_illegal_parameter_list);
  475. err:=true;
  476. end;
  477. else
  478. if p1.resultdef.typ<>undefineddef then
  479. begin
  480. Message(parser_e_illegal_parameter_list);
  481. err:=true;
  482. end;
  483. end;
  484. end
  485. else
  486. err:=true;
  487. if not err then
  488. begin
  489. p2:=ccallparanode.create(p1,nil);
  490. p2:=geninlinenode(in_assigned_x,false,p2);
  491. end
  492. else
  493. begin
  494. p1.free;
  495. p2:=cerrornode.create;
  496. end;
  497. consume(_RKLAMMER);
  498. statement_syssym:=p2;
  499. end;
  500. in_addr_x :
  501. begin
  502. consume(_LKLAMMER);
  503. in_args:=true;
  504. p1:=comp_expr(true,false);
  505. p1:=caddrnode.create(p1);
  506. consume(_RKLAMMER);
  507. statement_syssym:=p1;
  508. end;
  509. in_ofs_x :
  510. begin
  511. consume(_LKLAMMER);
  512. in_args:=true;
  513. p1:=comp_expr(true,false);
  514. p1:=caddrnode.create(p1);
  515. do_typecheckpass(p1);
  516. { Ofs() returns a cardinal/qword, not a pointer }
  517. p1.resultdef:=uinttype;
  518. consume(_RKLAMMER);
  519. statement_syssym:=p1;
  520. end;
  521. in_seg_x :
  522. begin
  523. consume(_LKLAMMER);
  524. in_args:=true;
  525. p1:=comp_expr(true,false);
  526. p1:=geninlinenode(in_seg_x,false,p1);
  527. consume(_RKLAMMER);
  528. statement_syssym:=p1;
  529. end;
  530. in_high_x,
  531. in_low_x :
  532. begin
  533. consume(_LKLAMMER);
  534. in_args:=true;
  535. p1:=comp_expr(true,false);
  536. p2:=geninlinenode(l,false,p1);
  537. consume(_RKLAMMER);
  538. statement_syssym:=p2;
  539. end;
  540. in_succ_x,
  541. in_pred_x :
  542. begin
  543. consume(_LKLAMMER);
  544. in_args:=true;
  545. p1:=comp_expr(true,false);
  546. p2:=geninlinenode(l,false,p1);
  547. consume(_RKLAMMER);
  548. statement_syssym:=p2;
  549. end;
  550. in_inc_x,
  551. in_dec_x :
  552. begin
  553. consume(_LKLAMMER);
  554. in_args:=true;
  555. p1:=comp_expr(true,false);
  556. if try_to_consume(_COMMA) then
  557. p2:=ccallparanode.create(comp_expr(true,false),nil)
  558. else
  559. p2:=nil;
  560. p2:=ccallparanode.create(p1,p2);
  561. statement_syssym:=geninlinenode(l,false,p2);
  562. consume(_RKLAMMER);
  563. end;
  564. in_slice_x:
  565. begin
  566. if not(in_args) then
  567. begin
  568. message(parser_e_illegal_slice);
  569. consume(_LKLAMMER);
  570. in_args:=true;
  571. comp_expr(true,false).free;
  572. if try_to_consume(_COMMA) then
  573. comp_expr(true,false).free;
  574. statement_syssym:=cerrornode.create;
  575. consume(_RKLAMMER);
  576. end
  577. else
  578. begin
  579. consume(_LKLAMMER);
  580. in_args:=true;
  581. p1:=comp_expr(true,false);
  582. Consume(_COMMA);
  583. if not(codegenerror) then
  584. p2:=ccallparanode.create(comp_expr(true,false),nil)
  585. else
  586. p2:=cerrornode.create;
  587. p2:=ccallparanode.create(p1,p2);
  588. statement_syssym:=geninlinenode(l,false,p2);
  589. consume(_RKLAMMER);
  590. end;
  591. end;
  592. in_initialize_x:
  593. begin
  594. statement_syssym:=inline_initialize;
  595. end;
  596. in_finalize_x:
  597. begin
  598. statement_syssym:=inline_finalize;
  599. end;
  600. in_copy_x:
  601. begin
  602. statement_syssym:=inline_copy;
  603. end;
  604. in_concat_x :
  605. begin
  606. consume(_LKLAMMER);
  607. in_args:=true;
  608. { Translate to x:=x+y[+z]. The addnode will do the
  609. type checking }
  610. p2:=nil;
  611. repeat
  612. p1:=comp_expr(true,false);
  613. if p2<>nil then
  614. p2:=caddnode.create(addn,p2,p1)
  615. else
  616. begin
  617. { Force string type if it isn't yet }
  618. if not(
  619. (p1.resultdef.typ=stringdef) or
  620. is_chararray(p1.resultdef) or
  621. is_char(p1.resultdef)
  622. ) then
  623. inserttypeconv(p1,cshortstringtype);
  624. p2:=p1;
  625. end;
  626. until not try_to_consume(_COMMA);
  627. consume(_RKLAMMER);
  628. statement_syssym:=p2;
  629. end;
  630. in_read_x,
  631. in_readln_x,
  632. in_readstr_x:
  633. begin
  634. if try_to_consume(_LKLAMMER) then
  635. begin
  636. paras:=parse_paras(false,false,_RKLAMMER);
  637. consume(_RKLAMMER);
  638. end
  639. else
  640. paras:=nil;
  641. p1:=geninlinenode(l,false,paras);
  642. statement_syssym := p1;
  643. end;
  644. in_setlength_x:
  645. begin
  646. statement_syssym := inline_setlength;
  647. end;
  648. in_objc_selector_x:
  649. begin
  650. if (m_objectivec1 in current_settings.modeswitches) then
  651. begin
  652. consume(_LKLAMMER);
  653. in_args:=true;
  654. { don't turn procsyms into calls (getaddr = true) }
  655. p1:=factor(true,false);
  656. p2:=geninlinenode(l,false,p1);
  657. consume(_RKLAMMER);
  658. statement_syssym:=p2;
  659. end
  660. else
  661. begin
  662. Message1(sym_e_id_not_found, orgpattern);
  663. statement_syssym:=cerrornode.create;
  664. end;
  665. end;
  666. in_length_x:
  667. begin
  668. consume(_LKLAMMER);
  669. in_args:=true;
  670. p1:=comp_expr(true,false);
  671. p2:=geninlinenode(l,false,p1);
  672. consume(_RKLAMMER);
  673. statement_syssym:=p2;
  674. end;
  675. in_write_x,
  676. in_writeln_x,
  677. in_writestr_x :
  678. begin
  679. if try_to_consume(_LKLAMMER) then
  680. begin
  681. paras:=parse_paras(true,false,_RKLAMMER);
  682. consume(_RKLAMMER);
  683. end
  684. else
  685. paras:=nil;
  686. p1 := geninlinenode(l,false,paras);
  687. statement_syssym := p1;
  688. end;
  689. in_str_x_string :
  690. begin
  691. consume(_LKLAMMER);
  692. paras:=parse_paras(true,false,_RKLAMMER);
  693. consume(_RKLAMMER);
  694. p1 := geninlinenode(l,false,paras);
  695. statement_syssym := p1;
  696. end;
  697. in_val_x:
  698. Begin
  699. consume(_LKLAMMER);
  700. in_args := true;
  701. p1:= ccallparanode.create(comp_expr(true,false), nil);
  702. consume(_COMMA);
  703. p2 := ccallparanode.create(comp_expr(true,false),p1);
  704. if try_to_consume(_COMMA) then
  705. p2 := ccallparanode.create(comp_expr(true,false),p2);
  706. consume(_RKLAMMER);
  707. p2 := geninlinenode(l,false,p2);
  708. statement_syssym := p2;
  709. End;
  710. in_include_x_y,
  711. in_exclude_x_y :
  712. begin
  713. consume(_LKLAMMER);
  714. in_args:=true;
  715. p1:=comp_expr(true,false);
  716. consume(_COMMA);
  717. p2:=comp_expr(true,false);
  718. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  719. consume(_RKLAMMER);
  720. end;
  721. in_pack_x_y_z,
  722. in_unpack_x_y_z :
  723. begin
  724. consume(_LKLAMMER);
  725. in_args:=true;
  726. p1:=comp_expr(true,false);
  727. consume(_COMMA);
  728. p2:=comp_expr(true,false);
  729. consume(_COMMA);
  730. paras:=comp_expr(true,false);
  731. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  732. consume(_RKLAMMER);
  733. end;
  734. in_assert_x_y :
  735. begin
  736. consume(_LKLAMMER);
  737. in_args:=true;
  738. p1:=comp_expr(true,false);
  739. if try_to_consume(_COMMA) then
  740. p2:=comp_expr(true,false)
  741. else
  742. begin
  743. { then insert an empty string }
  744. p2:=cstringconstnode.createstr('');
  745. end;
  746. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  747. consume(_RKLAMMER);
  748. end;
  749. in_get_frame:
  750. begin
  751. statement_syssym:=geninlinenode(l,false,nil);
  752. end;
  753. (*
  754. in_get_caller_frame:
  755. begin
  756. if try_to_consume(_LKLAMMER) then
  757. begin
  758. {You used to call get_caller_frame as get_caller_frame(get_frame),
  759. however, as a stack frame may not exist, it does more harm than
  760. good, so ignore it.}
  761. in_args:=true;
  762. p1:=comp_expr(true,false);
  763. p1.destroy;
  764. consume(_RKLAMMER);
  765. end;
  766. statement_syssym:=geninlinenode(l,false,nil);
  767. end;
  768. *)
  769. else
  770. internalerror(15);
  771. end;
  772. in_args:=prev_in_args;
  773. end;
  774. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  775. begin
  776. maybe_load_methodpointer:=false;
  777. if not assigned(p1) then
  778. begin
  779. case st.symtabletype of
  780. withsymtable :
  781. begin
  782. if (st.defowner.typ=objectdef) then
  783. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  784. end;
  785. ObjectSymtable,
  786. recordsymtable:
  787. begin
  788. { We are calling from the static class method which has no self node }
  789. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  790. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  791. else
  792. p1:=load_self_node;
  793. { We are calling a member }
  794. maybe_load_methodpointer:=true;
  795. end;
  796. end;
  797. end;
  798. end;
  799. { reads the parameter for a subroutine call }
  800. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags);
  801. var
  802. membercall,
  803. prevafterassn : boolean;
  804. i : integer;
  805. para,p2 : tnode;
  806. currpara : tparavarsym;
  807. aprocdef : tprocdef;
  808. begin
  809. prevafterassn:=afterassignment;
  810. afterassignment:=false;
  811. membercall:=false;
  812. aprocdef:=nil;
  813. { when it is a call to a member we need to load the
  814. methodpointer first
  815. }
  816. membercall:=maybe_load_methodpointer(st,p1);
  817. { When we are expecting a procvar we also need
  818. to get the address in some cases }
  819. if assigned(getprocvardef) then
  820. begin
  821. if (block_type=bt_const) or
  822. getaddr then
  823. begin
  824. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  825. getaddr:=true;
  826. end
  827. else
  828. if (m_tp_procvar in current_settings.modeswitches) or
  829. (m_mac_procvar in current_settings.modeswitches) then
  830. begin
  831. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  832. if assigned(aprocdef) then
  833. getaddr:=true;
  834. end;
  835. end;
  836. { only need to get the address of the procedure? }
  837. if getaddr then
  838. begin
  839. { Retrieve info which procvar to call. For tp_procvar the
  840. aprocdef is already loaded above so we can reuse it }
  841. if not assigned(aprocdef) and
  842. assigned(getprocvardef) then
  843. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  844. { generate a methodcallnode or proccallnode }
  845. { we shouldn't convert things like @tcollection.load }
  846. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  847. if assigned(p1) then
  848. begin
  849. { for loading methodpointer of an inherited function
  850. we use self as instance and load the address of
  851. the function directly and not through the vmt (PFV) }
  852. if (cnf_inherited in callflags) then
  853. begin
  854. include(p2.flags,nf_inherited);
  855. p1.free;
  856. p1:=load_self_node;
  857. end;
  858. if (p1.nodetype<>typen) then
  859. tloadnode(p2).set_mp(p1)
  860. else
  861. p1.free;
  862. end;
  863. p1:=p2;
  864. { no postfix operators }
  865. again:=false;
  866. end
  867. else
  868. begin
  869. para:=nil;
  870. if anon_inherited then
  871. begin
  872. if not assigned(current_procinfo) then
  873. internalerror(200305054);
  874. for i:=0 to current_procinfo.procdef.paras.count-1 do
  875. begin
  876. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  877. if not(vo_is_hidden_para in currpara.varoptions) then
  878. begin
  879. { inheritance by msgint? }
  880. if assigned(srdef) then
  881. { anonymous inherited via msgid calls only require a var parameter for
  882. both methods, so we need some type casting here }
  883. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  884. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  885. para)
  886. else
  887. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  888. end;
  889. end;
  890. end
  891. else
  892. begin
  893. if try_to_consume(_LKLAMMER) then
  894. begin
  895. para:=parse_paras(false,false,_RKLAMMER);
  896. consume(_RKLAMMER);
  897. end;
  898. end;
  899. { indicate if this call was generated by a member and
  900. no explicit self is used, this is needed to determine
  901. how to handle a destructor call (PFV) }
  902. if membercall then
  903. include(callflags,cnf_member_call);
  904. if assigned(obj) then
  905. begin
  906. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  907. internalerror(200310031);
  908. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags);
  909. end
  910. else
  911. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags);
  912. end;
  913. afterassignment:=prevafterassn;
  914. end;
  915. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  916. var
  917. hp,hp2 : tnode;
  918. hpp : ^tnode;
  919. currprocdef : tprocdef;
  920. begin
  921. if not assigned(pv) then
  922. internalerror(200301121);
  923. if (m_tp_procvar in current_settings.modeswitches) or
  924. (m_mac_procvar in current_settings.modeswitches) then
  925. begin
  926. hp:=p2;
  927. hpp:=@p2;
  928. while assigned(hp) and
  929. (hp.nodetype=typeconvn) do
  930. begin
  931. hp:=ttypeconvnode(hp).left;
  932. { save orignal address of the old tree so we can replace the node }
  933. hpp:=@hp;
  934. end;
  935. if (hp.nodetype=calln) and
  936. { a procvar can't have parameters! }
  937. not assigned(tcallnode(hp).left) then
  938. begin
  939. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  940. if assigned(currprocdef) then
  941. begin
  942. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  943. if (po_methodpointer in pv.procoptions) then
  944. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  945. hp.destroy;
  946. { replace the old callnode with the new loadnode }
  947. hpp^:=hp2;
  948. end;
  949. end;
  950. end;
  951. end;
  952. { checks whether sym is a static field and if so, translates the access
  953. to the appropriate node tree }
  954. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  955. var
  956. static_name: shortstring;
  957. srsymtable: tsymtable;
  958. begin
  959. result:=false;
  960. { generate access code }
  961. if (sp_static in sym.symoptions) then
  962. begin
  963. result:=true;
  964. if not nested then
  965. static_name:=lower(sym.owner.name^)+'_'+sym.name
  966. else
  967. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  968. if sym.owner.defowner.typ=objectdef then
  969. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,true)
  970. else
  971. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  972. if assigned(sym) then
  973. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  974. p1.free;
  975. p1:=nil;
  976. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  977. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  978. end;
  979. end;
  980. { the following procedure handles the access to a property symbol }
  981. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  982. var
  983. paras : tnode;
  984. p2 : tnode;
  985. membercall : boolean;
  986. callflags : tcallnodeflags;
  987. propaccesslist : tpropaccesslist;
  988. sym: tsym;
  989. begin
  990. { property parameters? read them only if the property really }
  991. { has parameters }
  992. paras:=nil;
  993. if (ppo_hasparameters in propsym.propoptions) then
  994. begin
  995. if try_to_consume(_LECKKLAMMER) then
  996. begin
  997. paras:=parse_paras(false,false,_RECKKLAMMER);
  998. consume(_RECKKLAMMER);
  999. end;
  1000. end;
  1001. { indexed property }
  1002. if (ppo_indexed in propsym.propoptions) then
  1003. begin
  1004. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1005. paras:=ccallparanode.create(p2,paras);
  1006. end;
  1007. { we need only a write property if a := follows }
  1008. { if not(afterassignment) and not(in_args) then }
  1009. if token=_ASSIGNMENT then
  1010. begin
  1011. if getpropaccesslist(propsym,palt_write,propaccesslist) then
  1012. begin
  1013. sym:=propaccesslist.firstsym^.sym;
  1014. case sym.typ of
  1015. procsym :
  1016. begin
  1017. callflags:=[];
  1018. { generate the method call }
  1019. membercall:=maybe_load_methodpointer(st,p1);
  1020. if membercall then
  1021. include(callflags,cnf_member_call);
  1022. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1023. addsymref(sym);
  1024. paras:=nil;
  1025. consume(_ASSIGNMENT);
  1026. { read the expression }
  1027. if propsym.propdef.typ=procvardef then
  1028. getprocvardef:=tprocvardef(propsym.propdef);
  1029. p2:=comp_expr(true,false);
  1030. if assigned(getprocvardef) then
  1031. handle_procvar(getprocvardef,p2);
  1032. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1033. { mark as property, both the tcallnode and the real call block }
  1034. include(p1.flags,nf_isproperty);
  1035. getprocvardef:=nil;
  1036. end;
  1037. fieldvarsym :
  1038. begin
  1039. { generate access code }
  1040. if not handle_staticfield_access(sym,false,p1) then
  1041. propaccesslist_to_node(p1,st,propaccesslist);
  1042. include(p1.flags,nf_isproperty);
  1043. consume(_ASSIGNMENT);
  1044. { read the expression }
  1045. p2:=comp_expr(true,false);
  1046. p1:=cassignmentnode.create(p1,p2);
  1047. end
  1048. else
  1049. begin
  1050. p1:=cerrornode.create;
  1051. Message(parser_e_no_procedure_to_access_property);
  1052. end;
  1053. end;
  1054. end
  1055. else
  1056. begin
  1057. p1:=cerrornode.create;
  1058. Message(parser_e_no_procedure_to_access_property);
  1059. end;
  1060. end
  1061. else
  1062. begin
  1063. if getpropaccesslist(propsym,palt_read,propaccesslist) then
  1064. begin
  1065. sym := propaccesslist.firstsym^.sym;
  1066. case sym.typ of
  1067. fieldvarsym :
  1068. begin
  1069. { generate access code }
  1070. if not handle_staticfield_access(sym,false,p1) then
  1071. propaccesslist_to_node(p1,st,propaccesslist);
  1072. include(p1.flags,nf_isproperty);
  1073. end;
  1074. procsym :
  1075. begin
  1076. callflags:=[];
  1077. { generate the method call }
  1078. membercall:=maybe_load_methodpointer(st,p1);
  1079. if membercall then
  1080. include(callflags,cnf_member_call);
  1081. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags);
  1082. paras:=nil;
  1083. include(p1.flags,nf_isproperty);
  1084. end
  1085. else
  1086. begin
  1087. p1:=cerrornode.create;
  1088. Message(type_e_mismatch);
  1089. end;
  1090. end;
  1091. end
  1092. else
  1093. begin
  1094. { error, no function to read property }
  1095. p1:=cerrornode.create;
  1096. Message(parser_e_no_procedure_to_access_property);
  1097. end;
  1098. end;
  1099. { release paras if not used }
  1100. if assigned(paras) then
  1101. paras.free;
  1102. end;
  1103. { the ID token has to be consumed before calling this function }
  1104. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags);
  1105. var
  1106. isclassref : boolean;
  1107. begin
  1108. if sym=nil then
  1109. begin
  1110. { pattern is still valid unless
  1111. there is another ID just after the ID of sym }
  1112. Message1(sym_e_id_no_member,orgpattern);
  1113. p1.free;
  1114. p1:=cerrornode.create;
  1115. { try to clean up }
  1116. again:=false;
  1117. end
  1118. else
  1119. begin
  1120. if assigned(p1) then
  1121. begin
  1122. if not assigned(p1.resultdef) then
  1123. do_typecheckpass(p1);
  1124. isclassref:=(p1.resultdef.typ=classrefdef);
  1125. end
  1126. else
  1127. isclassref:=false;
  1128. { we assume, that only procsyms and varsyms are in an object }
  1129. { symbol table, for classes, properties are allowed }
  1130. case sym.typ of
  1131. procsym:
  1132. begin
  1133. do_proc_call(sym,sym.owner,structh,
  1134. (getaddr and not(token in [_CARET,_POINT])),
  1135. again,p1,callflags);
  1136. { we need to know which procedure is called }
  1137. do_typecheckpass(p1);
  1138. { calling using classref? }
  1139. if isclassref and
  1140. (p1.nodetype=calln) and
  1141. assigned(tcallnode(p1).procdefinition) and
  1142. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1143. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1144. Message(parser_e_only_class_members_via_class_ref);
  1145. end;
  1146. fieldvarsym:
  1147. begin
  1148. if not handle_staticfield_access(sym,true,p1) then
  1149. begin
  1150. if isclassref then
  1151. if assigned(p1) and
  1152. (
  1153. is_self_node(p1) or
  1154. (assigned(current_procinfo) and (current_procinfo.procdef.no_self_node) and
  1155. (current_procinfo.procdef.struct=structh))) then
  1156. Message(parser_e_only_class_members)
  1157. else
  1158. Message(parser_e_only_class_members_via_class_ref);
  1159. p1:=csubscriptnode.create(sym,p1);
  1160. end;
  1161. end;
  1162. propertysym:
  1163. begin
  1164. if isclassref and not (sp_static in sym.symoptions) then
  1165. Message(parser_e_only_class_members_via_class_ref);
  1166. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1167. end;
  1168. typesym:
  1169. begin
  1170. p1.free;
  1171. if try_to_consume(_LKLAMMER) then
  1172. begin
  1173. p1:=comp_expr(true,false);
  1174. consume(_RKLAMMER);
  1175. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1176. end
  1177. else
  1178. begin
  1179. p1:=ctypenode.create(ttypesym(sym).typedef);
  1180. if (is_class(ttypesym(sym).typedef) or is_objcclass(ttypesym(sym).typedef)) and
  1181. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1182. p1:=cloadvmtaddrnode.create(p1);
  1183. end;
  1184. end;
  1185. constsym:
  1186. begin
  1187. p1.free;
  1188. p1:=genconstsymtree(tconstsym(sym));
  1189. end;
  1190. staticvarsym:
  1191. begin
  1192. { typed constant is a staticvarsym
  1193. now they are absolutevarsym }
  1194. p1.free;
  1195. p1:=cloadnode.create(sym,sym.Owner);
  1196. end;
  1197. absolutevarsym:
  1198. begin
  1199. p1.free;
  1200. p1:=nil;
  1201. { typed constants are absolutebarsyms now to handle storage properly }
  1202. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1203. end
  1204. else
  1205. internalerror(16);
  1206. end;
  1207. end;
  1208. end;
  1209. {****************************************************************************
  1210. Factor
  1211. ****************************************************************************}
  1212. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  1213. out memberparentdef: tdef): boolean;
  1214. var
  1215. hdef : tdef;
  1216. begin
  1217. result:=true;
  1218. memberparentdef:=nil;
  1219. case st.symtabletype of
  1220. ObjectSymtable,
  1221. recordsymtable:
  1222. begin
  1223. memberparentdef:=tdef(st.defowner);
  1224. exit;
  1225. end;
  1226. WithSymtable:
  1227. begin
  1228. if assigned(p1) then
  1229. internalerror(2007012002);
  1230. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  1231. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1232. if not(hdef.typ in [objectdef,classrefdef]) then
  1233. exit;
  1234. if (hdef.typ=classrefdef) then
  1235. hdef:=tclassrefdef(hdef).pointeddef;
  1236. memberparentdef:=hdef;
  1237. end;
  1238. else
  1239. result:=false;
  1240. end;
  1241. end;
  1242. {$maxfpuregisters 0}
  1243. function factor(getaddr,typeonly:boolean) : tnode;
  1244. {---------------------------------------------
  1245. Factor_read_id
  1246. ---------------------------------------------}
  1247. procedure factor_read_id(out p1:tnode;var again:boolean);
  1248. var
  1249. srsym : tsym;
  1250. srsymtable : TSymtable;
  1251. hdef : tdef;
  1252. orgstoredpattern,
  1253. storedpattern : string;
  1254. callflags: tcallnodeflags;
  1255. t : ttoken;
  1256. unit_found : boolean;
  1257. begin
  1258. { allow post fix operators }
  1259. again:=true;
  1260. { first check for identifier }
  1261. if token<>_ID then
  1262. begin
  1263. srsym:=generrorsym;
  1264. srsymtable:=nil;
  1265. consume(_ID);
  1266. end
  1267. else
  1268. begin
  1269. if typeonly then
  1270. searchsym_type(pattern,srsym,srsymtable)
  1271. else
  1272. searchsym(pattern,srsym,srsymtable);
  1273. { handle unit specification like System.Writeln }
  1274. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true);
  1275. storedpattern:=pattern;
  1276. orgstoredpattern:=orgpattern;
  1277. consume(t);
  1278. { named parameter support }
  1279. found_arg_name:=false;
  1280. if not(unit_found) and
  1281. named_args_allowed and
  1282. (token=_ASSIGNMENT) then
  1283. begin
  1284. found_arg_name:=true;
  1285. p1:=cstringconstnode.createstr(storedpattern);
  1286. consume(_ASSIGNMENT);
  1287. exit;
  1288. end;
  1289. { if nothing found give error and return errorsym }
  1290. if assigned(srsym) then
  1291. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg)
  1292. else
  1293. begin
  1294. identifier_not_found(orgstoredpattern);
  1295. srsym:=generrorsym;
  1296. srsymtable:=nil;
  1297. end;
  1298. end;
  1299. { Access to funcret or need to call the function? }
  1300. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  1301. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  1302. { result(x) is not allowed }
  1303. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  1304. (
  1305. (token=_LKLAMMER) or
  1306. (
  1307. (
  1308. (m_tp7 in current_settings.modeswitches) or
  1309. (m_delphi in current_settings.modeswitches)
  1310. ) and
  1311. (afterassignment or in_args)
  1312. )
  1313. ) then
  1314. begin
  1315. hdef:=tdef(srsym.owner.defowner);
  1316. if assigned(hdef) and
  1317. (hdef.typ=procdef) then
  1318. srsym:=tprocdef(hdef).procsym
  1319. else
  1320. begin
  1321. Message(parser_e_illegal_expression);
  1322. srsym:=generrorsym;
  1323. end;
  1324. srsymtable:=srsym.owner;
  1325. end;
  1326. begin
  1327. case srsym.typ of
  1328. absolutevarsym :
  1329. begin
  1330. if (tabsolutevarsym(srsym).abstyp=tovar) then
  1331. begin
  1332. p1:=nil;
  1333. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  1334. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  1335. include(p1.flags,nf_absolute);
  1336. end
  1337. else
  1338. p1:=cloadnode.create(srsym,srsymtable);
  1339. end;
  1340. staticvarsym,
  1341. localvarsym,
  1342. paravarsym,
  1343. fieldvarsym :
  1344. begin
  1345. { check if we are reading a field of an object/class/ }
  1346. { record. is_member_read() will deal with withsymtables }
  1347. { if needed. }
  1348. p1:=nil;
  1349. if is_member_read(srsym,srsymtable,p1,hdef) then
  1350. begin
  1351. { if the field was originally found in an }
  1352. { objectsymtable, it means it's part of self
  1353. if only method from which it was called is
  1354. not class static }
  1355. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  1356. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1357. p1:=cloadvmtaddrnode.create(ctypenode.create(current_procinfo.procdef.struct))
  1358. else
  1359. p1:=load_self_node;
  1360. { now, if the field itself is part of an objectsymtab }
  1361. { (it can be even if it was found in a withsymtable, }
  1362. { e.g., "with classinstance do field := 5"), then }
  1363. { let do_member_read handle it }
  1364. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1365. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1366. else
  1367. { otherwise it's a regular record subscript }
  1368. p1:=csubscriptnode.create(srsym,p1);
  1369. end
  1370. else
  1371. { regular non-field load }
  1372. p1:=cloadnode.create(srsym,srsymtable);
  1373. end;
  1374. syssym :
  1375. begin
  1376. p1:=statement_syssym(tsyssym(srsym).number);
  1377. end;
  1378. typesym :
  1379. begin
  1380. hdef:=ttypesym(srsym).typedef;
  1381. if not assigned(hdef) then
  1382. begin
  1383. again:=false;
  1384. end
  1385. else
  1386. begin
  1387. { We need to know if this unit uses Variants }
  1388. if (hdef=cvarianttype) and
  1389. not(cs_compilesystem in current_settings.moduleswitches) then
  1390. current_module.flags:=current_module.flags or uf_uses_variants;
  1391. { if we get a generic then check that it is not an inline specialization }
  1392. if (df_generic in hdef.defoptions) and
  1393. (token=_LT) and
  1394. (m_delphi in current_settings.modeswitches) then
  1395. generate_specialization(hdef,false,'');
  1396. { allow Ordinal(Value) for type declarations since it
  1397. can be an enummeration declaration or a set lke:
  1398. (OrdinalType(const1)..OrdinalType(const2) }
  1399. if (not typeonly or is_ordinal(hdef))and try_to_consume(_LKLAMMER) then
  1400. begin
  1401. p1:=comp_expr(true,false);
  1402. consume(_RKLAMMER);
  1403. { type casts to class helpers aren't allowed }
  1404. if is_objectpascal_helper(hdef) then
  1405. Message(parser_e_no_category_as_types)
  1406. { recovery by not creating a conversion node }
  1407. else
  1408. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1409. end
  1410. else { not LKLAMMER }
  1411. if (token=_POINT) and
  1412. (is_object(hdef) or is_record(hdef)) then
  1413. begin
  1414. consume(_POINT);
  1415. { handles calling methods declared in parent objects
  1416. using "parentobject.methodname()" }
  1417. if assigned(current_structdef) and
  1418. not(getaddr) and
  1419. current_structdef.is_related(hdef) then
  1420. begin
  1421. p1:=ctypenode.create(hdef);
  1422. { search also in inherited methods }
  1423. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,true);
  1424. if assigned(srsym) then
  1425. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1426. consume(_ID);
  1427. do_member_read(tabstractrecorddef(hdef),false,srsym,p1,again,[]);
  1428. end
  1429. else
  1430. begin
  1431. { handles:
  1432. * @TObject.Load
  1433. * static methods and variables }
  1434. p1:=ctypenode.create(hdef);
  1435. { TP allows also @TMenu.Load if Load is only }
  1436. { defined in an anchestor class }
  1437. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1438. if assigned(srsym) then
  1439. begin
  1440. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1441. consume(_ID);
  1442. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[]);
  1443. end
  1444. else
  1445. Message1(sym_e_id_no_member,orgpattern);
  1446. end;
  1447. end
  1448. else
  1449. begin
  1450. { Normally here would be the check against the usage
  1451. of "TClassHelper.Something", but as that might be
  1452. used inside of system symbols like sizeof and
  1453. typeinfo this check is put into ttypenode.pass_1
  1454. (for "TClassHelper" alone) and tcallnode.pass_1
  1455. (for "TClassHelper.Something") }
  1456. { class reference ? }
  1457. if is_class(hdef) or
  1458. is_objcclass(hdef) then
  1459. begin
  1460. if getaddr and (token=_POINT) then
  1461. begin
  1462. consume(_POINT);
  1463. { allows @Object.Method }
  1464. { also allows static methods and variables }
  1465. p1:=ctypenode.create(hdef);
  1466. { TP allows also @TMenu.Load if Load is only }
  1467. { defined in an anchestor class }
  1468. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1469. if assigned(srsym) then
  1470. begin
  1471. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1472. consume(_ID);
  1473. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[]);
  1474. end
  1475. else
  1476. begin
  1477. Message1(sym_e_id_no_member,orgpattern);
  1478. consume(_ID);
  1479. end;
  1480. end
  1481. else
  1482. begin
  1483. p1:=ctypenode.create(hdef);
  1484. { For a type block we simply return only
  1485. the type. For all other blocks we return
  1486. a loadvmt node }
  1487. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1488. p1:=cloadvmtaddrnode.create(p1);
  1489. end;
  1490. end
  1491. else
  1492. p1:=ctypenode.create(hdef);
  1493. end;
  1494. end;
  1495. end;
  1496. enumsym :
  1497. begin
  1498. p1:=genenumnode(tenumsym(srsym));
  1499. end;
  1500. constsym :
  1501. begin
  1502. if tconstsym(srsym).consttyp=constresourcestring then
  1503. begin
  1504. p1:=cloadnode.create(srsym,srsymtable);
  1505. do_typecheckpass(p1);
  1506. p1.resultdef:=cansistringtype;
  1507. end
  1508. else
  1509. p1:=genconstsymtree(tconstsym(srsym));
  1510. end;
  1511. procsym :
  1512. begin
  1513. p1:=nil;
  1514. { check if it's a method/class method }
  1515. if is_member_read(srsym,srsymtable,p1,hdef) then
  1516. begin
  1517. { not srsymtable.symtabletype since that can be }
  1518. { withsymtable as well }
  1519. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1520. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1521. else
  1522. { no procsyms in records (yet) }
  1523. internalerror(2007012006);
  1524. end
  1525. else
  1526. begin
  1527. { regular procedure/function call }
  1528. if not unit_found then
  1529. callflags:=[]
  1530. else
  1531. callflags:=[cnf_unit_specified];
  1532. do_proc_call(srsym,srsymtable,nil,
  1533. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  1534. again,p1,callflags);
  1535. end;
  1536. end;
  1537. propertysym :
  1538. begin
  1539. p1:=nil;
  1540. { property of a class/object? }
  1541. if is_member_read(srsym,srsymtable,p1,hdef) then
  1542. begin
  1543. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  1544. if assigned(current_procinfo) and current_procinfo.procdef.no_self_node then
  1545. { no self node in static class methods }
  1546. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  1547. else
  1548. p1:=load_self_node;
  1549. { not srsymtable.symtabletype since that can be }
  1550. { withsymtable as well }
  1551. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1552. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[])
  1553. else
  1554. { no propertysyms in records (yet) }
  1555. internalerror(2009111510);
  1556. end
  1557. else
  1558. { no method pointer }
  1559. begin
  1560. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1561. end;
  1562. end;
  1563. labelsym :
  1564. begin
  1565. { Support @label }
  1566. if getaddr then
  1567. begin
  1568. if srsym.owner<>current_procinfo.procdef.localst then
  1569. CGMessage(parser_e_label_outside_proc);
  1570. p1:=cloadnode.create(srsym,srsym.owner)
  1571. end
  1572. else
  1573. begin
  1574. consume(_COLON);
  1575. if tlabelsym(srsym).defined then
  1576. Message(sym_e_label_already_defined);
  1577. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1578. begin
  1579. tlabelsym(srsym).nonlocal:=true;
  1580. exclude(current_procinfo.procdef.procoptions,po_inline);
  1581. end;
  1582. if tlabelsym(srsym).nonlocal and
  1583. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1584. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  1585. tlabelsym(srsym).defined:=true;
  1586. p1:=clabelnode.create(nil,tlabelsym(srsym));
  1587. tlabelsym(srsym).code:=p1;
  1588. end;
  1589. end;
  1590. errorsym :
  1591. begin
  1592. p1:=cerrornode.create;
  1593. if try_to_consume(_LKLAMMER) then
  1594. begin
  1595. parse_paras(false,false,_RKLAMMER);
  1596. consume(_RKLAMMER);
  1597. end;
  1598. end;
  1599. else
  1600. begin
  1601. p1:=cerrornode.create;
  1602. Message(parser_e_illegal_expression);
  1603. end;
  1604. end; { end case }
  1605. end;
  1606. end;
  1607. {---------------------------------------------
  1608. Factor_Read_Set
  1609. ---------------------------------------------}
  1610. { Read a set between [] }
  1611. function factor_read_set:tnode;
  1612. var
  1613. p1,p2 : tnode;
  1614. lastp,
  1615. buildp : tarrayconstructornode;
  1616. old_allow_array_constructor : boolean;
  1617. begin
  1618. buildp:=nil;
  1619. { be sure that a least one arrayconstructn is used, also for an
  1620. empty [] }
  1621. if token=_RECKKLAMMER then
  1622. buildp:=carrayconstructornode.create(nil,buildp)
  1623. else
  1624. repeat
  1625. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  1626. old_allow_array_constructor:=allow_array_constructor;
  1627. allow_array_constructor:=false;
  1628. p1:=comp_expr(true,false);
  1629. if try_to_consume(_POINTPOINT) then
  1630. begin
  1631. p2:=comp_expr(true,false);
  1632. p1:=carrayconstructorrangenode.create(p1,p2);
  1633. end;
  1634. { insert at the end of the tree, to get the correct order }
  1635. if not assigned(buildp) then
  1636. begin
  1637. buildp:=carrayconstructornode.create(p1,nil);
  1638. lastp:=buildp;
  1639. end
  1640. else
  1641. begin
  1642. lastp.right:=carrayconstructornode.create(p1,nil);
  1643. lastp:=tarrayconstructornode(lastp.right);
  1644. end;
  1645. allow_array_constructor:=old_allow_array_constructor;
  1646. { there could be more elements }
  1647. until not try_to_consume(_COMMA);
  1648. factor_read_set:=buildp;
  1649. end;
  1650. {---------------------------------------------
  1651. PostFixOperators
  1652. ---------------------------------------------}
  1653. { returns whether or not p1 has been changed }
  1654. function postfixoperators(var p1:tnode;var again:boolean): boolean;
  1655. { tries to avoid syntax errors after invalid qualifiers }
  1656. procedure recoverconsume_postfixops;
  1657. begin
  1658. repeat
  1659. if not try_to_consume(_CARET) then
  1660. if try_to_consume(_POINT) then
  1661. try_to_consume(_ID)
  1662. else if try_to_consume(_LECKKLAMMER) then
  1663. begin
  1664. repeat
  1665. comp_expr(true,false);
  1666. until not try_to_consume(_COMMA);
  1667. consume(_RECKKLAMMER);
  1668. end
  1669. else if try_to_consume(_LKLAMMER) then
  1670. begin
  1671. repeat
  1672. comp_expr(true,false);
  1673. until not try_to_consume(_COMMA);
  1674. consume(_RKLAMMER);
  1675. end
  1676. else
  1677. break;
  1678. until false;
  1679. end;
  1680. procedure handle_variantarray;
  1681. var
  1682. p4 : tnode;
  1683. newstatement : tstatementnode;
  1684. tempresultvariant,
  1685. temp : ttempcreatenode;
  1686. paras : tcallparanode;
  1687. newblock : tnode;
  1688. countindices : aint;
  1689. begin
  1690. { create statements with call initialize the arguments and
  1691. call fpc_dynarr_setlength }
  1692. newblock:=internalstatements(newstatement);
  1693. { get temp for array of indicies,
  1694. we set the real size later }
  1695. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1696. addstatement(newstatement,temp);
  1697. countindices:=0;
  1698. repeat
  1699. p4:=comp_expr(true,false);
  1700. addstatement(newstatement,cassignmentnode.create(
  1701. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1702. inc(countindices);
  1703. until not try_to_consume(_COMMA);
  1704. { set real size }
  1705. temp.size:=countindices*s32inttype.size;
  1706. consume(_RECKKLAMMER);
  1707. { we need only a write access if a := follows }
  1708. if token=_ASSIGNMENT then
  1709. begin
  1710. consume(_ASSIGNMENT);
  1711. p4:=comp_expr(true,false);
  1712. { create call to fpc_vararray_put }
  1713. paras:=ccallparanode.create(cordconstnode.create
  1714. (countindices,s32inttype,true),
  1715. ccallparanode.create(caddrnode.create_internal
  1716. (ctemprefnode.create(temp)),
  1717. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1718. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1719. ,nil))));
  1720. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1721. addstatement(newstatement,ctempdeletenode.create(temp));
  1722. end
  1723. else
  1724. begin
  1725. { create temp for result }
  1726. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1727. addstatement(newstatement,tempresultvariant);
  1728. { create call to fpc_vararray_get }
  1729. paras:=ccallparanode.create(cordconstnode.create
  1730. (countindices,s32inttype,true),
  1731. ccallparanode.create(caddrnode.create_internal
  1732. (ctemprefnode.create(temp)),
  1733. ccallparanode.create(p1,
  1734. ccallparanode.create(
  1735. ctemprefnode.create(tempresultvariant)
  1736. ,nil))));
  1737. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1738. addstatement(newstatement,ctempdeletenode.create(temp));
  1739. { the last statement should return the value as
  1740. location and type, this is done be referencing the
  1741. temp and converting it first from a persistent temp to
  1742. normal temp }
  1743. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1744. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1745. end;
  1746. p1:=newblock;
  1747. end;
  1748. function parse_array_constructor(arrdef:tarraydef): tnode;
  1749. var
  1750. newstatement,assstatement:tstatementnode;
  1751. arrnode:ttempcreatenode;
  1752. temp2:ttempcreatenode;
  1753. assnode:tnode;
  1754. paracount:integer;
  1755. begin
  1756. result:=internalstatements(newstatement);
  1757. { create temp for result }
  1758. arrnode:=ctempcreatenode.create(arrdef,arrdef.size,tt_persistent,true);
  1759. addstatement(newstatement,arrnode);
  1760. paracount:=0;
  1761. { check arguments and create an assignment calls }
  1762. if try_to_consume(_LKLAMMER) then
  1763. begin
  1764. assnode:=internalstatements(assstatement);
  1765. repeat
  1766. { arr[i] := param_i }
  1767. addstatement(assstatement,
  1768. cassignmentnode.create(
  1769. cvecnode.create(
  1770. ctemprefnode.create(arrnode),
  1771. cordconstnode.create(paracount,arrdef.rangedef,false)),
  1772. comp_expr(true,false)));
  1773. inc(paracount);
  1774. until not try_to_consume(_COMMA);
  1775. consume(_RKLAMMER);
  1776. end
  1777. else
  1778. assnode:=nil;
  1779. { get temp for array of lengths }
  1780. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1781. addstatement(newstatement,temp2);
  1782. { one dimensional }
  1783. addstatement(newstatement,cassignmentnode.create(
  1784. ctemprefnode.create_offset(temp2,0),
  1785. cordconstnode.create
  1786. (paracount,s32inttype,true)));
  1787. { create call to fpc_dynarr_setlength }
  1788. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1789. ccallparanode.create(caddrnode.create_internal
  1790. (ctemprefnode.create(temp2)),
  1791. ccallparanode.create(cordconstnode.create
  1792. (1,s32inttype,true),
  1793. ccallparanode.create(caddrnode.create_internal
  1794. (crttinode.create(tstoreddef(arrdef),initrtti,rdt_normal)),
  1795. ccallparanode.create(
  1796. ctypeconvnode.create_internal(
  1797. ctemprefnode.create(arrnode),voidpointertype),
  1798. nil))))
  1799. ));
  1800. { add assignment statememnts }
  1801. addstatement(newstatement,ctempdeletenode.create(temp2));
  1802. if assigned(assnode) then
  1803. addstatement(newstatement,assnode);
  1804. { the last statement should return the value as
  1805. location and type, this is done be referencing the
  1806. temp and converting it first from a persistent temp to
  1807. normal temp }
  1808. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1809. addstatement(newstatement,ctemprefnode.create(arrnode));
  1810. end;
  1811. var
  1812. protsym : tpropertysym;
  1813. p2,p3 : tnode;
  1814. srsym : tsym;
  1815. srsymtable : TSymtable;
  1816. structh : tabstractrecorddef;
  1817. { shouldn't be used that often, so the extra overhead is ok to save
  1818. stack space }
  1819. dispatchstring : ansistring;
  1820. nodechanged : boolean;
  1821. calltype: tdispcalltype;
  1822. label
  1823. skipreckklammercheck;
  1824. begin
  1825. result:=false;
  1826. again:=true;
  1827. while again do
  1828. begin
  1829. { we need the resultdef }
  1830. do_typecheckpass_changed(p1,nodechanged);
  1831. result:=result or nodechanged;
  1832. if codegenerror then
  1833. begin
  1834. recoverconsume_postfixops;
  1835. exit;
  1836. end;
  1837. { handle token }
  1838. case token of
  1839. _CARET:
  1840. begin
  1841. consume(_CARET);
  1842. { support tp/mac procvar^ if the procvar returns a
  1843. pointer type }
  1844. if ((m_tp_procvar in current_settings.modeswitches) or
  1845. (m_mac_procvar in current_settings.modeswitches)) and
  1846. (p1.resultdef.typ=procvardef) and
  1847. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1848. begin
  1849. p1:=ccallnode.create_procvar(nil,p1);
  1850. typecheckpass(p1);
  1851. end;
  1852. if (p1.resultdef.typ<>pointerdef) then
  1853. begin
  1854. { ^ as binary operator is a problem!!!! (FK) }
  1855. again:=false;
  1856. Message(parser_e_invalid_qualifier);
  1857. recoverconsume_postfixops;
  1858. p1.destroy;
  1859. p1:=cerrornode.create;
  1860. end
  1861. else
  1862. p1:=cderefnode.create(p1);
  1863. end;
  1864. _LECKKLAMMER:
  1865. begin
  1866. if is_class_or_interface_or_object(p1.resultdef) or
  1867. is_dispinterface(p1.resultdef) or is_record(p1.resultdef) then
  1868. begin
  1869. { default property }
  1870. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1871. if not(assigned(protsym)) then
  1872. begin
  1873. p1.destroy;
  1874. p1:=cerrornode.create;
  1875. again:=false;
  1876. message(parser_e_no_default_property_available);
  1877. end
  1878. else
  1879. begin
  1880. { The property symbol is referenced indirect }
  1881. protsym.IncRefCount;
  1882. handle_propertysym(protsym,protsym.owner,p1);
  1883. end;
  1884. end
  1885. else
  1886. begin
  1887. consume(_LECKKLAMMER);
  1888. repeat
  1889. { in all of the cases below, p1 is changed }
  1890. case p1.resultdef.typ of
  1891. pointerdef:
  1892. begin
  1893. { support delphi autoderef }
  1894. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1895. (m_autoderef in current_settings.modeswitches) then
  1896. p1:=cderefnode.create(p1);
  1897. p2:=comp_expr(true,false);
  1898. { Support Pbytevar[0..9] which returns array [0..9].}
  1899. if try_to_consume(_POINTPOINT) then
  1900. p2:=crangenode.create(p2,comp_expr(true,false));
  1901. p1:=cvecnode.create(p1,p2);
  1902. end;
  1903. variantdef:
  1904. begin
  1905. handle_variantarray;
  1906. { the RECKKLAMMER is already read }
  1907. goto skipreckklammercheck;
  1908. end;
  1909. stringdef :
  1910. begin
  1911. p2:=comp_expr(true,false);
  1912. { Support string[0..9] which returns array [0..9] of char.}
  1913. if try_to_consume(_POINTPOINT) then
  1914. p2:=crangenode.create(p2,comp_expr(true,false));
  1915. p1:=cvecnode.create(p1,p2);
  1916. end;
  1917. arraydef:
  1918. begin
  1919. p2:=comp_expr(true,false);
  1920. { support SEG:OFS for go32v2 Mem[] }
  1921. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1922. (p1.nodetype=loadn) and
  1923. assigned(tloadnode(p1).symtableentry) and
  1924. assigned(tloadnode(p1).symtableentry.owner.name) and
  1925. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1926. ((tloadnode(p1).symtableentry.name='MEM') or
  1927. (tloadnode(p1).symtableentry.name='MEMW') or
  1928. (tloadnode(p1).symtableentry.name='MEML')) then
  1929. begin
  1930. if try_to_consume(_COLON) then
  1931. begin
  1932. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1933. p2:=comp_expr(true,false);
  1934. p2:=caddnode.create(addn,p2,p3);
  1935. if try_to_consume(_POINTPOINT) then
  1936. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1937. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1938. p1:=cvecnode.create(p1,p2);
  1939. include(tvecnode(p1).flags,nf_memseg);
  1940. include(tvecnode(p1).flags,nf_memindex);
  1941. end
  1942. else
  1943. begin
  1944. if try_to_consume(_POINTPOINT) then
  1945. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1946. p2:=crangenode.create(p2,comp_expr(true,false));
  1947. p1:=cvecnode.create(p1,p2);
  1948. include(tvecnode(p1).flags,nf_memindex);
  1949. end;
  1950. end
  1951. else
  1952. begin
  1953. if try_to_consume(_POINTPOINT) then
  1954. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1955. p2:=crangenode.create(p2,comp_expr(true,false));
  1956. p1:=cvecnode.create(p1,p2);
  1957. end;
  1958. end;
  1959. else
  1960. begin
  1961. if p1.resultdef.typ<>undefineddef then
  1962. Message(parser_e_invalid_qualifier);
  1963. p1.destroy;
  1964. p1:=cerrornode.create;
  1965. comp_expr(true,false);
  1966. again:=false;
  1967. end;
  1968. end;
  1969. do_typecheckpass(p1);
  1970. until not try_to_consume(_COMMA);
  1971. consume(_RECKKLAMMER);
  1972. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1973. skipreckklammercheck:
  1974. end;
  1975. end;
  1976. _POINT :
  1977. begin
  1978. consume(_POINT);
  1979. if (p1.resultdef.typ=pointerdef) and
  1980. (m_autoderef in current_settings.modeswitches) and
  1981. { don't auto-deref objc.id, because then the code
  1982. below for supporting id.anyobjcmethod isn't triggered }
  1983. (p1.resultdef<>objc_idtype) then
  1984. begin
  1985. p1:=cderefnode.create(p1);
  1986. do_typecheckpass(p1);
  1987. end;
  1988. { procvar.<something> can never mean anything so always
  1989. try to call it in case it returns a record/object/... }
  1990. maybe_call_procvar(p1,false);
  1991. case p1.resultdef.typ of
  1992. recorddef:
  1993. begin
  1994. if token=_ID then
  1995. begin
  1996. structh:=tabstractrecorddef(p1.resultdef);
  1997. searchsym_in_record(structh,pattern,srsym,srsymtable);
  1998. if assigned(srsym) then
  1999. begin
  2000. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2001. consume(_ID);
  2002. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2003. end
  2004. else
  2005. begin
  2006. Message1(sym_e_id_no_member,orgpattern);
  2007. p1.destroy;
  2008. p1:=cerrornode.create;
  2009. { try to clean up }
  2010. consume(_ID);
  2011. end;
  2012. end
  2013. else
  2014. consume(_ID);
  2015. end;
  2016. enumdef:
  2017. begin
  2018. if token=_ID then
  2019. begin
  2020. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  2021. p1.destroy;
  2022. if assigned(srsym) and (srsym.typ=enumsym) then
  2023. begin
  2024. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2025. p1:=genenumnode(tenumsym(srsym));
  2026. end
  2027. else
  2028. begin
  2029. Message1(sym_e_id_no_member,orgpattern);
  2030. p1:=cerrornode.create;
  2031. end;
  2032. end;
  2033. consume(_ID);
  2034. end;
  2035. arraydef:
  2036. begin
  2037. if is_dynamic_array(p1.resultdef) then
  2038. begin
  2039. if token=_ID then
  2040. begin
  2041. if pattern='CREATE' then
  2042. begin
  2043. consume(_ID);
  2044. p2:=parse_array_constructor(tarraydef(p1.resultdef));
  2045. p1.destroy;
  2046. p1:=p2;
  2047. end
  2048. else
  2049. begin
  2050. Message2(scan_f_syn_expected,'CREATE',pattern);
  2051. p1.destroy;
  2052. p1:=cerrornode.create;
  2053. consume(_ID);
  2054. end;
  2055. end;
  2056. end
  2057. else
  2058. begin
  2059. Message(parser_e_invalid_qualifier);
  2060. p1.destroy;
  2061. p1:=cerrornode.create;
  2062. consume(_ID);
  2063. end;
  2064. end;
  2065. variantdef:
  2066. begin
  2067. { dispatch call? }
  2068. { lhs := v.ident[parameters] -> property get
  2069. lhs := v.ident(parameters) -> method call
  2070. v.ident[parameters] := rhs -> property put
  2071. v.ident(parameters) := rhs -> also property put }
  2072. if token=_ID then
  2073. begin
  2074. dispatchstring:=orgpattern;
  2075. consume(_ID);
  2076. calltype:=dct_method;
  2077. if try_to_consume(_LKLAMMER) then
  2078. begin
  2079. p2:=parse_paras(false,true,_RKLAMMER);
  2080. consume(_RKLAMMER);
  2081. end
  2082. else if try_to_consume(_LECKKLAMMER) then
  2083. begin
  2084. p2:=parse_paras(false,true,_RECKKLAMMER);
  2085. consume(_RECKKLAMMER);
  2086. calltype:=dct_propget;
  2087. end
  2088. else
  2089. p2:=nil;
  2090. { property setter? }
  2091. if (token=_ASSIGNMENT) and not(afterassignment) then
  2092. begin
  2093. consume(_ASSIGNMENT);
  2094. { read the expression }
  2095. p3:=comp_expr(true,false);
  2096. { concat value parameter too }
  2097. p2:=ccallparanode.create(p3,p2);
  2098. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  2099. end
  2100. else
  2101. { this is only an approximation
  2102. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  2103. if afterassignment or in_args or (token<>_SEMICOLON) then
  2104. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  2105. else
  2106. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  2107. end
  2108. else { Error }
  2109. Consume(_ID);
  2110. end;
  2111. classrefdef:
  2112. begin
  2113. if token=_ID then
  2114. begin
  2115. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  2116. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  2117. if assigned(srsym) then
  2118. begin
  2119. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2120. consume(_ID);
  2121. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2122. end
  2123. else
  2124. begin
  2125. Message1(sym_e_id_no_member,orgpattern);
  2126. p1.destroy;
  2127. p1:=cerrornode.create;
  2128. { try to clean up }
  2129. consume(_ID);
  2130. end;
  2131. end
  2132. else { Error }
  2133. Consume(_ID);
  2134. end;
  2135. objectdef:
  2136. begin
  2137. if token=_ID then
  2138. begin
  2139. structh:=tobjectdef(p1.resultdef);
  2140. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,true);
  2141. if assigned(srsym) then
  2142. begin
  2143. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2144. consume(_ID);
  2145. do_member_read(structh,getaddr,srsym,p1,again,[]);
  2146. end
  2147. else
  2148. begin
  2149. Message1(sym_e_id_no_member,orgpattern);
  2150. p1.destroy;
  2151. p1:=cerrornode.create;
  2152. { try to clean up }
  2153. consume(_ID);
  2154. end;
  2155. end
  2156. else { Error }
  2157. Consume(_ID);
  2158. end;
  2159. pointerdef:
  2160. begin
  2161. if (p1.resultdef=objc_idtype) then
  2162. begin
  2163. { objc's id type can be used to call any
  2164. Objective-C method of any Objective-C class
  2165. type that's currently in scope }
  2166. if search_objc_method(pattern,srsym,srsymtable) then
  2167. begin
  2168. consume(_ID);
  2169. do_proc_call(srsym,srsymtable,nil,
  2170. (getaddr and not(token in [_CARET,_POINT])),
  2171. again,p1,[cnf_objc_id_call]);
  2172. { we need to know which procedure is called }
  2173. do_typecheckpass(p1);
  2174. end
  2175. else
  2176. begin
  2177. consume(_ID);
  2178. Message(parser_e_methode_id_expected);
  2179. end;
  2180. end
  2181. else
  2182. begin
  2183. Message(parser_e_invalid_qualifier);
  2184. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2185. Message(parser_h_maybe_deref_caret_missing);
  2186. end
  2187. end;
  2188. else
  2189. begin
  2190. if p1.resultdef.typ<>undefineddef then
  2191. Message(parser_e_invalid_qualifier);
  2192. p1.destroy;
  2193. p1:=cerrornode.create;
  2194. { Error }
  2195. consume(_ID);
  2196. end;
  2197. end;
  2198. end;
  2199. else
  2200. begin
  2201. { is this a procedure variable ? }
  2202. if assigned(p1.resultdef) and
  2203. (p1.resultdef.typ=procvardef) then
  2204. begin
  2205. { Typenode for typecasting or expecting a procvar }
  2206. if (p1.nodetype=typen) or
  2207. (
  2208. assigned(getprocvardef) and
  2209. equal_defs(p1.resultdef,getprocvardef)
  2210. ) then
  2211. begin
  2212. if try_to_consume(_LKLAMMER) then
  2213. begin
  2214. p1:=comp_expr(true,false);
  2215. consume(_RKLAMMER);
  2216. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2217. end
  2218. else
  2219. again:=false
  2220. end
  2221. else
  2222. begin
  2223. if try_to_consume(_LKLAMMER) then
  2224. begin
  2225. p2:=parse_paras(false,false,_RKLAMMER);
  2226. consume(_RKLAMMER);
  2227. p1:=ccallnode.create_procvar(p2,p1);
  2228. { proc():= is never possible }
  2229. if token=_ASSIGNMENT then
  2230. begin
  2231. Message(parser_e_illegal_expression);
  2232. p1.free;
  2233. p1:=cerrornode.create;
  2234. again:=false;
  2235. end;
  2236. end
  2237. else
  2238. again:=false;
  2239. end;
  2240. end
  2241. else
  2242. again:=false;
  2243. end;
  2244. end;
  2245. { we only try again if p1 was changed }
  2246. if again or
  2247. (p1.nodetype=errorn) then
  2248. result:=true;
  2249. end; { while again }
  2250. end;
  2251. {---------------------------------------------
  2252. Factor (Main)
  2253. ---------------------------------------------}
  2254. var
  2255. l : longint;
  2256. ic : int64;
  2257. qc : qword;
  2258. p1 : tnode;
  2259. code : integer;
  2260. srsym : tsym;
  2261. srsymtable : TSymtable;
  2262. pd : tprocdef;
  2263. hclassdef : tobjectdef;
  2264. d : bestreal;
  2265. cur : currency;
  2266. hs,hsorg : string;
  2267. hdef : tdef;
  2268. filepos : tfileposinfo;
  2269. callflags : tcallnodeflags;
  2270. again,
  2271. updatefpos,
  2272. nodechanged : boolean;
  2273. begin
  2274. { can't keep a copy of p1 and compare pointers afterwards, because
  2275. p1 may be freed and reallocated in the same place! }
  2276. updatefpos:=false;
  2277. p1:=nil;
  2278. filepos:=current_tokenpos;
  2279. again:=false;
  2280. if token=_ID then
  2281. begin
  2282. again:=true;
  2283. { Handle references to self }
  2284. if (idtoken=_SELF) and
  2285. not(block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) and
  2286. assigned(current_structdef) then
  2287. begin
  2288. p1:=load_self_node;
  2289. consume(_ID);
  2290. again:=true;
  2291. end
  2292. else
  2293. factor_read_id(p1,again);
  2294. if assigned(p1) then
  2295. begin
  2296. { factor_read_id will set the filepos to after the id,
  2297. and in case of _SELF the filepos will already be the
  2298. same as filepos (so setting it again doesn't hurt). }
  2299. p1.fileinfo:=filepos;
  2300. filepos:=current_tokenpos;
  2301. end;
  2302. { handle post fix operators }
  2303. updatefpos:=postfixoperators(p1,again);
  2304. end
  2305. else
  2306. begin
  2307. updatefpos:=true;
  2308. case token of
  2309. _RETURN :
  2310. begin
  2311. consume(_RETURN);
  2312. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2313. p1 := cexitnode.create(comp_expr(true,false))
  2314. else
  2315. p1 := cexitnode.create(nil);
  2316. end;
  2317. _INHERITED :
  2318. begin
  2319. again:=true;
  2320. consume(_INHERITED);
  2321. if assigned(current_procinfo) and
  2322. assigned(current_structdef) and
  2323. (current_structdef.typ=objectdef) then
  2324. begin
  2325. { for record helpers in mode Delphi "inherited" is not
  2326. allowed }
  2327. if is_objectpascal_helper(current_structdef) and
  2328. (m_delphi in current_settings.modeswitches) and
  2329. is_record(tobjectdef(current_structdef).extendeddef) then
  2330. Message(parser_e_inherited_not_in_record);
  2331. hclassdef:=tobjectdef(current_structdef).childof;
  2332. { Objective-C categories *replace* methods in the class
  2333. they extend, or add methods to it. So calling an
  2334. inherited method always calls the method inherited from
  2335. the parent of the extended class }
  2336. if is_objccategory(current_structdef) then
  2337. hclassdef:=hclassdef.childof;
  2338. { if inherited; only then we need the method with
  2339. the same name }
  2340. if token <> _ID then
  2341. begin
  2342. hs:=current_procinfo.procdef.procsym.name;
  2343. hsorg:=current_procinfo.procdef.procsym.realname;
  2344. anon_inherited:=true;
  2345. { For message methods we need to search using the message
  2346. number or string }
  2347. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2348. srdef:=nil;
  2349. if (po_msgint in pd.procoptions) then
  2350. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2351. else
  2352. if (po_msgstr in pd.procoptions) then
  2353. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2354. else
  2355. { helpers have their own ways of dealing with inherited }
  2356. if is_objectpascal_helper(current_structdef) then
  2357. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2358. else
  2359. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2360. end
  2361. else
  2362. begin
  2363. hs:=pattern;
  2364. hsorg:=orgpattern;
  2365. consume(_ID);
  2366. anon_inherited:=false;
  2367. { helpers have their own ways of dealing with inherited }
  2368. if is_objectpascal_helper(current_structdef) then
  2369. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,true)
  2370. else
  2371. searchsym_in_class(hclassdef,tobjectdef(current_structdef),hs,srsym,srsymtable,true);
  2372. end;
  2373. if assigned(srsym) then
  2374. begin
  2375. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2376. { load the procdef from the inherited class and
  2377. not from self }
  2378. case srsym.typ of
  2379. procsym:
  2380. begin
  2381. if is_objectpascal_helper(current_structdef) then
  2382. begin
  2383. { for a helper load the procdef either from the
  2384. extended type, from the parent helper or from
  2385. the extended type of the parent helper
  2386. depending on the def the found symbol belongs
  2387. to }
  2388. if (srsym.Owner.defowner.typ=objectdef) and
  2389. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  2390. if current_structdef.is_related(tdef(srsym.Owner.defowner)) and
  2391. assigned(tobjectdef(current_structdef).childof) then
  2392. hdef:=tobjectdef(current_structdef).childof
  2393. else
  2394. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  2395. else
  2396. hdef:=tdef(srsym.Owner.defowner);
  2397. end
  2398. else
  2399. hdef:=hclassdef;
  2400. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2401. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2402. hdef:=tclassrefdef.create(hdef);
  2403. p1:=ctypenode.create(hdef);
  2404. { we need to allow helpers here }
  2405. ttypenode(p1).helperallowed:=true;
  2406. end;
  2407. propertysym:
  2408. ;
  2409. else
  2410. begin
  2411. Message(parser_e_methode_id_expected);
  2412. p1:=cerrornode.create;
  2413. end;
  2414. end;
  2415. callflags:=[cnf_inherited];
  2416. if anon_inherited then
  2417. include(callflags,cnf_anon_inherited);
  2418. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags);
  2419. end
  2420. else
  2421. begin
  2422. if anon_inherited then
  2423. begin
  2424. { For message methods we need to call DefaultHandler }
  2425. if (po_msgint in pd.procoptions) or
  2426. (po_msgstr in pd.procoptions) then
  2427. begin
  2428. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,true);
  2429. if not assigned(srsym) or
  2430. (srsym.typ<>procsym) then
  2431. internalerror(200303171);
  2432. p1:=nil;
  2433. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2434. end
  2435. else
  2436. begin
  2437. { we need to ignore the inherited; }
  2438. p1:=cnothingnode.create;
  2439. end;
  2440. end
  2441. else
  2442. begin
  2443. Message1(sym_e_id_no_member,hsorg);
  2444. p1:=cerrornode.create;
  2445. end;
  2446. again:=false;
  2447. end;
  2448. { turn auto inheriting off }
  2449. anon_inherited:=false;
  2450. end
  2451. else
  2452. begin
  2453. { in case of records we use a more clear error message }
  2454. if assigned(current_structdef) and
  2455. (current_structdef.typ=recorddef) then
  2456. Message(parser_e_inherited_not_in_record)
  2457. else
  2458. Message(parser_e_generic_methods_only_in_methods);
  2459. again:=false;
  2460. p1:=cerrornode.create;
  2461. end;
  2462. postfixoperators(p1,again);
  2463. end;
  2464. _INTCONST :
  2465. begin
  2466. {Try first wether the value fits in an int64.}
  2467. val(pattern,ic,code);
  2468. if code=0 then
  2469. begin
  2470. consume(_INTCONST);
  2471. int_to_type(ic,hdef);
  2472. p1:=cordconstnode.create(ic,hdef,true);
  2473. end
  2474. else
  2475. begin
  2476. { try qword next }
  2477. val(pattern,qc,code);
  2478. if code=0 then
  2479. begin
  2480. consume(_INTCONST);
  2481. int_to_type(qc,hdef);
  2482. p1:=cordconstnode.create(qc,hdef,true);
  2483. end;
  2484. end;
  2485. if code<>0 then
  2486. begin
  2487. { finally float }
  2488. val(pattern,d,code);
  2489. if code<>0 then
  2490. begin
  2491. Message(parser_e_invalid_integer);
  2492. consume(_INTCONST);
  2493. l:=1;
  2494. p1:=cordconstnode.create(l,sinttype,true);
  2495. end
  2496. else
  2497. begin
  2498. consume(_INTCONST);
  2499. p1:=crealconstnode.create(d,pbestrealtype^);
  2500. end;
  2501. end
  2502. else
  2503. { the necessary range checking has already been done by val }
  2504. tordconstnode(p1).rangecheck:=false;
  2505. end;
  2506. _REALNUMBER :
  2507. begin
  2508. val(pattern,d,code);
  2509. if code<>0 then
  2510. begin
  2511. Message(parser_e_error_in_real);
  2512. d:=1.0;
  2513. end;
  2514. consume(_REALNUMBER);
  2515. {$ifdef FPC_REAL2REAL_FIXED}
  2516. if current_settings.fputype=fpu_none then
  2517. Message(parser_e_unsupported_real);
  2518. if (current_settings.minfpconstprec=s32real) and
  2519. (d = single(d)) then
  2520. p1:=crealconstnode.create(d,s32floattype)
  2521. else if (current_settings.minfpconstprec=s64real) and
  2522. (d = double(d)) then
  2523. p1:=crealconstnode.create(d,s64floattype)
  2524. else
  2525. {$endif FPC_REAL2REAL_FIXED}
  2526. p1:=crealconstnode.create(d,pbestrealtype^);
  2527. {$ifdef FPC_HAS_STR_CURRENCY}
  2528. val(pattern,cur,code);
  2529. if code=0 then
  2530. trealconstnode(p1).value_currency:=cur;
  2531. {$endif FPC_HAS_STR_CURRENCY}
  2532. end;
  2533. _STRING :
  2534. begin
  2535. string_dec(hdef,true);
  2536. { STRING can be also a type cast }
  2537. if try_to_consume(_LKLAMMER) then
  2538. begin
  2539. p1:=comp_expr(true,false);
  2540. consume(_RKLAMMER);
  2541. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2542. { handle postfix operators here e.g. string(a)[10] }
  2543. again:=true;
  2544. postfixoperators(p1,again);
  2545. end
  2546. else
  2547. p1:=ctypenode.create(hdef);
  2548. end;
  2549. _FILE :
  2550. begin
  2551. hdef:=cfiletype;
  2552. consume(_FILE);
  2553. { FILE can be also a type cast }
  2554. if try_to_consume(_LKLAMMER) then
  2555. begin
  2556. p1:=comp_expr(true,false);
  2557. consume(_RKLAMMER);
  2558. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2559. { handle postfix operators here e.g. string(a)[10] }
  2560. again:=true;
  2561. postfixoperators(p1,again);
  2562. end
  2563. else
  2564. begin
  2565. p1:=ctypenode.create(hdef);
  2566. end;
  2567. end;
  2568. _CSTRING :
  2569. begin
  2570. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern));
  2571. consume(_CSTRING);
  2572. end;
  2573. _CCHAR :
  2574. begin
  2575. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2576. consume(_CCHAR);
  2577. end;
  2578. _CWSTRING:
  2579. begin
  2580. p1:=cstringconstnode.createwstr(patternw);
  2581. consume(_CWSTRING);
  2582. end;
  2583. _CWCHAR:
  2584. begin
  2585. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2586. consume(_CWCHAR);
  2587. end;
  2588. _KLAMMERAFFE :
  2589. begin
  2590. consume(_KLAMMERAFFE);
  2591. got_addrn:=true;
  2592. { support both @<x> and @(<x>) }
  2593. if try_to_consume(_LKLAMMER) then
  2594. begin
  2595. p1:=factor(true,false);
  2596. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2597. begin
  2598. again:=true;
  2599. postfixoperators(p1,again);
  2600. end
  2601. else
  2602. consume(_RKLAMMER);
  2603. end
  2604. else
  2605. p1:=factor(true,false);
  2606. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2607. begin
  2608. again:=true;
  2609. postfixoperators(p1,again);
  2610. end;
  2611. got_addrn:=false;
  2612. p1:=caddrnode.create(p1);
  2613. p1.fileinfo:=filepos;
  2614. if cs_typed_addresses in current_settings.localswitches then
  2615. include(p1.flags,nf_typedaddr);
  2616. { Store the procvar that we are expecting, the
  2617. addrn will use the information to find the correct
  2618. procdef or it will return an error }
  2619. if assigned(getprocvardef) and
  2620. (taddrnode(p1).left.nodetype = loadn) then
  2621. taddrnode(p1).getprocvardef:=getprocvardef;
  2622. end;
  2623. _LKLAMMER :
  2624. begin
  2625. consume(_LKLAMMER);
  2626. p1:=comp_expr(true,false);
  2627. consume(_RKLAMMER);
  2628. { it's not a good solution }
  2629. { but (a+b)^ makes some problems }
  2630. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2631. begin
  2632. again:=true;
  2633. postfixoperators(p1,again);
  2634. end;
  2635. end;
  2636. _LECKKLAMMER :
  2637. begin
  2638. consume(_LECKKLAMMER);
  2639. p1:=factor_read_set;
  2640. consume(_RECKKLAMMER);
  2641. end;
  2642. _PLUS :
  2643. begin
  2644. consume(_PLUS);
  2645. p1:=factor(false,false);
  2646. p1:=cunaryplusnode.create(p1);
  2647. end;
  2648. _MINUS :
  2649. begin
  2650. consume(_MINUS);
  2651. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  2652. begin
  2653. { ugly hack, but necessary to be able to parse }
  2654. { -9223372036854775808 as int64 (JM) }
  2655. pattern := '-'+pattern;
  2656. p1:=sub_expr(oppower,false,false);
  2657. { -1 ** 4 should be - (1 ** 4) and not
  2658. (-1) ** 4
  2659. This was the reason of tw0869.pp test failure PM }
  2660. if p1.nodetype=starstarn then
  2661. begin
  2662. if tbinarynode(p1).left.nodetype=ordconstn then
  2663. begin
  2664. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2665. p1:=cunaryminusnode.create(p1);
  2666. end
  2667. else if tbinarynode(p1).left.nodetype=realconstn then
  2668. begin
  2669. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2670. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2671. p1:=cunaryminusnode.create(p1);
  2672. end
  2673. else
  2674. internalerror(20021029);
  2675. end;
  2676. end
  2677. else
  2678. begin
  2679. if m_isolike_unary_minus in current_settings.modeswitches then
  2680. p1:=sub_expr(opmultiply,false,false)
  2681. else
  2682. p1:=sub_expr(oppower,false,false);
  2683. p1:=cunaryminusnode.create(p1);
  2684. end;
  2685. end;
  2686. _OP_NOT :
  2687. begin
  2688. consume(_OP_NOT);
  2689. p1:=factor(false,false);
  2690. p1:=cnotnode.create(p1);
  2691. end;
  2692. _TRUE :
  2693. begin
  2694. consume(_TRUE);
  2695. p1:=cordconstnode.create(1,pasbool8type,false);
  2696. end;
  2697. _FALSE :
  2698. begin
  2699. consume(_FALSE);
  2700. p1:=cordconstnode.create(0,pasbool8type,false);
  2701. end;
  2702. _NIL :
  2703. begin
  2704. consume(_NIL);
  2705. p1:=cnilnode.create;
  2706. { It's really ugly code nil^, but delphi allows it }
  2707. if token in [_CARET] then
  2708. begin
  2709. again:=true;
  2710. postfixoperators(p1,again);
  2711. end;
  2712. end;
  2713. _OBJCPROTOCOL:
  2714. begin
  2715. { The @protocol keyword is used in two ways in Objective-C:
  2716. 1) to declare protocols (~ Object Pascal interfaces)
  2717. 2) to obtain the metaclass (~ Object Pascal) "class of")
  2718. of a declared protocol
  2719. This code is for handling the second case. Because of 1),
  2720. we cannot simply use a system unit symbol.
  2721. }
  2722. consume(_OBJCPROTOCOL);
  2723. consume(_LKLAMMER);
  2724. p1:=factor(false,false);
  2725. consume(_RKLAMMER);
  2726. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  2727. end;
  2728. else
  2729. begin
  2730. Message(parser_e_illegal_expression);
  2731. p1:=cerrornode.create;
  2732. { recover }
  2733. consume(token);
  2734. end;
  2735. end;
  2736. end;
  2737. { generate error node if no node is created }
  2738. if not assigned(p1) then
  2739. begin
  2740. {$ifdef EXTDEBUG}
  2741. Comment(V_Warning,'factor: p1=nil');
  2742. {$endif}
  2743. p1:=cerrornode.create;
  2744. updatefpos:=true;
  2745. end;
  2746. { get the resultdef for the node }
  2747. if (not assigned(p1.resultdef)) then
  2748. begin
  2749. do_typecheckpass_changed(p1,nodechanged);
  2750. updatefpos:=updatefpos or nodechanged;
  2751. end;
  2752. if assigned(p1) and
  2753. updatefpos then
  2754. p1.fileinfo:=filepos;
  2755. factor:=p1;
  2756. end;
  2757. {$maxfpuregisters default}
  2758. {****************************************************************************
  2759. Sub_Expr
  2760. ****************************************************************************}
  2761. const
  2762. { Warning these stay be ordered !! }
  2763. operator_levels:array[Toperator_precedence] of set of NOTOKEN..last_operator=
  2764. ([_LT,_LTE,_GT,_GTE,_EQ,_NE,_OP_IN],
  2765. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2766. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2767. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2768. [_STARSTAR] );
  2769. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean):tnode;
  2770. {Reads a subexpression while the operators are of the current precedence
  2771. level, or any higher level. Replaces the old term, simpl_expr and
  2772. simpl2_expr.}
  2773. var
  2774. p1,p2 : tnode;
  2775. oldt : Ttoken;
  2776. filepos : tfileposinfo;
  2777. begin
  2778. if pred_level=highest_precedence then
  2779. p1:=factor(false,typeonly)
  2780. else
  2781. p1:=sub_expr(succ(pred_level),true,typeonly);
  2782. repeat
  2783. if (token in [NOTOKEN..last_operator]) and
  2784. (token in operator_levels[pred_level]) and
  2785. ((token<>_EQ) or accept_equal) then
  2786. begin
  2787. oldt:=token;
  2788. filepos:=current_tokenpos;
  2789. consume(token);
  2790. if pred_level=highest_precedence then
  2791. p2:=factor(false,false)
  2792. else
  2793. p2:=sub_expr(succ(pred_level),true,typeonly);
  2794. case oldt of
  2795. _PLUS :
  2796. p1:=caddnode.create(addn,p1,p2);
  2797. _MINUS :
  2798. p1:=caddnode.create(subn,p1,p2);
  2799. _STAR :
  2800. p1:=caddnode.create(muln,p1,p2);
  2801. _SLASH :
  2802. p1:=caddnode.create(slashn,p1,p2);
  2803. _EQ:
  2804. p1:=caddnode.create(equaln,p1,p2);
  2805. _GT :
  2806. p1:=caddnode.create(gtn,p1,p2);
  2807. _LT :
  2808. p1:=caddnode.create(ltn,p1,p2);
  2809. _GTE :
  2810. p1:=caddnode.create(gten,p1,p2);
  2811. _LTE :
  2812. p1:=caddnode.create(lten,p1,p2);
  2813. _SYMDIF :
  2814. p1:=caddnode.create(symdifn,p1,p2);
  2815. _STARSTAR :
  2816. p1:=caddnode.create(starstarn,p1,p2);
  2817. _OP_AS :
  2818. p1:=casnode.create(p1,p2);
  2819. _OP_IN :
  2820. p1:=cinnode.create(p1,p2);
  2821. _OP_IS :
  2822. p1:=cisnode.create(p1,p2);
  2823. _OP_OR,
  2824. _PIPE {macpas only} :
  2825. begin
  2826. p1:=caddnode.create(orn,p1,p2);
  2827. if (oldt = _PIPE) then
  2828. include(p1.flags,nf_short_bool);
  2829. end;
  2830. _OP_AND,
  2831. _AMPERSAND {macpas only} :
  2832. begin
  2833. p1:=caddnode.create(andn,p1,p2);
  2834. if (oldt = _AMPERSAND) then
  2835. include(p1.flags,nf_short_bool);
  2836. end;
  2837. _OP_DIV :
  2838. p1:=cmoddivnode.create(divn,p1,p2);
  2839. _OP_NOT :
  2840. p1:=cnotnode.create(p1);
  2841. _OP_MOD :
  2842. p1:=cmoddivnode.create(modn,p1,p2);
  2843. _OP_SHL :
  2844. p1:=cshlshrnode.create(shln,p1,p2);
  2845. _OP_SHR :
  2846. p1:=cshlshrnode.create(shrn,p1,p2);
  2847. _OP_XOR :
  2848. p1:=caddnode.create(xorn,p1,p2);
  2849. _ASSIGNMENT :
  2850. p1:=cassignmentnode.create(p1,p2);
  2851. _NE :
  2852. p1:=caddnode.create(unequaln,p1,p2);
  2853. end;
  2854. p1.fileinfo:=filepos;
  2855. end
  2856. else
  2857. break;
  2858. until false;
  2859. sub_expr:=p1;
  2860. end;
  2861. function comp_expr(accept_equal,typeonly:boolean):tnode;
  2862. var
  2863. oldafterassignment : boolean;
  2864. p1 : tnode;
  2865. begin
  2866. oldafterassignment:=afterassignment;
  2867. afterassignment:=true;
  2868. p1:=sub_expr(opcompare,accept_equal,typeonly);
  2869. { get the resultdef for this expression }
  2870. if not assigned(p1.resultdef) then
  2871. do_typecheckpass(p1);
  2872. afterassignment:=oldafterassignment;
  2873. comp_expr:=p1;
  2874. end;
  2875. function expr(dotypecheck : boolean) : tnode;
  2876. var
  2877. p1,p2 : tnode;
  2878. filepos : tfileposinfo;
  2879. oldafterassignment,
  2880. updatefpos : boolean;
  2881. begin
  2882. oldafterassignment:=afterassignment;
  2883. p1:=sub_expr(opcompare,true,false);
  2884. { get the resultdef for this expression }
  2885. if not assigned(p1.resultdef) and
  2886. dotypecheck then
  2887. do_typecheckpass(p1);
  2888. filepos:=current_tokenpos;
  2889. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2890. afterassignment:=true;
  2891. updatefpos:=true;
  2892. case token of
  2893. _POINTPOINT :
  2894. begin
  2895. consume(_POINTPOINT);
  2896. p2:=sub_expr(opcompare,true,false);
  2897. p1:=crangenode.create(p1,p2);
  2898. end;
  2899. _ASSIGNMENT :
  2900. begin
  2901. consume(_ASSIGNMENT);
  2902. if (p1.resultdef.typ=procvardef) then
  2903. getprocvardef:=tprocvardef(p1.resultdef);
  2904. p2:=sub_expr(opcompare,true,false);
  2905. if assigned(getprocvardef) then
  2906. handle_procvar(getprocvardef,p2);
  2907. getprocvardef:=nil;
  2908. p1:=cassignmentnode.create(p1,p2);
  2909. end;
  2910. _PLUSASN :
  2911. begin
  2912. consume(_PLUSASN);
  2913. p2:=sub_expr(opcompare,true,false);
  2914. p1:=gen_c_style_operator(addn,p1,p2);
  2915. end;
  2916. _MINUSASN :
  2917. begin
  2918. consume(_MINUSASN);
  2919. p2:=sub_expr(opcompare,true,false);
  2920. p1:=gen_c_style_operator(subn,p1,p2);
  2921. end;
  2922. _STARASN :
  2923. begin
  2924. consume(_STARASN );
  2925. p2:=sub_expr(opcompare,true,false);
  2926. p1:=gen_c_style_operator(muln,p1,p2);
  2927. end;
  2928. _SLASHASN :
  2929. begin
  2930. consume(_SLASHASN );
  2931. p2:=sub_expr(opcompare,true,false);
  2932. p1:=gen_c_style_operator(slashn,p1,p2);
  2933. end;
  2934. else
  2935. updatefpos:=false;
  2936. end;
  2937. { get the resultdef for this expression }
  2938. if not assigned(p1.resultdef) and
  2939. dotypecheck then
  2940. do_typecheckpass(p1);
  2941. afterassignment:=oldafterassignment;
  2942. if updatefpos then
  2943. p1.fileinfo:=filepos;
  2944. expr:=p1;
  2945. end;
  2946. function get_intconst:TConstExprInt;
  2947. {Reads an expression, tries to evalute it and check if it is an integer
  2948. constant. Then the constant is returned.}
  2949. var
  2950. p:tnode;
  2951. begin
  2952. result:=0;
  2953. p:=comp_expr(true,false);
  2954. if not codegenerror then
  2955. begin
  2956. if (p.nodetype<>ordconstn) or
  2957. not(is_integer(p.resultdef)) then
  2958. Message(parser_e_illegal_expression)
  2959. else
  2960. result:=tordconstnode(p).value;
  2961. end;
  2962. p.free;
  2963. end;
  2964. function get_stringconst:string;
  2965. {Reads an expression, tries to evaluate it and checks if it is a string
  2966. constant. Then the constant is returned.}
  2967. var
  2968. p:tnode;
  2969. begin
  2970. get_stringconst:='';
  2971. p:=comp_expr(true,false);
  2972. if p.nodetype<>stringconstn then
  2973. begin
  2974. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2975. get_stringconst:=char(int64(tordconstnode(p).value))
  2976. else
  2977. Message(parser_e_illegal_expression);
  2978. end
  2979. else
  2980. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2981. p.free;
  2982. end;
  2983. end.