pexpr.pas 111 KB

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