pexpr.pas 106 KB

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