pexpr.pas 109 KB

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