pexpr.pas 126 KB

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