pexpr.pas 112 KB

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