pexpr.pas 106 KB

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