pexpr.pas 171 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283
  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. pgentype;
  25. { reads a whole expression }
  26. function expr(dotypecheck:boolean) : tnode;
  27. { reads an expression without assignements and .. }
  28. function comp_expr(accept_equal,typeonly:boolean):tnode;
  29. { reads a single factor }
  30. function factor(getaddr,typeonly,hadspecialize:boolean) : tnode;
  31. procedure string_dec(var def: tdef; allowtypedef: boolean);
  32. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  33. { the ID token has to be consumed before calling this function }
  34. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  35. function get_intconst:TConstExprInt;
  36. function get_stringconst:string;
  37. { Does some postprocessing for a generic type (especially when nested types
  38. of the specialization are used) }
  39. procedure post_comp_expr_gendef(var def: tdef);
  40. implementation
  41. uses
  42. { common }
  43. cutils,
  44. { global }
  45. verbose,
  46. systems,widestr,
  47. { symtable }
  48. symconst,symtable,symsym,symcpu,defutil,defcmp,
  49. { module }
  50. fmodule,ppu,
  51. { pass 1 }
  52. pass_1,htypechk,
  53. nmat,nadd,nmem,nset,ncnv,ninl,ncon,nld,nflw,nbas,nutils,
  54. { parser }
  55. scanner,
  56. pbase,pinline,ptype,pgenutil,procinfo,cpuinfo
  57. ;
  58. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;forward;
  59. const
  60. { true, if the inherited call is anonymous }
  61. anon_inherited : boolean = false;
  62. { last def found, only used by anon. inherited calls to insert proper type casts }
  63. srdef : tdef = nil;
  64. procedure string_dec(var def:tdef; allowtypedef: boolean);
  65. { reads a string type with optional length }
  66. { and returns a pointer to the string }
  67. { definition }
  68. var
  69. p : tnode;
  70. begin
  71. def:=cshortstringtype;
  72. consume(_STRING);
  73. if token=_LECKKLAMMER then
  74. begin
  75. if not(allowtypedef) then
  76. Message(parser_e_no_local_para_def);
  77. consume(_LECKKLAMMER);
  78. p:=comp_expr(true,false);
  79. if not is_constintnode(p) then
  80. begin
  81. Message(parser_e_illegal_expression);
  82. { error recovery }
  83. consume(_RECKKLAMMER);
  84. end
  85. else
  86. begin
  87. if (tordconstnode(p).value<=0) then
  88. begin
  89. Message(parser_e_invalid_string_size);
  90. tordconstnode(p).value:=255;
  91. end;
  92. if tordconstnode(p).value>255 then
  93. begin
  94. { longstring is currently unsupported (CEC)! }
  95. { t:=cstringdef.createlong(tordconstnode(p).value))}
  96. Message(parser_e_invalid_string_size);
  97. tordconstnode(p).value:=255;
  98. def:=cstringdef.createshort(int64(tordconstnode(p).value),true);
  99. end
  100. else
  101. if tordconstnode(p).value<>255 then
  102. def:=cstringdef.createshort(int64(tordconstnode(p).value),true);
  103. consume(_RECKKLAMMER);
  104. end;
  105. p.free;
  106. end
  107. else
  108. begin
  109. if cs_refcountedstrings in current_settings.localswitches then
  110. begin
  111. if m_default_unicodestring in current_settings.modeswitches then
  112. def:=cunicodestringtype
  113. else
  114. def:=cansistringtype
  115. end
  116. else
  117. def:=cshortstringtype;
  118. end;
  119. end;
  120. function parse_paras(__colon,__namedpara : boolean;end_of_paras : ttoken) : tnode;
  121. var
  122. p1,p2,argname : tnode;
  123. prev_in_args,
  124. old_named_args_allowed,
  125. old_allow_array_constructor : boolean;
  126. begin
  127. if token=end_of_paras then
  128. begin
  129. parse_paras:=nil;
  130. exit;
  131. end;
  132. { save old values }
  133. prev_in_args:=in_args;
  134. old_allow_array_constructor:=allow_array_constructor;
  135. old_named_args_allowed:=named_args_allowed;
  136. { set para parsing values }
  137. in_args:=true;
  138. named_args_allowed:=false;
  139. allow_array_constructor:=true;
  140. p2:=nil;
  141. repeat
  142. if __namedpara then
  143. begin
  144. if token=_COMMA then
  145. begin
  146. { empty parameter }
  147. p2:=ccallparanode.create(cnothingnode.create,p2);
  148. end
  149. else
  150. begin
  151. named_args_allowed:=true;
  152. p1:=comp_expr(true,false);
  153. named_args_allowed:=false;
  154. if found_arg_name then
  155. begin
  156. argname:=p1;
  157. p1:=comp_expr(true,false);
  158. p2:=ccallparanode.create(p1,p2);
  159. tcallparanode(p2).parametername:=argname;
  160. end
  161. else
  162. p2:=ccallparanode.create(p1,p2);
  163. found_arg_name:=false;
  164. end;
  165. end
  166. else
  167. begin
  168. p1:=comp_expr(true,false);
  169. p2:=ccallparanode.create(p1,p2);
  170. end;
  171. { it's for the str(l:5,s); }
  172. if __colon and (token=_COLON) then
  173. begin
  174. consume(_COLON);
  175. p1:=comp_expr(true,false);
  176. p2:=ccallparanode.create(p1,p2);
  177. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  178. if try_to_consume(_COLON) then
  179. begin
  180. p1:=comp_expr(true,false);
  181. p2:=ccallparanode.create(p1,p2);
  182. include(tcallparanode(p2).callparaflags,cpf_is_colon_para);
  183. end
  184. end;
  185. until not try_to_consume(_COMMA);
  186. allow_array_constructor:=old_allow_array_constructor;
  187. in_args:=prev_in_args;
  188. named_args_allowed:=old_named_args_allowed;
  189. parse_paras:=p2;
  190. end;
  191. function gen_c_style_operator(ntyp:tnodetype;p1,p2:tnode) : tnode;
  192. var
  193. hp : tnode;
  194. hdef : tdef;
  195. temp : ttempcreatenode;
  196. newstatement : tstatementnode;
  197. begin
  198. { Properties are not allowed, because the write can
  199. be different from the read }
  200. if (nf_isproperty in p1.flags) then
  201. begin
  202. Message(type_e_variable_id_expected);
  203. { We can continue with the loading,
  204. it'll not create errors. Only the expected
  205. result can be wrong }
  206. end;
  207. hp:=p1;
  208. while assigned(hp) and
  209. (hp.nodetype in [derefn,subscriptn,vecn,typeconvn]) do
  210. hp:=tunarynode(hp).left;
  211. if not assigned(hp) then
  212. internalerror(200410121);
  213. if (hp.nodetype=calln) then
  214. begin
  215. typecheckpass(p1);
  216. result:=internalstatements(newstatement);
  217. hdef:=cpointerdef.getreusable(p1.resultdef);
  218. temp:=ctempcreatenode.create(hdef,sizeof(pint),tt_persistent,false);
  219. addstatement(newstatement,temp);
  220. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),caddrnode.create_internal(p1)));
  221. addstatement(newstatement,cassignmentnode.create(
  222. cderefnode.create(ctemprefnode.create(temp)),
  223. caddnode.create(ntyp,
  224. cderefnode.create(ctemprefnode.create(temp)),
  225. p2)));
  226. addstatement(newstatement,ctempdeletenode.create(temp));
  227. end
  228. else
  229. result:=cassignmentnode.create(p1,caddnode.create(ntyp,p1.getcopy,p2));
  230. end;
  231. function statement_syssym(l : byte) : tnode;
  232. var
  233. p1,p2,paras : tnode;
  234. err,
  235. prev_in_args : boolean;
  236. def : tdef;
  237. exit_procinfo: tprocinfo;
  238. begin
  239. prev_in_args:=in_args;
  240. case l of
  241. in_new_x :
  242. begin
  243. if afterassignment or in_args then
  244. statement_syssym:=new_function
  245. else
  246. statement_syssym:=new_dispose_statement(true);
  247. end;
  248. in_dispose_x :
  249. begin
  250. statement_syssym:=new_dispose_statement(false);
  251. end;
  252. in_ord_x :
  253. begin
  254. consume(_LKLAMMER);
  255. in_args:=true;
  256. p1:=comp_expr(true,false);
  257. consume(_RKLAMMER);
  258. p1:=geninlinenode(in_ord_x,false,p1);
  259. statement_syssym := p1;
  260. end;
  261. in_exit :
  262. begin
  263. statement_syssym:=nil;
  264. if try_to_consume(_LKLAMMER) then
  265. begin
  266. if not (m_mac in current_settings.modeswitches) then
  267. begin
  268. if not(try_to_consume(_RKLAMMER)) then
  269. begin
  270. p1:=comp_expr(true,false);
  271. consume(_RKLAMMER);
  272. if not assigned(current_procinfo) or
  273. (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
  274. is_void(current_procinfo.procdef.returndef) then
  275. begin
  276. Message(parser_e_void_function);
  277. { recovery }
  278. p1.free;
  279. p1:=nil;
  280. end;
  281. end
  282. else
  283. p1:=nil;
  284. end
  285. else
  286. begin
  287. { non local exit ? }
  288. if current_procinfo.procdef.procsym.name<>pattern then
  289. begin
  290. exit_procinfo:=current_procinfo.parent;
  291. while assigned(exit_procinfo) do
  292. begin
  293. if exit_procinfo.procdef.procsym.name=pattern then
  294. break;
  295. exit_procinfo:=exit_procinfo.parent;
  296. end;
  297. if assigned(exit_procinfo) then
  298. begin
  299. if not(assigned(exit_procinfo.nestedexitlabel)) then
  300. begin
  301. include(current_procinfo.flags,pi_has_nested_exit);
  302. exclude(current_procinfo.procdef.procoptions,po_inline);
  303. exit_procinfo.nestedexitlabel:=clabelsym.create('$nestedexit');
  304. { the compiler is responsible to define this label }
  305. exit_procinfo.nestedexitlabel.defined:=true;
  306. exit_procinfo.nestedexitlabel.used:=true;
  307. exit_procinfo.nestedexitlabel.jumpbuf:=clocalvarsym.create('LABEL$_'+exit_procinfo.nestedexitlabel.name,vs_value,rec_jmp_buf,[]);
  308. exit_procinfo.procdef.localst.insert(exit_procinfo.nestedexitlabel);
  309. exit_procinfo.procdef.localst.insert(exit_procinfo.nestedexitlabel.jumpbuf);
  310. end;
  311. statement_syssym:=cgotonode.create(exit_procinfo.nestedexitlabel);
  312. tgotonode(statement_syssym).labelsym:=exit_procinfo.nestedexitlabel;
  313. end
  314. else
  315. Message(parser_e_macpas_exit_wrong_param);
  316. end;
  317. consume(_ID);
  318. consume(_RKLAMMER);
  319. p1:=nil;
  320. end
  321. end
  322. else
  323. p1:=nil;
  324. if not assigned(statement_syssym) then
  325. statement_syssym:=cexitnode.create(p1);
  326. end;
  327. in_break :
  328. begin
  329. statement_syssym:=cbreaknode.create
  330. end;
  331. in_continue :
  332. begin
  333. statement_syssym:=ccontinuenode.create
  334. end;
  335. in_leave :
  336. begin
  337. if m_mac in current_settings.modeswitches then
  338. statement_syssym:=cbreaknode.create
  339. else
  340. begin
  341. Message1(sym_e_id_not_found, orgpattern);
  342. statement_syssym:=cerrornode.create;
  343. end;
  344. end;
  345. in_cycle :
  346. begin
  347. if m_mac in current_settings.modeswitches then
  348. statement_syssym:=ccontinuenode.create
  349. else
  350. begin
  351. Message1(sym_e_id_not_found, orgpattern);
  352. statement_syssym:=cerrornode.create;
  353. end;
  354. end;
  355. in_typeof_x :
  356. begin
  357. consume(_LKLAMMER);
  358. in_args:=true;
  359. p1:=comp_expr(true,false);
  360. consume(_RKLAMMER);
  361. if p1.nodetype=typen then
  362. ttypenode(p1).allowed:=true;
  363. { Allow classrefdef, which is required for
  364. Typeof(self) in static class methods }
  365. if not(is_objc_class_or_protocol(p1.resultdef)) and
  366. not(is_java_class_or_interface(p1.resultdef)) and
  367. ((p1.resultdef.typ = objectdef) or
  368. (assigned(current_procinfo) and
  369. ((po_classmethod in current_procinfo.procdef.procoptions) or
  370. (po_staticmethod in current_procinfo.procdef.procoptions)) and
  371. (p1.resultdef.typ=classrefdef))) then
  372. statement_syssym:=geninlinenode(in_typeof_x,false,p1)
  373. else
  374. begin
  375. Message(parser_e_class_id_expected);
  376. p1.destroy;
  377. statement_syssym:=cerrornode.create;
  378. end;
  379. end;
  380. in_sizeof_x,
  381. in_bitsizeof_x :
  382. begin
  383. consume(_LKLAMMER);
  384. in_args:=true;
  385. p1:=comp_expr(true,false);
  386. consume(_RKLAMMER);
  387. if ((p1.nodetype<>typen) and
  388. (
  389. (is_object(p1.resultdef) and
  390. (oo_has_constructor in tobjectdef(p1.resultdef).objectoptions)) or
  391. is_open_array(p1.resultdef) or
  392. is_array_of_const(p1.resultdef) or
  393. is_open_string(p1.resultdef)
  394. )) or
  395. { keep the function call if it is a type parameter to avoid arithmetic errors due to constant folding }
  396. (p1.resultdef.typ=undefineddef) then
  397. begin
  398. statement_syssym:=geninlinenode(in_sizeof_x,false,p1);
  399. { no packed bit support for these things }
  400. if l=in_bitsizeof_x then
  401. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  402. end
  403. else
  404. begin
  405. { allow helpers for SizeOf and BitSizeOf }
  406. if p1.nodetype=typen then
  407. ttypenode(p1).helperallowed:=true;
  408. if (p1.resultdef.typ=forwarddef) then
  409. Message1(type_e_type_is_not_completly_defined,tforwarddef(p1.resultdef).tosymname^);
  410. if (l = in_sizeof_x) or
  411. (not((p1.nodetype = vecn) and
  412. is_packed_array(tvecnode(p1).left.resultdef)) and
  413. not((p1.nodetype = subscriptn) and
  414. is_packed_record_or_object(tsubscriptnode(p1).left.resultdef))) then
  415. begin
  416. statement_syssym:=cordconstnode.create(p1.resultdef.size,sinttype,true);
  417. if (l = in_bitsizeof_x) then
  418. statement_syssym:=caddnode.create(muln,statement_syssym,cordconstnode.create(8,sinttype,true));
  419. end
  420. else
  421. statement_syssym:=cordconstnode.create(p1.resultdef.packedbitsize,sinttype,true);
  422. { p1 not needed !}
  423. p1.destroy;
  424. end;
  425. end;
  426. in_typeinfo_x,
  427. in_objc_encode_x :
  428. begin
  429. if (l=in_typeinfo_x) or
  430. (m_objectivec1 in current_settings.modeswitches) then
  431. begin
  432. consume(_LKLAMMER);
  433. in_args:=true;
  434. p1:=comp_expr(true,false);
  435. { When reading a class type it is parsed as loadvmtaddrn,
  436. typeinfo only needs the type so we remove the loadvmtaddrn }
  437. if p1.nodetype=loadvmtaddrn then
  438. begin
  439. p2:=tloadvmtaddrnode(p1).left;
  440. tloadvmtaddrnode(p1).left:=nil;
  441. p1.free;
  442. p1:=p2;
  443. end;
  444. if p1.nodetype=typen then
  445. begin
  446. ttypenode(p1).allowed:=true;
  447. { allow helpers for TypeInfo }
  448. if l=in_typeinfo_x then
  449. ttypenode(p1).helperallowed:=true;
  450. end;
  451. { else
  452. begin
  453. p1.destroy;
  454. p1:=cerrornode.create;
  455. Message(parser_e_illegal_parameter_list);
  456. end;}
  457. consume(_RKLAMMER);
  458. p2:=geninlinenode(l,false,p1);
  459. statement_syssym:=p2;
  460. end
  461. else
  462. begin
  463. Message1(sym_e_id_not_found, orgpattern);
  464. statement_syssym:=cerrornode.create;
  465. end;
  466. end;
  467. in_aligned_x,
  468. in_unaligned_x :
  469. begin
  470. err:=false;
  471. consume(_LKLAMMER);
  472. in_args:=true;
  473. p1:=comp_expr(true,false);
  474. p2:=ccallparanode.create(p1,nil);
  475. p2:=geninlinenode(l,false,p2);
  476. consume(_RKLAMMER);
  477. statement_syssym:=p2;
  478. end;
  479. in_assigned_x :
  480. begin
  481. err:=false;
  482. consume(_LKLAMMER);
  483. in_args:=true;
  484. p1:=comp_expr(true,false);
  485. { When reading a class type it is parsed as loadvmtaddrn,
  486. typeinfo only needs the type so we remove the loadvmtaddrn }
  487. if p1.nodetype=loadvmtaddrn then
  488. begin
  489. p2:=tloadvmtaddrnode(p1).left;
  490. tloadvmtaddrnode(p1).left:=nil;
  491. p1.free;
  492. p1:=p2;
  493. end;
  494. if not codegenerror then
  495. begin
  496. case p1.resultdef.typ of
  497. procdef, { procvar }
  498. pointerdef,
  499. procvardef,
  500. classrefdef : ;
  501. objectdef :
  502. if not is_implicit_pointer_object_type(p1.resultdef) then
  503. begin
  504. Message(parser_e_illegal_parameter_list);
  505. err:=true;
  506. end;
  507. arraydef :
  508. if not is_dynamic_array(p1.resultdef) then
  509. begin
  510. Message(parser_e_illegal_parameter_list);
  511. err:=true;
  512. end;
  513. else
  514. if p1.resultdef.typ<>undefineddef then
  515. begin
  516. Message(parser_e_illegal_parameter_list);
  517. err:=true;
  518. end;
  519. end;
  520. end
  521. else
  522. err:=true;
  523. if not err then
  524. begin
  525. p2:=ccallparanode.create(p1,nil);
  526. p2:=geninlinenode(in_assigned_x,false,p2);
  527. end
  528. else
  529. begin
  530. p1.free;
  531. p2:=cerrornode.create;
  532. end;
  533. consume(_RKLAMMER);
  534. statement_syssym:=p2;
  535. end;
  536. in_addr_x :
  537. begin
  538. consume(_LKLAMMER);
  539. got_addrn:=true;
  540. p1:=factor(true,false,false);
  541. { inside parentheses a full expression is allowed, see also tests\webtbs\tb27517.pp }
  542. if token<>_RKLAMMER then
  543. p1:=sub_expr(opcompare,true,false,p1);
  544. p1:=caddrnode.create(p1);
  545. got_addrn:=false;
  546. consume(_RKLAMMER);
  547. statement_syssym:=p1;
  548. end;
  549. in_ofs_x :
  550. begin
  551. if target_info.system in systems_managed_vm then
  552. message(parser_e_feature_unsupported_for_vm);
  553. consume(_LKLAMMER);
  554. got_addrn:=true;
  555. p1:=factor(true,false,false);
  556. { inside parentheses a full expression is allowed, see also tests\webtbs\tb27517.pp }
  557. if token<>_RKLAMMER then
  558. p1:=sub_expr(opcompare,true,false,p1);
  559. p1:=caddrnode.create(p1);
  560. got_addrn:=false;
  561. { Ofs() returns a cardinal/qword, not a pointer }
  562. inserttypeconv_internal(p1,uinttype);
  563. consume(_RKLAMMER);
  564. statement_syssym:=p1;
  565. end;
  566. in_seg_x :
  567. begin
  568. consume(_LKLAMMER);
  569. in_args:=true;
  570. p1:=comp_expr(true,false);
  571. p1:=geninlinenode(in_seg_x,false,p1);
  572. consume(_RKLAMMER);
  573. statement_syssym:=p1;
  574. end;
  575. in_high_x,
  576. in_low_x :
  577. begin
  578. consume(_LKLAMMER);
  579. in_args:=true;
  580. p1:=comp_expr(true,false);
  581. p2:=geninlinenode(l,false,p1);
  582. consume(_RKLAMMER);
  583. statement_syssym:=p2;
  584. end;
  585. in_succ_x,
  586. in_pred_x :
  587. begin
  588. consume(_LKLAMMER);
  589. in_args:=true;
  590. p1:=comp_expr(true,false);
  591. p2:=geninlinenode(l,false,p1);
  592. consume(_RKLAMMER);
  593. statement_syssym:=p2;
  594. end;
  595. in_inc_x,
  596. in_dec_x :
  597. begin
  598. consume(_LKLAMMER);
  599. in_args:=true;
  600. p1:=comp_expr(true,false);
  601. if try_to_consume(_COMMA) then
  602. p2:=ccallparanode.create(comp_expr(true,false),nil)
  603. else
  604. p2:=nil;
  605. p2:=ccallparanode.create(p1,p2);
  606. statement_syssym:=geninlinenode(l,false,p2);
  607. consume(_RKLAMMER);
  608. end;
  609. in_slice_x:
  610. begin
  611. if not(in_args) then
  612. begin
  613. message(parser_e_illegal_slice);
  614. consume(_LKLAMMER);
  615. in_args:=true;
  616. comp_expr(true,false).free;
  617. if try_to_consume(_COMMA) then
  618. comp_expr(true,false).free;
  619. statement_syssym:=cerrornode.create;
  620. consume(_RKLAMMER);
  621. end
  622. else
  623. begin
  624. consume(_LKLAMMER);
  625. in_args:=true;
  626. p1:=comp_expr(true,false);
  627. Consume(_COMMA);
  628. if not(codegenerror) then
  629. p2:=ccallparanode.create(comp_expr(true,false),nil)
  630. else
  631. p2:=cerrornode.create;
  632. p2:=ccallparanode.create(p1,p2);
  633. statement_syssym:=geninlinenode(l,false,p2);
  634. consume(_RKLAMMER);
  635. end;
  636. end;
  637. in_initialize_x:
  638. begin
  639. statement_syssym:=inline_initialize;
  640. end;
  641. in_finalize_x:
  642. begin
  643. statement_syssym:=inline_finalize;
  644. end;
  645. in_copy_x:
  646. begin
  647. statement_syssym:=inline_copy;
  648. end;
  649. in_concat_x :
  650. begin
  651. consume(_LKLAMMER);
  652. in_args:=true;
  653. { Translate to x:=x+y[+z]. The addnode will do the
  654. type checking }
  655. p2:=nil;
  656. repeat
  657. p1:=comp_expr(true,false);
  658. if p2<>nil then
  659. p2:=caddnode.create(addn,p2,p1)
  660. else
  661. begin
  662. { Force string type if it isn't yet }
  663. if not(
  664. (p1.resultdef.typ=stringdef) or
  665. is_chararray(p1.resultdef) or
  666. is_char(p1.resultdef)
  667. ) then
  668. inserttypeconv(p1,cshortstringtype);
  669. p2:=p1;
  670. end;
  671. until not try_to_consume(_COMMA);
  672. consume(_RKLAMMER);
  673. statement_syssym:=p2;
  674. end;
  675. in_read_x,
  676. in_readln_x,
  677. in_readstr_x:
  678. begin
  679. if try_to_consume(_LKLAMMER) then
  680. begin
  681. paras:=parse_paras(false,false,_RKLAMMER);
  682. consume(_RKLAMMER);
  683. end
  684. else
  685. paras:=nil;
  686. p1:=geninlinenode(l,false,paras);
  687. statement_syssym := p1;
  688. end;
  689. in_setlength_x:
  690. begin
  691. statement_syssym := inline_setlength;
  692. end;
  693. in_objc_selector_x:
  694. begin
  695. if (m_objectivec1 in current_settings.modeswitches) then
  696. begin
  697. consume(_LKLAMMER);
  698. in_args:=true;
  699. { don't turn procsyms into calls (getaddr = true) }
  700. p1:=factor(true,false,false);
  701. p2:=geninlinenode(l,false,p1);
  702. consume(_RKLAMMER);
  703. statement_syssym:=p2;
  704. end
  705. else
  706. begin
  707. Message1(sym_e_id_not_found, orgpattern);
  708. statement_syssym:=cerrornode.create;
  709. end;
  710. end;
  711. in_length_x:
  712. begin
  713. consume(_LKLAMMER);
  714. in_args:=true;
  715. p1:=comp_expr(true,false);
  716. p2:=geninlinenode(l,false,p1);
  717. consume(_RKLAMMER);
  718. statement_syssym:=p2;
  719. end;
  720. in_write_x,
  721. in_writeln_x,
  722. in_writestr_x :
  723. begin
  724. if try_to_consume(_LKLAMMER) then
  725. begin
  726. paras:=parse_paras(true,false,_RKLAMMER);
  727. consume(_RKLAMMER);
  728. end
  729. else
  730. paras:=nil;
  731. p1 := geninlinenode(l,false,paras);
  732. statement_syssym := p1;
  733. end;
  734. in_str_x_string :
  735. begin
  736. consume(_LKLAMMER);
  737. paras:=parse_paras(true,false,_RKLAMMER);
  738. consume(_RKLAMMER);
  739. p1 := geninlinenode(l,false,paras);
  740. statement_syssym := p1;
  741. end;
  742. in_val_x:
  743. Begin
  744. consume(_LKLAMMER);
  745. in_args := true;
  746. p1:= ccallparanode.create(comp_expr(true,false), nil);
  747. consume(_COMMA);
  748. p2 := ccallparanode.create(comp_expr(true,false),p1);
  749. if try_to_consume(_COMMA) then
  750. p2 := ccallparanode.create(comp_expr(true,false),p2);
  751. consume(_RKLAMMER);
  752. p2 := geninlinenode(l,false,p2);
  753. statement_syssym := p2;
  754. End;
  755. in_include_x_y,
  756. in_exclude_x_y :
  757. begin
  758. consume(_LKLAMMER);
  759. in_args:=true;
  760. p1:=comp_expr(true,false);
  761. consume(_COMMA);
  762. p2:=comp_expr(true,false);
  763. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  764. consume(_RKLAMMER);
  765. end;
  766. in_pack_x_y_z,
  767. in_unpack_x_y_z :
  768. begin
  769. consume(_LKLAMMER);
  770. in_args:=true;
  771. p1:=comp_expr(true,false);
  772. consume(_COMMA);
  773. p2:=comp_expr(true,false);
  774. consume(_COMMA);
  775. paras:=comp_expr(true,false);
  776. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,ccallparanode.create(paras,nil))));
  777. consume(_RKLAMMER);
  778. end;
  779. in_assert_x_y :
  780. begin
  781. consume(_LKLAMMER);
  782. in_args:=true;
  783. p1:=comp_expr(true,false);
  784. if try_to_consume(_COMMA) then
  785. p2:=comp_expr(true,false)
  786. else
  787. begin
  788. { then insert an empty string }
  789. p2:=cstringconstnode.createstr('');
  790. end;
  791. statement_syssym:=geninlinenode(l,false,ccallparanode.create(p1,ccallparanode.create(p2,nil)));
  792. consume(_RKLAMMER);
  793. end;
  794. in_get_frame:
  795. begin
  796. statement_syssym:=geninlinenode(l,false,nil);
  797. end;
  798. (*
  799. in_get_caller_frame:
  800. begin
  801. if try_to_consume(_LKLAMMER) then
  802. begin
  803. {You used to call get_caller_frame as get_caller_frame(get_frame),
  804. however, as a stack frame may not exist, it does more harm than
  805. good, so ignore it.}
  806. in_args:=true;
  807. p1:=comp_expr(true,false);
  808. p1.destroy;
  809. consume(_RKLAMMER);
  810. end;
  811. statement_syssym:=geninlinenode(l,false,nil);
  812. end;
  813. *)
  814. in_default_x:
  815. begin
  816. consume(_LKLAMMER);
  817. in_args:=true;
  818. def:=nil;
  819. single_type(def,[stoAllowSpecialization]);
  820. statement_syssym:=cerrornode.create;
  821. if def<>generrordef then
  822. { "type expected" error is already done by single_type }
  823. if def.typ=forwarddef then
  824. Message1(type_e_type_is_not_completly_defined,tforwarddef(def).tosymname^)
  825. else
  826. begin
  827. statement_syssym.free;
  828. statement_syssym:=geninlinenode(in_default_x,false,ctypenode.create(def));
  829. end;
  830. { consume the right bracket here for a nicer error position }
  831. consume(_RKLAMMER);
  832. end;
  833. in_setstring_x_y_z:
  834. begin
  835. statement_syssym := inline_setstring;
  836. end;
  837. else
  838. internalerror(15);
  839. end;
  840. in_args:=prev_in_args;
  841. end;
  842. function maybe_load_methodpointer(st:TSymtable;var p1:tnode):boolean;
  843. var
  844. pd: tprocdef;
  845. begin
  846. maybe_load_methodpointer:=false;
  847. if not assigned(p1) then
  848. begin
  849. case st.symtabletype of
  850. withsymtable :
  851. begin
  852. if (st.defowner.typ=objectdef) then
  853. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  854. end;
  855. ObjectSymtable,
  856. recordsymtable:
  857. begin
  858. { Escape nested procedures }
  859. if assigned(current_procinfo) then
  860. begin
  861. pd:=current_procinfo.get_normal_proc.procdef;
  862. { We are calling from the static class method which has no self node }
  863. if assigned(pd) and pd.no_self_node then
  864. if st.symtabletype=recordsymtable then
  865. p1:=ctypenode.create(pd.struct)
  866. else
  867. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  868. else
  869. p1:=load_self_node;
  870. end
  871. else
  872. p1:=load_self_node;
  873. { We are calling a member }
  874. maybe_load_methodpointer:=true;
  875. end;
  876. end;
  877. end;
  878. end;
  879. { reads the parameter for a subroutine call }
  880. procedure do_proc_call(sym:tsym;st:TSymtable;obj:tabstractrecorddef;getaddr:boolean;var again : boolean;var p1:tnode;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  881. var
  882. membercall,
  883. prevafterassn : boolean;
  884. i : integer;
  885. para,p2 : tnode;
  886. currpara : tparavarsym;
  887. aprocdef : tprocdef;
  888. begin
  889. prevafterassn:=afterassignment;
  890. afterassignment:=false;
  891. membercall:=false;
  892. aprocdef:=nil;
  893. { when it is a call to a member we need to load the
  894. methodpointer first
  895. }
  896. membercall:=maybe_load_methodpointer(st,p1);
  897. { When we are expecting a procvar we also need
  898. to get the address in some cases }
  899. if assigned(getprocvardef) then
  900. begin
  901. if (block_type=bt_const) or
  902. getaddr then
  903. begin
  904. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  905. getaddr:=true;
  906. end
  907. else
  908. if ((m_tp_procvar in current_settings.modeswitches) or
  909. (m_mac_procvar in current_settings.modeswitches)) and
  910. not(token in [_CARET,_POINT,_LKLAMMER]) then
  911. begin
  912. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  913. if assigned(aprocdef) then
  914. getaddr:=true;
  915. end;
  916. end;
  917. { only need to get the address of the procedure? }
  918. if getaddr then
  919. begin
  920. { for now we don't support pointers to generic functions, but since
  921. this is only temporary we use a non translated message }
  922. if assigned(spezcontext) then
  923. begin
  924. comment(v_error, 'Pointers to generics functions not implemented');
  925. p1:=cerrornode.create;
  926. spezcontext.free;
  927. exit;
  928. end;
  929. { Retrieve info which procvar to call. For tp_procvar the
  930. aprocdef is already loaded above so we can reuse it }
  931. if not assigned(aprocdef) and
  932. assigned(getprocvardef) then
  933. aprocdef:=Tprocsym(sym).Find_procdef_byprocvardef(getprocvardef);
  934. { generate a methodcallnode or proccallnode }
  935. { we shouldn't convert things like @tcollection.load }
  936. p2:=cloadnode.create_procvar(sym,aprocdef,st);
  937. if assigned(p1) then
  938. begin
  939. { for loading methodpointer of an inherited function
  940. we use self as instance and load the address of
  941. the function directly and not through the vmt (PFV) }
  942. if (cnf_inherited in callflags) then
  943. begin
  944. include(tloadnode(p2).loadnodeflags,loadnf_inherited);
  945. p1.free;
  946. p1:=load_self_node;
  947. end;
  948. if (p1.nodetype<>typen) then
  949. tloadnode(p2).set_mp(p1)
  950. else
  951. p1.free;
  952. end;
  953. p1:=p2;
  954. { no postfix operators }
  955. again:=false;
  956. end
  957. else
  958. begin
  959. para:=nil;
  960. if anon_inherited then
  961. begin
  962. if not assigned(current_procinfo) then
  963. internalerror(200305054);
  964. for i:=0 to current_procinfo.procdef.paras.count-1 do
  965. begin
  966. currpara:=tparavarsym(current_procinfo.procdef.paras[i]);
  967. if not(vo_is_hidden_para in currpara.varoptions) then
  968. begin
  969. { inheritance by msgint? }
  970. if assigned(srdef) then
  971. { anonymous inherited via msgid calls only require a var parameter for
  972. both methods, so we need some type casting here }
  973. para:=ccallparanode.create(ctypeconvnode.create_internal(ctypeconvnode.create_internal(
  974. cloadnode.create(currpara,currpara.owner),cformaltype),tparavarsym(tprocdef(srdef).paras[i]).vardef),
  975. para)
  976. else
  977. para:=ccallparanode.create(cloadnode.create(currpara,currpara.owner),para);
  978. end;
  979. end;
  980. end
  981. else
  982. begin
  983. if try_to_consume(_LKLAMMER) then
  984. begin
  985. para:=parse_paras(false,false,_RKLAMMER);
  986. consume(_RKLAMMER);
  987. end;
  988. end;
  989. { indicate if this call was generated by a member and
  990. no explicit self is used, this is needed to determine
  991. how to handle a destructor call (PFV) }
  992. if membercall then
  993. include(callflags,cnf_member_call);
  994. if assigned(obj) then
  995. begin
  996. if not (st.symtabletype in [ObjectSymtable,recordsymtable]) then
  997. internalerror(200310031);
  998. p1:=ccallnode.create(para,tprocsym(sym),obj.symtable,p1,callflags,spezcontext);
  999. end
  1000. else
  1001. p1:=ccallnode.create(para,tprocsym(sym),st,p1,callflags,spezcontext);
  1002. end;
  1003. afterassignment:=prevafterassn;
  1004. end;
  1005. procedure handle_procvar(pv : tprocvardef;var p2 : tnode);
  1006. var
  1007. hp,hp2 : tnode;
  1008. hpp : ^tnode;
  1009. currprocdef : tprocdef;
  1010. begin
  1011. if not assigned(pv) then
  1012. internalerror(200301121);
  1013. if (m_tp_procvar in current_settings.modeswitches) or
  1014. (m_mac_procvar in current_settings.modeswitches) then
  1015. begin
  1016. hp:=p2;
  1017. hpp:=@p2;
  1018. while assigned(hp) and
  1019. (hp.nodetype=typeconvn) do
  1020. begin
  1021. hp:=ttypeconvnode(hp).left;
  1022. { save orignal address of the old tree so we can replace the node }
  1023. hpp:=@hp;
  1024. end;
  1025. if (hp.nodetype=calln) and
  1026. { a procvar can't have parameters! }
  1027. not assigned(tcallnode(hp).left) then
  1028. begin
  1029. currprocdef:=tcallnode(hp).symtableprocentry.Find_procdef_byprocvardef(pv);
  1030. if assigned(currprocdef) then
  1031. begin
  1032. hp2:=cloadnode.create_procvar(tprocsym(tcallnode(hp).symtableprocentry),currprocdef,tcallnode(hp).symtableproc);
  1033. if (po_methodpointer in pv.procoptions) then
  1034. tloadnode(hp2).set_mp(tcallnode(hp).methodpointer.getcopy);
  1035. hp.destroy;
  1036. { replace the old callnode with the new loadnode }
  1037. hpp^:=hp2;
  1038. end;
  1039. end;
  1040. end;
  1041. end;
  1042. { the following procedure handles the access to a property symbol }
  1043. procedure handle_propertysym(propsym : tpropertysym;st : TSymtable;var p1 : tnode);
  1044. var
  1045. paras : tnode;
  1046. p2 : tnode;
  1047. membercall : boolean;
  1048. callflags : tcallnodeflags;
  1049. propaccesslist : tpropaccesslist;
  1050. sym: tsym;
  1051. begin
  1052. { property parameters? read them only if the property really }
  1053. { has parameters }
  1054. paras:=nil;
  1055. if (ppo_hasparameters in propsym.propoptions) then
  1056. begin
  1057. if try_to_consume(_LECKKLAMMER) then
  1058. begin
  1059. paras:=parse_paras(false,false,_RECKKLAMMER);
  1060. consume(_RECKKLAMMER);
  1061. end;
  1062. end;
  1063. { indexed property }
  1064. if (ppo_indexed in propsym.propoptions) then
  1065. begin
  1066. p2:=cordconstnode.create(propsym.index,propsym.indexdef,true);
  1067. paras:=ccallparanode.create(p2,paras);
  1068. end;
  1069. { we need only a write property if a := follows }
  1070. { if not(afterassignment) and not(in_args) then }
  1071. if token=_ASSIGNMENT then
  1072. begin
  1073. if propsym.getpropaccesslist(palt_write,propaccesslist) then
  1074. begin
  1075. sym:=propaccesslist.firstsym^.sym;
  1076. case sym.typ of
  1077. procsym :
  1078. begin
  1079. callflags:=[];
  1080. { generate the method call }
  1081. membercall:=maybe_load_methodpointer(st,p1);
  1082. if membercall then
  1083. include(callflags,cnf_member_call);
  1084. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags,nil);
  1085. addsymref(sym);
  1086. paras:=nil;
  1087. consume(_ASSIGNMENT);
  1088. { read the expression }
  1089. if propsym.propdef.typ=procvardef then
  1090. getprocvardef:=tprocvardef(propsym.propdef);
  1091. p2:=comp_expr(true,false);
  1092. if assigned(getprocvardef) then
  1093. handle_procvar(getprocvardef,p2);
  1094. tcallnode(p1).left:=ccallparanode.create(p2,tcallnode(p1).left);
  1095. { mark as property, both the tcallnode and the real call block }
  1096. include(p1.flags,nf_isproperty);
  1097. getprocvardef:=nil;
  1098. end;
  1099. fieldvarsym :
  1100. begin
  1101. { generate access code }
  1102. if not handle_staticfield_access(sym,false,p1) then
  1103. propaccesslist_to_node(p1,st,propaccesslist);
  1104. include(p1.flags,nf_isproperty);
  1105. consume(_ASSIGNMENT);
  1106. { read the expression }
  1107. p2:=comp_expr(true,false);
  1108. p1:=cassignmentnode.create(p1,p2);
  1109. end
  1110. else
  1111. begin
  1112. p1:=cerrornode.create;
  1113. Message(parser_e_no_procedure_to_access_property);
  1114. end;
  1115. end;
  1116. end
  1117. else
  1118. begin
  1119. p1:=cerrornode.create;
  1120. Message(parser_e_no_procedure_to_access_property);
  1121. end;
  1122. end
  1123. else
  1124. begin
  1125. if propsym.getpropaccesslist(palt_read,propaccesslist) then
  1126. begin
  1127. sym := propaccesslist.firstsym^.sym;
  1128. case sym.typ of
  1129. fieldvarsym :
  1130. begin
  1131. { generate access code }
  1132. if not handle_staticfield_access(sym,false,p1) then
  1133. propaccesslist_to_node(p1,st,propaccesslist);
  1134. include(p1.flags,nf_isproperty);
  1135. { catch expressions like "(propx):=1;" }
  1136. include(p1.flags,nf_no_lvalue);
  1137. end;
  1138. procsym :
  1139. begin
  1140. callflags:=[];
  1141. { generate the method call }
  1142. membercall:=maybe_load_methodpointer(st,p1);
  1143. if membercall then
  1144. include(callflags,cnf_member_call);
  1145. p1:=ccallnode.create(paras,tprocsym(sym),st,p1,callflags,nil);
  1146. paras:=nil;
  1147. include(p1.flags,nf_isproperty);
  1148. include(p1.flags,nf_no_lvalue);
  1149. end
  1150. else
  1151. begin
  1152. p1:=cerrornode.create;
  1153. Message(type_e_mismatch);
  1154. end;
  1155. end;
  1156. end
  1157. else
  1158. begin
  1159. { error, no function to read property }
  1160. p1:=cerrornode.create;
  1161. Message(parser_e_no_procedure_to_access_property);
  1162. end;
  1163. end;
  1164. { release paras if not used }
  1165. if assigned(paras) then
  1166. paras.free;
  1167. end;
  1168. { the ID token has to be consumed before calling this function }
  1169. procedure do_member_read(structh:tabstractrecorddef;getaddr:boolean;sym:tsym;var p1:tnode;var again:boolean;callflags:tcallnodeflags;spezcontext:tspecializationcontext);
  1170. var
  1171. isclassref:boolean;
  1172. begin
  1173. if sym=nil then
  1174. begin
  1175. { pattern is still valid unless
  1176. there is another ID just after the ID of sym }
  1177. Message1(sym_e_id_no_member,orgpattern);
  1178. p1.free;
  1179. p1:=cerrornode.create;
  1180. { try to clean up }
  1181. spezcontext.free;
  1182. again:=false;
  1183. end
  1184. else
  1185. begin
  1186. if assigned(p1) then
  1187. begin
  1188. if not assigned(p1.resultdef) then
  1189. do_typecheckpass(p1);
  1190. isclassref:=(p1.resultdef.typ=classrefdef);
  1191. end
  1192. else
  1193. isclassref:=false;
  1194. if assigned(spezcontext) and not (sym.typ=procsym) then
  1195. internalerror(2015091801);
  1196. { we assume, that only procsyms and varsyms are in an object }
  1197. { symbol table, for classes, properties are allowed }
  1198. case sym.typ of
  1199. procsym:
  1200. begin
  1201. do_proc_call(sym,sym.owner,structh,
  1202. (getaddr and not(token in [_CARET,_POINT])),
  1203. again,p1,callflags,spezcontext);
  1204. { we need to know which procedure is called }
  1205. do_typecheckpass(p1);
  1206. { calling using classref? }
  1207. if isclassref and
  1208. (p1.nodetype=calln) and
  1209. assigned(tcallnode(p1).procdefinition) then
  1210. begin
  1211. if not(po_classmethod in tcallnode(p1).procdefinition.procoptions) and
  1212. not(tcallnode(p1).procdefinition.proctypeoption=potype_constructor) then
  1213. Message(parser_e_only_class_members_via_class_ref);
  1214. { in Java, constructors are not automatically inherited
  1215. -> calling a constructor from a parent type will create
  1216. an instance of that parent type! }
  1217. if is_javaclass(structh) and
  1218. (tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1219. (tcallnode(p1).procdefinition.owner.defowner<>find_real_class_definition(tobjectdef(structh),false)) then
  1220. Message(parser_e_java_no_inherited_constructor);
  1221. { Provide a warning if we try to create an instance of a
  1222. abstract class using the type name of that class. We
  1223. must not provide a warning if we use a "class of"
  1224. variable of that type though as we don't know the
  1225. type of the class }
  1226. if (tcallnode(p1).procdefinition.proctypeoption=potype_constructor) and
  1227. (oo_is_abstract in structh.objectoptions) and
  1228. assigned(tcallnode(p1).methodpointer) and
  1229. (tcallnode(p1).methodpointer.nodetype=loadvmtaddrn) then
  1230. Message1(type_w_instance_abstract_class,structh.RttiName);
  1231. end;
  1232. end;
  1233. fieldvarsym:
  1234. begin
  1235. if not handle_staticfield_access(sym,true,p1) then
  1236. begin
  1237. if isclassref then
  1238. if assigned(p1) and
  1239. (
  1240. is_self_node(p1) or
  1241. (assigned(current_procinfo) and (current_procinfo.get_normal_proc.procdef.no_self_node) and
  1242. (current_procinfo.procdef.struct=structh))) then
  1243. Message(parser_e_only_class_members)
  1244. else
  1245. Message(parser_e_only_class_members_via_class_ref);
  1246. p1:=csubscriptnode.create(sym,p1);
  1247. end;
  1248. end;
  1249. propertysym:
  1250. begin
  1251. if isclassref and not (sp_static in sym.symoptions) then
  1252. Message(parser_e_only_class_members_via_class_ref);
  1253. handle_propertysym(tpropertysym(sym),sym.owner,p1);
  1254. end;
  1255. typesym:
  1256. begin
  1257. p1.free;
  1258. if try_to_consume(_LKLAMMER) then
  1259. begin
  1260. p1:=comp_expr(true,false);
  1261. consume(_RKLAMMER);
  1262. p1:=ctypeconvnode.create_explicit(p1,ttypesym(sym).typedef);
  1263. end
  1264. else
  1265. begin
  1266. p1:=ctypenode.create(ttypesym(sym).typedef);
  1267. if (is_class(ttypesym(sym).typedef) or
  1268. is_objcclass(ttypesym(sym).typedef) or
  1269. is_javaclass(ttypesym(sym).typedef)) and
  1270. not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1271. p1:=cloadvmtaddrnode.create(p1);
  1272. end;
  1273. end;
  1274. constsym:
  1275. begin
  1276. p1.free;
  1277. p1:=genconstsymtree(tconstsym(sym));
  1278. end;
  1279. staticvarsym:
  1280. begin
  1281. { typed constant is a staticvarsym
  1282. now they are absolutevarsym }
  1283. p1.free;
  1284. p1:=cloadnode.create(sym,sym.Owner);
  1285. end;
  1286. absolutevarsym:
  1287. begin
  1288. p1.free;
  1289. p1:=nil;
  1290. { typed constants are absolutebarsyms now to handle storage properly }
  1291. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1292. end
  1293. else
  1294. internalerror(16);
  1295. end;
  1296. end;
  1297. end;
  1298. function handle_specialize_inline_specialization(var srsym:tsym;out srsymtable:tsymtable;out spezcontext:tspecializationcontext):boolean;
  1299. var
  1300. spezdef : tdef;
  1301. begin
  1302. result:=false;
  1303. spezcontext:=nil;
  1304. srsymtable:=nil;
  1305. if not assigned(srsym) then
  1306. message1(sym_e_id_no_member,orgpattern)
  1307. else
  1308. if not (srsym.typ in [typesym,procsym]) then
  1309. message(type_e_type_id_expected)
  1310. else
  1311. begin
  1312. if srsym.typ=typesym then
  1313. spezdef:=ttypesym(srsym).typedef
  1314. else
  1315. spezdef:=tdef(tprocsym(srsym).procdeflist[0]);
  1316. spezdef:=generate_specialization_phase1(spezcontext,spezdef);
  1317. case spezdef.typ of
  1318. errordef:
  1319. begin
  1320. spezcontext.free;
  1321. spezcontext:=nil;
  1322. srsym:=generrorsym;
  1323. end;
  1324. procdef:
  1325. begin
  1326. if block_type<>bt_body then
  1327. begin
  1328. message(parser_e_illegal_expression);
  1329. spezcontext.free;
  1330. spezcontext:=nil;
  1331. srsym:=generrorsym;
  1332. end
  1333. else
  1334. begin
  1335. srsym:=tprocdef(spezdef).procsym;
  1336. srsymtable:=srsym.owner;
  1337. result:=true;
  1338. end;
  1339. end;
  1340. objectdef,
  1341. recorddef,
  1342. arraydef,
  1343. procvardef:
  1344. begin
  1345. spezdef:=generate_specialization_phase2(spezcontext,tstoreddef(spezdef),false,'');
  1346. spezcontext.free;
  1347. spezcontext:=nil;
  1348. srsym:=spezdef.typesym;
  1349. srsymtable:=srsym.owner;
  1350. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1351. result:=true;
  1352. end;
  1353. else
  1354. internalerror(2015070302);
  1355. end;
  1356. end;
  1357. end;
  1358. function handle_factor_typenode(hdef:tdef;getaddr:boolean;var again:boolean;sym:tsym;typeonly:boolean):tnode;
  1359. var
  1360. srsym : tsym;
  1361. srsymtable : tsymtable;
  1362. isspecialize : boolean;
  1363. spezcontext : tspecializationcontext;
  1364. begin
  1365. spezcontext:=nil;
  1366. if sym=nil then
  1367. sym:=hdef.typesym;
  1368. { allow Ordinal(Value) for type declarations since it
  1369. can be an enummeration declaration or a set lke:
  1370. (OrdinalType(const1)..OrdinalType(const2) }
  1371. if (not typeonly or is_ordinal(hdef)) and
  1372. try_to_consume(_LKLAMMER) then
  1373. begin
  1374. result:=comp_expr(true,false);
  1375. consume(_RKLAMMER);
  1376. { type casts to class helpers aren't allowed }
  1377. if is_objectpascal_helper(hdef) then
  1378. Message(parser_e_no_category_as_types)
  1379. { recovery by not creating a conversion node }
  1380. else
  1381. result:=ctypeconvnode.create_explicit(result,hdef);
  1382. end
  1383. { not LKLAMMER }
  1384. else if (token=_POINT) and
  1385. (is_object(hdef) or is_record(hdef)) then
  1386. begin
  1387. consume(_POINT);
  1388. { handles calling methods declared in parent objects
  1389. using "parentobject.methodname()" }
  1390. if assigned(current_structdef) and
  1391. not(getaddr) and
  1392. def_is_related(current_structdef,hdef) then
  1393. begin
  1394. result:=ctypenode.create(hdef);
  1395. ttypenode(result).typesym:=sym;
  1396. if not (m_delphi in current_settings.modeswitches) and
  1397. (block_type in [bt_type,bt_var_type,bt_const_type]) and
  1398. (token=_ID) and
  1399. (idtoken=_SPECIALIZE) then
  1400. begin
  1401. consume(_ID);
  1402. if token<>_ID then
  1403. message(type_e_type_id_expected);
  1404. isspecialize:=true;
  1405. end
  1406. else
  1407. isspecialize:=false;
  1408. { search also in inherited methods }
  1409. searchsym_in_class(tobjectdef(hdef),tobjectdef(current_structdef),pattern,srsym,srsymtable,[ssf_search_helper]);
  1410. if isspecialize then
  1411. begin
  1412. consume(_ID);
  1413. if not handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  1414. begin
  1415. result.free;
  1416. result:=cerrornode.create;
  1417. end;
  1418. end
  1419. else
  1420. begin
  1421. if assigned(srsym) then
  1422. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1423. consume(_ID);
  1424. end;
  1425. if result.nodetype<>errorn then
  1426. do_member_read(tabstractrecorddef(hdef),false,srsym,result,again,[],spezcontext)
  1427. else
  1428. spezcontext.free;
  1429. end
  1430. else
  1431. begin
  1432. { handles:
  1433. * @TObject.Load
  1434. * static methods and variables }
  1435. result:=ctypenode.create(hdef);
  1436. ttypenode(result).typesym:=sym;
  1437. if not (m_delphi in current_settings.modeswitches) and
  1438. (block_type in [bt_type,bt_var_type,bt_const_type]) and
  1439. (token=_ID) and
  1440. (idtoken=_SPECIALIZE) then
  1441. begin
  1442. consume(_ID);
  1443. if token<>_ID then
  1444. message(type_e_type_id_expected);
  1445. isspecialize:=true;
  1446. end
  1447. else
  1448. isspecialize:=false;
  1449. { TP allows also @TMenu.Load if Load is only }
  1450. { defined in an anchestor class }
  1451. srsym:=search_struct_member(tabstractrecorddef(hdef),pattern);
  1452. if isspecialize then
  1453. begin
  1454. consume(_ID);
  1455. if not handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  1456. begin
  1457. result.free;
  1458. result:=cerrornode.create;
  1459. end;
  1460. end
  1461. else
  1462. begin
  1463. if assigned(srsym) then
  1464. begin
  1465. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1466. consume(_ID);
  1467. end
  1468. else
  1469. Message1(sym_e_id_no_member,orgpattern);
  1470. end;
  1471. if (result.nodetype<>errorn) and assigned(srsym) then
  1472. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],spezcontext)
  1473. else
  1474. spezcontext.free;
  1475. end;
  1476. end
  1477. else
  1478. begin
  1479. { Normally here would be the check against the usage
  1480. of "TClassHelper.Something", but as that might be
  1481. used inside of system symbols like sizeof and
  1482. typeinfo this check is put into ttypenode.pass_1
  1483. (for "TClassHelper" alone) and tcallnode.pass_1
  1484. (for "TClassHelper.Something") }
  1485. { class reference ? }
  1486. if is_class(hdef) or
  1487. is_objcclass(hdef) or
  1488. { Java interfaces also can have loadvmtaddrnodes,
  1489. e.g. for expressions such as JLClass(intftype) }
  1490. is_java_class_or_interface(hdef) then
  1491. begin
  1492. if getaddr and (token=_POINT) and
  1493. not is_javainterface(hdef) then
  1494. begin
  1495. consume(_POINT);
  1496. { allows @Object.Method }
  1497. { also allows static methods and variables }
  1498. result:=ctypenode.create(hdef);
  1499. ttypenode(result).typesym:=sym;
  1500. { TP allows also @TMenu.Load if Load is only }
  1501. { defined in an anchestor class }
  1502. srsym:=search_struct_member(tobjectdef(hdef),pattern);
  1503. if assigned(srsym) then
  1504. begin
  1505. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1506. consume(_ID);
  1507. { in case of @Object.Method1.Method2, we have to call
  1508. Method1 -> create a loadvmtaddr node as self instead of
  1509. a typen (the typenode would be changed to self of the
  1510. current method in case Method1 is a constructor, see
  1511. mantis #24844) }
  1512. if not(block_type in [bt_type,bt_const_type,bt_var_type]) and
  1513. (srsym.typ=procsym) and
  1514. (token in [_CARET,_POINT]) then
  1515. result:=cloadvmtaddrnode.create(result);
  1516. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,result,again,[],nil);
  1517. end
  1518. else
  1519. begin
  1520. Message1(sym_e_id_no_member,orgpattern);
  1521. consume(_ID);
  1522. end;
  1523. end
  1524. else
  1525. begin
  1526. result:=ctypenode.create(hdef);
  1527. ttypenode(result).typesym:=sym;
  1528. { For a type block we simply return only
  1529. the type. For all other blocks we return
  1530. a loadvmt node }
  1531. if not(block_type in [bt_type,bt_const_type,bt_var_type]) then
  1532. result:=cloadvmtaddrnode.create(result);
  1533. end;
  1534. end
  1535. else
  1536. begin
  1537. result:=ctypenode.create(hdef);
  1538. ttypenode(result).typesym:=sym;
  1539. end;
  1540. end;
  1541. end;
  1542. {****************************************************************************
  1543. Factor
  1544. ****************************************************************************}
  1545. function real_const_node_from_pattern(s:string):tnode;
  1546. var
  1547. d : bestreal;
  1548. code : integer;
  1549. cur : currency;
  1550. begin
  1551. val(s,d,code);
  1552. if code<>0 then
  1553. begin
  1554. Message(parser_e_error_in_real);
  1555. d:=1.0;
  1556. end;
  1557. if current_settings.fputype=fpu_none then
  1558. Message(parser_e_unsupported_real);
  1559. if (current_settings.minfpconstprec=s32real) and
  1560. (d = single(d)) then
  1561. result:=crealconstnode.create(d,s32floattype)
  1562. else if (current_settings.minfpconstprec=s64real) and
  1563. (d = double(d)) then
  1564. result:=crealconstnode.create(d,s64floattype)
  1565. else
  1566. result:=crealconstnode.create(d,pbestrealtype^);
  1567. val(pattern,cur,code);
  1568. if code=0 then
  1569. trealconstnode(result).value_currency:=cur;
  1570. end;
  1571. {---------------------------------------------
  1572. PostFixOperators
  1573. ---------------------------------------------}
  1574. { returns whether or not p1 has been changed }
  1575. function postfixoperators(var p1:tnode;var again:boolean;getaddr:boolean): boolean;
  1576. { tries to avoid syntax errors after invalid qualifiers }
  1577. procedure recoverconsume_postfixops;
  1578. begin
  1579. repeat
  1580. if not try_to_consume(_CARET) then
  1581. if try_to_consume(_POINT) then
  1582. try_to_consume(_ID)
  1583. else if try_to_consume(_LECKKLAMMER) then
  1584. begin
  1585. repeat
  1586. comp_expr(true,false);
  1587. until not try_to_consume(_COMMA);
  1588. consume(_RECKKLAMMER);
  1589. end
  1590. else if try_to_consume(_LKLAMMER) then
  1591. begin
  1592. repeat
  1593. comp_expr(true,false);
  1594. until not try_to_consume(_COMMA);
  1595. consume(_RKLAMMER);
  1596. end
  1597. else
  1598. break;
  1599. until false;
  1600. end;
  1601. procedure handle_variantarray;
  1602. var
  1603. p4 : tnode;
  1604. newstatement : tstatementnode;
  1605. tempresultvariant,
  1606. temp : ttempcreatenode;
  1607. paras : tcallparanode;
  1608. newblock : tnode;
  1609. countindices : aint;
  1610. begin
  1611. { create statements with call initialize the arguments and
  1612. call fpc_dynarr_setlength }
  1613. newblock:=internalstatements(newstatement);
  1614. { get temp for array of indicies,
  1615. we set the real size later }
  1616. temp:=ctempcreatenode.create(s32inttype,4,tt_persistent,false);
  1617. addstatement(newstatement,temp);
  1618. countindices:=0;
  1619. repeat
  1620. p4:=comp_expr(true,false);
  1621. addstatement(newstatement,cassignmentnode.create(
  1622. ctemprefnode.create_offset(temp,countindices*s32inttype.size),p4));
  1623. inc(countindices);
  1624. until not try_to_consume(_COMMA);
  1625. { set real size }
  1626. temp.size:=countindices*s32inttype.size;
  1627. consume(_RECKKLAMMER);
  1628. { we need only a write access if a := follows }
  1629. if token=_ASSIGNMENT then
  1630. begin
  1631. consume(_ASSIGNMENT);
  1632. p4:=comp_expr(true,false);
  1633. { create call to fpc_vararray_put }
  1634. paras:=ccallparanode.create(cordconstnode.create
  1635. (countindices,s32inttype,true),
  1636. ccallparanode.create(caddrnode.create_internal
  1637. (ctemprefnode.create(temp)),
  1638. ccallparanode.create(ctypeconvnode.create_internal(p4,cvarianttype),
  1639. ccallparanode.create(ctypeconvnode.create_internal(p1,cvarianttype)
  1640. ,nil))));
  1641. addstatement(newstatement,ccallnode.createintern('fpc_vararray_put',paras));
  1642. addstatement(newstatement,ctempdeletenode.create(temp));
  1643. end
  1644. else
  1645. begin
  1646. { create temp for result }
  1647. tempresultvariant:=ctempcreatenode.create(cvarianttype,cvarianttype.size,tt_persistent,true);
  1648. addstatement(newstatement,tempresultvariant);
  1649. { create call to fpc_vararray_get }
  1650. paras:=ccallparanode.create(cordconstnode.create
  1651. (countindices,s32inttype,true),
  1652. ccallparanode.create(caddrnode.create_internal
  1653. (ctemprefnode.create(temp)),
  1654. ccallparanode.create(p1,
  1655. ccallparanode.create(
  1656. ctemprefnode.create(tempresultvariant)
  1657. ,nil))));
  1658. addstatement(newstatement,ccallnode.createintern('fpc_vararray_get',paras));
  1659. addstatement(newstatement,ctempdeletenode.create(temp));
  1660. { the last statement should return the value as
  1661. location and type, this is done be referencing the
  1662. temp and converting it first from a persistent temp to
  1663. normal temp }
  1664. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempresultvariant));
  1665. addstatement(newstatement,ctemprefnode.create(tempresultvariant));
  1666. end;
  1667. p1:=newblock;
  1668. end;
  1669. function parse_array_constructor(arrdef:tarraydef): tnode;
  1670. var
  1671. newstatement,assstatement:tstatementnode;
  1672. arrnode:ttempcreatenode;
  1673. temp2:ttempcreatenode;
  1674. assnode:tnode;
  1675. paracount:integer;
  1676. begin
  1677. result:=internalstatements(newstatement);
  1678. { create temp for result }
  1679. arrnode:=ctempcreatenode.create(arrdef,arrdef.size,tt_persistent,true);
  1680. addstatement(newstatement,arrnode);
  1681. paracount:=0;
  1682. { check arguments and create an assignment calls }
  1683. if try_to_consume(_LKLAMMER) then
  1684. begin
  1685. assnode:=internalstatements(assstatement);
  1686. repeat
  1687. { arr[i] := param_i }
  1688. addstatement(assstatement,
  1689. cassignmentnode.create(
  1690. cvecnode.create(
  1691. ctemprefnode.create(arrnode),
  1692. cordconstnode.create(paracount,arrdef.rangedef,false)),
  1693. comp_expr(true,false)));
  1694. inc(paracount);
  1695. until not try_to_consume(_COMMA);
  1696. consume(_RKLAMMER);
  1697. end
  1698. else
  1699. assnode:=nil;
  1700. { get temp for array of lengths }
  1701. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1702. addstatement(newstatement,temp2);
  1703. { one dimensional }
  1704. addstatement(newstatement,cassignmentnode.create(
  1705. ctemprefnode.create_offset(temp2,0),
  1706. cordconstnode.create
  1707. (paracount,s32inttype,true)));
  1708. { create call to fpc_dynarr_setlength }
  1709. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1710. ccallparanode.create(caddrnode.create_internal
  1711. (ctemprefnode.create(temp2)),
  1712. ccallparanode.create(cordconstnode.create
  1713. (1,s32inttype,true),
  1714. ccallparanode.create(caddrnode.create_internal
  1715. (crttinode.create(tstoreddef(arrdef),initrtti,rdt_normal)),
  1716. ccallparanode.create(
  1717. ctypeconvnode.create_internal(
  1718. ctemprefnode.create(arrnode),voidpointertype),
  1719. nil))))
  1720. ));
  1721. { add assignment statememnts }
  1722. addstatement(newstatement,ctempdeletenode.create(temp2));
  1723. if assigned(assnode) then
  1724. addstatement(newstatement,assnode);
  1725. { the last statement should return the value as
  1726. location and type, this is done be referencing the
  1727. temp and converting it first from a persistent temp to
  1728. normal temp }
  1729. addstatement(newstatement,ctempdeletenode.create_normal_temp(arrnode));
  1730. addstatement(newstatement,ctemprefnode.create(arrnode));
  1731. end;
  1732. function try_type_helper(var node:tnode;def:tdef):boolean;
  1733. var
  1734. srsym : tsym;
  1735. srsymtable : tsymtable;
  1736. n : tnode;
  1737. newstatement : tstatementnode;
  1738. temp : ttempcreatenode;
  1739. extdef : tdef;
  1740. begin
  1741. result:=false;
  1742. if (token=_ID) and (block_type in [bt_body,bt_general,bt_except,bt_const]) then
  1743. begin
  1744. if not assigned(def) then
  1745. if node.nodetype=addrn then
  1746. { always use the pointer type for addr nodes as otherwise
  1747. we'll have an anonymous pointertype with no name }
  1748. def:=voidpointertype
  1749. else
  1750. def:=node.resultdef;
  1751. result:=search_objectpascal_helper(def,nil,pattern,srsym,srsymtable);
  1752. if result then
  1753. begin
  1754. if not (srsymtable.symtabletype=objectsymtable) or
  1755. not is_objectpascal_helper(tdef(srsymtable.defowner)) then
  1756. internalerror(2013011401);
  1757. { convert const node to temp node of the extended type }
  1758. if node.nodetype in (nodetype_const+[niln,addrn]) then
  1759. begin
  1760. extdef:=tobjectdef(srsymtable.defowner).extendeddef;
  1761. newstatement:=nil;
  1762. n:=internalstatements(newstatement);
  1763. temp:=ctempcreatenode.create(extdef,extdef.size,tt_persistent,false);
  1764. addstatement(newstatement,temp);
  1765. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(temp),node));
  1766. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1767. addstatement(newstatement,ctemprefnode.create(temp));
  1768. node:=n;
  1769. do_typecheckpass(node)
  1770. end;
  1771. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  1772. consume(_ID);
  1773. do_member_read(nil,getaddr,srsym,node,again,[],nil);
  1774. end;
  1775. end;
  1776. end;
  1777. var
  1778. protsym : tpropertysym;
  1779. p2,p3 : tnode;
  1780. srsym : tsym;
  1781. srsymtable : TSymtable;
  1782. structh : tabstractrecorddef;
  1783. { shouldn't be used that often, so the extra overhead is ok to save
  1784. stack space }
  1785. dispatchstring : ansistring;
  1786. erroroutp1,
  1787. allowspecialize,
  1788. isspecialize,
  1789. found,
  1790. haderror,
  1791. nodechanged : boolean;
  1792. calltype: tdispcalltype;
  1793. valstr,expstr : string;
  1794. intval : qword;
  1795. code : integer;
  1796. strdef : tdef;
  1797. spezcontext : tspecializationcontext;
  1798. old_current_filepos : tfileposinfo;
  1799. label
  1800. skipreckklammercheck,
  1801. skippointdefcheck;
  1802. begin
  1803. result:=false;
  1804. again:=true;
  1805. while again do
  1806. begin
  1807. spezcontext:=nil;
  1808. { we need the resultdef }
  1809. do_typecheckpass_changed(p1,nodechanged);
  1810. result:=result or nodechanged;
  1811. if codegenerror then
  1812. begin
  1813. recoverconsume_postfixops;
  1814. exit;
  1815. end;
  1816. { handle token }
  1817. case token of
  1818. _CARET:
  1819. begin
  1820. consume(_CARET);
  1821. { support tp/mac procvar^ if the procvar returns a
  1822. pointer type }
  1823. if ((m_tp_procvar in current_settings.modeswitches) or
  1824. (m_mac_procvar in current_settings.modeswitches)) and
  1825. (p1.resultdef.typ=procvardef) and
  1826. (tprocvardef(p1.resultdef).returndef.typ=pointerdef) then
  1827. begin
  1828. p1:=ccallnode.create_procvar(nil,p1);
  1829. typecheckpass(p1);
  1830. end;
  1831. { iso file buf access? }
  1832. if (m_iso in current_settings.modeswitches) and
  1833. (p1.resultdef.typ=filedef) then
  1834. begin
  1835. case tfiledef(p1.resultdef).filetyp of
  1836. ft_text:
  1837. begin
  1838. p1:=cderefnode.create(ccallnode.createintern('fpc_getbuf_text',ccallparanode.create(p1,nil)));
  1839. typecheckpass(p1);
  1840. end;
  1841. ft_typed:
  1842. begin
  1843. p1:=cderefnode.create(ctypeconvnode.create_internal(ccallnode.createintern('fpc_getbuf_typedfile',ccallparanode.create(p1,nil)),
  1844. cpointerdef.getreusable(tfiledef(p1.resultdef).typedfiledef)));
  1845. typecheckpass(p1);
  1846. end;
  1847. end;
  1848. end
  1849. else if (p1.resultdef.typ<>pointerdef) then
  1850. begin
  1851. { ^ as binary operator is a problem!!!! (FK) }
  1852. again:=false;
  1853. Message(parser_e_invalid_qualifier);
  1854. recoverconsume_postfixops;
  1855. p1.destroy;
  1856. p1:=cerrornode.create;
  1857. end
  1858. else
  1859. p1:=cderefnode.create(p1);
  1860. end;
  1861. _LECKKLAMMER:
  1862. begin
  1863. if is_class_or_interface_or_object(p1.resultdef) or
  1864. is_dispinterface(p1.resultdef) or
  1865. is_record(p1.resultdef) or
  1866. is_javaclass(p1.resultdef) then
  1867. begin
  1868. { default property }
  1869. protsym:=search_default_property(tabstractrecorddef(p1.resultdef));
  1870. if not(assigned(protsym)) then
  1871. begin
  1872. p1.destroy;
  1873. p1:=cerrornode.create;
  1874. again:=false;
  1875. message(parser_e_no_default_property_available);
  1876. end
  1877. else
  1878. begin
  1879. { The property symbol is referenced indirect }
  1880. protsym.IncRefCount;
  1881. handle_propertysym(protsym,protsym.owner,p1);
  1882. end;
  1883. end
  1884. else
  1885. begin
  1886. consume(_LECKKLAMMER);
  1887. repeat
  1888. { in all of the cases below, p1 is changed }
  1889. case p1.resultdef.typ of
  1890. pointerdef:
  1891. begin
  1892. { support delphi autoderef }
  1893. if (tpointerdef(p1.resultdef).pointeddef.typ=arraydef) and
  1894. (m_autoderef in current_settings.modeswitches) then
  1895. p1:=cderefnode.create(p1);
  1896. p2:=comp_expr(true,false);
  1897. { Support Pbytevar[0..9] which returns array [0..9].}
  1898. if try_to_consume(_POINTPOINT) then
  1899. p2:=crangenode.create(p2,comp_expr(true,false));
  1900. p1:=cvecnode.create(p1,p2);
  1901. end;
  1902. variantdef:
  1903. begin
  1904. handle_variantarray;
  1905. { the RECKKLAMMER is already read }
  1906. goto skipreckklammercheck;
  1907. end;
  1908. stringdef :
  1909. begin
  1910. p2:=comp_expr(true,false);
  1911. { Support string[0..9] which returns array [0..9] of char.}
  1912. if try_to_consume(_POINTPOINT) then
  1913. p2:=crangenode.create(p2,comp_expr(true,false));
  1914. p1:=cvecnode.create(p1,p2);
  1915. end;
  1916. arraydef:
  1917. begin
  1918. p2:=comp_expr(true,false);
  1919. { support SEG:OFS for go32v2/msdos Mem[] }
  1920. if (target_info.system in [system_i386_go32v2,system_i386_watcom,system_i8086_msdos,system_i8086_win16]) and
  1921. (p1.nodetype=loadn) and
  1922. assigned(tloadnode(p1).symtableentry) and
  1923. assigned(tloadnode(p1).symtableentry.owner.name) and
  1924. (tloadnode(p1).symtableentry.owner.name^='SYSTEM') and
  1925. ((tloadnode(p1).symtableentry.name='MEM') or
  1926. (tloadnode(p1).symtableentry.name='MEMW') or
  1927. (tloadnode(p1).symtableentry.name='MEML')) then
  1928. begin
  1929. {$if defined(i8086)}
  1930. consume(_COLON);
  1931. inserttypeconv(p2,u16inttype);
  1932. inserttypeconv_internal(p2,u32inttype);
  1933. p3:=cshlshrnode.create(shln,p2,cordconstnode.create($10,s16inttype,false));
  1934. p2:=comp_expr(true,false);
  1935. inserttypeconv(p2,u16inttype);
  1936. inserttypeconv_internal(p2,u32inttype);
  1937. p2:=caddnode.create(addn,p2,p3);
  1938. case tloadnode(p1).symtableentry.name of
  1939. 'MEM': p2:=ctypeconvnode.create_internal(p2,bytefarpointertype);
  1940. 'MEMW': p2:=ctypeconvnode.create_internal(p2,wordfarpointertype);
  1941. 'MEML': p2:=ctypeconvnode.create_internal(p2,longintfarpointertype);
  1942. else
  1943. internalerror(2013053102);
  1944. end;
  1945. p1:=cderefnode.create(p2);
  1946. {$elseif defined(i386)}
  1947. if try_to_consume(_COLON) then
  1948. begin
  1949. p3:=caddnode.create(muln,cordconstnode.create($10,s32inttype,false),p2);
  1950. p2:=comp_expr(true,false);
  1951. p2:=caddnode.create(addn,p2,p3);
  1952. if try_to_consume(_POINTPOINT) then
  1953. { Support mem[$a000:$0000..$07ff] which returns array [0..$7ff] of memtype.}
  1954. p2:=crangenode.create(p2,caddnode.create(addn,comp_expr(true,false),p3.getcopy));
  1955. p1:=cvecnode.create(p1,p2);
  1956. include(tvecnode(p1).flags,nf_memseg);
  1957. include(tvecnode(p1).flags,nf_memindex);
  1958. end
  1959. else
  1960. begin
  1961. if try_to_consume(_POINTPOINT) then
  1962. { Support mem[$80000000..$80000002] which returns array [0..2] of memtype.}
  1963. p2:=crangenode.create(p2,comp_expr(true,false));
  1964. p1:=cvecnode.create(p1,p2);
  1965. include(tvecnode(p1).flags,nf_memindex);
  1966. end;
  1967. {$else}
  1968. internalerror(2013053101);
  1969. {$endif}
  1970. end
  1971. else
  1972. begin
  1973. if try_to_consume(_POINTPOINT) then
  1974. { Support arrayvar[0..9] which returns array [0..9] of arraytype.}
  1975. p2:=crangenode.create(p2,comp_expr(true,false));
  1976. p1:=cvecnode.create(p1,p2);
  1977. end;
  1978. end;
  1979. else
  1980. begin
  1981. if p1.resultdef.typ<>undefineddef then
  1982. Message(parser_e_invalid_qualifier);
  1983. p1.destroy;
  1984. p1:=cerrornode.create;
  1985. comp_expr(true,false);
  1986. again:=false;
  1987. end;
  1988. end;
  1989. do_typecheckpass(p1);
  1990. until not try_to_consume(_COMMA);
  1991. consume(_RECKKLAMMER);
  1992. { handle_variantarray eats the RECKKLAMMER and jumps here }
  1993. skipreckklammercheck:
  1994. end;
  1995. end;
  1996. _POINT :
  1997. begin
  1998. consume(_POINT);
  1999. allowspecialize:=not (m_delphi in current_settings.modeswitches) and (block_type in [bt_type,bt_var_type,bt_const_type]);
  2000. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  2001. begin
  2002. //consume(_ID);
  2003. isspecialize:=true;
  2004. end
  2005. else
  2006. isspecialize:=false;
  2007. if (p1.resultdef.typ=pointerdef) and
  2008. (m_autoderef in current_settings.modeswitches) and
  2009. { don't auto-deref objc.id, because then the code
  2010. below for supporting id.anyobjcmethod isn't triggered }
  2011. (p1.resultdef<>objc_idtype) then
  2012. begin
  2013. p1:=cderefnode.create(p1);
  2014. do_typecheckpass(p1);
  2015. end;
  2016. { procvar.<something> can never mean anything so always
  2017. try to call it in case it returns a record/object/... }
  2018. maybe_call_procvar(p1,false);
  2019. if (p1.nodetype=ordconstn) and
  2020. not is_boolean(p1.resultdef) and
  2021. not is_enum(p1.resultdef) then
  2022. begin
  2023. { type helpers are checked first }
  2024. if (token=_ID) and try_type_helper(p1,nil) then
  2025. goto skippointdefcheck;
  2026. { only an "e" or "E" can follow an intconst with a ".", the
  2027. other case (another intconst) is handled by the scanner }
  2028. if (token=_ID) and (pattern[1]='E') then
  2029. begin
  2030. haderror:=false;
  2031. if length(pattern)>1 then
  2032. begin
  2033. expstr:=copy(pattern,2,length(pattern)-1);
  2034. val(expstr,intval,code);
  2035. if code<>0 then
  2036. begin
  2037. haderror:=true;
  2038. intval:=intval; // Hackfix the "var assigned but never used" note.
  2039. end;
  2040. end
  2041. else
  2042. expstr:='';
  2043. consume(token);
  2044. if tordconstnode(p1).value.signed then
  2045. str(tordconstnode(p1).value.svalue,valstr)
  2046. else
  2047. str(tordconstnode(p1).value.uvalue,valstr);
  2048. valstr:=valstr+'.0E';
  2049. if expstr='' then
  2050. case token of
  2051. _MINUS:
  2052. begin
  2053. consume(token);
  2054. if token=_INTCONST then
  2055. begin
  2056. valstr:=valstr+'-'+pattern;
  2057. consume(token);
  2058. end
  2059. else
  2060. haderror:=true;
  2061. end;
  2062. _PLUS:
  2063. begin
  2064. consume(token);
  2065. if token=_INTCONST then
  2066. begin
  2067. valstr:=valstr+pattern;
  2068. consume(token);
  2069. end
  2070. else
  2071. haderror:=true;
  2072. end;
  2073. _INTCONST:
  2074. begin
  2075. valstr:=valstr+pattern;
  2076. consume(_INTCONST);
  2077. end;
  2078. else
  2079. haderror:=true;
  2080. end
  2081. else
  2082. valstr:=valstr+expstr;
  2083. if haderror then
  2084. begin
  2085. Message(parser_e_error_in_real);
  2086. p2:=cerrornode.create;
  2087. end
  2088. else
  2089. p2:=real_const_node_from_pattern(valstr);
  2090. p1.free;
  2091. p1:=p2;
  2092. again:=false;
  2093. goto skippointdefcheck;
  2094. end
  2095. else
  2096. begin
  2097. { just convert the ordconst to a realconst }
  2098. p2:=crealconstnode.create(tordconstnode(p1).value,pbestrealtype^);
  2099. p1.free;
  2100. p1:=p2;
  2101. again:=false;
  2102. goto skippointdefcheck;
  2103. end;
  2104. end;
  2105. if (p1.nodetype=stringconstn) and (token=_ID) then
  2106. begin
  2107. { the def of a string const is an array }
  2108. case tstringconstnode(p1).cst_type of
  2109. cst_conststring:
  2110. if cs_refcountedstrings in current_settings.localswitches then
  2111. if m_default_unicodestring in current_settings.modeswitches then
  2112. strdef:=cunicodestringtype
  2113. else
  2114. strdef:=cansistringtype
  2115. else
  2116. strdef:=cshortstringtype;
  2117. cst_shortstring:
  2118. strdef:=cshortstringtype;
  2119. cst_ansistring:
  2120. { use getansistringdef? }
  2121. strdef:=cansistringtype;
  2122. cst_widestring:
  2123. strdef:=cwidestringtype;
  2124. cst_unicodestring:
  2125. strdef:=cunicodestringtype;
  2126. cst_longstring:
  2127. { let's see when someone stumbles upon this...}
  2128. internalerror(201301111);
  2129. else
  2130. internalerror(2013112903);
  2131. end;
  2132. if try_type_helper(p1,strdef) then
  2133. goto skippointdefcheck;
  2134. end;
  2135. { this is skipped if label skippointdefcheck is used }
  2136. case p1.resultdef.typ of
  2137. recorddef:
  2138. begin
  2139. if isspecialize or (token=_ID) then
  2140. begin
  2141. erroroutp1:=true;
  2142. srsym:=nil;
  2143. structh:=tabstractrecorddef(p1.resultdef);
  2144. if isspecialize then
  2145. begin
  2146. { consume the specialize }
  2147. consume(_ID);
  2148. if token<>_ID then
  2149. consume(_ID)
  2150. else
  2151. begin
  2152. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2153. consume(_ID);
  2154. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2155. erroroutp1:=false;
  2156. end;
  2157. end
  2158. else
  2159. begin
  2160. searchsym_in_record(structh,pattern,srsym,srsymtable);
  2161. if assigned(srsym) then
  2162. begin
  2163. old_current_filepos:=current_filepos;
  2164. consume(_ID);
  2165. if not (sp_generic_dummy in srsym.symoptions) or
  2166. not (token in [_LT,_LSHARPBRACKET]) then
  2167. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2168. else
  2169. p1:=cspecializenode.create(p1,getaddr,srsym);
  2170. erroroutp1:=false;
  2171. end
  2172. else
  2173. begin
  2174. Message1(sym_e_id_no_member,orgpattern);
  2175. { try to clean up }
  2176. consume(_ID);
  2177. end;
  2178. end;
  2179. if erroroutp1 then
  2180. begin
  2181. p1.free;
  2182. p1:=cerrornode.create;
  2183. end
  2184. else
  2185. if p1.nodetype<>specializen then
  2186. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2187. end
  2188. else
  2189. consume(_ID);
  2190. end;
  2191. enumdef:
  2192. begin
  2193. if token=_ID then
  2194. begin
  2195. srsym:=tsym(tenumdef(p1.resultdef).symtable.Find(pattern));
  2196. if assigned(srsym) and (srsym.typ=enumsym) and (p1.nodetype=typen) then
  2197. begin
  2198. p1.destroy;
  2199. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2200. p1:=genenumnode(tenumsym(srsym));
  2201. consume(_ID);
  2202. end
  2203. else
  2204. if not try_type_helper(p1,nil) then
  2205. begin
  2206. p1.destroy;
  2207. Message1(sym_e_id_no_member,orgpattern);
  2208. p1:=cerrornode.create;
  2209. consume(_ID);
  2210. end;
  2211. end;
  2212. end;
  2213. arraydef:
  2214. begin
  2215. if is_dynamic_array(p1.resultdef) then
  2216. begin
  2217. if (token=_ID) and not try_type_helper(p1,nil) then
  2218. begin
  2219. if pattern='CREATE' then
  2220. begin
  2221. consume(_ID);
  2222. p2:=parse_array_constructor(tarraydef(p1.resultdef));
  2223. p1.destroy;
  2224. p1:=p2;
  2225. end
  2226. else
  2227. begin
  2228. Message2(scan_f_syn_expected,'CREATE',pattern);
  2229. p1.destroy;
  2230. p1:=cerrornode.create;
  2231. consume(_ID);
  2232. end;
  2233. end;
  2234. end
  2235. else
  2236. begin
  2237. Message(parser_e_invalid_qualifier);
  2238. p1.destroy;
  2239. p1:=cerrornode.create;
  2240. consume(_ID);
  2241. end;
  2242. end;
  2243. variantdef:
  2244. begin
  2245. { dispatch call? }
  2246. { lhs := v.ident[parameters] -> property get
  2247. lhs := v.ident(parameters) -> method call
  2248. v.ident[parameters] := rhs -> property put
  2249. v.ident(parameters) := rhs -> also property put }
  2250. if token=_ID then
  2251. begin
  2252. if not try_type_helper(p1,nil) then
  2253. begin
  2254. dispatchstring:=orgpattern;
  2255. consume(_ID);
  2256. calltype:=dct_method;
  2257. if try_to_consume(_LKLAMMER) then
  2258. begin
  2259. p2:=parse_paras(false,true,_RKLAMMER);
  2260. consume(_RKLAMMER);
  2261. end
  2262. else if try_to_consume(_LECKKLAMMER) then
  2263. begin
  2264. p2:=parse_paras(false,true,_RECKKLAMMER);
  2265. consume(_RECKKLAMMER);
  2266. calltype:=dct_propget;
  2267. end
  2268. else
  2269. p2:=nil;
  2270. { property setter? }
  2271. if (token=_ASSIGNMENT) and not(afterassignment) then
  2272. begin
  2273. consume(_ASSIGNMENT);
  2274. { read the expression }
  2275. p3:=comp_expr(true,false);
  2276. { concat value parameter too }
  2277. p2:=ccallparanode.create(p3,p2);
  2278. p1:=translate_disp_call(p1,p2,dct_propput,dispatchstring,0,voidtype);
  2279. end
  2280. else
  2281. { this is only an approximation
  2282. setting useresult if not necessary is only a waste of time, no more, no less (FK) }
  2283. if afterassignment or in_args or (token<>_SEMICOLON) then
  2284. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,cvarianttype)
  2285. else
  2286. p1:=translate_disp_call(p1,p2,calltype,dispatchstring,0,voidtype);
  2287. end;
  2288. end
  2289. else { Error }
  2290. Consume(_ID);
  2291. end;
  2292. classrefdef:
  2293. begin
  2294. erroroutp1:=true;
  2295. if token=_ID then
  2296. begin
  2297. srsym:=nil;
  2298. structh:=tobjectdef(tclassrefdef(p1.resultdef).pointeddef);
  2299. if isspecialize then
  2300. begin
  2301. { consume the specialize }
  2302. consume(_ID);
  2303. if token<>_ID then
  2304. consume(_ID)
  2305. else
  2306. begin
  2307. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2308. consume(_ID);
  2309. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2310. erroroutp1:=false;
  2311. end;
  2312. end
  2313. else
  2314. begin
  2315. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2316. if assigned(srsym) then
  2317. begin
  2318. old_current_filepos:=current_filepos;
  2319. consume(_ID);
  2320. if not (sp_generic_dummy in srsym.symoptions) or
  2321. not (token in [_LT,_LSHARPBRACKET]) then
  2322. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2323. else
  2324. p1:=cspecializenode.create(p1,getaddr,srsym);
  2325. erroroutp1:=false;
  2326. end
  2327. else
  2328. begin
  2329. Message1(sym_e_id_no_member,orgpattern);
  2330. { try to clean up }
  2331. consume(_ID);
  2332. end;
  2333. end;
  2334. if erroroutp1 then
  2335. begin
  2336. p1.free;
  2337. p1:=cerrornode.create;
  2338. end
  2339. else
  2340. if p1.nodetype<>specializen then
  2341. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2342. end
  2343. else { Error }
  2344. Consume(_ID);
  2345. end;
  2346. objectdef:
  2347. begin
  2348. if isspecialize or (token=_ID) then
  2349. begin
  2350. erroroutp1:=true;
  2351. srsym:=nil;
  2352. structh:=tobjectdef(p1.resultdef);
  2353. if isspecialize then
  2354. begin
  2355. { consume the "specialize" }
  2356. consume(_ID);
  2357. if token<>_ID then
  2358. consume(_ID)
  2359. else
  2360. begin
  2361. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2362. consume(_ID);
  2363. if handle_specialize_inline_specialization(srsym,srsymtable,spezcontext) then
  2364. erroroutp1:=false;
  2365. end;
  2366. end
  2367. else
  2368. begin
  2369. searchsym_in_class(tobjectdef(structh),tobjectdef(structh),pattern,srsym,srsymtable,[ssf_search_helper]);
  2370. if assigned(srsym) then
  2371. begin
  2372. old_current_filepos:=current_filepos;
  2373. consume(_ID);
  2374. if not (sp_generic_dummy in srsym.symoptions) or
  2375. not (token in [_LT,_LSHARPBRACKET]) then
  2376. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg,old_current_filepos)
  2377. else
  2378. p1:=cspecializenode.create(p1,getaddr,srsym);
  2379. erroroutp1:=false;
  2380. end
  2381. else
  2382. begin
  2383. Message1(sym_e_id_no_member,orgpattern);
  2384. { try to clean up }
  2385. consume(_ID);
  2386. end;
  2387. end;
  2388. if erroroutp1 then
  2389. begin
  2390. p1.free;
  2391. p1:=cerrornode.create;
  2392. end
  2393. else
  2394. if p1.nodetype<>specializen then
  2395. do_member_read(structh,getaddr,srsym,p1,again,[],spezcontext);
  2396. end
  2397. else { Error }
  2398. Consume(_ID);
  2399. end;
  2400. pointerdef:
  2401. begin
  2402. if (p1.resultdef=objc_idtype) then
  2403. begin
  2404. { objc's id type can be used to call any
  2405. Objective-C method of any Objective-C class
  2406. type that's currently in scope }
  2407. if search_objc_method(pattern,srsym,srsymtable) then
  2408. begin
  2409. consume(_ID);
  2410. do_proc_call(srsym,srsymtable,nil,
  2411. (getaddr and not(token in [_CARET,_POINT])),
  2412. again,p1,[cnf_objc_id_call],nil);
  2413. { we need to know which procedure is called }
  2414. do_typecheckpass(p1);
  2415. end
  2416. else
  2417. begin
  2418. consume(_ID);
  2419. Message(parser_e_methode_id_expected);
  2420. end;
  2421. end
  2422. else
  2423. begin
  2424. if not try_type_helper(p1,nil) then
  2425. begin
  2426. Message(parser_e_invalid_qualifier);
  2427. if tpointerdef(p1.resultdef).pointeddef.typ in [recorddef,objectdef,classrefdef] then
  2428. Message(parser_h_maybe_deref_caret_missing);
  2429. end;
  2430. end
  2431. end;
  2432. else
  2433. begin
  2434. found:=try_type_helper(p1,nil);
  2435. if not found then
  2436. begin
  2437. if p1.resultdef.typ<>undefineddef then
  2438. Message(parser_e_invalid_qualifier);
  2439. p1.destroy;
  2440. p1:=cerrornode.create;
  2441. { Error }
  2442. consume(_ID);
  2443. end;
  2444. end;
  2445. end;
  2446. { processing an ordconstnode avoids the resultdef check }
  2447. skippointdefcheck:
  2448. end;
  2449. else
  2450. begin
  2451. { is this a procedure variable ? }
  2452. if assigned(p1.resultdef) and
  2453. (p1.resultdef.typ=procvardef) then
  2454. begin
  2455. { Typenode for typecasting or expecting a procvar }
  2456. if (p1.nodetype=typen) or
  2457. (
  2458. assigned(getprocvardef) and
  2459. equal_defs(p1.resultdef,getprocvardef)
  2460. ) then
  2461. begin
  2462. if try_to_consume(_LKLAMMER) then
  2463. begin
  2464. p1:=comp_expr(true,false);
  2465. consume(_RKLAMMER);
  2466. p1:=ctypeconvnode.create_explicit(p1,p1.resultdef);
  2467. end
  2468. else
  2469. again:=false
  2470. end
  2471. else
  2472. begin
  2473. if try_to_consume(_LKLAMMER) then
  2474. begin
  2475. p2:=parse_paras(false,false,_RKLAMMER);
  2476. consume(_RKLAMMER);
  2477. p1:=ccallnode.create_procvar(p2,p1);
  2478. { proc():= is never possible }
  2479. if token=_ASSIGNMENT then
  2480. begin
  2481. Message(parser_e_illegal_expression);
  2482. p1.free;
  2483. p1:=cerrornode.create;
  2484. again:=false;
  2485. end;
  2486. end
  2487. else
  2488. again:=false;
  2489. end;
  2490. end
  2491. else
  2492. again:=false;
  2493. end;
  2494. end;
  2495. { we only try again if p1 was changed }
  2496. if again or
  2497. (p1.nodetype=errorn) then
  2498. result:=true;
  2499. end; { while again }
  2500. end;
  2501. function is_member_read(sym: tsym; st: tsymtable; var p1: tnode;
  2502. out memberparentdef: tdef): boolean;
  2503. var
  2504. hdef : tdef;
  2505. begin
  2506. result:=true;
  2507. memberparentdef:=nil;
  2508. case st.symtabletype of
  2509. ObjectSymtable,
  2510. recordsymtable:
  2511. begin
  2512. memberparentdef:=tdef(st.defowner);
  2513. exit;
  2514. end;
  2515. WithSymtable:
  2516. begin
  2517. if assigned(p1) then
  2518. internalerror(2007012002);
  2519. hdef:=tnode(twithsymtable(st).withrefnode).resultdef;
  2520. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  2521. if not(hdef.typ in [objectdef,classrefdef]) then
  2522. exit;
  2523. if (hdef.typ=classrefdef) then
  2524. hdef:=tclassrefdef(hdef).pointeddef;
  2525. memberparentdef:=hdef;
  2526. end;
  2527. else
  2528. result:=false;
  2529. end;
  2530. end;
  2531. {$maxfpuregisters 0}
  2532. function factor(getaddr,typeonly,hadspecialize:boolean) : tnode;
  2533. {---------------------------------------------
  2534. Factor_read_id
  2535. ---------------------------------------------}
  2536. procedure factor_read_id(out p1:tnode;var again:boolean);
  2537. function findwithsymtable : boolean;
  2538. var
  2539. hp : psymtablestackitem;
  2540. begin
  2541. result:=true;
  2542. hp:=symtablestack.stack;
  2543. while assigned(hp) do
  2544. begin
  2545. if hp^.symtable.symtabletype=withsymtable then
  2546. exit;
  2547. hp:=hp^.next;
  2548. end;
  2549. result:=false;
  2550. end;
  2551. var
  2552. srsym: tsym;
  2553. srsymtable: TSymtable;
  2554. hdef: tdef;
  2555. pd: tprocdef;
  2556. orgstoredpattern,
  2557. storedpattern: string;
  2558. callflags: tcallnodeflags;
  2559. t : ttoken;
  2560. allowspecialize,
  2561. isspecialize,
  2562. unit_found : boolean;
  2563. tokenpos: tfileposinfo;
  2564. begin
  2565. { allow post fix operators }
  2566. again:=true;
  2567. { preinitalize tokenpos }
  2568. tokenpos:=current_filepos;
  2569. p1:=nil;
  2570. allowspecialize:=not (m_delphi in current_settings.modeswitches) and
  2571. not hadspecialize and
  2572. (block_type in [bt_type,bt_var_type,bt_const_type]);
  2573. if allowspecialize and (token=_ID) and (idtoken=_SPECIALIZE) then
  2574. begin
  2575. consume(_ID);
  2576. isspecialize:=true;
  2577. end
  2578. else
  2579. isspecialize:=hadspecialize;
  2580. { first check for identifier }
  2581. if token<>_ID then
  2582. begin
  2583. srsym:=generrorsym;
  2584. srsymtable:=nil;
  2585. consume(_ID);
  2586. unit_found:=false;
  2587. end
  2588. else
  2589. begin
  2590. if typeonly then
  2591. searchsym_type(pattern,srsym,srsymtable)
  2592. else
  2593. searchsym(pattern,srsym,srsymtable);
  2594. { handle unit specification like System.Writeln }
  2595. if not isspecialize then
  2596. unit_found:=try_consume_unitsym(srsym,srsymtable,t,true,allowspecialize,isspecialize)
  2597. else
  2598. begin
  2599. unit_found:=false;
  2600. t:=_ID;
  2601. end;
  2602. storedpattern:=pattern;
  2603. orgstoredpattern:=orgpattern;
  2604. { store the position of the token before consuming it }
  2605. tokenpos:=current_filepos;
  2606. consume(t);
  2607. { named parameter support }
  2608. found_arg_name:=false;
  2609. if not(unit_found) and
  2610. not isspecialize and
  2611. named_args_allowed and
  2612. (token=_ASSIGNMENT) then
  2613. begin
  2614. found_arg_name:=true;
  2615. p1:=cstringconstnode.createstr(storedpattern);
  2616. consume(_ASSIGNMENT);
  2617. exit;
  2618. end;
  2619. if isspecialize then
  2620. begin
  2621. if not assigned(srsym) or
  2622. (srsym.typ<>typesym) then
  2623. begin
  2624. identifier_not_found(orgstoredpattern,tokenpos);
  2625. srsym:=generrorsym;
  2626. srsymtable:=nil;
  2627. end
  2628. else
  2629. begin
  2630. hdef:=ttypesym(srsym).typedef;
  2631. generate_specialization(hdef,false,'');
  2632. if hdef=generrordef then
  2633. begin
  2634. srsym:=generrorsym;
  2635. srsymtable:=nil;
  2636. end
  2637. else
  2638. begin
  2639. srsym:=hdef.typesym;
  2640. srsymtable:=srsym.owner;
  2641. end;
  2642. end;
  2643. end;
  2644. { check hints, but only if it isn't a potential generic symbol;
  2645. that is checked in sub_expr if it isn't a generic }
  2646. if assigned(srsym) and
  2647. not (
  2648. (srsym.typ=typesym) and
  2649. (ttypesym(srsym).typedef.typ in [recorddef,objectdef,arraydef,procvardef,undefineddef]) and
  2650. not (sp_generic_para in srsym.symoptions) and
  2651. (token in [_LT, _LSHARPBRACKET])
  2652. ) then
  2653. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  2654. { if nothing found give error and return errorsym }
  2655. if not assigned(srsym) or
  2656. { is this a generic dummy symbol? }
  2657. ((srsym.typ=typesym) and
  2658. assigned(ttypesym(srsym).typedef) and
  2659. (ttypesym(srsym).typedef.typ=undefineddef) and
  2660. not (sp_generic_para in srsym.symoptions) and
  2661. not (token in [_LT, _LSHARPBRACKET]) and
  2662. not (
  2663. { in non-Delphi modes the generic class' name without a
  2664. "specialization" or "<T>" may be used to identify the
  2665. current class }
  2666. (sp_generic_dummy in srsym.symoptions) and
  2667. assigned(current_structdef) and
  2668. (df_generic in current_structdef.defoptions) and
  2669. not (m_delphi in current_settings.modeswitches) and
  2670. assigned(get_generic_in_hierarchy_by_name(srsym,current_structdef))
  2671. )) and
  2672. { it could be a rename of a generic para }
  2673. { Note: if this generates false positives we'll need to
  2674. include a "basesym" to tsym to track the original
  2675. symbol }
  2676. not (sp_explicitrename in srsym.symoptions) then
  2677. begin
  2678. { if a generic is parsed and when we are inside an with block,
  2679. a symbol might not be defined }
  2680. if assigned(current_procinfo) and (df_generic in current_procinfo.procdef.defoptions) and
  2681. findwithsymtable then
  2682. begin
  2683. { create dummy symbol, it will be freed later on }
  2684. srsym:=tstoredsym.create(undefinedsym,'$undefinedsym',false);
  2685. srsymtable:=nil;
  2686. end
  2687. else
  2688. begin
  2689. identifier_not_found(orgstoredpattern,tokenpos);
  2690. srsym:=generrorsym;
  2691. srsymtable:=nil;
  2692. end;
  2693. end;
  2694. end;
  2695. { Access to funcret or need to call the function? }
  2696. if (srsym.typ in [absolutevarsym,localvarsym,paravarsym]) and
  2697. (vo_is_funcret in tabstractvarsym(srsym).varoptions) and
  2698. { result(x) is not allowed }
  2699. not(vo_is_result in tabstractvarsym(srsym).varoptions) and
  2700. (
  2701. (token=_LKLAMMER) or
  2702. (
  2703. (([m_tp7,m_delphi,m_mac,m_iso] * current_settings.modeswitches) <> []) and
  2704. (afterassignment or in_args)
  2705. )
  2706. ) then
  2707. begin
  2708. hdef:=tdef(srsym.owner.defowner);
  2709. if assigned(hdef) and
  2710. (hdef.typ=procdef) then
  2711. srsym:=tprocdef(hdef).procsym
  2712. else
  2713. begin
  2714. Message(parser_e_illegal_expression);
  2715. srsym:=generrorsym;
  2716. end;
  2717. srsymtable:=srsym.owner;
  2718. end;
  2719. begin
  2720. case srsym.typ of
  2721. absolutevarsym :
  2722. begin
  2723. if (tabsolutevarsym(srsym).abstyp=tovar) then
  2724. begin
  2725. p1:=nil;
  2726. propaccesslist_to_node(p1,nil,tabsolutevarsym(srsym).ref);
  2727. p1:=ctypeconvnode.create(p1,tabsolutevarsym(srsym).vardef);
  2728. include(p1.flags,nf_absolute);
  2729. end
  2730. else
  2731. p1:=cloadnode.create(srsym,srsymtable);
  2732. end;
  2733. staticvarsym,
  2734. localvarsym,
  2735. paravarsym,
  2736. fieldvarsym :
  2737. begin
  2738. { check if we are reading a field of an object/class/ }
  2739. { record. is_member_read() will deal with withsymtables }
  2740. { if needed. }
  2741. p1:=nil;
  2742. if is_member_read(srsym,srsymtable,p1,hdef) then
  2743. begin
  2744. { if the field was originally found in an }
  2745. { objectsymtable, it means it's part of self }
  2746. { if only method from which it was called is }
  2747. { not class static }
  2748. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2749. { if we are accessing a owner procsym from the nested }
  2750. { class we need to call it as a class member }
  2751. if assigned(current_structdef) and
  2752. (((current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2753. (sp_static in srsym.symoptions)) then
  2754. if srsymtable.symtabletype=recordsymtable then
  2755. p1:=ctypenode.create(hdef)
  2756. else
  2757. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2758. else
  2759. begin
  2760. if assigned(current_procinfo) then
  2761. begin
  2762. pd:=current_procinfo.get_normal_proc.procdef;
  2763. if assigned(pd) and pd.no_self_node then
  2764. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  2765. else
  2766. p1:=load_self_node;
  2767. end
  2768. else
  2769. p1:=load_self_node;
  2770. end;
  2771. { now, if the field itself is part of an objectsymtab }
  2772. { (it can be even if it was found in a withsymtable, }
  2773. { e.g., "with classinstance do field := 5"), then }
  2774. { let do_member_read handle it }
  2775. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2776. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2777. else
  2778. { otherwise it's a regular record subscript }
  2779. p1:=csubscriptnode.create(srsym,p1);
  2780. end
  2781. else
  2782. { regular non-field load }
  2783. p1:=cloadnode.create(srsym,srsymtable);
  2784. end;
  2785. syssym :
  2786. begin
  2787. p1:=statement_syssym(tsyssym(srsym).number);
  2788. end;
  2789. typesym :
  2790. begin
  2791. hdef:=ttypesym(srsym).typedef;
  2792. if not assigned(hdef) then
  2793. begin
  2794. again:=false;
  2795. end
  2796. else
  2797. begin
  2798. { We need to know if this unit uses Variants }
  2799. if ((hdef=cvarianttype) or (hdef=colevarianttype)) and
  2800. not(cs_compilesystem in current_settings.moduleswitches) then
  2801. current_module.flags:=current_module.flags or uf_uses_variants;
  2802. p1:=handle_factor_typenode(hdef,getaddr,again,srsym,typeonly);
  2803. end;
  2804. end;
  2805. enumsym :
  2806. begin
  2807. p1:=genenumnode(tenumsym(srsym));
  2808. end;
  2809. constsym :
  2810. begin
  2811. if tconstsym(srsym).consttyp=constresourcestring then
  2812. begin
  2813. p1:=cloadnode.create(srsym,srsymtable);
  2814. do_typecheckpass(p1);
  2815. p1.resultdef:=getansistringdef;
  2816. end
  2817. else
  2818. p1:=genconstsymtree(tconstsym(srsym));
  2819. end;
  2820. procsym :
  2821. begin
  2822. p1:=nil;
  2823. { check if it's a method/class method }
  2824. if is_member_read(srsym,srsymtable,p1,hdef) then
  2825. begin
  2826. { if we are accessing a owner procsym from the nested }
  2827. { class we need to call it as a class member }
  2828. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) and
  2829. assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef) then
  2830. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef));
  2831. { not srsymtable.symtabletype since that can be }
  2832. { withsymtable as well }
  2833. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2834. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2835. else
  2836. { no procsyms in records (yet) }
  2837. internalerror(2007012006);
  2838. end
  2839. else
  2840. begin
  2841. { regular procedure/function call }
  2842. if not unit_found then
  2843. callflags:=[]
  2844. else
  2845. callflags:=[cnf_unit_specified];
  2846. do_proc_call(srsym,srsymtable,nil,
  2847. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  2848. again,p1,callflags,nil);
  2849. end;
  2850. end;
  2851. propertysym :
  2852. begin
  2853. p1:=nil;
  2854. { property of a class/object? }
  2855. if is_member_read(srsym,srsymtable,p1,hdef) then
  2856. begin
  2857. if (srsymtable.symtabletype in [ObjectSymtable,recordsymtable]) then
  2858. { if we are accessing a owner procsym from the nested }
  2859. { class or from a static class method we need to call }
  2860. { it as a class member }
  2861. if (assigned(current_structdef) and (current_structdef<>hdef) and is_owned_by(current_structdef,hdef)) or
  2862. (assigned(current_procinfo) and current_procinfo.get_normal_proc.procdef.no_self_node) then
  2863. p1:=cloadvmtaddrnode.create(ctypenode.create(hdef))
  2864. else
  2865. p1:=load_self_node;
  2866. { not srsymtable.symtabletype since that can be }
  2867. { withsymtable as well }
  2868. if (srsym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2869. do_member_read(tabstractrecorddef(hdef),getaddr,srsym,p1,again,[],nil)
  2870. else
  2871. { no propertysyms in records (yet) }
  2872. internalerror(2009111510);
  2873. end
  2874. else
  2875. { no method pointer }
  2876. begin
  2877. handle_propertysym(tpropertysym(srsym),srsymtable,p1);
  2878. end;
  2879. end;
  2880. labelsym :
  2881. begin
  2882. { Support @label }
  2883. if getaddr then
  2884. begin
  2885. if srsym.owner<>current_procinfo.procdef.localst then
  2886. CGMessage(parser_e_label_outside_proc);
  2887. p1:=cloadnode.create(srsym,srsym.owner)
  2888. end
  2889. else
  2890. begin
  2891. consume(_COLON);
  2892. if tlabelsym(srsym).defined then
  2893. Message(sym_e_label_already_defined);
  2894. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  2895. begin
  2896. tlabelsym(srsym).nonlocal:=true;
  2897. exclude(current_procinfo.procdef.procoptions,po_inline);
  2898. end;
  2899. if tlabelsym(srsym).nonlocal and
  2900. (current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  2901. Message(sym_e_interprocgoto_into_init_final_code_not_allowed);
  2902. tlabelsym(srsym).defined:=true;
  2903. p1:=clabelnode.create(nil,tlabelsym(srsym));
  2904. tlabelsym(srsym).code:=p1;
  2905. end;
  2906. end;
  2907. undefinedsym :
  2908. begin
  2909. p1:=cnothingnode.Create;
  2910. p1.resultdef:=cundefineddef.create(true);
  2911. { clean up previously created dummy symbol }
  2912. srsym.free;
  2913. end;
  2914. errorsym :
  2915. begin
  2916. p1:=cerrornode.create;
  2917. if try_to_consume(_LKLAMMER) then
  2918. begin
  2919. parse_paras(false,false,_RKLAMMER);
  2920. consume(_RKLAMMER);
  2921. end;
  2922. end;
  2923. else
  2924. begin
  2925. p1:=cerrornode.create;
  2926. Message(parser_e_illegal_expression);
  2927. end;
  2928. end; { end case }
  2929. if assigned(p1) and (p1.nodetype<>errorn) then
  2930. p1.fileinfo:=tokenpos;
  2931. end;
  2932. end;
  2933. {---------------------------------------------
  2934. Factor_Read_Set
  2935. ---------------------------------------------}
  2936. { Read a set between [] }
  2937. function factor_read_set:tnode;
  2938. var
  2939. p1,p2 : tnode;
  2940. lastp,
  2941. buildp : tarrayconstructornode;
  2942. old_allow_array_constructor : boolean;
  2943. begin
  2944. buildp:=nil;
  2945. lastp:=nil;
  2946. { be sure that a least one arrayconstructn is used, also for an
  2947. empty [] }
  2948. if token=_RECKKLAMMER then
  2949. buildp:=carrayconstructornode.create(nil,buildp)
  2950. else
  2951. repeat
  2952. { nested array constructors are not allowed, see also tests/webtbs/tw17213.pp }
  2953. old_allow_array_constructor:=allow_array_constructor;
  2954. allow_array_constructor:=false;
  2955. p1:=comp_expr(true,false);
  2956. if try_to_consume(_POINTPOINT) then
  2957. begin
  2958. p2:=comp_expr(true,false);
  2959. p1:=carrayconstructorrangenode.create(p1,p2);
  2960. end;
  2961. { insert at the end of the tree, to get the correct order }
  2962. if not assigned(buildp) then
  2963. begin
  2964. buildp:=carrayconstructornode.create(p1,nil);
  2965. lastp:=buildp;
  2966. end
  2967. else
  2968. begin
  2969. lastp.right:=carrayconstructornode.create(p1,nil);
  2970. lastp:=tarrayconstructornode(lastp.right);
  2971. end;
  2972. allow_array_constructor:=old_allow_array_constructor;
  2973. { there could be more elements }
  2974. until not try_to_consume(_COMMA);
  2975. factor_read_set:=buildp;
  2976. end;
  2977. function can_load_self_node: boolean;
  2978. begin
  2979. result:=false;
  2980. if (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  2981. not assigned(current_structdef) or
  2982. not assigned(current_procinfo) then
  2983. exit;
  2984. result:=not current_procinfo.get_normal_proc.procdef.no_self_node;
  2985. end;
  2986. {---------------------------------------------
  2987. Factor (Main)
  2988. ---------------------------------------------}
  2989. var
  2990. l : longint;
  2991. ic : int64;
  2992. qc : qword;
  2993. p1 : tnode;
  2994. code : integer;
  2995. srsym : tsym;
  2996. srsymtable : TSymtable;
  2997. pd : tprocdef;
  2998. hclassdef : tobjectdef;
  2999. d : bestreal;
  3000. hs,hsorg : string;
  3001. hdef : tdef;
  3002. filepos : tfileposinfo;
  3003. callflags : tcallnodeflags;
  3004. idstr : tidstring;
  3005. dopostfix,
  3006. again,
  3007. updatefpos,
  3008. nodechanged : boolean;
  3009. begin
  3010. { can't keep a copy of p1 and compare pointers afterwards, because
  3011. p1 may be freed and reallocated in the same place! }
  3012. dopostfix:=true;
  3013. updatefpos:=false;
  3014. p1:=nil;
  3015. filepos:=current_tokenpos;
  3016. again:=false;
  3017. pd:=nil;
  3018. if token=_ID then
  3019. begin
  3020. again:=true;
  3021. { Handle references to self }
  3022. if (idtoken=_SELF) and can_load_self_node then
  3023. begin
  3024. p1:=load_self_node;
  3025. consume(_ID);
  3026. again:=true;
  3027. end
  3028. else
  3029. factor_read_id(p1,again);
  3030. if assigned(p1) then
  3031. begin
  3032. { factor_read_id will set the filepos to after the id,
  3033. and in case of _SELF the filepos will already be the
  3034. same as filepos (so setting it again doesn't hurt). }
  3035. p1.fileinfo:=filepos;
  3036. filepos:=current_tokenpos;
  3037. end;
  3038. { handle post fix operators }
  3039. if (p1.nodetype=specializen) then
  3040. { post fix operators are handled after specialization }
  3041. dopostfix:=false
  3042. else
  3043. if (m_delphi in current_settings.modeswitches) and
  3044. (block_type=bt_body) and
  3045. (token in [_LT,_LSHARPBRACKET]) then
  3046. begin
  3047. if p1.nodetype=typen then
  3048. idstr:=ttypenode(p1).typesym.name
  3049. else
  3050. if (p1.nodetype=loadvmtaddrn) and
  3051. (tloadvmtaddrnode(p1).left.nodetype=typen) then
  3052. idstr:=ttypenode(tloadvmtaddrnode(p1).left).typesym.name
  3053. else
  3054. if (p1.nodetype=loadn) then
  3055. idstr:=tloadnode(p1).symtableentry.name
  3056. else
  3057. idstr:='';
  3058. { if this is the case then the postfix handling is done in
  3059. sub_expr if necessary }
  3060. dopostfix:=not could_be_generic(idstr);
  3061. end;
  3062. { maybe an additional parameter instead of misusing hadspezialize? }
  3063. if dopostfix and not hadspecialize then
  3064. updatefpos:=postfixoperators(p1,again,getaddr);
  3065. end
  3066. else
  3067. begin
  3068. updatefpos:=true;
  3069. case token of
  3070. _RETURN :
  3071. begin
  3072. consume(_RETURN);
  3073. p1:=nil;
  3074. if not(token in [_SEMICOLON,_ELSE,_END]) then
  3075. begin
  3076. p1:=comp_expr(true,false);
  3077. if not assigned(current_procinfo) or
  3078. (current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) or
  3079. is_void(current_procinfo.procdef.returndef) then
  3080. begin
  3081. Message(parser_e_void_function);
  3082. { recovery }
  3083. p1.free;
  3084. p1:=nil;
  3085. end;
  3086. end;
  3087. p1 := cexitnode.create(p1);
  3088. end;
  3089. _INHERITED :
  3090. begin
  3091. again:=true;
  3092. consume(_INHERITED);
  3093. if assigned(current_procinfo) and
  3094. assigned(current_structdef) and
  3095. ((current_structdef.typ=objectdef) or
  3096. ((target_info.system in systems_jvm) and
  3097. (current_structdef.typ=recorddef)))then
  3098. begin
  3099. { for record helpers in mode Delphi "inherited" is not
  3100. allowed }
  3101. if is_objectpascal_helper(current_structdef) and
  3102. (m_delphi in current_settings.modeswitches) and
  3103. is_record(tobjectdef(current_structdef).extendeddef) then
  3104. Message(parser_e_inherited_not_in_record);
  3105. if (current_structdef.typ=objectdef) then
  3106. begin
  3107. hclassdef:=tobjectdef(current_structdef).childof;
  3108. { Objective-C categories *replace* methods in the class
  3109. they extend, or add methods to it. So calling an
  3110. inherited method always calls the method inherited from
  3111. the parent of the extended class }
  3112. if is_objccategory(current_structdef) then
  3113. hclassdef:=hclassdef.childof;
  3114. end
  3115. else if target_info.system in systems_jvm then
  3116. hclassdef:=java_fpcbaserecordtype
  3117. else
  3118. internalerror(2012012401);
  3119. { if inherited; only then we need the method with
  3120. the same name }
  3121. if token <> _ID then
  3122. begin
  3123. hs:=current_procinfo.procdef.procsym.name;
  3124. hsorg:=current_procinfo.procdef.procsym.realname;
  3125. anon_inherited:=true;
  3126. { For message methods we need to search using the message
  3127. number or string }
  3128. pd:=tprocdef(tprocsym(current_procinfo.procdef.procsym).ProcdefList[0]);
  3129. srdef:=nil;
  3130. if (po_msgint in pd.procoptions) then
  3131. searchsym_in_class_by_msgint(hclassdef,pd.messageinf.i,srdef,srsym,srsymtable)
  3132. else
  3133. if (po_msgstr in pd.procoptions) then
  3134. searchsym_in_class_by_msgstr(hclassdef,pd.messageinf.str^,srsym,srsymtable)
  3135. else
  3136. { helpers have their own ways of dealing with inherited }
  3137. if is_objectpascal_helper(current_structdef) then
  3138. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3139. else
  3140. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3141. end
  3142. else
  3143. begin
  3144. hs:=pattern;
  3145. hsorg:=orgpattern;
  3146. consume(_ID);
  3147. anon_inherited:=false;
  3148. { helpers have their own ways of dealing with inherited }
  3149. if is_objectpascal_helper(current_structdef) then
  3150. searchsym_in_helper(tobjectdef(current_structdef),tobjectdef(current_structdef),hs,srsym,srsymtable,[ssf_has_inherited])
  3151. else
  3152. searchsym_in_class(hclassdef,current_structdef,hs,srsym,srsymtable,[ssf_search_helper]);
  3153. end;
  3154. if assigned(srsym) then
  3155. begin
  3156. check_hints(srsym,srsym.symoptions,srsym.deprecatedmsg);
  3157. { load the procdef from the inherited class and
  3158. not from self }
  3159. case srsym.typ of
  3160. procsym:
  3161. begin
  3162. if is_objectpascal_helper(current_structdef) then
  3163. begin
  3164. { for a helper load the procdef either from the
  3165. extended type, from the parent helper or from
  3166. the extended type of the parent helper
  3167. depending on the def the found symbol belongs
  3168. to }
  3169. if (srsym.Owner.defowner.typ=objectdef) and
  3170. is_objectpascal_helper(tobjectdef(srsym.Owner.defowner)) then
  3171. if def_is_related(current_structdef,tdef(srsym.Owner.defowner)) and
  3172. assigned(tobjectdef(current_structdef).childof) then
  3173. hdef:=tobjectdef(current_structdef).childof
  3174. else
  3175. hdef:=tobjectdef(srsym.Owner.defowner).extendeddef
  3176. else
  3177. hdef:=tdef(srsym.Owner.defowner);
  3178. end
  3179. else
  3180. hdef:=hclassdef;
  3181. if (po_classmethod in current_procinfo.procdef.procoptions) or
  3182. (po_staticmethod in current_procinfo.procdef.procoptions) then
  3183. hdef:=cclassrefdef.create(hdef);
  3184. p1:=ctypenode.create(hdef);
  3185. { we need to allow helpers here }
  3186. ttypenode(p1).helperallowed:=true;
  3187. end;
  3188. propertysym:
  3189. ;
  3190. else
  3191. begin
  3192. Message(parser_e_methode_id_expected);
  3193. p1:=cerrornode.create;
  3194. end;
  3195. end;
  3196. callflags:=[cnf_inherited];
  3197. include(current_procinfo.flags,pi_has_inherited);
  3198. if anon_inherited then
  3199. include(callflags,cnf_anon_inherited);
  3200. do_member_read(hclassdef,getaddr,srsym,p1,again,callflags,nil);
  3201. end
  3202. else
  3203. begin
  3204. if anon_inherited then
  3205. begin
  3206. { For message methods we need to call DefaultHandler }
  3207. if (po_msgint in pd.procoptions) or
  3208. (po_msgstr in pd.procoptions) then
  3209. begin
  3210. searchsym_in_class(hclassdef,hclassdef,'DEFAULTHANDLER',srsym,srsymtable,[ssf_search_helper]);
  3211. if not assigned(srsym) or
  3212. (srsym.typ<>procsym) then
  3213. internalerror(200303171);
  3214. p1:=nil;
  3215. do_proc_call(srsym,srsym.owner,hclassdef,false,again,p1,[],nil);
  3216. end
  3217. else
  3218. begin
  3219. { we need to ignore the inherited; }
  3220. p1:=cnothingnode.create;
  3221. end;
  3222. end
  3223. else
  3224. begin
  3225. Message1(sym_e_id_no_member,hsorg);
  3226. p1:=cerrornode.create;
  3227. end;
  3228. again:=false;
  3229. end;
  3230. { turn auto inheriting off }
  3231. anon_inherited:=false;
  3232. end
  3233. else
  3234. begin
  3235. { in case of records we use a more clear error message }
  3236. if assigned(current_structdef) and
  3237. (current_structdef.typ=recorddef) then
  3238. Message(parser_e_inherited_not_in_record)
  3239. else
  3240. Message(parser_e_generic_methods_only_in_methods);
  3241. again:=false;
  3242. p1:=cerrornode.create;
  3243. end;
  3244. postfixoperators(p1,again,getaddr);
  3245. end;
  3246. _INTCONST :
  3247. begin
  3248. {Try first wether the value fits in an int64.}
  3249. val(pattern,ic,code);
  3250. if code=0 then
  3251. begin
  3252. consume(_INTCONST);
  3253. int_to_type(ic,hdef);
  3254. p1:=cordconstnode.create(ic,hdef,true);
  3255. end
  3256. else
  3257. begin
  3258. { try qword next }
  3259. val(pattern,qc,code);
  3260. if code=0 then
  3261. begin
  3262. consume(_INTCONST);
  3263. int_to_type(qc,hdef);
  3264. p1:=cordconstnode.create(qc,hdef,true);
  3265. end;
  3266. end;
  3267. if code<>0 then
  3268. begin
  3269. { finally float }
  3270. val(pattern,d,code);
  3271. if code<>0 then
  3272. begin
  3273. Message(parser_e_invalid_integer);
  3274. consume(_INTCONST);
  3275. l:=1;
  3276. p1:=cordconstnode.create(l,sinttype,true);
  3277. end
  3278. else
  3279. begin
  3280. consume(_INTCONST);
  3281. p1:=crealconstnode.create(d,pbestrealtype^);
  3282. end;
  3283. end
  3284. else
  3285. { the necessary range checking has already been done by val }
  3286. tordconstnode(p1).rangecheck:=false;
  3287. if token=_POINT then
  3288. begin
  3289. again:=true;
  3290. postfixoperators(p1,again,getaddr);
  3291. end;
  3292. end;
  3293. _REALNUMBER :
  3294. begin
  3295. p1:=real_const_node_from_pattern(pattern);
  3296. consume(_REALNUMBER);
  3297. if token=_POINT then
  3298. begin
  3299. again:=true;
  3300. postfixoperators(p1,again,getaddr);
  3301. end;
  3302. end;
  3303. _STRING :
  3304. begin
  3305. string_dec(hdef,true);
  3306. { STRING can be also a type cast }
  3307. if try_to_consume(_LKLAMMER) then
  3308. begin
  3309. p1:=comp_expr(true,false);
  3310. consume(_RKLAMMER);
  3311. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3312. { handle postfix operators here e.g. string(a)[10] }
  3313. again:=true;
  3314. postfixoperators(p1,again,getaddr);
  3315. end
  3316. else
  3317. p1:=ctypenode.create(hdef);
  3318. end;
  3319. _FILE :
  3320. begin
  3321. hdef:=cfiletype;
  3322. consume(_FILE);
  3323. { FILE can be also a type cast }
  3324. if try_to_consume(_LKLAMMER) then
  3325. begin
  3326. p1:=comp_expr(true,false);
  3327. consume(_RKLAMMER);
  3328. p1:=ctypeconvnode.create_explicit(p1,hdef);
  3329. { handle postfix operators here e.g. string(a)[10] }
  3330. again:=true;
  3331. postfixoperators(p1,again,getaddr);
  3332. end
  3333. else
  3334. begin
  3335. p1:=ctypenode.create(hdef);
  3336. end;
  3337. end;
  3338. _CSTRING :
  3339. begin
  3340. p1:=cstringconstnode.createpchar(ansistring2pchar(cstringpattern),length(cstringpattern),nil);
  3341. consume(_CSTRING);
  3342. if token in postfixoperator_tokens then
  3343. begin
  3344. again:=true;
  3345. postfixoperators(p1,again,getaddr);
  3346. end;
  3347. end;
  3348. _CCHAR :
  3349. begin
  3350. p1:=cordconstnode.create(ord(pattern[1]),cansichartype,true);
  3351. consume(_CCHAR);
  3352. if token=_POINT then
  3353. begin
  3354. again:=true;
  3355. postfixoperators(p1,again,getaddr);
  3356. end;
  3357. end;
  3358. _CWSTRING:
  3359. begin
  3360. p1:=cstringconstnode.createunistr(patternw);
  3361. consume(_CWSTRING);
  3362. if token in postfixoperator_tokens then
  3363. begin
  3364. again:=true;
  3365. postfixoperators(p1,again,getaddr);
  3366. end;
  3367. end;
  3368. _CWCHAR:
  3369. begin
  3370. p1:=cordconstnode.create(ord(getcharwidestring(patternw,0)),cwidechartype,true);
  3371. consume(_CWCHAR);
  3372. if token=_POINT then
  3373. begin
  3374. again:=true;
  3375. postfixoperators(p1,again,getaddr);
  3376. end;
  3377. end;
  3378. _KLAMMERAFFE :
  3379. begin
  3380. consume(_KLAMMERAFFE);
  3381. got_addrn:=true;
  3382. { support both @<x> and @(<x>) }
  3383. if try_to_consume(_LKLAMMER) then
  3384. begin
  3385. p1:=factor(true,false,false);
  3386. { inside parentheses a full expression is allowed, see also tests\webtbs\tb27517.pp }
  3387. if token<>_RKLAMMER then
  3388. p1:=sub_expr(opcompare,true,false,p1);
  3389. consume(_RKLAMMER);
  3390. end
  3391. else
  3392. p1:=factor(true,false,false);
  3393. if token in postfixoperator_tokens then
  3394. begin
  3395. again:=true;
  3396. postfixoperators(p1,again,getaddr);
  3397. end;
  3398. got_addrn:=false;
  3399. p1:=caddrnode.create(p1);
  3400. p1.fileinfo:=filepos;
  3401. if cs_typed_addresses in current_settings.localswitches then
  3402. include(p1.flags,nf_typedaddr);
  3403. { Store the procvar that we are expecting, the
  3404. addrn will use the information to find the correct
  3405. procdef or it will return an error }
  3406. if assigned(getprocvardef) and
  3407. (taddrnode(p1).left.nodetype = loadn) then
  3408. taddrnode(p1).getprocvardef:=getprocvardef;
  3409. end;
  3410. _LKLAMMER :
  3411. begin
  3412. consume(_LKLAMMER);
  3413. p1:=comp_expr(true,false);
  3414. consume(_RKLAMMER);
  3415. { it's not a good solution
  3416. but (a+b)^ makes some problems }
  3417. if token in postfixoperator_tokens then
  3418. begin
  3419. again:=true;
  3420. postfixoperators(p1,again,getaddr);
  3421. end;
  3422. end;
  3423. _LECKKLAMMER :
  3424. begin
  3425. consume(_LECKKLAMMER);
  3426. p1:=factor_read_set;
  3427. consume(_RECKKLAMMER);
  3428. end;
  3429. _PLUS :
  3430. begin
  3431. consume(_PLUS);
  3432. p1:=factor(false,false,false);
  3433. p1:=cunaryplusnode.create(p1);
  3434. end;
  3435. _MINUS :
  3436. begin
  3437. consume(_MINUS);
  3438. if (token = _INTCONST) and not(m_isolike_unary_minus in current_settings.modeswitches) then
  3439. begin
  3440. { ugly hack, but necessary to be able to parse }
  3441. { -9223372036854775808 as int64 (JM) }
  3442. pattern := '-'+pattern;
  3443. p1:=sub_expr(oppower,false,false,nil);
  3444. { -1 ** 4 should be - (1 ** 4) and not
  3445. (-1) ** 4
  3446. This was the reason of tw0869.pp test failure PM }
  3447. if p1.nodetype=starstarn then
  3448. begin
  3449. if tbinarynode(p1).left.nodetype=ordconstn then
  3450. begin
  3451. tordconstnode(tbinarynode(p1).left).value:=-tordconstnode(tbinarynode(p1).left).value;
  3452. p1:=cunaryminusnode.create(p1);
  3453. end
  3454. else if tbinarynode(p1).left.nodetype=realconstn then
  3455. begin
  3456. trealconstnode(tbinarynode(p1).left).value_real:=-trealconstnode(tbinarynode(p1).left).value_real;
  3457. trealconstnode(tbinarynode(p1).left).value_currency:=-trealconstnode(tbinarynode(p1).left).value_currency;
  3458. p1:=cunaryminusnode.create(p1);
  3459. end
  3460. else
  3461. internalerror(20021029);
  3462. end;
  3463. end
  3464. else
  3465. begin
  3466. if m_isolike_unary_minus in current_settings.modeswitches then
  3467. p1:=sub_expr(opmultiply,false,false,nil)
  3468. else
  3469. p1:=sub_expr(oppower,false,false,nil);
  3470. p1:=cunaryminusnode.create(p1);
  3471. end;
  3472. end;
  3473. _OP_NOT :
  3474. begin
  3475. consume(_OP_NOT);
  3476. p1:=factor(false,false,false);
  3477. p1:=cnotnode.create(p1);
  3478. end;
  3479. _NIL :
  3480. begin
  3481. consume(_NIL);
  3482. p1:=cnilnode.create;
  3483. { It's really ugly code nil^, but delphi allows it }
  3484. if token in [_CARET,_POINT] then
  3485. begin
  3486. again:=true;
  3487. postfixoperators(p1,again,getaddr);
  3488. end;
  3489. end;
  3490. _OBJCPROTOCOL:
  3491. begin
  3492. { The @protocol keyword is used in two ways in Objective-C:
  3493. 1) to declare protocols (~ Object Pascal interfaces)
  3494. 2) to obtain the metaclass (~ Object Pascal) "class of")
  3495. of a declared protocol
  3496. This code is for handling the second case. Because of 1),
  3497. we cannot simply use a system unit symbol.
  3498. }
  3499. consume(_OBJCPROTOCOL);
  3500. consume(_LKLAMMER);
  3501. p1:=factor(false,false,false);
  3502. consume(_RKLAMMER);
  3503. p1:=cinlinenode.create(in_objc_protocol_x,false,p1);
  3504. end;
  3505. else
  3506. begin
  3507. Message(parser_e_illegal_expression);
  3508. p1:=cerrornode.create;
  3509. { recover }
  3510. consume(token);
  3511. end;
  3512. end;
  3513. end;
  3514. { generate error node if no node is created }
  3515. if not assigned(p1) then
  3516. begin
  3517. {$ifdef EXTDEBUG}
  3518. Comment(V_Warning,'factor: p1=nil');
  3519. {$endif}
  3520. p1:=cerrornode.create;
  3521. updatefpos:=true;
  3522. end;
  3523. { get the resultdef for the node if nothing stops us }
  3524. if (not assigned(p1.resultdef)) and dopostfix then
  3525. begin
  3526. do_typecheckpass_changed(p1,nodechanged);
  3527. updatefpos:=updatefpos or nodechanged;
  3528. end;
  3529. if assigned(p1) and
  3530. updatefpos then
  3531. p1.fileinfo:=filepos;
  3532. factor:=p1;
  3533. end;
  3534. {$maxfpuregisters default}
  3535. procedure post_comp_expr_gendef(var def: tdef);
  3536. var
  3537. p1 : tnode;
  3538. again : boolean;
  3539. begin
  3540. if not assigned(def) then
  3541. internalerror(2011053001);
  3542. again:=false;
  3543. { handle potential typecasts, etc }
  3544. p1:=handle_factor_typenode(def,false,again,nil,false);
  3545. { parse postfix operators }
  3546. postfixoperators(p1,again,false);
  3547. if assigned(p1) and (p1.nodetype=typen) then
  3548. def:=ttypenode(p1).typedef
  3549. else
  3550. def:=generrordef;
  3551. end;
  3552. {****************************************************************************
  3553. Sub_Expr
  3554. ****************************************************************************}
  3555. function sub_expr(pred_level:Toperator_precedence;accept_equal,typeonly:boolean;factornode:tnode):tnode;
  3556. {Reads a subexpression while the operators are of the current precedence
  3557. level, or any higher level. Replaces the old term, simpl_expr and
  3558. simpl2_expr.}
  3559. function istypenode(n:tnode):boolean;inline;
  3560. { Checks whether the given node is a type node or a VMT node containing a
  3561. typenode. This is used in the code for inline specializations in the
  3562. _LT branch below }
  3563. begin
  3564. result:=assigned(n) and
  3565. (
  3566. (n.nodetype=typen) or
  3567. (
  3568. (n.nodetype=loadvmtaddrn) and
  3569. (tloadvmtaddrnode(n).left.nodetype=typen)
  3570. )
  3571. );
  3572. end;
  3573. function gettypedef(n:tnode):tdef;inline;
  3574. { This returns the typedef that belongs to the given typenode or
  3575. loadvmtaddrnode. n must not be Nil! }
  3576. begin
  3577. if n.nodetype=typen then
  3578. result:=ttypenode(n).typedef
  3579. else
  3580. result:=ttypenode(tloadvmtaddrnode(n).left).typedef;
  3581. end;
  3582. function gettypedef(sym:tsym):tdef;inline;
  3583. begin
  3584. result:=nil;
  3585. case sym.typ of
  3586. typesym:
  3587. result:=ttypesym(sym).typedef;
  3588. procsym:
  3589. result:=tdef(tprocsym(sym).procdeflist[0]);
  3590. else
  3591. internalerror(2015092701);
  3592. end;
  3593. end;
  3594. function getgenericsym(n:tnode;out srsym:tsym):boolean;
  3595. var
  3596. srsymtable : tsymtable;
  3597. begin
  3598. srsym:=nil;
  3599. case n.nodetype of
  3600. typen:
  3601. srsym:=ttypenode(n).typedef.typesym;
  3602. loadvmtaddrn:
  3603. srsym:=ttypenode(tloadvmtaddrnode(n).left).typedef.typesym;
  3604. loadn:
  3605. if not searchsym_with_symoption(tloadnode(n).symtableentry.Name,srsym,srsymtable,sp_generic_dummy) then
  3606. srsym:=nil;
  3607. specializen:
  3608. srsym:=tspecializenode(n).sym;
  3609. { TODO : handle const nodes }
  3610. end;
  3611. result:=assigned(srsym);
  3612. end;
  3613. function generate_inline_specialization(gendef:tdef;n:tnode;filepos:tfileposinfo;parseddef:tdef;gensym:tsym;p2:tnode):tnode;
  3614. var
  3615. again,
  3616. getaddr : boolean;
  3617. pload : tnode;
  3618. spezcontext : tspecializationcontext;
  3619. structdef : tabstractrecorddef;
  3620. begin
  3621. if n.nodetype=specializen then
  3622. begin
  3623. getaddr:=tspecializenode(n).getaddr;
  3624. pload:=tspecializenode(n).left;
  3625. tspecializenode(n).left:=nil;
  3626. end
  3627. else
  3628. begin
  3629. getaddr:=false;
  3630. pload:=nil;
  3631. end;
  3632. if assigned(parseddef) and assigned(gensym) and assigned(p2) then
  3633. gendef:=generate_specialization_phase1(spezcontext,gendef,parseddef,gensym.realname,p2.fileinfo)
  3634. else
  3635. gendef:=generate_specialization_phase1(spezcontext,gendef);
  3636. case gendef.typ of
  3637. errordef:
  3638. begin
  3639. spezcontext.free;
  3640. spezcontext:=nil;
  3641. gensym:=generrorsym;
  3642. end;
  3643. objectdef,
  3644. recorddef,
  3645. procvardef,
  3646. arraydef:
  3647. begin
  3648. gendef:=generate_specialization_phase2(spezcontext,tstoreddef(gendef),false,'');
  3649. spezcontext.free;
  3650. spezcontext:=nil;
  3651. gensym:=gendef.typesym;
  3652. end;
  3653. procdef:
  3654. begin
  3655. if block_type<>bt_body then
  3656. begin
  3657. message(parser_e_illegal_expression);
  3658. gensym:=generrorsym;
  3659. end
  3660. else
  3661. begin
  3662. gensym:=tprocdef(gendef).procsym;
  3663. end;
  3664. end;
  3665. else
  3666. internalerror(2015092702);
  3667. end;
  3668. { in case of a class or a record the specialized generic
  3669. is always a classrefdef }
  3670. again:=false;
  3671. if assigned(pload) then
  3672. begin
  3673. result:=pload;
  3674. structdef:=nil;
  3675. case result.resultdef.typ of
  3676. objectdef,
  3677. recorddef:
  3678. begin
  3679. structdef:=tabstractrecorddef(result.resultdef);
  3680. end;
  3681. classrefdef:
  3682. begin
  3683. structdef:=tabstractrecorddef(tclassrefdef(result.resultdef).pointeddef);
  3684. end;
  3685. else
  3686. internalerror(2015092703);
  3687. end;
  3688. do_member_read(structdef,getaddr,gensym,result,again,[],spezcontext);
  3689. end
  3690. else
  3691. begin
  3692. result:=nil;
  3693. { check if it's a method/class method }
  3694. if is_member_read(gensym,gensym.owner,result,parseddef) then
  3695. begin
  3696. { if we are accessing a owner procsym from the nested }
  3697. { class we need to call it as a class member }
  3698. if (gensym.owner.symtabletype in [ObjectSymtable,recordsymtable]) and
  3699. assigned(current_structdef) and (current_structdef<>parseddef) and is_owned_by(current_structdef,parseddef) then
  3700. begin
  3701. result:=cloadvmtaddrnode.create(ctypenode.create(parseddef));
  3702. { not srsymtable.symtabletype since that can be }
  3703. { withsymtable as well }
  3704. if (gensym.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  3705. begin
  3706. do_member_read(tabstractrecorddef(parseddef),getaddr,gensym,result,again,[],spezcontext);
  3707. spezcontext:=nil;
  3708. end
  3709. else
  3710. { no procsyms in records (yet) }
  3711. internalerror(2015092704);
  3712. end
  3713. else
  3714. begin
  3715. { regular procedure/function call }
  3716. do_proc_call(gensym,gensym.owner,nil,
  3717. (getaddr and not(token in [_CARET,_POINT,_LECKKLAMMER])),
  3718. again,result,[],spezcontext);
  3719. spezcontext:=nil;
  3720. end;
  3721. end
  3722. else
  3723. { handle potential typecasts, etc }
  3724. result:=handle_factor_typenode(gendef,false,again,nil,false);
  3725. end;
  3726. { parse postfix operators }
  3727. if postfixoperators(result,again,false) then
  3728. if assigned(result) then
  3729. result.fileinfo:=filepos
  3730. else
  3731. result:=cerrornode.create;
  3732. spezcontext.free;
  3733. end;
  3734. label
  3735. SubExprStart;
  3736. var
  3737. p1,p2,ptmp : tnode;
  3738. oldt : Ttoken;
  3739. filepos : tfileposinfo;
  3740. gendef,parseddef : tdef;
  3741. gensym : tsym;
  3742. begin
  3743. SubExprStart:
  3744. if pred_level=highest_precedence then
  3745. begin
  3746. if factornode=nil then
  3747. p1:=factor(false,typeonly,false)
  3748. else
  3749. p1:=factornode;
  3750. end
  3751. else
  3752. p1:=sub_expr(succ(pred_level),true,typeonly,factornode);
  3753. repeat
  3754. if (token in [NOTOKEN..last_operator]) and
  3755. (token in operator_levels[pred_level]) and
  3756. ((token<>_EQ) or accept_equal) then
  3757. begin
  3758. oldt:=token;
  3759. filepos:=current_tokenpos;
  3760. consume(token);
  3761. if pred_level=highest_precedence then
  3762. p2:=factor(false,false,false)
  3763. else
  3764. p2:=sub_expr(succ(pred_level),true,typeonly,nil);
  3765. case oldt of
  3766. _PLUS :
  3767. p1:=caddnode.create(addn,p1,p2);
  3768. _MINUS :
  3769. p1:=caddnode.create(subn,p1,p2);
  3770. _STAR :
  3771. p1:=caddnode.create(muln,p1,p2);
  3772. _SLASH :
  3773. p1:=caddnode.create(slashn,p1,p2);
  3774. _EQ:
  3775. p1:=caddnode.create(equaln,p1,p2);
  3776. _GT :
  3777. p1:=caddnode.create(gtn,p1,p2);
  3778. _LT :
  3779. begin
  3780. { we need to decice whether we have an inline specialization
  3781. (type nodes to the left and right of "<", mode Delphi and
  3782. ">" or "," following) or a normal "<" comparison }
  3783. { TODO : p1 could be a non type if e.g. a variable with the
  3784. same name is defined in the same unit where the
  3785. generic is defined (though "same unit" is not
  3786. necessarily needed) }
  3787. if getgenericsym(p1,gensym) and
  3788. { Attention: when nested specializations are supported
  3789. p2 could be a loadn if a "<" follows }
  3790. istypenode(p2) and
  3791. (m_delphi in current_settings.modeswitches) and
  3792. { TODO : add _LT, _LSHARPBRACKET for nested specializations }
  3793. (token in [_GT,_RSHARPBRACKET,_COMMA]) then
  3794. begin
  3795. { this is an inline specialization }
  3796. { retrieve the defs of two nodes }
  3797. if p1.nodetype=specializen then
  3798. gendef:=gettypedef(tspecializenode(p1).sym)
  3799. else
  3800. gendef:=nil;
  3801. parseddef:=gettypedef(p2);
  3802. { check the hints for parseddef }
  3803. check_hints(parseddef.typesym,parseddef.typesym.symoptions,parseddef.typesym.deprecatedmsg,p1.fileinfo);
  3804. ptmp:=generate_inline_specialization(gendef,p1,filepos,parseddef,gensym,p2);
  3805. { we don't need these nodes anymore }
  3806. p1.free;
  3807. p2.free;
  3808. p1:=ptmp;
  3809. { with p1 now set we are in reality directly behind the
  3810. call to "factor" thus we need to call down to that
  3811. again }
  3812. { This is disabled until specializations on the right
  3813. hand side work as well, because
  3814. "not working expressions" is better than "half working
  3815. expressions" }
  3816. {factornode:=p1;
  3817. goto SubExprStart;}
  3818. end
  3819. else
  3820. begin
  3821. { this is a normal "<" comparison }
  3822. { potential generic types that are followed by a "<": }
  3823. { a) might not have their resultdef set }
  3824. if not assigned(p1.resultdef) then
  3825. do_typecheckpass(p1);
  3826. { b) are not checked whether they are an undefined def,
  3827. but not a generic parameter }
  3828. if (p1.nodetype=typen) and
  3829. (ttypenode(p1).typedef.typ=undefineddef) and
  3830. assigned(ttypenode(p1).typedef.typesym) and
  3831. not (sp_generic_para in ttypenode(p1).typedef.typesym.symoptions) then
  3832. begin
  3833. identifier_not_found(ttypenode(p1).typedef.typesym.RealName);
  3834. p1.Free;
  3835. p1:=cerrornode.create;
  3836. end;
  3837. { c) don't have their hints checked }
  3838. if istypenode(p1) then
  3839. begin
  3840. gendef:=gettypedef(p1);
  3841. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3842. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3843. end;
  3844. { Note: the second part of the expression will be needed
  3845. for nested specializations }
  3846. if istypenode(p2) {and
  3847. not (token in [_LT, _LSHARPBRACKET])} then
  3848. begin
  3849. gendef:=gettypedef(p2);
  3850. if gendef.typ in [objectdef,recorddef,arraydef,procvardef] then
  3851. check_hints(gendef.typesym,gendef.typesym.symoptions,gendef.typesym.deprecatedmsg);
  3852. end;
  3853. { create the comparison node for "<" }
  3854. p1:=caddnode.create(ltn,p1,p2)
  3855. end;
  3856. end;
  3857. _GTE :
  3858. p1:=caddnode.create(gten,p1,p2);
  3859. _LTE :
  3860. p1:=caddnode.create(lten,p1,p2);
  3861. _SYMDIF :
  3862. p1:=caddnode.create(symdifn,p1,p2);
  3863. _STARSTAR :
  3864. p1:=caddnode.create(starstarn,p1,p2);
  3865. _OP_AS,
  3866. _OP_IS :
  3867. begin
  3868. if (m_delphi in current_settings.modeswitches) and
  3869. (token in [_LT, _LSHARPBRACKET]) and
  3870. getgenericsym(p2,gensym) then
  3871. begin
  3872. { for now we're handling this as a generic declaration;
  3873. there could be cases though (because of operator
  3874. overloading) where this is the wrong decision... }
  3875. { TODO : here the same note as in _LT applies as p2 could
  3876. point to a variable, etc }
  3877. gendef:=gettypedef(p2);
  3878. ptmp:=generate_inline_specialization(gendef,p2,filepos,nil,nil,nil);
  3879. { we don't need the old p2 anymore }
  3880. p2.Free;
  3881. p2:=ptmp;
  3882. { here we don't need to call back down to "factor", thus
  3883. no "goto" }
  3884. end;
  3885. { now generate the "is" or "as" node }
  3886. case oldt of
  3887. _OP_AS:
  3888. p1:=casnode.create(p1,p2);
  3889. _OP_IS:
  3890. p1:=cisnode.create(p1,p2);
  3891. end;
  3892. end;
  3893. _OP_IN :
  3894. p1:=cinnode.create(p1,p2);
  3895. _OP_OR,
  3896. _PIPE {macpas only} :
  3897. begin
  3898. p1:=caddnode.create(orn,p1,p2);
  3899. if (oldt = _PIPE) then
  3900. include(p1.flags,nf_short_bool);
  3901. end;
  3902. _OP_AND,
  3903. _AMPERSAND {macpas only} :
  3904. begin
  3905. p1:=caddnode.create(andn,p1,p2);
  3906. if (oldt = _AMPERSAND) then
  3907. include(p1.flags,nf_short_bool);
  3908. end;
  3909. _OP_DIV :
  3910. p1:=cmoddivnode.create(divn,p1,p2);
  3911. _OP_NOT :
  3912. p1:=cnotnode.create(p1);
  3913. _OP_MOD :
  3914. begin
  3915. p1:=cmoddivnode.create(modn,p1,p2);
  3916. if m_iso in current_settings.modeswitches then
  3917. include(p1.flags,nf_isomod);
  3918. end;
  3919. _OP_SHL :
  3920. p1:=cshlshrnode.create(shln,p1,p2);
  3921. _OP_SHR :
  3922. p1:=cshlshrnode.create(shrn,p1,p2);
  3923. _OP_XOR :
  3924. p1:=caddnode.create(xorn,p1,p2);
  3925. _ASSIGNMENT :
  3926. p1:=cassignmentnode.create(p1,p2);
  3927. _NE :
  3928. p1:=caddnode.create(unequaln,p1,p2);
  3929. end;
  3930. p1.fileinfo:=filepos;
  3931. end
  3932. else
  3933. break;
  3934. until false;
  3935. sub_expr:=p1;
  3936. end;
  3937. function comp_expr(accept_equal,typeonly:boolean):tnode;
  3938. var
  3939. oldafterassignment : boolean;
  3940. p1 : tnode;
  3941. begin
  3942. oldafterassignment:=afterassignment;
  3943. afterassignment:=true;
  3944. p1:=sub_expr(opcompare,accept_equal,typeonly,nil);
  3945. { get the resultdef for this expression }
  3946. if not assigned(p1.resultdef) then
  3947. do_typecheckpass(p1);
  3948. afterassignment:=oldafterassignment;
  3949. comp_expr:=p1;
  3950. end;
  3951. function expr(dotypecheck : boolean) : tnode;
  3952. var
  3953. p1,p2 : tnode;
  3954. filepos : tfileposinfo;
  3955. oldafterassignment,
  3956. updatefpos : boolean;
  3957. begin
  3958. oldafterassignment:=afterassignment;
  3959. p1:=sub_expr(opcompare,true,false,nil);
  3960. { get the resultdef for this expression }
  3961. if not assigned(p1.resultdef) and
  3962. dotypecheck then
  3963. do_typecheckpass(p1);
  3964. filepos:=current_tokenpos;
  3965. if token in [_ASSIGNMENT,_PLUSASN,_MINUSASN,_STARASN,_SLASHASN] then
  3966. afterassignment:=true;
  3967. updatefpos:=true;
  3968. case token of
  3969. _POINTPOINT :
  3970. begin
  3971. consume(_POINTPOINT);
  3972. p2:=sub_expr(opcompare,true,false,nil);
  3973. p1:=crangenode.create(p1,p2);
  3974. end;
  3975. _ASSIGNMENT :
  3976. begin
  3977. consume(_ASSIGNMENT);
  3978. if assigned(p1.resultdef) and (p1.resultdef.typ=procvardef) then
  3979. getprocvardef:=tprocvardef(p1.resultdef);
  3980. p2:=sub_expr(opcompare,true,false,nil);
  3981. if assigned(getprocvardef) then
  3982. handle_procvar(getprocvardef,p2);
  3983. getprocvardef:=nil;
  3984. p1:=cassignmentnode.create(p1,p2);
  3985. end;
  3986. _PLUSASN :
  3987. begin
  3988. consume(_PLUSASN);
  3989. p2:=sub_expr(opcompare,true,false,nil);
  3990. p1:=gen_c_style_operator(addn,p1,p2);
  3991. end;
  3992. _MINUSASN :
  3993. begin
  3994. consume(_MINUSASN);
  3995. p2:=sub_expr(opcompare,true,false,nil);
  3996. p1:=gen_c_style_operator(subn,p1,p2);
  3997. end;
  3998. _STARASN :
  3999. begin
  4000. consume(_STARASN );
  4001. p2:=sub_expr(opcompare,true,false,nil);
  4002. p1:=gen_c_style_operator(muln,p1,p2);
  4003. end;
  4004. _SLASHASN :
  4005. begin
  4006. consume(_SLASHASN );
  4007. p2:=sub_expr(opcompare,true,false,nil);
  4008. p1:=gen_c_style_operator(slashn,p1,p2);
  4009. end;
  4010. else
  4011. updatefpos:=false;
  4012. end;
  4013. { get the resultdef for this expression }
  4014. if not assigned(p1.resultdef) and
  4015. dotypecheck then
  4016. do_typecheckpass(p1);
  4017. afterassignment:=oldafterassignment;
  4018. if updatefpos then
  4019. p1.fileinfo:=filepos;
  4020. expr:=p1;
  4021. end;
  4022. function get_intconst:TConstExprInt;
  4023. {Reads an expression, tries to evalute it and check if it is an integer
  4024. constant. Then the constant is returned.}
  4025. var
  4026. p:tnode;
  4027. begin
  4028. result:=0;
  4029. p:=comp_expr(true,false);
  4030. if not codegenerror then
  4031. begin
  4032. if (p.nodetype<>ordconstn) or
  4033. not(is_integer(p.resultdef)) then
  4034. Message(parser_e_illegal_expression)
  4035. else
  4036. result:=tordconstnode(p).value;
  4037. end;
  4038. p.free;
  4039. end;
  4040. function get_stringconst:string;
  4041. {Reads an expression, tries to evaluate it and checks if it is a string
  4042. constant. Then the constant is returned.}
  4043. var
  4044. p:tnode;
  4045. begin
  4046. get_stringconst:='';
  4047. p:=comp_expr(true,false);
  4048. if p.nodetype<>stringconstn then
  4049. begin
  4050. if (p.nodetype=ordconstn) and is_char(p.resultdef) then
  4051. get_stringconst:=char(int64(tordconstnode(p).value))
  4052. else
  4053. Message(parser_e_illegal_expression);
  4054. end
  4055. else
  4056. get_stringconst:=strpas(tstringconstnode(p).value_str);
  4057. p.free;
  4058. end;
  4059. end.