2
0

pexpr.pas 108 KB

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