pexpr.pas 107 KB

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