pexpr.pas 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911
  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. staticvarsym,
  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. syssym :
  1368. begin
  1369. p1:=statement_syssym(tsyssym(srsym).number);
  1370. end;
  1371. typesym :
  1372. begin
  1373. hdef:=ttypesym(srsym).typedef;
  1374. if not assigned(hdef) then
  1375. begin
  1376. again:=false;
  1377. end
  1378. else
  1379. begin
  1380. { We need to know if this unit uses Variants }
  1381. if (hdef=cvarianttype) and
  1382. not(cs_compilesystem in current_settings.moduleswitches) then
  1383. current_module.flags:=current_module.flags or uf_uses_variants;
  1384. if (block_type<>bt_specialize) and
  1385. try_to_consume(_LKLAMMER) then
  1386. begin
  1387. p1:=comp_expr(true);
  1388. consume(_RKLAMMER);
  1389. p1:=ctypeconvnode.create_explicit(p1,hdef);
  1390. end
  1391. else { not LKLAMMER }
  1392. if (token=_POINT) and
  1393. is_object(hdef) then
  1394. begin
  1395. consume(_POINT);
  1396. if assigned(current_procinfo) and
  1397. assigned(current_procinfo.procdef._class) and
  1398. not(getaddr) then
  1399. begin
  1400. if current_procinfo.procdef._class.is_related(tobjectdef(hdef)) then
  1401. begin
  1402. p1:=ctypenode.create(hdef);
  1403. { search also in inherited methods }
  1404. searchsym_in_class(tobjectdef(hdef),current_procinfo.procdef._class,pattern,srsym,srsymtable);
  1405. if assigned(srsym) then
  1406. check_hints(srsym,srsym.symoptions);
  1407. consume(_ID);
  1408. do_member_read(tobjectdef(hdef),false,srsym,p1,again,[]);
  1409. end
  1410. else
  1411. begin
  1412. Message(parser_e_no_super_class);
  1413. again:=false;
  1414. end;
  1415. end
  1416. else
  1417. begin
  1418. { allows @TObject.Load }
  1419. { also allows static methods and variables }
  1420. p1:=ctypenode.create(hdef);
  1421. { TP allows also @TMenu.Load if Load is only }
  1422. { defined in an anchestor class }
  1423. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1424. if assigned(srsym) then
  1425. begin
  1426. check_hints(srsym,srsym.symoptions);
  1427. if not(getaddr) and not(sp_static in srsym.symoptions) then
  1428. Message(sym_e_only_static_in_static)
  1429. else
  1430. begin
  1431. consume(_ID);
  1432. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1433. end;
  1434. end
  1435. else
  1436. Message1(sym_e_id_no_member,orgpattern);
  1437. end;
  1438. end
  1439. else
  1440. begin
  1441. { class reference ? }
  1442. if is_class(hdef) then
  1443. begin
  1444. if getaddr and (token=_POINT) then
  1445. begin
  1446. consume(_POINT);
  1447. { allows @Object.Method }
  1448. { also allows static methods and variables }
  1449. p1:=ctypenode.create(hdef);
  1450. { TP allows also @TMenu.Load if Load is only }
  1451. { defined in an anchestor class }
  1452. srsym:=search_class_member(tobjectdef(hdef),pattern);
  1453. if assigned(srsym) then
  1454. begin
  1455. check_hints(srsym,srsym.symoptions);
  1456. consume(_ID);
  1457. do_member_read(tobjectdef(hdef),getaddr,srsym,p1,again,[]);
  1458. end
  1459. else
  1460. begin
  1461. Message1(sym_e_id_no_member,orgpattern);
  1462. consume(_ID);
  1463. end;
  1464. end
  1465. else
  1466. begin
  1467. p1:=ctypenode.create(hdef);
  1468. { For a type block we simply return only
  1469. the type. For all other blocks we return
  1470. a loadvmt node }
  1471. if not(block_type in [bt_type,bt_specialize]) then
  1472. p1:=cloadvmtaddrnode.create(p1);
  1473. end;
  1474. end
  1475. else
  1476. p1:=ctypenode.create(hdef);
  1477. end;
  1478. end;
  1479. end;
  1480. enumsym :
  1481. begin
  1482. p1:=genenumnode(tenumsym(srsym));
  1483. end;
  1484. constsym :
  1485. begin
  1486. case tconstsym(srsym).consttyp of
  1487. constord :
  1488. begin
  1489. if tconstsym(srsym).constdef=nil then
  1490. internalerror(200403232);
  1491. p1:=cordconstnode.create(tconstsym(srsym).value.valueord,tconstsym(srsym).constdef,true);
  1492. end;
  1493. conststring :
  1494. begin
  1495. len:=tconstsym(srsym).value.len;
  1496. if not(cs_ansistrings in current_settings.localswitches) and (len>255) then
  1497. len:=255;
  1498. getmem(pc,len+1);
  1499. move(pchar(tconstsym(srsym).value.valueptr)^,pc^,len);
  1500. pc[len]:=#0;
  1501. p1:=cstringconstnode.createpchar(pc,len);
  1502. end;
  1503. constwstring :
  1504. p1:=cstringconstnode.createwstr(pcompilerwidestring(tconstsym(srsym).value.valueptr));
  1505. constreal :
  1506. p1:=crealconstnode.create(pbestreal(tconstsym(srsym).value.valueptr)^,pbestrealtype^);
  1507. constset :
  1508. p1:=csetconstnode.create(pconstset(tconstsym(srsym).value.valueptr),tconstsym(srsym).constdef);
  1509. constpointer :
  1510. p1:=cpointerconstnode.create(tconstsym(srsym).value.valueordptr,tconstsym(srsym).constdef);
  1511. constnil :
  1512. p1:=cnilnode.create;
  1513. constresourcestring:
  1514. begin
  1515. p1:=cloadnode.create(srsym,srsymtable);
  1516. do_typecheckpass(p1);
  1517. p1.resultdef:=cansistringtype;
  1518. end;
  1519. constguid :
  1520. p1:=cguidconstnode.create(pguid(tconstsym(srsym).value.valueptr)^);
  1521. else
  1522. internalerror(200507181);
  1523. end;
  1524. end;
  1525. procsym :
  1526. begin
  1527. { are we in a class method ? }
  1528. possible_error:=(srsymtable.symtabletype<>withsymtable) and
  1529. (srsym.owner.symtabletype=ObjectSymtable) and
  1530. not(is_interface(tdef(srsym.owner.defowner))) and
  1531. assigned(current_procinfo) and
  1532. (po_classmethod in current_procinfo.procdef.procoptions);
  1533. do_proc_call(srsym,srsymtable,nil,
  1534. (getaddr and not(token in [_CARET,_POINT])),
  1535. again,p1,[]);
  1536. { we need to know which procedure is called }
  1537. if possible_error then
  1538. begin
  1539. do_typecheckpass(p1);
  1540. if (p1.nodetype=calln) and
  1541. assigned(tcallnode(p1).procdefinition) and
  1542. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1543. not(po_classmethod in tcallnode(p1).procdefinition.procoptions) then
  1544. Message(parser_e_only_class_methods);
  1545. end;
  1546. end;
  1547. propertysym :
  1548. begin
  1549. { access to property in a method }
  1550. { are we in a class method ? }
  1551. if (srsymtable.symtabletype=ObjectSymtable) and
  1552. assigned(current_procinfo) and
  1553. (po_classmethod in current_procinfo.procdef.procoptions) then
  1554. Message(parser_e_only_class_methods);
  1555. { no method pointer }
  1556. p1:=nil;
  1557. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  1558. end;
  1559. labelsym :
  1560. begin
  1561. { Support @label }
  1562. if getaddr then
  1563. p1:=cloadnode.create(srsym,srsym.owner)
  1564. else
  1565. begin
  1566. consume(_COLON);
  1567. if tlabelsym(srsym).defined then
  1568. Message(sym_e_label_already_defined);
  1569. tlabelsym(srsym).defined:=true;
  1570. p1:=clabelnode.create(nil);
  1571. tlabelsym(srsym).code:=p1;
  1572. end;
  1573. end;
  1574. errorsym :
  1575. begin
  1576. p1:=cerrornode.create;
  1577. if try_to_consume(_LKLAMMER) then
  1578. begin
  1579. parse_paras(false,false,_RKLAMMER);
  1580. consume(_RKLAMMER);
  1581. end;
  1582. end;
  1583. else
  1584. begin
  1585. p1:=cerrornode.create;
  1586. Message(parser_e_illegal_expression);
  1587. end;
  1588. end; { end case }
  1589. end;
  1590. end;
  1591. {---------------------------------------------
  1592. Factor_Read_Set
  1593. ---------------------------------------------}
  1594. { Read a set between [] }
  1595. function factor_read_set:tnode;
  1596. var
  1597. p1,p2 : tnode;
  1598. lastp,
  1599. buildp : tarrayconstructornode;
  1600. begin
  1601. buildp:=nil;
  1602. { be sure that a least one arrayconstructn is used, also for an
  1603. empty [] }
  1604. if token=_RECKKLAMMER then
  1605. buildp:=carrayconstructornode.create(nil,buildp)
  1606. else
  1607. repeat
  1608. p1:=comp_expr(true);
  1609. if try_to_consume(_POINTPOINT) then
  1610. begin
  1611. p2:=comp_expr(true);
  1612. p1:=carrayconstructorrangenode.create(p1,p2);
  1613. end;
  1614. { insert at the end of the tree, to get the correct order }
  1615. if not assigned(buildp) then
  1616. begin
  1617. buildp:=carrayconstructornode.create(p1,nil);
  1618. lastp:=buildp;
  1619. end
  1620. else
  1621. begin
  1622. lastp.right:=carrayconstructornode.create(p1,nil);
  1623. lastp:=tarrayconstructornode(lastp.right);
  1624. end;
  1625. { there could be more elements }
  1626. until not try_to_consume(_COMMA);
  1627. factor_read_set:=buildp;
  1628. end;
  1629. {---------------------------------------------
  1630. PostFixOperators
  1631. ---------------------------------------------}
  1632. procedure postfixoperators(var p1:tnode;var again:boolean);
  1633. { tries to avoid syntax errors after invalid qualifiers }
  1634. procedure recoverconsume_postfixops;
  1635. begin
  1636. repeat
  1637. if not try_to_consume(_CARET) then
  1638. if try_to_consume(_POINT) then
  1639. try_to_consume(_ID)
  1640. else if try_to_consume(_LECKKLAMMER) then
  1641. begin
  1642. repeat
  1643. comp_expr(true);
  1644. until not try_to_consume(_COMMA);
  1645. consume(_RECKKLAMMER);
  1646. end
  1647. else
  1648. break;
  1649. until false;
  1650. end;
  1651. procedure handle_variantarray;
  1652. var
  1653. p4 : tnode;
  1654. newstatement : tstatementnode;
  1655. tempresultvariant,
  1656. temp : ttempcreatenode;
  1657. paras : tcallparanode;
  1658. newblock : tnode;
  1659. countindices : aint;
  1660. begin
  1661. { create statements with call initialize the arguments and
  1662. call fpc_dynarr_setlength }
  1663. newblock:=internalstatements(newstatement);
  1664. { get temp for array of indicies,
  1665. we set the real size later }
  1666. temp:=ctempcreatenode.create(sinttype,4,tt_persistent,false);
  1667. addstatement(newstatement,temp);
  1668. countindices:=0;
  1669. repeat
  1670. p4:=comp_expr(true);
  1671. addstatement(newstatement,cassignmentnode.create(
  1672. ctemprefnode.create_offset(temp,countindices*sinttype.size),p4));
  1673. inc(countindices);
  1674. until not try_to_consume(_COMMA);
  1675. { set real size }
  1676. temp.size:=countindices*sinttype.size;
  1677. consume(_RECKKLAMMER);
  1678. { we need only a write access if a := follows }
  1679. if token=_ASSIGNMENT then
  1680. begin
  1681. consume(_ASSIGNMENT);
  1682. p4:=comp_expr(true);
  1683. { create call to fpc_vararray_put }
  1684. paras:=ccallparanode.create(cordconstnode.create
  1685. (countindices,sinttype,true),
  1686. ccallparanode.create(caddrnode.create_internal
  1687. (ctemprefnode.create(temp)),
  1688. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1689. ccallparanode.create(p1
  1690. ,nil))));
  1691. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1692. addstatement(newstatement,ctempdeletenode.create(temp));
  1693. end
  1694. else
  1695. begin
  1696. { create temp for result }
  1697. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1698. addstatement(newstatement,tempresultvariant);
  1699. { create call to fpc_vararray_get }
  1700. paras:=ccallparanode.create(cordconstnode.create
  1701. (countindices,sinttype,true),
  1702. ccallparanode.create(caddrnode.create_internal
  1703. (ctemprefnode.create(temp)),
  1704. ccallparanode.create(p1,
  1705. ccallparanode.create(
  1706. ctemprefnode.create(tempresultvariant)
  1707. ,nil))));
  1708. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1709. addstatement(newstatement,ctempdeletenode.create(temp));
  1710. { the last statement should return the value as
  1711. location and type, this is done be referencing the
  1712. temp and converting it first from a persistent temp to
  1713. normal temp }
  1714. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1715. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1716. end;
  1717. p1:=newblock;
  1718. end;
  1719. var
  1720. store_static : boolean;
  1721. protsym : tpropertysym;
  1722. p2,p3 : tnode;
  1723. srsym : tsym;
  1724. srsymtable : TSymtable;
  1725. classh : tobjectdef;
  1726. { shouldn't be used that often, so the extra overhead is ok to save
  1727. stack space }
  1728. dispatchstring : ansistring;
  1729. label
  1730. skipreckklammercheck;
  1731. begin
  1732. again:=true;
  1733. while again do
  1734. begin
  1735. { we need the resultdef }
  1736. do_typecheckpass(p1);
  1737. if codegenerror then
  1738. begin
  1739. recoverconsume_postfixops;
  1740. exit;
  1741. end;
  1742. { handle token }
  1743. case token of
  1744. _CARET:
  1745. begin
  1746. consume(_CARET);
  1747. { support tp/mac procvar^ if the procvar returns a
  1748. pointer type }
  1749. if ((m_tp_procvar in current_settings.modeswitches) or
  1750. (m_mac_procvar in current_settings.modeswitches)) and
  1751. (p1.resultdef.typ=procvardef) and
  1752. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1753. begin
  1754. p1:=ccallnode.create_procvar(nil,p1);
  1755. typecheckpass(p1);
  1756. end;
  1757. if (p1.resultdef.typ<>pointerdef) then
  1758. begin
  1759. { ^ as binary operator is a problem!!!! (FK) }
  1760. again:=false;
  1761. Message(parser_e_invalid_qualifier);
  1762. recoverconsume_postfixops;
  1763. p1.destroy;
  1764. p1:=cerrornode.create;
  1765. end
  1766. else
  1767. p1:=cderefnode.create(p1);
  1768. end;
  1769. _LECKKLAMMER:
  1770. begin
  1771. if is_class_or_interface(p1.resultdef) then
  1772. begin
  1773. { default property }
  1774. protsym:=search_default_property(tobjectdef(p1.resultdef));
  1775. if not(assigned(protsym)) then
  1776. begin
  1777. p1.destroy;
  1778. p1:=cerrornode.create;
  1779. again:=false;
  1780. message(parser_e_no_default_property_available);
  1781. end
  1782. else
  1783. begin
  1784. { The property symbol is referenced indirect }
  1785. inc(protsym.refs);
  1786. handle_propertysym(protsym,protsym.owner,p1);
  1787. end;
  1788. end
  1789. else
  1790. begin
  1791. consume(_LECKKLAMMER);
  1792. repeat
  1793. case p1.resultdef.typ of
  1794. pointerdef:
  1795. begin
  1796. { support delphi autoderef }
  1797. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1798. (m_autoderef in current_settings.modeswitches) then
  1799. p1:=cderefnode.create(p1);
  1800. p2:=comp_expr(true);
  1801. { Support Pbytevar[0..9] which returns array [0..9].}
  1802. if try_to_consume(_POINTPOINT) then
  1803. p2:=crangenode.create(p2,comp_expr(true));
  1804. p1:=cvecnode.create(p1,p2);
  1805. end;
  1806. variantdef:
  1807. begin
  1808. handle_variantarray;
  1809. { the RECKKLAMMER is already read }
  1810. goto skipreckklammercheck;
  1811. end;
  1812. stringdef :
  1813. begin
  1814. p2:=comp_expr(true);
  1815. { Support string[0..9] which returns array [0..9] of char.}
  1816. if try_to_consume(_POINTPOINT) then
  1817. p2:=crangenode.create(p2,comp_expr(true));
  1818. p1:=cvecnode.create(p1,p2);
  1819. end;
  1820. arraydef:
  1821. begin
  1822. p2:=comp_expr(true);
  1823. { support SEG:OFS for go32v2 Mem[] }
  1824. if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
  1825. (p1.nodetype=loadn) and
  1826. assigned(tloadnode(p1).symtableentry) and
  1827. assigned(tloadnode(p1).symtableentry.owner.name) and
  1828. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1829. ((tloadnode(p1).symtableentry.name='MEM') or
  1830. (tloadnode(p1).symtableentry.name='MEMW') or
  1831. (tloadnode(p1).symtableentry.name='MEML')) then
  1832. begin
  1833. if try_to_consume(_COLON) then
  1834. begin
  1835. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1836. p2:=comp_expr(true);
  1837. p2:=caddnode.create(addn,p2,p3);
  1838. if try_to_consume(_POINTPOINT) then
  1839. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1840. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true),p3.getcopy));
  1841. p1:=cvecnode.create(p1,p2);
  1842. include(tvecnode(p1).flags,nf_memseg);
  1843. include(tvecnode(p1).flags,nf_memindex);
  1844. end
  1845. else
  1846. begin
  1847. if try_to_consume(_POINTPOINT) then
  1848. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1849. p2:=crangenode.create(p2,comp_expr(true));
  1850. p1:=cvecnode.create(p1,p2);
  1851. include(tvecnode(p1).flags,nf_memindex);
  1852. end;
  1853. end
  1854. else
  1855. begin
  1856. if try_to_consume(_POINTPOINT) then
  1857. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1858. p2:=crangenode.create(p2,comp_expr(true));
  1859. p1:=cvecnode.create(p1,p2);
  1860. end;
  1861. end;
  1862. else
  1863. begin
  1864. Message(parser_e_invalid_qualifier);
  1865. p1.destroy;
  1866. p1:=cerrornode.create;
  1867. comp_expr(true);
  1868. again:=false;
  1869. end;
  1870. end;
  1871. do_typecheckpass(p1);
  1872. until not try_to_consume(_COMMA);
  1873. consume(_RECKKLAMMER);
  1874. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1875. skipreckklammercheck:
  1876. end;
  1877. end;
  1878. _POINT :
  1879. begin
  1880. consume(_POINT);
  1881. if (p1.resultdef.typ=pointerdef) and
  1882. (m_autoderef in current_settings.modeswitches) then
  1883. begin
  1884. p1:=cderefnode.create(p1);
  1885. do_typecheckpass(p1);
  1886. end;
  1887. case p1.resultdef.typ of
  1888. recorddef:
  1889. begin
  1890. if token=_ID then
  1891. begin
  1892. srsym:=tsym(trecorddef(p1.resultdef).symtable.Find(pattern));
  1893. if assigned(srsym) and
  1894. (srsym.typ=fieldvarsym) then
  1895. begin
  1896. check_hints(srsym,srsym.symoptions);
  1897. p1:=csubscriptnode.create(srsym,p1)
  1898. end
  1899. else
  1900. begin
  1901. Message1(sym_e_illegal_field,pattern);
  1902. p1.destroy;
  1903. p1:=cerrornode.create;
  1904. end;
  1905. end;
  1906. consume(_ID);
  1907. end;
  1908. variantdef:
  1909. begin
  1910. { dispatch call? }
  1911. if token=_ID then
  1912. begin
  1913. dispatchstring:=orgpattern;
  1914. consume(_ID);
  1915. if try_to_consume(_LKLAMMER) then
  1916. begin
  1917. p2:=parse_paras(false,true,_RKLAMMER);
  1918. consume(_RKLAMMER);
  1919. p1:=translate_disp_call(p1,p2,dispatchstring);
  1920. end
  1921. else
  1922. p2:=nil;
  1923. end
  1924. else { Error }
  1925. Consume(_ID);
  1926. end;
  1927. classrefdef:
  1928. begin
  1929. if token=_ID then
  1930. begin
  1931. classh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  1932. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1933. if assigned(srsym) then
  1934. begin
  1935. check_hints(srsym,srsym.symoptions);
  1936. consume(_ID);
  1937. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1938. end
  1939. else
  1940. begin
  1941. Message1(sym_e_id_no_member,orgpattern);
  1942. p1.destroy;
  1943. p1:=cerrornode.create;
  1944. { try to clean up }
  1945. consume(_ID);
  1946. end;
  1947. end
  1948. else { Error }
  1949. Consume(_ID);
  1950. end;
  1951. objectdef:
  1952. begin
  1953. if token=_ID then
  1954. begin
  1955. store_static:=allow_only_static;
  1956. allow_only_static:=false;
  1957. classh:=tobjectdef(p1.resultdef);
  1958. searchsym_in_class(classh,classh,pattern,srsym,srsymtable);
  1959. allow_only_static:=store_static;
  1960. if assigned(srsym) then
  1961. begin
  1962. check_hints(srsym,srsym.symoptions);
  1963. consume(_ID);
  1964. do_member_read(classh,getaddr,srsym,p1,again,[]);
  1965. end
  1966. else
  1967. begin
  1968. Message1(sym_e_id_no_member,orgpattern);
  1969. p1.destroy;
  1970. p1:=cerrornode.create;
  1971. { try to clean up }
  1972. consume(_ID);
  1973. end;
  1974. end
  1975. else { Error }
  1976. Consume(_ID);
  1977. end;
  1978. pointerdef:
  1979. begin
  1980. Message(parser_e_invalid_qualifier);
  1981. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  1982. Message(parser_h_maybe_deref_caret_missing);
  1983. end;
  1984. else
  1985. begin
  1986. Message(parser_e_invalid_qualifier);
  1987. p1.destroy;
  1988. p1:=cerrornode.create;
  1989. { Error }
  1990. consume(_ID);
  1991. end;
  1992. end;
  1993. end;
  1994. else
  1995. begin
  1996. { is this a procedure variable ? }
  1997. if assigned(p1.resultdef) and
  1998. (p1.resultdef.typ=procvardef) then
  1999. begin
  2000. if assigned(getprocvardef) and
  2001. equal_defs(p1.resultdef,getprocvardef) then
  2002. begin
  2003. { classes can define now types so we've to allow
  2004. type casts with these nested types as well }
  2005. if (p1.nodetype=typen) and
  2006. try_to_consume(_LKLAMMER) then
  2007. begin
  2008. p1:=comp_expr(true);
  2009. consume(_RKLAMMER);
  2010. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2011. end
  2012. else
  2013. again:=false
  2014. end
  2015. else
  2016. begin
  2017. if try_to_consume(_LKLAMMER) then
  2018. begin
  2019. p2:=parse_paras(false,false,_RKLAMMER);
  2020. consume(_RKLAMMER);
  2021. p1:=ccallnode.create_procvar(p2,p1);
  2022. { proc():= is never possible }
  2023. if token=_ASSIGNMENT then
  2024. begin
  2025. Message(parser_e_illegal_expression);
  2026. p1.free;
  2027. p1:=cerrornode.create;
  2028. again:=false;
  2029. end;
  2030. end
  2031. else
  2032. again:=false;
  2033. end;
  2034. end
  2035. else
  2036. again:=false;
  2037. end;
  2038. end;
  2039. end; { while again }
  2040. end;
  2041. {---------------------------------------------
  2042. Factor (Main)
  2043. ---------------------------------------------}
  2044. var
  2045. l : longint;
  2046. ic : int64;
  2047. qc : qword;
  2048. {$ifndef cpu64}
  2049. card : cardinal;
  2050. {$endif cpu64}
  2051. oldp1,
  2052. p1 : tnode;
  2053. code : integer;
  2054. again : boolean;
  2055. srsym : tsym;
  2056. srsymtable : TSymtable;
  2057. pd : tprocdef;
  2058. hclassdef : tobjectdef;
  2059. d : bestreal;
  2060. cur : currency;
  2061. hs,hsorg : string;
  2062. hdef : tdef;
  2063. filepos : tfileposinfo;
  2064. begin
  2065. oldp1:=nil;
  2066. p1:=nil;
  2067. filepos:=current_tokenpos;
  2068. again:=false;
  2069. if token=_ID then
  2070. begin
  2071. again:=true;
  2072. { Handle references to self }
  2073. if (idtoken=_SELF) and
  2074. not(block_type in [bt_const,bt_type]) and
  2075. assigned(current_procinfo) and
  2076. assigned(current_procinfo.procdef._class) then
  2077. begin
  2078. p1:=load_self_node;
  2079. consume(_ID);
  2080. again:=true;
  2081. end
  2082. else
  2083. factor_read_id(p1,again);
  2084. if again then
  2085. begin
  2086. if (p1<>oldp1) then
  2087. begin
  2088. if assigned(p1) then
  2089. p1.fileinfo:=filepos;
  2090. oldp1:=p1;
  2091. filepos:=current_tokenpos;
  2092. end;
  2093. { handle post fix operators }
  2094. postfixoperators(p1,again);
  2095. end;
  2096. end
  2097. else
  2098. case token of
  2099. _RETURN :
  2100. begin
  2101. consume(_RETURN);
  2102. if not(token in [_SEMICOLON,_ELSE,_END]) then
  2103. p1 := cexitnode.create(comp_expr(true))
  2104. else
  2105. p1 := cexitnode.create(nil);
  2106. end;
  2107. _INHERITED :
  2108. begin
  2109. again:=true;
  2110. consume(_INHERITED);
  2111. if assigned(current_procinfo) and
  2112. assigned(current_procinfo.procdef._class) then
  2113. begin
  2114. hclassdef:=current_procinfo.procdef._class.childof;
  2115. { if inherited; only then we need the method with
  2116. the same name }
  2117. if token in endtokens then
  2118. begin
  2119. hs:=current_procinfo.procdef.procsym.name;
  2120. hsorg:=current_procinfo.procdef.procsym.realname;
  2121. anon_inherited:=true;
  2122. { For message methods we need to search using the message
  2123. number or string }
  2124. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  2125. srdef:=nil;
  2126. if (po_msgint in pd.procoptions) then
  2127. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  2128. else
  2129. if (po_msgstr in pd.procoptions) then
  2130. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  2131. else
  2132. searchsym_in_class(hclassdef,current_procinfo.procdef._class,hs,srsym,srsymtable);
  2133. end
  2134. else
  2135. begin
  2136. hs:=pattern;
  2137. hsorg:=orgpattern;
  2138. consume(_ID);
  2139. anon_inherited:=false;
  2140. searchsym_in_class(hclassdef,current_procinfo.procdef._class,hs,srsym,srsymtable);
  2141. end;
  2142. if assigned(srsym) then
  2143. begin
  2144. check_hints(srsym,srsym.symoptions);
  2145. { load the procdef from the inherited class and
  2146. not from self }
  2147. if srsym.typ in [procsym,propertysym] then
  2148. begin
  2149. if (srsym.typ = procsym) then
  2150. begin
  2151. hdef:=hclassdef;
  2152. if (po_classmethod in current_procinfo.procdef.procoptions) or
  2153. (po_staticmethod in current_procinfo.procdef.procoptions) then
  2154. hdef:=tclassrefdef.create(hdef);
  2155. p1:=ctypenode.create(hdef);
  2156. end;
  2157. end
  2158. else
  2159. begin
  2160. Message(parser_e_methode_id_expected);
  2161. p1:=cerrornode.create;
  2162. end;
  2163. do_member_read(hclassdef,getaddr,srsym,p1,again,[cnf_inherited,cnf_anon_inherited]);
  2164. end
  2165. else
  2166. begin
  2167. if anon_inherited then
  2168. begin
  2169. { For message methods we need to call DefaultHandler }
  2170. if (po_msgint in pd.procoptions) or
  2171. (po_msgstr in pd.procoptions) then
  2172. begin
  2173. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable);
  2174. if not assigned(srsym) or
  2175. (srsym.typ<>procsym) then
  2176. internalerror(200303171);
  2177. p1:=nil;
  2178. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[]);
  2179. end
  2180. else
  2181. begin
  2182. { we need to ignore the inherited; }
  2183. p1:=cnothingnode.create;
  2184. end;
  2185. end
  2186. else
  2187. begin
  2188. Message1(sym_e_id_no_member,hsorg);
  2189. p1:=cerrornode.create;
  2190. end;
  2191. again:=false;
  2192. end;
  2193. { turn auto inheriting off }
  2194. anon_inherited:=false;
  2195. end
  2196. else
  2197. begin
  2198. Message(parser_e_generic_methods_only_in_methods);
  2199. again:=false;
  2200. p1:=cerrornode.create;
  2201. end;
  2202. postfixoperators(p1,again);
  2203. end;
  2204. _INTCONST :
  2205. begin
  2206. {$ifdef cpu64}
  2207. { when already running under 64bit must read int64 constant, because reading
  2208. cardinal first will also succeed (code=0) for values > maxcardinal, because
  2209. range checking is off by default (PFV) }
  2210. val(pattern,ic,code);
  2211. if code=0 then
  2212. begin
  2213. consume(_INTCONST);
  2214. int_to_type(ic,hdef);
  2215. p1:=cordconstnode.create(ic,hdef,true);
  2216. end
  2217. else
  2218. begin
  2219. { try qword next }
  2220. val(pattern,qc,code);
  2221. if code=0 then
  2222. begin
  2223. consume(_INTCONST);
  2224. hdef:=u64inttype;
  2225. p1:=cordconstnode.create(qc,hdef,true);
  2226. end;
  2227. end;
  2228. {$else}
  2229. { try cardinal first }
  2230. val(pattern,card,code);
  2231. if code=0 then
  2232. begin
  2233. consume(_INTCONST);
  2234. int_to_type(card,hdef);
  2235. p1:=cordconstnode.create(card,hdef,true);
  2236. end
  2237. else
  2238. begin
  2239. { then longint }
  2240. val(pattern,l,code);
  2241. if code = 0 then
  2242. begin
  2243. consume(_INTCONST);
  2244. int_to_type(l,hdef);
  2245. p1:=cordconstnode.create(l,hdef,true);
  2246. end
  2247. else
  2248. begin
  2249. { then int64 }
  2250. val(pattern,ic,code);
  2251. if code=0 then
  2252. begin
  2253. consume(_INTCONST);
  2254. int_to_type(ic,hdef);
  2255. p1:=cordconstnode.create(ic,hdef,true);
  2256. end
  2257. else
  2258. begin
  2259. { try qword next }
  2260. val(pattern,qc,code);
  2261. if code=0 then
  2262. begin
  2263. consume(_INTCONST);
  2264. hdef:=u64inttype;
  2265. p1:=cordconstnode.create(tconstexprint(qc),hdef,true);
  2266. end;
  2267. end;
  2268. end;
  2269. end;
  2270. {$endif}
  2271. if code<>0 then
  2272. begin
  2273. { finally float }
  2274. val(pattern,d,code);
  2275. if code<>0 then
  2276. begin
  2277. Message(parser_e_invalid_integer);
  2278. consume(_INTCONST);
  2279. l:=1;
  2280. p1:=cordconstnode.create(l,sinttype,true);
  2281. end
  2282. else
  2283. begin
  2284. consume(_INTCONST);
  2285. p1:=crealconstnode.create(d,pbestrealtype^);
  2286. end;
  2287. end;
  2288. end;
  2289. _REALNUMBER :
  2290. begin
  2291. val(pattern,d,code);
  2292. if code<>0 then
  2293. begin
  2294. Message(parser_e_error_in_real);
  2295. d:=1.0;
  2296. end;
  2297. consume(_REALNUMBER);
  2298. {$ifdef FPC_REAL2REAL_FIXED}
  2299. if (d = single(d)) then
  2300. p1:=crealconstnode.create(d,s32floattype)
  2301. else if (d = double(d)) then
  2302. p1:=crealconstnode.create(d,s64floattype)
  2303. else
  2304. {$endif FPC_REAL2REAL_FIXED}
  2305. p1:=crealconstnode.create(d,pbestrealtype^);
  2306. {$ifdef FPC_HAS_STR_CURRENCY}
  2307. val(pattern,cur,code);
  2308. if code=0 then
  2309. trealconstnode(p1).value_currency:=cur;
  2310. {$endif FPC_HAS_STR_CURRENCY}
  2311. end;
  2312. _STRING :
  2313. begin
  2314. string_dec(hdef);
  2315. { STRING can be also a type cast }
  2316. if try_to_consume(_LKLAMMER) then
  2317. begin
  2318. p1:=comp_expr(true);
  2319. consume(_RKLAMMER);
  2320. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2321. { handle postfix operators here e.g. string(a)[10] }
  2322. again:=true;
  2323. postfixoperators(p1,again);
  2324. end
  2325. else
  2326. p1:=ctypenode.create(hdef);
  2327. end;
  2328. _FILE :
  2329. begin
  2330. hdef:=cfiletype;
  2331. consume(_FILE);
  2332. { FILE can be also a type cast }
  2333. if try_to_consume(_LKLAMMER) then
  2334. begin
  2335. p1:=comp_expr(true);
  2336. consume(_RKLAMMER);
  2337. p1:=ctypeconvnode.create_explicit(p1,hdef);
  2338. { handle postfix operators here e.g. string(a)[10] }
  2339. again:=true;
  2340. postfixoperators(p1,again);
  2341. end
  2342. else
  2343. begin
  2344. p1:=ctypenode.create(hdef);
  2345. end;
  2346. end;
  2347. _CSTRING :
  2348. begin
  2349. p1:=cstringconstnode.createstr(pattern);
  2350. consume(_CSTRING);
  2351. end;
  2352. _CCHAR :
  2353. begin
  2354. p1:=cordconstnode.create(ord(pattern[1]),cchartype,true);
  2355. consume(_CCHAR);
  2356. end;
  2357. _CWSTRING:
  2358. begin
  2359. p1:=cstringconstnode.createwstr(patternw);
  2360. consume(_CWSTRING);
  2361. end;
  2362. _CWCHAR:
  2363. begin
  2364. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  2365. consume(_CWCHAR);
  2366. end;
  2367. _KLAMMERAFFE :
  2368. begin
  2369. consume(_KLAMMERAFFE);
  2370. got_addrn:=true;
  2371. { support both @<x> and @(<x>) }
  2372. if try_to_consume(_LKLAMMER) then
  2373. begin
  2374. p1:=factor(true);
  2375. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2376. begin
  2377. again:=true;
  2378. postfixoperators(p1,again);
  2379. end;
  2380. consume(_RKLAMMER);
  2381. end
  2382. else
  2383. p1:=factor(true);
  2384. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2385. begin
  2386. again:=true;
  2387. postfixoperators(p1,again);
  2388. end;
  2389. got_addrn:=false;
  2390. p1:=caddrnode.create(p1);
  2391. if cs_typed_addresses in current_settings.localswitches then
  2392. include(p1.flags,nf_typedaddr);
  2393. { Store the procvar that we are expecting, the
  2394. addrn will use the information to find the correct
  2395. procdef or it will return an error }
  2396. if assigned(getprocvardef) and
  2397. (taddrnode(p1).left.nodetype = loadn) then
  2398. taddrnode(p1).getprocvardef:=getprocvardef;
  2399. end;
  2400. _LKLAMMER :
  2401. begin
  2402. consume(_LKLAMMER);
  2403. p1:=comp_expr(true);
  2404. consume(_RKLAMMER);
  2405. { it's not a good solution }
  2406. { but (a+b)^ makes some problems }
  2407. if token in [_CARET,_POINT,_LECKKLAMMER] then
  2408. begin
  2409. again:=true;
  2410. postfixoperators(p1,again);
  2411. end;
  2412. end;
  2413. _LECKKLAMMER :
  2414. begin
  2415. consume(_LECKKLAMMER);
  2416. p1:=factor_read_set;
  2417. consume(_RECKKLAMMER);
  2418. end;
  2419. _PLUS :
  2420. begin
  2421. consume(_PLUS);
  2422. p1:=factor(false);
  2423. { we must generate a new node to do 0+<p1> otherwise the + will
  2424. not be checked }
  2425. p1:=caddnode.create(addn,genintconstnode(0),p1);
  2426. end;
  2427. _MINUS :
  2428. begin
  2429. consume(_MINUS);
  2430. if (token = _INTCONST) then
  2431. begin
  2432. { ugly hack, but necessary to be able to parse }
  2433. { -9223372036854775808 as int64 (JM) }
  2434. pattern := '-'+pattern;
  2435. p1:=sub_expr(oppower,false);
  2436. { -1 ** 4 should be - (1 ** 4) and not
  2437. (-1) ** 4
  2438. This was the reason of tw0869.pp test failure PM }
  2439. if p1.nodetype=starstarn then
  2440. begin
  2441. if tbinarynode(p1).left.nodetype=ordconstn then
  2442. begin
  2443. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  2444. p1:=cunaryminusnode.create(p1);
  2445. end
  2446. else if tbinarynode(p1).left.nodetype=realconstn then
  2447. begin
  2448. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  2449. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  2450. p1:=cunaryminusnode.create(p1);
  2451. end
  2452. else
  2453. internalerror(20021029);
  2454. end;
  2455. end
  2456. else
  2457. begin
  2458. p1:=sub_expr(oppower,false);
  2459. p1:=cunaryminusnode.create(p1);
  2460. end;
  2461. end;
  2462. _OP_NOT :
  2463. begin
  2464. consume(_OP_NOT);
  2465. p1:=factor(false);
  2466. p1:=cnotnode.create(p1);
  2467. end;
  2468. _TRUE :
  2469. begin
  2470. consume(_TRUE);
  2471. p1:=cordconstnode.create(1,booltype,false);
  2472. end;
  2473. _FALSE :
  2474. begin
  2475. consume(_FALSE);
  2476. p1:=cordconstnode.create(0,booltype,false);
  2477. end;
  2478. _NIL :
  2479. begin
  2480. consume(_NIL);
  2481. p1:=cnilnode.create;
  2482. { It's really ugly code nil^, but delphi allows it }
  2483. if token in [_CARET] then
  2484. begin
  2485. again:=true;
  2486. postfixoperators(p1,again);
  2487. end;
  2488. end;
  2489. else
  2490. begin
  2491. Message(parser_e_illegal_expression);
  2492. p1:=cerrornode.create;
  2493. { recover }
  2494. consume(token);
  2495. end;
  2496. end;
  2497. { generate error node if no node is created }
  2498. if not assigned(p1) then
  2499. begin
  2500. {$ifdef EXTDEBUG}
  2501. Comment(V_Warning,'factor: p1=nil');
  2502. {$endif}
  2503. p1:=cerrornode.create;
  2504. end;
  2505. { get the resultdef for the node }
  2506. if (not assigned(p1.resultdef)) then
  2507. do_typecheckpass(p1);
  2508. if assigned(p1) and
  2509. (p1<>oldp1) then
  2510. p1.fileinfo:=filepos;
  2511. factor:=p1;
  2512. end;
  2513. {$maxfpuregisters default}
  2514. {****************************************************************************
  2515. Sub_Expr
  2516. ****************************************************************************}
  2517. const
  2518. { Warning these stay be ordered !! }
  2519. operator_levels:array[Toperator_precedence] of set of Ttoken=
  2520. ([_LT,_LTE,_GT,_GTE,_EQUAL,_UNEQUAL,_OP_IN],
  2521. [_PLUS,_MINUS,_OP_OR,_PIPE,_OP_XOR],
  2522. [_CARET,_SYMDIF,_STARSTAR,_STAR,_SLASH,
  2523. _OP_AS,_OP_IS,_OP_AND,_AMPERSAND,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR],
  2524. [_STARSTAR] );
  2525. function sub_expr(pred_level:Toperator_precedence;accept_equal : boolean):tnode;
  2526. {Reads a subexpression while the operators are of the current precedence
  2527. level, or any higher level. Replaces the old term, simpl_expr and
  2528. simpl2_expr.}
  2529. var
  2530. p1,p2 : tnode;
  2531. oldt : Ttoken;
  2532. filepos : tfileposinfo;
  2533. begin
  2534. if pred_level=highest_precedence then
  2535. p1:=factor(false)
  2536. else
  2537. p1:=sub_expr(succ(pred_level),true);
  2538. repeat
  2539. if (token in operator_levels[pred_level]) and
  2540. ((token<>_EQUAL) or accept_equal) then
  2541. begin
  2542. oldt:=token;
  2543. filepos:=current_tokenpos;
  2544. consume(token);
  2545. if pred_level=highest_precedence then
  2546. p2:=factor(false)
  2547. else
  2548. p2:=sub_expr(succ(pred_level),true);
  2549. case oldt of
  2550. _PLUS :
  2551. p1:=caddnode.create(addn,p1,p2);
  2552. _MINUS :
  2553. p1:=caddnode.create(subn,p1,p2);
  2554. _STAR :
  2555. p1:=caddnode.create(muln,p1,p2);
  2556. _SLASH :
  2557. p1:=caddnode.create(slashn,p1,p2);
  2558. _EQUAL :
  2559. p1:=caddnode.create(equaln,p1,p2);
  2560. _GT :
  2561. p1:=caddnode.create(gtn,p1,p2);
  2562. _LT :
  2563. p1:=caddnode.create(ltn,p1,p2);
  2564. _GTE :
  2565. p1:=caddnode.create(gten,p1,p2);
  2566. _LTE :
  2567. p1:=caddnode.create(lten,p1,p2);
  2568. _SYMDIF :
  2569. p1:=caddnode.create(symdifn,p1,p2);
  2570. _STARSTAR :
  2571. p1:=caddnode.create(starstarn,p1,p2);
  2572. _OP_AS :
  2573. p1:=casnode.create(p1,p2);
  2574. _OP_IN :
  2575. p1:=cinnode.create(p1,p2);
  2576. _OP_IS :
  2577. p1:=cisnode.create(p1,p2);
  2578. _OP_OR,
  2579. _PIPE {macpas only} :
  2580. begin
  2581. p1:=caddnode.create(orn,p1,p2);
  2582. if (oldt = _PIPE) then
  2583. include(p1.flags,nf_short_bool);
  2584. end;
  2585. _OP_AND,
  2586. _AMPERSAND {macpas only} :
  2587. begin
  2588. p1:=caddnode.create(andn,p1,p2);
  2589. if (oldt = _AMPERSAND) then
  2590. include(p1.flags,nf_short_bool);
  2591. end;
  2592. _OP_DIV :
  2593. p1:=cmoddivnode.create(divn,p1,p2);
  2594. _OP_NOT :
  2595. p1:=cnotnode.create(p1);
  2596. _OP_MOD :
  2597. p1:=cmoddivnode.create(modn,p1,p2);
  2598. _OP_SHL :
  2599. p1:=cshlshrnode.create(shln,p1,p2);
  2600. _OP_SHR :
  2601. p1:=cshlshrnode.create(shrn,p1,p2);
  2602. _OP_XOR :
  2603. p1:=caddnode.create(xorn,p1,p2);
  2604. _ASSIGNMENT :
  2605. p1:=cassignmentnode.create(p1,p2);
  2606. _CARET :
  2607. p1:=caddnode.create(caretn,p1,p2);
  2608. _UNEQUAL :
  2609. p1:=caddnode.create(unequaln,p1,p2);
  2610. end;
  2611. p1.fileinfo:=filepos;
  2612. end
  2613. else
  2614. break;
  2615. until false;
  2616. sub_expr:=p1;
  2617. end;
  2618. function comp_expr(accept_equal : boolean):tnode;
  2619. var
  2620. oldafterassignment : boolean;
  2621. p1 : tnode;
  2622. begin
  2623. oldafterassignment:=afterassignment;
  2624. afterassignment:=true;
  2625. p1:=sub_expr(opcompare,accept_equal);
  2626. { get the resultdef for this expression }
  2627. if not assigned(p1.resultdef) then
  2628. do_typecheckpass(p1);
  2629. afterassignment:=oldafterassignment;
  2630. comp_expr:=p1;
  2631. end;
  2632. function expr : tnode;
  2633. var
  2634. p1,p2 : tnode;
  2635. oldafterassignment : boolean;
  2636. oldp1 : tnode;
  2637. filepos : tfileposinfo;
  2638. begin
  2639. oldafterassignment:=afterassignment;
  2640. p1:=sub_expr(opcompare,true);
  2641. { get the resultdef for this expression }
  2642. if not assigned(p1.resultdef) then
  2643. do_typecheckpass(p1);
  2644. filepos:=current_tokenpos;
  2645. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  2646. afterassignment:=true;
  2647. oldp1:=p1;
  2648. case token of
  2649. _POINTPOINT :
  2650. begin
  2651. consume(_POINTPOINT);
  2652. p2:=sub_expr(opcompare,true);
  2653. p1:=crangenode.create(p1,p2);
  2654. end;
  2655. _ASSIGNMENT :
  2656. begin
  2657. consume(_ASSIGNMENT);
  2658. if (p1.resultdef.typ=procvardef) then
  2659. getprocvardef:=tprocvardef(p1.resultdef);
  2660. p2:=sub_expr(opcompare,true);
  2661. if assigned(getprocvardef) then
  2662. handle_procvar(getprocvardef,p2);
  2663. getprocvardef:=nil;
  2664. p1:=cassignmentnode.create(p1,p2);
  2665. end;
  2666. _PLUSASN :
  2667. begin
  2668. consume(_PLUSASN);
  2669. p2:=sub_expr(opcompare,true);
  2670. p1:=gen_c_style_operator(addn,p1,p2);
  2671. end;
  2672. _MINUSASN :
  2673. begin
  2674. consume(_MINUSASN);
  2675. p2:=sub_expr(opcompare,true);
  2676. p1:=gen_c_style_operator(subn,p1,p2);
  2677. end;
  2678. _STARASN :
  2679. begin
  2680. consume(_STARASN );
  2681. p2:=sub_expr(opcompare,true);
  2682. p1:=gen_c_style_operator(muln,p1,p2);
  2683. end;
  2684. _SLASHASN :
  2685. begin
  2686. consume(_SLASHASN );
  2687. p2:=sub_expr(opcompare,true);
  2688. p1:=gen_c_style_operator(slashn,p1,p2);
  2689. end;
  2690. end;
  2691. { get the resultdef for this expression }
  2692. if not assigned(p1.resultdef) then
  2693. do_typecheckpass(p1);
  2694. afterassignment:=oldafterassignment;
  2695. if p1<>oldp1 then
  2696. p1.fileinfo:=filepos;
  2697. expr:=p1;
  2698. end;
  2699. function get_intconst:TConstExprInt;
  2700. {Reads an expression, tries to evalute it and check if it is an integer
  2701. constant. Then the constant is returned.}
  2702. var
  2703. p:tnode;
  2704. begin
  2705. result:=0;
  2706. p:=comp_expr(true);
  2707. if not codegenerror then
  2708. begin
  2709. if (p.nodetype<>ordconstn) or
  2710. not(is_integer(p.resultdef)) then
  2711. Message(parser_e_illegal_expression)
  2712. else
  2713. result:=tordconstnode(p).value;
  2714. end;
  2715. p.free;
  2716. end;
  2717. function get_stringconst:string;
  2718. {Reads an expression, tries to evaluate it and checks if it is a string
  2719. constant. Then the constant is returned.}
  2720. var
  2721. p:tnode;
  2722. begin
  2723. get_stringconst:='';
  2724. p:=comp_expr(true);
  2725. if p.nodetype<>stringconstn then
  2726. begin
  2727. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  2728. get_stringconst:=char(tordconstnode(p).value)
  2729. else
  2730. Message(parser_e_illegal_expression);
  2731. end
  2732. else
  2733. get_stringconst:=strpas(tstringconstnode(p).value_str);
  2734. p.free;
  2735. end;
  2736. end.