pexpr.pas 125 KB

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