scanner.pas 203 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  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 scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. verbose,comphook,
  24. finput,
  25. widestr;
  26. const
  27. max_include_nesting=32;
  28. max_macro_nesting=16;
  29. preprocbufsize=32*1024;
  30. { when parsing an internally generated macro, if an identifier is
  31. prefixed with this constant then it will always be interpreted as a
  32. unit name (to avoid clashes with user-specified parameter or field
  33. names duplicated in internally generated code) }
  34. internal_macro_escape_unit_namespace_name = #1;
  35. internal_macro_escape_begin = internal_macro_escape_unit_namespace_name;
  36. internal_macro_escape_end = internal_macro_escape_unit_namespace_name;
  37. type
  38. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  39. tscannerfile = class;
  40. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  41. tpreprocstack = class
  42. typ,
  43. { stores the preproctyp of the last (else)if(ndef) directive
  44. so we can check properly for ifend when legacyifend is on }
  45. iftyp : preproctyp;
  46. accept : boolean;
  47. next : tpreprocstack;
  48. name : TIDString;
  49. line_nb : longint;
  50. fileindex : longint;
  51. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  52. end;
  53. tdirectiveproc=procedure;
  54. tdirectiveitem = class(TFPHashObject)
  55. public
  56. is_conditional : boolean;
  57. proc : tdirectiveproc;
  58. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  59. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  60. end;
  61. // stack for replay buffers
  62. treplaystack = class
  63. token : ttoken;
  64. idtoken : ttoken;
  65. orgpattern,
  66. pattern : string;
  67. cstringpattern: ansistring;
  68. patternw : pcompilerwidestring;
  69. settings : tsettings;
  70. tokenbuf : tdynamicarray;
  71. tokenbuf_needs_swapping : boolean;
  72. next : treplaystack;
  73. constructor Create(atoken: ttoken;aidtoken:ttoken;
  74. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  75. apatternw:pcompilerwidestring;asettings:tsettings;
  76. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  77. destructor destroy;override;
  78. end;
  79. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  80. tspecialgenerictoken =
  81. (ST_LOADSETTINGS,
  82. ST_LINE,
  83. ST_COLUMN,
  84. ST_FILEINDEX,
  85. ST_LOADMESSAGES);
  86. { tscannerfile }
  87. tscannerfile = class
  88. private
  89. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  90. procedure cachenexttokenpos;
  91. procedure setnexttoken;
  92. procedure savetokenpos;
  93. procedure restoretokenpos;
  94. procedure writetoken(t: ttoken);
  95. function readtoken : ttoken;
  96. public
  97. inputfile : tinputfile; { current inputfile list }
  98. inputfilecount : longint;
  99. inputbuffer, { input buffer }
  100. inputpointer : pchar;
  101. inputstart : longint;
  102. line_no, { line }
  103. lastlinepos : longint;
  104. lasttokenpos,
  105. nexttokenpos : longint; { token }
  106. lasttoken,
  107. nexttoken : ttoken;
  108. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  109. oldcurrent_filepos,
  110. oldcurrent_tokenpos : tfileposinfo;
  111. replaytokenbuf,
  112. recordtokenbuf : tdynamicarray;
  113. { last settings we stored }
  114. last_settings : tsettings;
  115. last_message : pmessagestaterecord;
  116. { last filepos we stored }
  117. last_filepos,
  118. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  119. next_filepos : tfileposinfo;
  120. { current macro nesting depth }
  121. macro_nesting_depth,
  122. comment_level,
  123. yylexcount : longint;
  124. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  125. preprocstack : tpreprocstack;
  126. replaystack : treplaystack;
  127. preproc_pattern : string;
  128. preproc_token : ttoken;
  129. { true, if we are parsing preprocessor expressions }
  130. in_preproc_comp_expr : boolean;
  131. { true if tokens must be converted to opposite endianess}
  132. change_endian_for_replay : boolean;
  133. constructor Create(const fn:string; is_macro: boolean = false);
  134. destructor Destroy;override;
  135. { File buffer things }
  136. function openinputfile:boolean;
  137. procedure closeinputfile;
  138. function tempopeninputfile:boolean;
  139. procedure tempcloseinputfile;
  140. procedure saveinputfile;
  141. procedure restoreinputfile;
  142. procedure firstfile;
  143. procedure nextfile;
  144. procedure addfile(hp:tinputfile);
  145. procedure reload;
  146. { replaces current token with the text in p }
  147. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  148. { Scanner things }
  149. procedure gettokenpos;
  150. procedure inc_comment_level;
  151. procedure dec_comment_level;
  152. procedure illegal_char(c:char);
  153. procedure end_of_file;
  154. procedure checkpreprocstack;
  155. procedure poppreprocstack;
  156. procedure ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  157. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  158. procedure elsepreprocstack;
  159. procedure popreplaystack;
  160. function replay_stack_depth:longint;
  161. procedure handleconditional(p:tdirectiveitem);
  162. procedure handledirectives;
  163. procedure linebreak;
  164. procedure recordtoken;
  165. procedure startrecordtokens(buf:tdynamicarray);
  166. procedure stoprecordtokens;
  167. function is_recording_tokens:boolean;
  168. procedure replaytoken;
  169. procedure startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  170. { bit length asizeint is target depend }
  171. procedure tokenwritesizeint(val : asizeint);
  172. procedure tokenwritelongint(val : longint);
  173. procedure tokenwritelongword(val : longword);
  174. procedure tokenwriteword(val : word);
  175. procedure tokenwriteshortint(val : shortint);
  176. procedure tokenwriteset(var b;size : longint);
  177. procedure tokenwriteenum(var b;size : longint);
  178. function tokenreadsizeint : asizeint;
  179. procedure tokenwritesettings(var asettings : tsettings; var size : asizeint);
  180. { longword/longint are 32 bits on all targets }
  181. { word/smallint are 16-bits on all targest }
  182. function tokenreadlongword : longword;
  183. function tokenreadword : word;
  184. function tokenreadlongint : longint;
  185. function tokenreadsmallint : smallint;
  186. { short int is one a signed byte }
  187. function tokenreadshortint : shortint;
  188. function tokenreadbyte : byte;
  189. { This one takes the set size as an parameter }
  190. procedure tokenreadset(var b;size : longint);
  191. function tokenreadenum(size : longint) : longword;
  192. procedure tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  193. procedure readchar;
  194. procedure readstring;
  195. procedure readnumber;
  196. function readid:string;
  197. function readval:longint;
  198. function readcomment(include_special_char: boolean = false):string;
  199. function readquotedstring:string;
  200. function readstate:char;
  201. function readoptionalstate(fallback:char):char;
  202. function readstatedefault:char;
  203. procedure skipspace;
  204. procedure skipuntildirective;
  205. procedure skipcomment(read_first_char:boolean);
  206. procedure skipdelphicomment;
  207. procedure skipoldtpcomment(read_first_char:boolean);
  208. procedure readtoken(allowrecordtoken:boolean);
  209. function readpreproc:ttoken;
  210. function readpreprocint(var value:int64;const place:string):boolean;
  211. function asmgetchar:char;
  212. end;
  213. {$ifdef PREPROCWRITE}
  214. tpreprocfile=class
  215. f : text;
  216. buf : pointer;
  217. spacefound,
  218. eolfound : boolean;
  219. constructor create(const fn:string);
  220. destructor destroy; override;
  221. procedure Add(const s:string);
  222. procedure AddSpace;
  223. end;
  224. {$endif PREPROCWRITE}
  225. var
  226. { read strings }
  227. c : char;
  228. orgpattern,
  229. pattern : string;
  230. cstringpattern : ansistring;
  231. patternw : pcompilerwidestring;
  232. { token }
  233. token, { current token being parsed }
  234. idtoken : ttoken; { holds the token if the pattern is a known word }
  235. current_scanner : tscannerfile; { current scanner in use }
  236. current_commentstyle : tcommentstyle; { needed to use read_comment from directives }
  237. {$ifdef PREPROCWRITE}
  238. preprocfile : tpreprocfile; { used with only preprocessing }
  239. {$endif PREPROCWRITE}
  240. type
  241. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  242. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  243. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  244. procedure InitScanner;
  245. procedure DoneScanner;
  246. { To be called when the language mode is finally determined }
  247. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  248. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  249. procedure SetAppType(NewAppType:tapptype);
  250. implementation
  251. uses
  252. SysUtils,
  253. cutils,cfileutl,
  254. systems,
  255. switches,
  256. symbase,symtable,symtype,symsym,symconst,symdef,defutil,
  257. { This is needed for tcputype }
  258. cpuinfo,
  259. fmodule,fppu,
  260. { this is needed for $I %CURRENTROUTINE%}
  261. procinfo;
  262. var
  263. { dictionaries with the supported directives }
  264. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  265. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  266. {*****************************************************************************
  267. Helper routines
  268. *****************************************************************************}
  269. const
  270. { use any special name that is an invalid file name to avoid problems }
  271. preprocstring : array [preproctyp] of string[7]
  272. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  273. function is_keyword(const s:string):boolean;
  274. var
  275. low,high,mid : longint;
  276. begin
  277. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  278. not (s[1] in ['a'..'z','A'..'Z']) then
  279. begin
  280. is_keyword:=false;
  281. exit;
  282. end;
  283. low:=ord(tokenidx^[length(s),s[1]].first);
  284. high:=ord(tokenidx^[length(s),s[1]].last);
  285. while low<high do
  286. begin
  287. mid:=(high+low+1) shr 1;
  288. if pattern<tokeninfo^[ttoken(mid)].str then
  289. high:=mid-1
  290. else
  291. low:=mid;
  292. end;
  293. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  294. ((tokeninfo^[ttoken(high)].keyword*current_settings.modeswitches)<>[]);
  295. end;
  296. Procedure HandleModeSwitches(switch: tmodeswitch; changeInit: boolean);
  297. begin
  298. { turn ansi/unicodestrings on by default ? (only change when this
  299. particular setting is changed, so that a random modeswitch won't
  300. change the state of $h+/$h-) }
  301. if switch in [m_none,m_default_ansistring,m_default_unicodestring] then
  302. begin
  303. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  304. begin
  305. { can't have both ansistring and unicodestring as default }
  306. if switch=m_default_ansistring then
  307. begin
  308. exclude(current_settings.modeswitches,m_default_unicodestring);
  309. if changeinit then
  310. exclude(init_settings.modeswitches,m_default_unicodestring);
  311. end
  312. else if switch=m_default_unicodestring then
  313. begin
  314. exclude(current_settings.modeswitches,m_default_ansistring);
  315. if changeinit then
  316. exclude(init_settings.modeswitches,m_default_ansistring);
  317. end;
  318. { enable $h+ }
  319. include(current_settings.localswitches,cs_refcountedstrings);
  320. if changeinit then
  321. include(init_settings.localswitches,cs_refcountedstrings);
  322. if m_default_unicodestring in current_settings.modeswitches then
  323. begin
  324. def_system_macro('FPC_UNICODESTRINGS');
  325. def_system_macro('UNICODE');
  326. end;
  327. end
  328. else
  329. begin
  330. exclude(current_settings.localswitches,cs_refcountedstrings);
  331. if changeinit then
  332. exclude(init_settings.localswitches,cs_refcountedstrings);
  333. undef_system_macro('FPC_UNICODESTRINGS');
  334. undef_system_macro('UNICODE');
  335. end;
  336. end;
  337. { turn inline on by default ? }
  338. if switch in [m_none,m_default_inline] then
  339. begin
  340. if (m_default_inline in current_settings.modeswitches) then
  341. begin
  342. include(current_settings.localswitches,cs_do_inline);
  343. if changeinit then
  344. include(init_settings.localswitches,cs_do_inline);
  345. end
  346. else
  347. begin
  348. exclude(current_settings.localswitches,cs_do_inline);
  349. if changeinit then
  350. exclude(init_settings.localswitches,cs_do_inline);
  351. end;
  352. end;
  353. { turn on system codepage by default }
  354. if switch in [m_none,m_systemcodepage] then
  355. begin
  356. { both m_systemcodepage and specifying a code page via -FcXXX or
  357. "$codepage XXX" change current_settings.sourcecodepage. If
  358. we used -FcXXX and then have a sourcefile with "$mode objfpc",
  359. this routine will be called to disable m_systemcodepage (to ensure
  360. it's off in case it would have been set on the command line, or
  361. by a previous mode(switch).
  362. In that case, we have to ensure that we don't overwrite
  363. current_settings.sourcecodepage, as that would cancel out the
  364. -FcXXX. This is why we use two separate module switches
  365. (cs_explicit_codepage and cs_system_codepage) for the same setting
  366. (current_settings.sourcecodepage)
  367. }
  368. if m_systemcodepage in current_settings.modeswitches then
  369. begin
  370. { m_systemcodepage gets enabled -> disable any -FcXXX and
  371. "codepage XXX" settings (exclude cs_explicit_codepage), and
  372. overwrite the sourcecode page }
  373. current_settings.sourcecodepage:=DefaultSystemCodePage;
  374. if (current_settings.sourcecodepage<>CP_UTF8) and not cpavailable(current_settings.sourcecodepage) then
  375. begin
  376. Message2(scan_w_unavailable_system_codepage,IntToStr(current_settings.sourcecodepage),IntToStr(default_settings.sourcecodepage));
  377. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  378. end;
  379. exclude(current_settings.moduleswitches,cs_explicit_codepage);
  380. include(current_settings.moduleswitches,cs_system_codepage);
  381. if changeinit then
  382. begin
  383. init_settings.sourcecodepage:=current_settings.sourcecodepage;
  384. exclude(init_settings.moduleswitches,cs_explicit_codepage);
  385. include(init_settings.moduleswitches,cs_system_codepage);
  386. end;
  387. end
  388. else
  389. begin
  390. { m_systemcodepage gets disabled -> reset sourcecodepage only if
  391. cs_explicit_codepage is not set (it may be set in the scenario
  392. where -FcXXX was passed on the command line and then "$mode
  393. fpc" is used, because then the caller of this routine will
  394. set the "$mode fpc" modeswitches (which don't include
  395. m_systemcodepage) and call this routine with m_none).
  396. Or it can happen if -FcXXX was passed, and the sourcefile
  397. contains "$modeswitch systemcodepage-" statement.
  398. Since we unset cs_system_codepage if m_systemcodepage gets
  399. activated, we will revert to the default code page if you
  400. set a source file code page, then enable the systemcode page
  401. and finally disable it again. We don't keep a stack of
  402. settings, by design. The only thing we have to ensure is that
  403. disabling m_systemcodepage if it wasn't on in the first place
  404. doesn't overwrite the sourcecodepage }
  405. exclude(current_settings.moduleswitches,cs_system_codepage);
  406. if not(cs_explicit_codepage in current_settings.moduleswitches) then
  407. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  408. if changeinit then
  409. begin
  410. exclude(init_settings.moduleswitches,cs_system_codepage);
  411. if not(cs_explicit_codepage in init_settings.moduleswitches) then
  412. init_settings.sourcecodepage:=default_settings.sourcecodepage;
  413. end;
  414. end;
  415. end;
  416. {$ifdef i8086}
  417. { enable cs_force_far_calls when m_nested_procvars is enabled }
  418. if switch=m_nested_procvars then
  419. begin
  420. include(current_settings.localswitches,cs_force_far_calls);
  421. if changeinit then
  422. include(init_settings.localswitches,cs_force_far_calls);
  423. end;
  424. {$endif i8086}
  425. end;
  426. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  427. var
  428. b : boolean;
  429. oldmodeswitches : tmodeswitches;
  430. begin
  431. oldmodeswitches:=current_settings.modeswitches;
  432. b:=true;
  433. if s='DEFAULT' then
  434. current_settings.modeswitches:=fpcmodeswitches
  435. else
  436. if s='DELPHI' then
  437. current_settings.modeswitches:=delphimodeswitches
  438. else
  439. if s='DELPHIUNICODE' then
  440. current_settings.modeswitches:=delphiunicodemodeswitches
  441. else
  442. if s='TP' then
  443. current_settings.modeswitches:=tpmodeswitches
  444. else
  445. if s='FPC' then begin
  446. current_settings.modeswitches:=fpcmodeswitches;
  447. { TODO: enable this for 2.3/2.9 }
  448. // include(current_settings.localswitches, cs_typed_addresses);
  449. end else
  450. if s='OBJFPC' then begin
  451. current_settings.modeswitches:=objfpcmodeswitches;
  452. { TODO: enable this for 2.3/2.9 }
  453. // include(current_settings.localswitches, cs_typed_addresses);
  454. end
  455. {$ifdef gpc_mode}
  456. else if s='GPC' then
  457. current_settings.modeswitches:=gpcmodeswitches
  458. {$endif}
  459. else
  460. if s='MACPAS' then
  461. current_settings.modeswitches:=macmodeswitches
  462. else
  463. if s='ISO' then
  464. current_settings.modeswitches:=isomodeswitches
  465. else
  466. if s='EXTENDEDPASCAL' then
  467. current_settings.modeswitches:=extpasmodeswitches
  468. else
  469. b:=false;
  470. {$ifdef jvm}
  471. { enable final fields by default for the JVM targets }
  472. include(current_settings.modeswitches,m_final_fields);
  473. {$endif jvm}
  474. if b and changeInit then
  475. init_settings.modeswitches := current_settings.modeswitches;
  476. if b then
  477. begin
  478. { resolve all postponed switch changes }
  479. flushpendingswitchesstate;
  480. HandleModeSwitches(m_none,changeinit);
  481. { turn on bitpacking and case checking for mode macpas and iso pascal,
  482. as well as extended pascal }
  483. if ([m_mac,m_iso,m_extpas] * current_settings.modeswitches <> []) then
  484. begin
  485. include(current_settings.localswitches,cs_bitpacking);
  486. include(current_settings.localswitches,cs_check_all_case_coverage);
  487. if changeinit then
  488. begin
  489. include(init_settings.localswitches,cs_bitpacking);
  490. include(init_settings.localswitches,cs_check_all_case_coverage);
  491. end;
  492. end;
  493. { support goto/label by default in delphi/tp7/mac/iso/extpas modes }
  494. if ([m_delphi,m_tp7,m_mac,m_iso,m_extpas] * current_settings.modeswitches <> []) then
  495. begin
  496. include(current_settings.moduleswitches,cs_support_goto);
  497. if changeinit then
  498. include(init_settings.moduleswitches,cs_support_goto);
  499. end;
  500. { support pointer math by default in fpc/objfpc modes }
  501. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  502. begin
  503. include(current_settings.localswitches,cs_pointermath);
  504. if changeinit then
  505. include(init_settings.localswitches,cs_pointermath);
  506. end
  507. else
  508. begin
  509. exclude(current_settings.localswitches,cs_pointermath);
  510. if changeinit then
  511. exclude(init_settings.localswitches,cs_pointermath);
  512. end;
  513. { Default enum and set packing for delphi/tp7 }
  514. if (m_tp7 in current_settings.modeswitches) or
  515. (m_delphi in current_settings.modeswitches) then
  516. begin
  517. current_settings.packenum:=1;
  518. current_settings.setalloc:=1;
  519. end
  520. else if (m_mac in current_settings.modeswitches) then
  521. { compatible with Metrowerks Pascal }
  522. current_settings.packenum:=2
  523. else
  524. current_settings.packenum:=4;
  525. if changeinit then
  526. begin
  527. init_settings.packenum:=current_settings.packenum;
  528. init_settings.setalloc:=current_settings.setalloc;
  529. end;
  530. {$if defined(i386) or defined(i8086)}
  531. { Default to intel assembler for delphi/tp7 on i386/i8086 }
  532. if (m_delphi in current_settings.modeswitches) or
  533. (m_tp7 in current_settings.modeswitches) then
  534. begin
  535. {$ifdef i8086}
  536. current_settings.asmmode:=asmmode_i8086_intel;
  537. {$else i8086}
  538. current_settings.asmmode:=asmmode_i386_intel;
  539. {$endif i8086}
  540. if changeinit then
  541. init_settings.asmmode:=current_settings.asmmode;
  542. end;
  543. {$endif i386 or i8086}
  544. { Exception support explicitly turned on (mainly for macpas, to }
  545. { compensate for lack of interprocedural goto support) }
  546. if (cs_support_exceptions in current_settings.globalswitches) then
  547. include(current_settings.modeswitches,m_except);
  548. { Default strict string var checking in TP/Delphi modes }
  549. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  550. begin
  551. include(current_settings.localswitches,cs_strict_var_strings);
  552. if changeinit then
  553. include(init_settings.localswitches,cs_strict_var_strings);
  554. end;
  555. { in delphi mode, excess precision and open strings are by default on }
  556. if ([m_delphi] * current_settings.modeswitches <> []) then
  557. begin
  558. include(current_settings.localswitches,cs_excessprecision);
  559. include(current_settings.localswitches,cs_openstring);
  560. if changeinit then
  561. begin
  562. include(init_settings.localswitches,cs_excessprecision);
  563. include(init_settings.localswitches,cs_openstring);
  564. end;
  565. end;
  566. {$ifdef i8086}
  567. { Do not force far calls in the TP mode by default, force it in other modes }
  568. if (m_tp7 in current_settings.modeswitches) then
  569. begin
  570. exclude(current_settings.localswitches,cs_force_far_calls);
  571. if changeinit then
  572. exclude(init_settings.localswitches,cs_force_far_calls);
  573. end
  574. else
  575. begin
  576. include(current_settings.localswitches,cs_force_far_calls);
  577. if changeinit then
  578. include(init_settings.localswitches,cs_force_far_calls);
  579. end;
  580. {$endif i8086}
  581. { Undefine old symbol }
  582. if (m_delphi in oldmodeswitches) then
  583. undef_system_macro('FPC_DELPHI')
  584. else if (m_tp7 in oldmodeswitches) then
  585. undef_system_macro('FPC_TP')
  586. else if (m_objfpc in oldmodeswitches) then
  587. undef_system_macro('FPC_OBJFPC')
  588. {$ifdef gpc_mode}
  589. else if (m_gpc in oldmodeswitches) then
  590. undef_system_macro('FPC_GPC')
  591. {$endif}
  592. else if (m_mac in oldmodeswitches) then
  593. undef_system_macro('FPC_MACPAS')
  594. else if (m_iso in oldmodeswitches) then
  595. undef_system_macro('FPC_ISO')
  596. else if (m_extpas in oldmodeswitches) then
  597. undef_system_macro('FPC_EXTENDEDPASCAL');
  598. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  599. if (m_delphi in current_settings.modeswitches) then
  600. def_system_macro('FPC_DELPHI')
  601. else if (m_tp7 in current_settings.modeswitches) then
  602. def_system_macro('FPC_TP')
  603. else if (m_objfpc in current_settings.modeswitches) then
  604. def_system_macro('FPC_OBJFPC')
  605. {$ifdef gpc_mode}
  606. else if (m_gpc in current_settings.modeswitches) then
  607. def_system_macro('FPC_GPC')
  608. {$endif}
  609. else if (m_mac in current_settings.modeswitches) then
  610. def_system_macro('FPC_MACPAS')
  611. else if (m_iso in current_settings.modeswitches) then
  612. def_system_macro('FPC_ISO')
  613. else if (m_extpas in current_settings.modeswitches) then
  614. def_system_macro('FPC_EXTENDEDPASCAL');
  615. end;
  616. SetCompileMode:=b;
  617. end;
  618. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  619. var
  620. i : tmodeswitch;
  621. doinclude : boolean;
  622. begin
  623. s:=upper(s);
  624. { on/off? }
  625. doinclude:=true;
  626. case s[length(s)] of
  627. '+':
  628. s:=copy(s,1,length(s)-1);
  629. '-':
  630. begin
  631. s:=copy(s,1,length(s)-1);
  632. doinclude:=false;
  633. end;
  634. end;
  635. Result:=false;
  636. for i:=m_class to high(tmodeswitch) do
  637. if s=modeswitchstr[i] then
  638. begin
  639. { Objective-C is currently only supported for Darwin targets }
  640. if doinclude and
  641. (i in [m_objectivec1,m_objectivec2]) and
  642. not(target_info.system in systems_objc_supported) then
  643. begin
  644. Message1(option_unsupported_target_for_feature,'Objective-C');
  645. break;
  646. end;
  647. { Blocks supported? }
  648. if doinclude and
  649. (i = m_blocks) and
  650. not(target_info.system in systems_blocks_supported) then
  651. begin
  652. Message1(option_unsupported_target_for_feature,'Blocks');
  653. break;
  654. end;
  655. if changeInit then
  656. current_settings.modeswitches:=init_settings.modeswitches;
  657. Result:=true;
  658. if doinclude then
  659. begin
  660. include(current_settings.modeswitches,i);
  661. { Objective-C 2.0 support implies 1.0 support }
  662. if (i=m_objectivec2) then
  663. include(current_settings.modeswitches,m_objectivec1);
  664. if (i in [m_objectivec1,m_objectivec2]) then
  665. include(current_settings.modeswitches,m_class);
  666. end
  667. else
  668. begin
  669. exclude(current_settings.modeswitches,i);
  670. { Objective-C 2.0 support implies 1.0 support }
  671. if (i=m_objectivec2) then
  672. exclude(current_settings.modeswitches,m_objectivec1);
  673. if (i in [m_objectivec1,m_objectivec2]) and
  674. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  675. exclude(current_settings.modeswitches,m_class);
  676. end;
  677. { set other switches depending on changed mode switch }
  678. HandleModeSwitches(i,changeinit);
  679. if changeInit then
  680. init_settings.modeswitches:=current_settings.modeswitches;
  681. break;
  682. end;
  683. end;
  684. procedure SetAppType(NewAppType:tapptype);
  685. begin
  686. {$ifdef i8086}
  687. if (target_info.system in [system_i8086_msdos,system_i8086_embedded]) and (apptype<>NewAppType) then
  688. begin
  689. if NewAppType=app_com then
  690. begin
  691. targetinfos[target_info.system]^.exeext:='.com';
  692. target_info.exeext:='.com';
  693. end
  694. else
  695. begin
  696. targetinfos[target_info.system]^.exeext:='.exe';
  697. target_info.exeext:='.exe';
  698. end;
  699. end;
  700. {$endif i8086}
  701. {$ifdef m68k}
  702. if target_info.system in [system_m68k_atari] then
  703. case NewAppType of
  704. app_cui:
  705. begin
  706. targetinfos[target_info.system]^.exeext:='.ttp';
  707. target_info.exeext:='.ttp';
  708. end;
  709. app_gui:
  710. begin
  711. targetinfos[target_info.system]^.exeext:='.prg';
  712. target_info.exeext:='.prg';
  713. end;
  714. else
  715. ;
  716. end;
  717. {$endif m68k}
  718. if apptype in [app_cui,app_com] then
  719. undef_system_macro('CONSOLE');
  720. apptype:=NewAppType;
  721. if apptype in [app_cui,app_com] then
  722. def_system_macro('CONSOLE');
  723. end;
  724. {*****************************************************************************
  725. Conditional Directives
  726. *****************************************************************************}
  727. procedure dir_else;
  728. begin
  729. current_scanner.elsepreprocstack;
  730. end;
  731. procedure dir_endif;
  732. begin
  733. if (cs_legacyifend in current_settings.localswitches) and
  734. (current_scanner.preprocstack.typ<>pp_ifdef) and (current_scanner.preprocstack.typ<>pp_ifndef) and
  735. not((current_scanner.preprocstack.typ=pp_else) and (current_scanner.preprocstack.iftyp in [pp_ifdef,pp_ifndef])) then
  736. Message(scan_e_unexpected_endif);
  737. current_scanner.poppreprocstack;
  738. end;
  739. procedure dir_ifend;
  740. begin
  741. if (cs_legacyifend in current_settings.localswitches) and
  742. (current_scanner.preprocstack.typ<>pp_elseif) and (current_scanner.preprocstack.typ<>pp_if) and
  743. not((current_scanner.preprocstack.typ=pp_else) and (current_scanner.preprocstack.iftyp in [pp_if,pp_elseif])) then
  744. Message(scan_e_unexpected_ifend);
  745. current_scanner.poppreprocstack;
  746. end;
  747. function isdef(var valuedescr: String): Boolean;
  748. var
  749. hs : string;
  750. begin
  751. current_scanner.skipspace;
  752. hs:=current_scanner.readid;
  753. valuedescr:= hs;
  754. if hs='' then
  755. Message(scan_e_error_in_preproc_expr);
  756. isdef:=defined_macro(hs);
  757. end;
  758. procedure dir_ifdef;
  759. begin
  760. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  761. end;
  762. function isnotdef(var valuedescr: String): Boolean;
  763. var
  764. hs : string;
  765. begin
  766. current_scanner.skipspace;
  767. hs:=current_scanner.readid;
  768. valuedescr:= hs;
  769. if hs='' then
  770. Message(scan_e_error_in_preproc_expr);
  771. isnotdef:=not defined_macro(hs);
  772. end;
  773. procedure dir_ifndef;
  774. begin
  775. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  776. end;
  777. function opt_check(var valuedescr: String): Boolean;
  778. var
  779. hs : string;
  780. state : char;
  781. begin
  782. opt_check:= false;
  783. current_scanner.skipspace;
  784. hs:=current_scanner.readid;
  785. valuedescr:= hs;
  786. if (length(hs)>1) then
  787. Message1(scan_w_illegal_switch,hs)
  788. else
  789. begin
  790. state:=current_scanner.ReadState;
  791. if state in ['-','+'] then
  792. opt_check:=CheckSwitch(hs[1],state)
  793. else
  794. Message(scan_e_error_in_preproc_expr);
  795. end;
  796. end;
  797. procedure dir_ifopt;
  798. begin
  799. flushpendingswitchesstate;
  800. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  801. end;
  802. procedure dir_libprefix;
  803. var
  804. s : string;
  805. begin
  806. current_scanner.skipspace;
  807. if c <> '''' then
  808. Message2(scan_f_syn_expected, '''', c);
  809. s := current_scanner.readquotedstring;
  810. stringdispose(outputprefix);
  811. outputprefix := stringdup(s);
  812. with current_module do
  813. setfilename(paramfn, paramallowoutput);
  814. end;
  815. procedure dir_libsuffix;
  816. var
  817. s : string;
  818. begin
  819. current_scanner.skipspace;
  820. if c <> '''' then
  821. Message2(scan_f_syn_expected, '''', c);
  822. s := current_scanner.readquotedstring;
  823. stringdispose(outputsuffix);
  824. outputsuffix := stringdup(s);
  825. with current_module do
  826. setfilename(paramfn, paramallowoutput);
  827. end;
  828. procedure dir_extension;
  829. var
  830. s : string;
  831. begin
  832. current_scanner.skipspace;
  833. if c <> '''' then
  834. Message2(scan_f_syn_expected, '''', c);
  835. s := current_scanner.readquotedstring;
  836. if OutputFileName='' then
  837. OutputFileName:=InputFileName;
  838. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  839. with current_module do
  840. setfilename(paramfn, paramallowoutput);
  841. end;
  842. {
  843. Compile time expression type check
  844. ----------------------------------
  845. Each subexpression returns its type to the caller, which then can
  846. do type check. Since data types of compile time expressions is
  847. not well defined, the type system does a best effort. The drawback is
  848. that some errors might not be detected.
  849. Instead of returning a particular data type, a set of possible data types
  850. are returned. This way ambigouos types can be handled. For instance a
  851. value of 1 can be both a boolean and and integer.
  852. Booleans
  853. --------
  854. The following forms of boolean values are supported:
  855. * C coded, that is 0 is false, non-zero is true.
  856. * TRUE/FALSE for mac style compile time variables
  857. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  858. When a compile time expression is evaluated, they are then translated
  859. to C coded booleans (0/1), to simplify for the expression evaluator.
  860. Note that this scheme then also of support mac compile time variables which
  861. are 0/1 but with a boolean meaning.
  862. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  863. means that units which is not recompiled, and thus stores
  864. compile time variables as the old format (0/1), continue to work.
  865. Short circuit evaluation
  866. ------------------------
  867. For this to work, the part of a compile time expression which is short
  868. circuited, should not be evaluated, while it still should be parsed.
  869. Therefor there is a parameter eval, telling whether evaluation is needed.
  870. In case not, the value returned can be arbitrary.
  871. }
  872. type
  873. { texprvalue }
  874. texprvalue = class
  875. private
  876. { we can't use built-in defs since they
  877. may be not created at the moment }
  878. class var
  879. sintdef,uintdef,booldef,strdef,setdef,realdef: tdef;
  880. class constructor createdefs;
  881. class destructor destroydefs;
  882. public
  883. consttyp: tconsttyp;
  884. value: tconstvalue;
  885. def: tdef;
  886. constructor create_const(c:tconstsym);
  887. constructor create_error;
  888. constructor create_ord(v: Tconstexprint);
  889. constructor create_int(v: int64);
  890. constructor create_uint(v: qword);
  891. constructor create_bool(b: boolean);
  892. constructor create_str(const s: string);
  893. constructor create_set(ns: tnormalset);
  894. constructor create_real(r: bestreal);
  895. class function try_parse_number(const s:string):texprvalue; static;
  896. class function try_parse_real(const s:string):texprvalue; static;
  897. function evaluate(v:texprvalue;op:ttoken):texprvalue;
  898. procedure error(expecteddef, place: string);
  899. function isBoolean: Boolean;
  900. function isInt: Boolean;
  901. function asBool: Boolean;
  902. function asInt: Integer;
  903. function asInt64: Int64;
  904. function asStr: String;
  905. destructor destroy; override;
  906. end;
  907. class constructor texprvalue.createdefs;
  908. begin
  909. { do not use corddef etc here: this code is executed before those
  910. variables are initialised. Since these types are only used for
  911. compile-time evaluation of conditional expressions, it doesn't matter
  912. that we use the base types instead of the cpu-specific ones. }
  913. sintdef:=torddef.create(s64bit,low(int64),high(int64),false);
  914. uintdef:=torddef.create(u64bit,low(qword),high(qword),false);
  915. booldef:=torddef.create(pasbool1,0,1,false);
  916. strdef:=tstringdef.createansi(0,false);
  917. setdef:=tsetdef.create(sintdef,0,255,false);
  918. realdef:=tfloatdef.create(s80real,false);
  919. end;
  920. class destructor texprvalue.destroydefs;
  921. begin
  922. setdef.free;
  923. sintdef.free;
  924. uintdef.free;
  925. booldef.free;
  926. strdef.free;
  927. realdef.free;
  928. end;
  929. constructor texprvalue.create_const(c: tconstsym);
  930. begin
  931. consttyp:=c.consttyp;
  932. def:=c.constdef;
  933. case consttyp of
  934. conststring,
  935. constresourcestring:
  936. begin
  937. value.len:=c.value.len;
  938. getmem(value.valueptr,value.len+1);
  939. move(c.value.valueptr^,value.valueptr^,value.len+1);
  940. end;
  941. constwstring:
  942. begin
  943. initwidestring(value.valueptr);
  944. copywidestring(c.value.valueptr,value.valueptr);
  945. end;
  946. constreal:
  947. begin
  948. new(pbestreal(value.valueptr));
  949. pbestreal(value.valueptr)^:=pbestreal(c.value.valueptr)^;
  950. end;
  951. constset:
  952. begin
  953. new(pnormalset(value.valueptr));
  954. pnormalset(value.valueptr)^:=pnormalset(c.value.valueptr)^;
  955. end;
  956. constguid:
  957. begin
  958. new(pguid(value.valueptr));
  959. pguid(value.valueptr)^:=pguid(c.value.valueptr)^;
  960. end;
  961. else
  962. value:=c.value;
  963. end;
  964. end;
  965. constructor texprvalue.create_error;
  966. begin
  967. fillchar(value,sizeof(value),#0);
  968. consttyp:=constnone;
  969. def:=generrordef;
  970. end;
  971. constructor texprvalue.create_ord(v: Tconstexprint);
  972. begin
  973. fillchar(value,sizeof(value),#0);
  974. consttyp:=constord;
  975. value.valueord:=v;
  976. if v.signed then
  977. def:=sintdef
  978. else
  979. def:=uintdef;
  980. end;
  981. constructor texprvalue.create_int(v: int64);
  982. begin
  983. fillchar(value,sizeof(value),#0);
  984. consttyp:=constord;
  985. value.valueord:=v;
  986. def:=sintdef;
  987. end;
  988. constructor texprvalue.create_uint(v: qword);
  989. begin
  990. fillchar(value,sizeof(value),#0);
  991. consttyp:=constord;
  992. value.valueord:=v;
  993. def:=uintdef;
  994. end;
  995. constructor texprvalue.create_bool(b: boolean);
  996. begin
  997. fillchar(value,sizeof(value),#0);
  998. consttyp:=constord;
  999. value.valueord:=ord(b);
  1000. def:=booldef;
  1001. end;
  1002. constructor texprvalue.create_str(const s: string);
  1003. var
  1004. sp: pansichar;
  1005. len: integer;
  1006. begin
  1007. fillchar(value,sizeof(value),#0);
  1008. consttyp:=conststring;
  1009. len:=length(s);
  1010. getmem(sp,len+1);
  1011. move(s[1],sp^,len+1);
  1012. value.valueptr:=sp;
  1013. value.len:=len;
  1014. def:=strdef;
  1015. end;
  1016. constructor texprvalue.create_set(ns: tnormalset);
  1017. begin
  1018. fillchar(value,sizeof(value),#0);
  1019. consttyp:=constset;
  1020. new(pnormalset(value.valueptr));
  1021. pnormalset(value.valueptr)^:=ns;
  1022. def:=setdef;
  1023. end;
  1024. constructor texprvalue.create_real(r: bestreal);
  1025. begin
  1026. fillchar(value,sizeof(value),#0);
  1027. consttyp:=constreal;
  1028. new(pbestreal(value.valueptr));
  1029. pbestreal(value.valueptr)^:=r;
  1030. def:=realdef;
  1031. end;
  1032. class function texprvalue.try_parse_number(const s:string):texprvalue;
  1033. var
  1034. ic: int64;
  1035. qc: qword;
  1036. code: integer;
  1037. begin
  1038. { try int64 }
  1039. val(s,ic,code);
  1040. if code=0 then
  1041. result:=texprvalue.create_int(ic)
  1042. else
  1043. begin
  1044. { try qword }
  1045. val(s,qc,code);
  1046. if code=0 then
  1047. result:=texprvalue.create_uint(qc)
  1048. else
  1049. result:=try_parse_real(s);
  1050. end;
  1051. end;
  1052. class function texprvalue.try_parse_real(const s:string):texprvalue;
  1053. var
  1054. d: bestreal;
  1055. code: integer;
  1056. begin
  1057. val(s,d,code);
  1058. if code=0 then
  1059. result:=texprvalue.create_real(d)
  1060. else
  1061. result:=nil;
  1062. end;
  1063. function texprvalue.evaluate(v:texprvalue;op:ttoken):texprvalue;
  1064. function check_compatible: boolean;
  1065. begin
  1066. result:=(
  1067. (is_ordinal(v.def) or is_fpu(v.def)) and
  1068. (is_ordinal(def) or is_fpu(def))
  1069. ) or
  1070. (is_stringlike(v.def) and is_stringlike(def));
  1071. if not result then
  1072. Message2(type_e_incompatible_types,def.typename,v.def.typename);
  1073. end;
  1074. var
  1075. lv,rv: tconstexprint;
  1076. lvd,rvd: bestreal;
  1077. lvs,rvs: string;
  1078. begin
  1079. case op of
  1080. _OP_IN:
  1081. begin
  1082. if not is_set(v.def) then
  1083. begin
  1084. v.error('Set', 'IN');
  1085. result:=texprvalue.create_error;
  1086. end
  1087. else
  1088. if not is_ordinal(def) then
  1089. begin
  1090. error('Ordinal', 'IN');
  1091. result:=texprvalue.create_error;
  1092. end
  1093. else
  1094. if value.valueord.signed then
  1095. result:=texprvalue.create_bool(value.valueord.svalue in pnormalset(v.value.valueptr)^)
  1096. else
  1097. result:=texprvalue.create_bool(value.valueord.uvalue in pnormalset(v.value.valueptr)^);
  1098. end;
  1099. _OP_NOT:
  1100. begin
  1101. if isBoolean then
  1102. result:=texprvalue.create_bool(not asBool)
  1103. else if is_ordinal(def) then
  1104. begin
  1105. result:=texprvalue.create_ord(value.valueord);
  1106. result.def:=def;
  1107. calc_not_ordvalue(result.value.valueord,result.def);
  1108. end
  1109. else
  1110. begin
  1111. error('Boolean', 'NOT');
  1112. result:=texprvalue.create_error;
  1113. end;
  1114. end;
  1115. _OP_OR:
  1116. begin
  1117. if isBoolean then
  1118. if v.isBoolean then
  1119. result:=texprvalue.create_bool(asBool or v.asBool)
  1120. else
  1121. begin
  1122. v.error('Boolean','OR');
  1123. result:=texprvalue.create_error;
  1124. end
  1125. else if is_ordinal(def) then
  1126. if is_ordinal(v.def) then
  1127. result:=texprvalue.create_ord(value.valueord or v.value.valueord)
  1128. else
  1129. begin
  1130. v.error('Ordinal','OR');
  1131. result:=texprvalue.create_error;
  1132. end
  1133. else
  1134. begin
  1135. error('Boolean','OR');
  1136. result:=texprvalue.create_error;
  1137. end;
  1138. end;
  1139. _OP_XOR:
  1140. begin
  1141. if isBoolean then
  1142. if v.isBoolean then
  1143. result:=texprvalue.create_bool(asBool xor v.asBool)
  1144. else
  1145. begin
  1146. v.error('Boolean','XOR');
  1147. result:=texprvalue.create_error;
  1148. end
  1149. else if is_ordinal(def) then
  1150. if is_ordinal(v.def) then
  1151. result:=texprvalue.create_ord(value.valueord xor v.value.valueord)
  1152. else
  1153. begin
  1154. v.error('Ordinal','XOR');
  1155. result:=texprvalue.create_error;
  1156. end
  1157. else
  1158. begin
  1159. error('Boolean','XOR');
  1160. result:=texprvalue.create_error;
  1161. end;
  1162. end;
  1163. _OP_AND:
  1164. begin
  1165. if isBoolean then
  1166. if v.isBoolean then
  1167. result:=texprvalue.create_bool(asBool and v.asBool)
  1168. else
  1169. begin
  1170. v.error('Boolean','AND');
  1171. result:=texprvalue.create_error;
  1172. end
  1173. else if is_ordinal(def) then
  1174. if is_ordinal(v.def) then
  1175. result:=texprvalue.create_ord(value.valueord and v.value.valueord)
  1176. else
  1177. begin
  1178. v.error('Ordinal','AND');
  1179. result:=texprvalue.create_error;
  1180. end
  1181. else
  1182. begin
  1183. error('Boolean','AND');
  1184. result:=texprvalue.create_error;
  1185. end;
  1186. end;
  1187. _EQ,_NE,_LT,_GT,_GTE,_LTE,_PLUS,_MINUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR:
  1188. if check_compatible then
  1189. begin
  1190. if (is_ordinal(def) and is_ordinal(v.def)) then
  1191. begin
  1192. lv:=value.valueord;
  1193. rv:=v.value.valueord;
  1194. case op of
  1195. _EQ:
  1196. result:=texprvalue.create_bool(lv=rv);
  1197. _NE:
  1198. result:=texprvalue.create_bool(lv<>rv);
  1199. _LT:
  1200. result:=texprvalue.create_bool(lv<rv);
  1201. _GT:
  1202. result:=texprvalue.create_bool(lv>rv);
  1203. _GTE:
  1204. result:=texprvalue.create_bool(lv>=rv);
  1205. _LTE:
  1206. result:=texprvalue.create_bool(lv<=rv);
  1207. _PLUS:
  1208. result:=texprvalue.create_ord(lv+rv);
  1209. _MINUS:
  1210. result:=texprvalue.create_ord(lv-rv);
  1211. _STAR:
  1212. result:=texprvalue.create_ord(lv*rv);
  1213. _SLASH:
  1214. result:=texprvalue.create_real(lv/rv);
  1215. _OP_DIV:
  1216. result:=texprvalue.create_ord(lv div rv);
  1217. _OP_MOD:
  1218. result:=texprvalue.create_ord(lv mod rv);
  1219. _OP_SHL:
  1220. result:=texprvalue.create_ord(lv shl rv);
  1221. _OP_SHR:
  1222. result:=texprvalue.create_ord(lv shr rv);
  1223. else
  1224. begin
  1225. { actually we should never get here but this avoids a warning }
  1226. Message(parser_e_illegal_expression);
  1227. result:=texprvalue.create_error;
  1228. end;
  1229. end;
  1230. end
  1231. else
  1232. if (is_fpu(def) or is_ordinal(def)) and
  1233. (is_fpu(v.def) or is_ordinal(v.def)) then
  1234. begin
  1235. if is_fpu(def) then
  1236. lvd:=pbestreal(value.valueptr)^
  1237. else
  1238. lvd:=value.valueord;
  1239. if is_fpu(v.def) then
  1240. rvd:=pbestreal(v.value.valueptr)^
  1241. else
  1242. rvd:=v.value.valueord;
  1243. case op of
  1244. _EQ:
  1245. result:=texprvalue.create_bool(lvd=rvd);
  1246. _NE:
  1247. result:=texprvalue.create_bool(lvd<>rvd);
  1248. _LT:
  1249. result:=texprvalue.create_bool(lvd<rvd);
  1250. _GT:
  1251. result:=texprvalue.create_bool(lvd>rvd);
  1252. _GTE:
  1253. result:=texprvalue.create_bool(lvd>=rvd);
  1254. _LTE:
  1255. result:=texprvalue.create_bool(lvd<=rvd);
  1256. _PLUS:
  1257. result:=texprvalue.create_real(lvd+rvd);
  1258. _MINUS:
  1259. result:=texprvalue.create_real(lvd-rvd);
  1260. _STAR:
  1261. result:=texprvalue.create_real(lvd*rvd);
  1262. _SLASH:
  1263. result:=texprvalue.create_real(lvd/rvd);
  1264. else
  1265. begin
  1266. Message(parser_e_illegal_expression);
  1267. result:=texprvalue.create_error;
  1268. end;
  1269. end;
  1270. end
  1271. else
  1272. begin
  1273. lvs:=asStr;
  1274. rvs:=v.asStr;
  1275. case op of
  1276. _EQ:
  1277. result:=texprvalue.create_bool(lvs=rvs);
  1278. _NE:
  1279. result:=texprvalue.create_bool(lvs<>rvs);
  1280. _LT:
  1281. result:=texprvalue.create_bool(lvs<rvs);
  1282. _GT:
  1283. result:=texprvalue.create_bool(lvs>rvs);
  1284. _GTE:
  1285. result:=texprvalue.create_bool(lvs>=rvs);
  1286. _LTE:
  1287. result:=texprvalue.create_bool(lvs<=rvs);
  1288. _PLUS:
  1289. result:=texprvalue.create_str(lvs+rvs);
  1290. else
  1291. begin
  1292. Message(parser_e_illegal_expression);
  1293. result:=texprvalue.create_error;
  1294. end;
  1295. end;
  1296. end;
  1297. end
  1298. else
  1299. result:=texprvalue.create_error;
  1300. else
  1301. result:=texprvalue.create_error;
  1302. end;
  1303. end;
  1304. procedure texprvalue.error(expecteddef, place: string);
  1305. begin
  1306. Message3(scan_e_compile_time_typeerror,
  1307. expecteddef,
  1308. def.typename,
  1309. place
  1310. );
  1311. end;
  1312. function texprvalue.isBoolean: Boolean;
  1313. var
  1314. i: int64;
  1315. begin
  1316. result:=is_boolean(def);
  1317. if not result and is_integer(def) then
  1318. begin
  1319. i:=asInt64;
  1320. result:=(i=0)or(i=1);
  1321. end;
  1322. end;
  1323. function texprvalue.isInt: Boolean;
  1324. begin
  1325. result:=is_integer(def);
  1326. end;
  1327. function texprvalue.asBool: Boolean;
  1328. begin
  1329. result:=value.valueord<>0;
  1330. end;
  1331. function texprvalue.asInt: Integer;
  1332. begin
  1333. result:=value.valueord.svalue;
  1334. end;
  1335. function texprvalue.asInt64: Int64;
  1336. begin
  1337. result:=value.valueord.svalue;
  1338. end;
  1339. function texprvalue.asStr: String;
  1340. var
  1341. b:byte;
  1342. begin
  1343. case consttyp of
  1344. constord:
  1345. result:=tostr(value.valueord);
  1346. conststring,
  1347. constresourcestring:
  1348. SetString(result,pchar(value.valueptr),value.len);
  1349. constreal:
  1350. str(pbestreal(value.valueptr)^,result);
  1351. constset:
  1352. begin
  1353. result:=',';
  1354. for b:=0 to 255 do
  1355. if b in pconstset(value.valueptr)^ then
  1356. result:=result+tostr(b)+',';
  1357. end;
  1358. { error values }
  1359. constnone:
  1360. result:='';
  1361. else
  1362. internalerror(2013112801);
  1363. end;
  1364. end;
  1365. destructor texprvalue.destroy;
  1366. begin
  1367. case consttyp of
  1368. conststring,
  1369. constresourcestring :
  1370. freemem(value.valueptr,value.len+1);
  1371. constwstring :
  1372. donewidestring(pcompilerwidestring(value.valueptr));
  1373. constreal :
  1374. dispose(pbestreal(value.valueptr));
  1375. constset :
  1376. dispose(pnormalset(value.valueptr));
  1377. constguid :
  1378. dispose(pguid(value.valueptr));
  1379. constord,
  1380. { error values }
  1381. constnone:
  1382. ;
  1383. else
  1384. internalerror(2013112802);
  1385. end;
  1386. inherited destroy;
  1387. end;
  1388. const
  1389. preproc_operators=[_EQ,_NE,_LT,_GT,_LTE,_GTE,_MINUS,_PLUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR,_OP_IN,_OP_AND,_OP_OR,_OP_XOR];
  1390. function preproc_comp_expr:texprvalue;
  1391. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean):texprvalue; forward;
  1392. procedure preproc_consume(t:ttoken);
  1393. begin
  1394. if t<>current_scanner.preproc_token then
  1395. Message(scan_e_preproc_syntax_error);
  1396. current_scanner.preproc_token:=current_scanner.readpreproc;
  1397. end;
  1398. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;out tokentoconsume:ttoken):boolean;
  1399. var
  1400. hmodule: tmodule;
  1401. ns:ansistring;
  1402. nssym:tsym;
  1403. begin
  1404. result:=false;
  1405. tokentoconsume:=_ID;
  1406. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  1407. begin
  1408. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  1409. internalerror(200501154);
  1410. { only allow unit.symbol access if the name was
  1411. found in the current module
  1412. we can use iscurrentunit because generic specializations does not
  1413. change current_unit variable }
  1414. hmodule:=find_module_from_symtable(srsym.Owner);
  1415. if not Assigned(hmodule) then
  1416. internalerror(201001120);
  1417. if hmodule.unit_index=current_filepos.moduleindex then
  1418. begin
  1419. preproc_consume(_POINT);
  1420. current_scanner.skipspace;
  1421. if srsym.typ=namespacesym then
  1422. begin
  1423. ns:=srsym.name;
  1424. nssym:=srsym;
  1425. while assigned(srsym) and (srsym.typ=namespacesym) do
  1426. begin
  1427. { we have a namespace. the next identifier should be either a namespace or a unit }
  1428. searchsym_in_module(hmodule,ns+'.'+current_scanner.preproc_pattern,srsym,srsymtable);
  1429. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  1430. begin
  1431. ns:=ns+'.'+current_scanner.preproc_pattern;
  1432. nssym:=srsym;
  1433. preproc_consume(_ID);
  1434. current_scanner.skipspace;
  1435. preproc_consume(_POINT);
  1436. current_scanner.skipspace;
  1437. end;
  1438. end;
  1439. { check if there is a hidden unit with this pattern in the namespace }
  1440. if not assigned(srsym) and
  1441. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  1442. srsym:=tnamespacesym(nssym).unitsym;
  1443. if assigned(srsym) and (srsym.typ<>unitsym) then
  1444. internalerror(201108260);
  1445. if not assigned(srsym) then
  1446. begin
  1447. result:=true;
  1448. srsymtable:=nil;
  1449. exit;
  1450. end;
  1451. end;
  1452. case current_scanner.preproc_token of
  1453. _ID:
  1454. { system.char? (char=widechar comes from the implicit
  1455. uuchar unit -> override) }
  1456. if (current_scanner.preproc_pattern='CHAR') and
  1457. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  1458. begin
  1459. if m_default_unicodestring in current_settings.modeswitches then
  1460. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  1461. else
  1462. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  1463. end
  1464. else
  1465. searchsym_in_module(tunitsym(srsym).module,current_scanner.preproc_pattern,srsym,srsymtable);
  1466. _STRING:
  1467. begin
  1468. { system.string? }
  1469. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  1470. begin
  1471. if cs_refcountedstrings in current_settings.localswitches then
  1472. begin
  1473. if m_default_unicodestring in current_settings.modeswitches then
  1474. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  1475. else
  1476. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  1477. end
  1478. else
  1479. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  1480. tokentoconsume:=_STRING;
  1481. end;
  1482. end
  1483. else
  1484. ;
  1485. end;
  1486. end
  1487. else
  1488. begin
  1489. srsym:=nil;
  1490. srsymtable:=nil;
  1491. end;
  1492. result:=true;
  1493. end;
  1494. end;
  1495. procedure try_consume_nestedsym(var srsym:tsym;var srsymtable:TSymtable);
  1496. var
  1497. def:tdef;
  1498. tokentoconsume:ttoken;
  1499. found:boolean;
  1500. begin
  1501. found:=try_consume_unitsym(srsym,srsymtable,tokentoconsume);
  1502. if found then
  1503. begin
  1504. preproc_consume(tokentoconsume);
  1505. current_scanner.skipspace;
  1506. end;
  1507. while (current_scanner.preproc_token=_POINT) do
  1508. begin
  1509. if assigned(srsym)and(srsym.typ=typesym) then
  1510. begin
  1511. def:=ttypesym(srsym).typedef;
  1512. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  1513. begin
  1514. preproc_consume(_POINT);
  1515. current_scanner.skipspace;
  1516. if def.typ=objectdef then
  1517. found:=searchsym_in_class(tobjectdef(def),tobjectdef(def),current_scanner.preproc_pattern,srsym,srsymtable,[ssf_search_helper])
  1518. else
  1519. found:=searchsym_in_record(trecorddef(def),current_scanner.preproc_pattern,srsym,srsymtable);
  1520. if not found then
  1521. begin
  1522. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1523. exit;
  1524. end;
  1525. preproc_consume(_ID);
  1526. current_scanner.skipspace;
  1527. end
  1528. else
  1529. begin
  1530. Message(sym_e_type_must_be_rec_or_object_or_class);
  1531. exit;
  1532. end;
  1533. end
  1534. else
  1535. begin
  1536. Message(type_e_type_id_expected);
  1537. exit;
  1538. end;
  1539. end;
  1540. end;
  1541. function preproc_substitutedtoken(const basesearchstr:string;eval:Boolean):texprvalue;
  1542. { Currently this parses identifiers as well as numbers.
  1543. The result from this procedure can either be that the token
  1544. itself is a value, or that it is a compile time variable/macro,
  1545. which then is substituted for another value (for macros
  1546. recursivelly substituted).}
  1547. var
  1548. hs: string;
  1549. mac: tmacro;
  1550. macrocount,
  1551. len: integer;
  1552. foundmacro: boolean;
  1553. searchstr: pshortstring;
  1554. searchstr2store: string;
  1555. begin
  1556. if not eval then
  1557. begin
  1558. result:=texprvalue.create_str(basesearchstr);
  1559. exit;
  1560. end;
  1561. searchstr := @basesearchstr;
  1562. mac:=nil;
  1563. foundmacro:=false;
  1564. { Substitue macros and compiler variables with their content/value.
  1565. For real macros also do recursive substitution. }
  1566. macrocount:=0;
  1567. repeat
  1568. mac:=tmacro(search_macro(searchstr^));
  1569. inc(macrocount);
  1570. if macrocount>max_macro_nesting then
  1571. begin
  1572. Message(scan_w_macro_too_deep);
  1573. break;
  1574. end;
  1575. if assigned(mac) and mac.defined then
  1576. if assigned(mac.buftext) then
  1577. begin
  1578. if mac.buflen>255 then
  1579. begin
  1580. len:=255;
  1581. Message(scan_w_macro_cut_after_255_chars);
  1582. end
  1583. else
  1584. len:=mac.buflen;
  1585. hs[0]:=char(len);
  1586. move(mac.buftext^,hs[1],len);
  1587. searchstr2store:=upcase(hs);
  1588. searchstr:=@searchstr2store;
  1589. mac.is_used:=true;
  1590. foundmacro:=true;
  1591. end
  1592. else
  1593. begin
  1594. Message1(scan_e_error_macro_lacks_value,searchstr^);
  1595. break;
  1596. end
  1597. else
  1598. break;
  1599. if mac.is_compiler_var then
  1600. break;
  1601. until false;
  1602. { At this point, result do contain the value. Do some decoding and
  1603. determine the type.}
  1604. result:=texprvalue.try_parse_number(searchstr^);
  1605. if not assigned(result) then
  1606. begin
  1607. if foundmacro and (searchstr^='FALSE') then
  1608. result:=texprvalue.create_bool(false)
  1609. else if foundmacro and (searchstr^='TRUE') then
  1610. result:=texprvalue.create_bool(true)
  1611. else if (m_mac in current_settings.modeswitches) and
  1612. (not assigned(mac) or not mac.defined) and
  1613. (macrocount = 1) then
  1614. begin
  1615. {Errors in mode mac is issued here. For non macpas modes there is
  1616. more liberty, but the error will eventually be caught at a later stage.}
  1617. Message1(scan_e_error_macro_undefined,searchstr^);
  1618. result:=texprvalue.create_str(searchstr^); { just to have something }
  1619. end
  1620. else
  1621. result:=texprvalue.create_str(searchstr^);
  1622. end;
  1623. end;
  1624. function preproc_factor(eval: Boolean):texprvalue;
  1625. var
  1626. hs,countstr,storedpattern: string;
  1627. mac: tmacro;
  1628. srsym : tsym;
  1629. srsymtable : TSymtable;
  1630. hdef : TDef;
  1631. l : longint;
  1632. hasKlammer: Boolean;
  1633. exprvalue:texprvalue;
  1634. ns:tnormalset;
  1635. begin
  1636. result:=nil;
  1637. hasKlammer:=false;
  1638. if current_scanner.preproc_token=_ID then
  1639. begin
  1640. if current_scanner.preproc_pattern='DEFINED' then
  1641. begin
  1642. preproc_consume(_ID);
  1643. current_scanner.skipspace;
  1644. if current_scanner.preproc_token =_LKLAMMER then
  1645. begin
  1646. preproc_consume(_LKLAMMER);
  1647. current_scanner.skipspace;
  1648. hasKlammer:= true;
  1649. end
  1650. else if (m_mac in current_settings.modeswitches) then
  1651. hasKlammer:= false
  1652. else
  1653. Message(scan_e_error_in_preproc_expr);
  1654. if current_scanner.preproc_token =_ID then
  1655. begin
  1656. hs := current_scanner.preproc_pattern;
  1657. mac := tmacro(search_macro(hs));
  1658. if assigned(mac) and mac.defined then
  1659. begin
  1660. result:=texprvalue.create_bool(true);
  1661. mac.is_used:=true;
  1662. end
  1663. else
  1664. result:=texprvalue.create_bool(false);
  1665. preproc_consume(_ID);
  1666. current_scanner.skipspace;
  1667. end
  1668. else
  1669. Message(scan_e_error_in_preproc_expr);
  1670. if hasKlammer then
  1671. if current_scanner.preproc_token =_RKLAMMER then
  1672. preproc_consume(_RKLAMMER)
  1673. else
  1674. Message(scan_e_error_in_preproc_expr);
  1675. end
  1676. else
  1677. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1678. begin
  1679. preproc_consume(_ID);
  1680. current_scanner.skipspace;
  1681. if current_scanner.preproc_token =_ID then
  1682. begin
  1683. hs := current_scanner.preproc_pattern;
  1684. mac := tmacro(search_macro(hs));
  1685. if assigned(mac) then
  1686. begin
  1687. result:=texprvalue.create_bool(false);
  1688. mac.is_used:=true;
  1689. end
  1690. else
  1691. result:=texprvalue.create_bool(true);
  1692. preproc_consume(_ID);
  1693. current_scanner.skipspace;
  1694. end
  1695. else
  1696. Message(scan_e_error_in_preproc_expr);
  1697. end
  1698. else
  1699. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1700. begin
  1701. preproc_consume(_ID);
  1702. current_scanner.skipspace;
  1703. if current_scanner.preproc_token =_LKLAMMER then
  1704. begin
  1705. preproc_consume(_LKLAMMER);
  1706. current_scanner.skipspace;
  1707. end
  1708. else
  1709. Message(scan_e_error_in_preproc_expr);
  1710. if not (current_scanner.preproc_token = _ID) then
  1711. Message(scan_e_error_in_preproc_expr);
  1712. hs:=current_scanner.preproc_pattern;
  1713. if (length(hs) > 1) then
  1714. {This is allowed in Metrowerks Pascal}
  1715. Message(scan_e_error_in_preproc_expr)
  1716. else
  1717. begin
  1718. if CheckSwitch(hs[1],'+') then
  1719. result:=texprvalue.create_bool(true)
  1720. else
  1721. result:=texprvalue.create_bool(false);
  1722. end;
  1723. preproc_consume(_ID);
  1724. current_scanner.skipspace;
  1725. if current_scanner.preproc_token =_RKLAMMER then
  1726. preproc_consume(_RKLAMMER)
  1727. else
  1728. Message(scan_e_error_in_preproc_expr);
  1729. end
  1730. else
  1731. if current_scanner.preproc_pattern='SIZEOF' then
  1732. begin
  1733. preproc_consume(_ID);
  1734. current_scanner.skipspace;
  1735. if current_scanner.preproc_token =_LKLAMMER then
  1736. begin
  1737. preproc_consume(_LKLAMMER);
  1738. current_scanner.skipspace;
  1739. end
  1740. else
  1741. Message(scan_e_preproc_syntax_error);
  1742. storedpattern:=current_scanner.preproc_pattern;
  1743. preproc_consume(_ID);
  1744. current_scanner.skipspace;
  1745. if eval then
  1746. if searchsym(storedpattern,srsym,srsymtable) then
  1747. begin
  1748. try_consume_nestedsym(srsym,srsymtable);
  1749. l:=0;
  1750. if assigned(srsym) then
  1751. case srsym.typ of
  1752. staticvarsym,
  1753. localvarsym,
  1754. paravarsym :
  1755. l:=tabstractvarsym(srsym).getsize;
  1756. typesym:
  1757. l:=ttypesym(srsym).typedef.size;
  1758. else
  1759. Message(scan_e_error_in_preproc_expr);
  1760. end;
  1761. result:=texprvalue.create_int(l);
  1762. end
  1763. else
  1764. Message1(sym_e_id_not_found,storedpattern);
  1765. if current_scanner.preproc_token =_RKLAMMER then
  1766. preproc_consume(_RKLAMMER)
  1767. else
  1768. Message(scan_e_preproc_syntax_error);
  1769. end
  1770. else
  1771. if current_scanner.preproc_pattern='HIGH' then
  1772. begin
  1773. preproc_consume(_ID);
  1774. current_scanner.skipspace;
  1775. if current_scanner.preproc_token =_LKLAMMER then
  1776. begin
  1777. preproc_consume(_LKLAMMER);
  1778. current_scanner.skipspace;
  1779. end
  1780. else
  1781. Message(scan_e_preproc_syntax_error);
  1782. storedpattern:=current_scanner.preproc_pattern;
  1783. preproc_consume(_ID);
  1784. current_scanner.skipspace;
  1785. if eval then
  1786. if searchsym(storedpattern,srsym,srsymtable) then
  1787. begin
  1788. try_consume_nestedsym(srsym,srsymtable);
  1789. hdef:=nil;
  1790. hs:='';
  1791. l:=0;
  1792. if assigned(srsym) then
  1793. case srsym.typ of
  1794. staticvarsym,
  1795. localvarsym,
  1796. paravarsym :
  1797. hdef:=tabstractvarsym(srsym).vardef;
  1798. typesym:
  1799. hdef:=ttypesym(srsym).typedef;
  1800. else
  1801. Message(scan_e_error_in_preproc_expr);
  1802. end;
  1803. if assigned(hdef) then
  1804. begin
  1805. if hdef.typ=setdef then
  1806. hdef:=tsetdef(hdef).elementdef;
  1807. case hdef.typ of
  1808. orddef:
  1809. with torddef(hdef).high do
  1810. if signed then
  1811. result:=texprvalue.create_int(svalue)
  1812. else
  1813. result:=texprvalue.create_uint(uvalue);
  1814. enumdef:
  1815. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1816. arraydef:
  1817. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1818. Message(type_e_mismatch)
  1819. else
  1820. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1821. stringdef:
  1822. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1823. Message(type_e_mismatch)
  1824. else
  1825. result:=texprvalue.create_int(tstringdef(hdef).len);
  1826. else
  1827. Message(type_e_mismatch);
  1828. end;
  1829. end;
  1830. end
  1831. else
  1832. Message1(sym_e_id_not_found,storedpattern);
  1833. if current_scanner.preproc_token =_RKLAMMER then
  1834. preproc_consume(_RKLAMMER)
  1835. else
  1836. Message(scan_e_preproc_syntax_error);
  1837. end
  1838. else
  1839. if current_scanner.preproc_pattern='DECLARED' then
  1840. begin
  1841. preproc_consume(_ID);
  1842. current_scanner.skipspace;
  1843. if current_scanner.preproc_token =_LKLAMMER then
  1844. begin
  1845. preproc_consume(_LKLAMMER);
  1846. current_scanner.skipspace;
  1847. end
  1848. else
  1849. Message(scan_e_error_in_preproc_expr);
  1850. if current_scanner.preproc_token =_ID then
  1851. begin
  1852. hs := upper(current_scanner.preproc_pattern);
  1853. preproc_consume(_ID);
  1854. current_scanner.skipspace;
  1855. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  1856. begin
  1857. l:=1;
  1858. preproc_consume(current_scanner.preproc_token);
  1859. current_scanner.skipspace;
  1860. while current_scanner.preproc_token=_COMMA do
  1861. begin
  1862. inc(l);
  1863. preproc_consume(_COMMA);
  1864. current_scanner.skipspace;
  1865. end;
  1866. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  1867. Message(scan_e_error_in_preproc_expr)
  1868. else
  1869. preproc_consume(current_scanner.preproc_token);
  1870. str(l,countstr);
  1871. hs:=hs+'$'+countstr;
  1872. end
  1873. else
  1874. { special case: <> }
  1875. if current_scanner.preproc_token=_NE then
  1876. begin
  1877. hs:=hs+'$1';
  1878. preproc_consume(_NE);
  1879. end;
  1880. current_scanner.skipspace;
  1881. if searchsym(hs,srsym,srsymtable) then
  1882. begin
  1883. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  1884. if (sp_generic_dummy in srsym.symoptions) and
  1885. (srsym.typ=typesym) and
  1886. (
  1887. { mode delphi}
  1888. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  1889. { non-delphi modes }
  1890. (df_generic in ttypesym(srsym).typedef.defoptions)
  1891. ) then
  1892. result:=texprvalue.create_bool(false)
  1893. else
  1894. result:=texprvalue.create_bool(true);
  1895. end
  1896. else
  1897. result:=texprvalue.create_bool(false);
  1898. end
  1899. else
  1900. Message(scan_e_error_in_preproc_expr);
  1901. if current_scanner.preproc_token =_RKLAMMER then
  1902. preproc_consume(_RKLAMMER)
  1903. else
  1904. Message(scan_e_error_in_preproc_expr);
  1905. end
  1906. else
  1907. if current_scanner.preproc_pattern='ORD' then
  1908. begin
  1909. preproc_consume(_ID);
  1910. current_scanner.skipspace;
  1911. if current_scanner.preproc_token =_LKLAMMER then
  1912. begin
  1913. preproc_consume(_LKLAMMER);
  1914. current_scanner.skipspace;
  1915. end
  1916. else
  1917. Message(scan_e_preproc_syntax_error);
  1918. exprvalue:=preproc_factor(eval);
  1919. if eval then
  1920. begin
  1921. if is_ordinal(exprvalue.def) then
  1922. result:=texprvalue.create_int(exprvalue.asInt)
  1923. else
  1924. begin
  1925. exprvalue.error('Ordinal','ORD');
  1926. result:=texprvalue.create_int(0);
  1927. end;
  1928. end
  1929. else
  1930. result:=texprvalue.create_int(0);
  1931. exprvalue.free;
  1932. if current_scanner.preproc_token =_RKLAMMER then
  1933. preproc_consume(_RKLAMMER)
  1934. else
  1935. Message(scan_e_error_in_preproc_expr);
  1936. end
  1937. else
  1938. if current_scanner.preproc_pattern='NOT' then
  1939. begin
  1940. preproc_consume(_ID);
  1941. exprvalue:=preproc_factor(eval);
  1942. if eval then
  1943. result:=exprvalue.evaluate(nil,_OP_NOT)
  1944. else
  1945. result:=texprvalue.create_bool(false); {Just to have something}
  1946. exprvalue.free;
  1947. end
  1948. else
  1949. if (current_scanner.preproc_pattern='TRUE') then
  1950. begin
  1951. result:=texprvalue.create_bool(true);
  1952. preproc_consume(_ID);
  1953. end
  1954. else
  1955. if (current_scanner.preproc_pattern='FALSE') then
  1956. begin
  1957. result:=texprvalue.create_bool(false);
  1958. preproc_consume(_ID);
  1959. end
  1960. else
  1961. begin
  1962. storedpattern:=current_scanner.preproc_pattern;
  1963. preproc_consume(_ID);
  1964. current_scanner.skipspace;
  1965. { first look for a macros/int/float }
  1966. result:=preproc_substitutedtoken(storedpattern,eval);
  1967. if eval and (result.consttyp=conststring) then
  1968. begin
  1969. if searchsym(storedpattern,srsym,srsymtable) then
  1970. begin
  1971. try_consume_nestedsym(srsym,srsymtable);
  1972. if assigned(srsym) then
  1973. case srsym.typ of
  1974. constsym:
  1975. begin
  1976. result.free;
  1977. result:=texprvalue.create_const(tconstsym(srsym));
  1978. end;
  1979. enumsym:
  1980. begin
  1981. result.free;
  1982. result:=texprvalue.create_int(tenumsym(srsym).value);
  1983. end;
  1984. else
  1985. ;
  1986. end;
  1987. end
  1988. end
  1989. { skip id(<expr>) if expression must not be evaluated }
  1990. else if not(eval) and (result.consttyp=conststring) then
  1991. begin
  1992. if current_scanner.preproc_token =_LKLAMMER then
  1993. begin
  1994. preproc_consume(_LKLAMMER);
  1995. current_scanner.skipspace;
  1996. result:=preproc_factor(false);
  1997. if current_scanner.preproc_token =_RKLAMMER then
  1998. preproc_consume(_RKLAMMER)
  1999. else
  2000. Message(scan_e_error_in_preproc_expr);
  2001. end;
  2002. end;
  2003. end
  2004. end
  2005. else if current_scanner.preproc_token =_LKLAMMER then
  2006. begin
  2007. preproc_consume(_LKLAMMER);
  2008. result:=preproc_sub_expr(opcompare,eval);
  2009. preproc_consume(_RKLAMMER);
  2010. end
  2011. else if current_scanner.preproc_token = _LECKKLAMMER then
  2012. begin
  2013. preproc_consume(_LECKKLAMMER);
  2014. ns:=[];
  2015. while current_scanner.preproc_token in [_ID,_INTCONST] do
  2016. begin
  2017. exprvalue:=preproc_factor(eval);
  2018. include(ns,exprvalue.asInt);
  2019. if current_scanner.preproc_token = _COMMA then
  2020. preproc_consume(_COMMA);
  2021. end;
  2022. // TODO Add check of setElemType
  2023. preproc_consume(_RECKKLAMMER);
  2024. result:=texprvalue.create_set(ns);
  2025. end
  2026. else if current_scanner.preproc_token = _INTCONST then
  2027. begin
  2028. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  2029. if not assigned(result) then
  2030. begin
  2031. Message(parser_e_invalid_integer);
  2032. result:=texprvalue.create_int(1);
  2033. end;
  2034. preproc_consume(_INTCONST);
  2035. end
  2036. else if current_scanner.preproc_token = _CSTRING then
  2037. begin
  2038. result:=texprvalue.create_str(current_scanner.preproc_pattern);
  2039. preproc_consume(_CSTRING);
  2040. end
  2041. else if current_scanner.preproc_token = _REALNUMBER then
  2042. begin
  2043. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  2044. if not assigned(result) then
  2045. begin
  2046. Message(parser_e_error_in_real);
  2047. result:=texprvalue.create_real(1.0);
  2048. end;
  2049. preproc_consume(_REALNUMBER);
  2050. end
  2051. else
  2052. Message(scan_e_error_in_preproc_expr);
  2053. if not assigned(result) then
  2054. result:=texprvalue.create_error;
  2055. end;
  2056. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  2057. var
  2058. hs1,hs2: texprvalue;
  2059. op: ttoken;
  2060. begin
  2061. if pred_level=highest_precedence then
  2062. result:=preproc_factor(eval)
  2063. else
  2064. result:=preproc_sub_expr(succ(pred_level),eval);
  2065. repeat
  2066. op:=current_scanner.preproc_token;
  2067. if (op in preproc_operators) and
  2068. (op in operator_levels[pred_level]) then
  2069. begin
  2070. hs1:=result;
  2071. preproc_consume(op);
  2072. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  2073. begin
  2074. { stop evaluation the rest of expression }
  2075. result:=texprvalue.create_bool(true);
  2076. if pred_level=highest_precedence then
  2077. hs2:=preproc_factor(false)
  2078. else
  2079. hs2:=preproc_sub_expr(succ(pred_level),false);
  2080. end
  2081. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  2082. begin
  2083. { stop evaluation the rest of expression }
  2084. result:=texprvalue.create_bool(false);
  2085. if pred_level=highest_precedence then
  2086. hs2:=preproc_factor(false)
  2087. else
  2088. hs2:=preproc_sub_expr(succ(pred_level),false);
  2089. end
  2090. else
  2091. begin
  2092. if pred_level=highest_precedence then
  2093. hs2:=preproc_factor(eval)
  2094. else
  2095. hs2:=preproc_sub_expr(succ(pred_level),eval);
  2096. if eval then
  2097. result:=hs1.evaluate(hs2,op)
  2098. else
  2099. result:=texprvalue.create_bool(false); {Just to have something}
  2100. end;
  2101. hs1.free;
  2102. hs2.free;
  2103. end
  2104. else
  2105. break;
  2106. until false;
  2107. end;
  2108. begin
  2109. current_scanner.in_preproc_comp_expr:=true;
  2110. current_scanner.skipspace;
  2111. { start preproc expression scanner }
  2112. current_scanner.preproc_token:=current_scanner.readpreproc;
  2113. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  2114. current_scanner.in_preproc_comp_expr:=false;
  2115. end;
  2116. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  2117. var
  2118. hs: texprvalue;
  2119. begin
  2120. hs:=preproc_comp_expr;
  2121. if hs.isBoolean then
  2122. result:=hs.asBool
  2123. else
  2124. begin
  2125. hs.error('Boolean', 'IF or ELSEIF');
  2126. result:=false;
  2127. end;
  2128. valuedescr:=hs.asStr;
  2129. hs.free;
  2130. end;
  2131. procedure dir_if;
  2132. begin
  2133. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  2134. end;
  2135. procedure dir_elseif;
  2136. begin
  2137. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  2138. end;
  2139. procedure dir_define_impl(macstyle: boolean);
  2140. var
  2141. hs : string;
  2142. bracketcount : longint;
  2143. mac : tmacro;
  2144. macropos : longint;
  2145. macrobuffer : pmacrobuffer;
  2146. begin
  2147. current_scanner.skipspace;
  2148. hs:=current_scanner.readid;
  2149. if hs='' then
  2150. begin
  2151. Message(scan_e_emptymacroname);
  2152. exit;
  2153. end;
  2154. mac:=tmacro(search_macro(hs));
  2155. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  2156. begin
  2157. mac:=tmacro.create(hs);
  2158. mac.defined:=true;
  2159. current_module.localmacrosymtable.insertsym(mac);
  2160. end
  2161. else
  2162. begin
  2163. mac.defined:=true;
  2164. mac.is_compiler_var:=false;
  2165. { delete old definition }
  2166. if assigned(mac.buftext) then
  2167. begin
  2168. freemem(mac.buftext,mac.buflen);
  2169. mac.buftext:=nil;
  2170. end;
  2171. end;
  2172. Message1(parser_c_macro_defined,mac.name);
  2173. mac.is_used:=true;
  2174. if (cs_support_macro in current_settings.moduleswitches) then
  2175. begin
  2176. current_scanner.skipspace;
  2177. if not macstyle then
  2178. begin
  2179. { may be a macro? }
  2180. if c <> ':' then
  2181. exit;
  2182. current_scanner.readchar;
  2183. if c <> '=' then
  2184. exit;
  2185. current_scanner.readchar;
  2186. current_scanner.skipspace;
  2187. end;
  2188. { key words are never substituted }
  2189. if is_keyword(hs) then
  2190. Message(scan_e_keyword_cant_be_a_macro);
  2191. new(macrobuffer);
  2192. macropos:=0;
  2193. { parse macro, brackets are counted so it's possible
  2194. to have a $ifdef etc. in the macro }
  2195. bracketcount:=0;
  2196. repeat
  2197. case c of
  2198. '}' :
  2199. if (bracketcount=0) then
  2200. break
  2201. else
  2202. dec(bracketcount);
  2203. '{' :
  2204. inc(bracketcount);
  2205. #10,#13 :
  2206. current_scanner.linebreak;
  2207. #26 :
  2208. current_scanner.end_of_file;
  2209. end;
  2210. macrobuffer^[macropos]:=c;
  2211. inc(macropos);
  2212. if macropos>=maxmacrolen then
  2213. Message(scan_f_macro_buffer_overflow);
  2214. current_scanner.readchar;
  2215. until false;
  2216. { free buffer of macro ?}
  2217. if assigned(mac.buftext) then
  2218. freemem(mac.buftext,mac.buflen);
  2219. { get new mem }
  2220. getmem(mac.buftext,macropos);
  2221. mac.buflen:=macropos;
  2222. { copy the text }
  2223. move(macrobuffer^,mac.buftext^,macropos);
  2224. dispose(macrobuffer);
  2225. end
  2226. else
  2227. begin
  2228. { check if there is an assignment, then we need to give a
  2229. warning }
  2230. current_scanner.skipspace;
  2231. if c=':' then
  2232. begin
  2233. current_scanner.readchar;
  2234. if c='=' then
  2235. Message(scan_w_macro_support_turned_off);
  2236. end;
  2237. end;
  2238. end;
  2239. procedure dir_define;
  2240. begin
  2241. dir_define_impl(false);
  2242. end;
  2243. procedure dir_definec;
  2244. begin
  2245. dir_define_impl(true);
  2246. end;
  2247. procedure dir_setc;
  2248. var
  2249. hs : string;
  2250. mac : tmacro;
  2251. exprvalue: texprvalue;
  2252. begin
  2253. current_scanner.skipspace;
  2254. hs:=current_scanner.readid;
  2255. mac:=tmacro(search_macro(hs));
  2256. if not assigned(mac) or
  2257. (mac.owner <> current_module.localmacrosymtable) then
  2258. begin
  2259. mac:=tmacro.create(hs);
  2260. mac.defined:=true;
  2261. mac.is_compiler_var:=true;
  2262. current_module.localmacrosymtable.insertsym(mac);
  2263. end
  2264. else
  2265. begin
  2266. mac.defined:=true;
  2267. mac.is_compiler_var:=true;
  2268. { delete old definition }
  2269. if assigned(mac.buftext) then
  2270. begin
  2271. freemem(mac.buftext,mac.buflen);
  2272. mac.buftext:=nil;
  2273. end;
  2274. end;
  2275. Message1(parser_c_macro_defined,mac.name);
  2276. mac.is_used:=true;
  2277. { key words are never substituted }
  2278. if is_keyword(hs) then
  2279. Message(scan_e_keyword_cant_be_a_macro);
  2280. { macro assignment can be both := and = }
  2281. current_scanner.skipspace;
  2282. if c=':' then
  2283. current_scanner.readchar;
  2284. if c='=' then
  2285. begin
  2286. current_scanner.readchar;
  2287. exprvalue:=preproc_comp_expr;
  2288. if not is_boolean(exprvalue.def) and
  2289. not is_integer(exprvalue.def) then
  2290. exprvalue.error('Boolean, Integer', 'SETC');
  2291. hs:=exprvalue.asStr;
  2292. if length(hs) <> 0 then
  2293. begin
  2294. {If we are absolutely shure it is boolean, translate
  2295. to TRUE/FALSE to increase possibility to do future type check}
  2296. if exprvalue.isBoolean then
  2297. begin
  2298. if exprvalue.asBool then
  2299. hs:='TRUE'
  2300. else
  2301. hs:='FALSE';
  2302. end;
  2303. Message2(parser_c_macro_set_to,mac.name,hs);
  2304. { free buffer of macro ?}
  2305. if assigned(mac.buftext) then
  2306. freemem(mac.buftext,mac.buflen);
  2307. { get new mem }
  2308. getmem(mac.buftext,length(hs));
  2309. mac.buflen:=length(hs);
  2310. { copy the text }
  2311. move(hs[1],mac.buftext^,mac.buflen);
  2312. end
  2313. else
  2314. Message(scan_e_preproc_syntax_error);
  2315. exprvalue.free;
  2316. end
  2317. else
  2318. Message(scan_e_preproc_syntax_error);
  2319. end;
  2320. procedure dir_undef;
  2321. var
  2322. hs : string;
  2323. mac : tmacro;
  2324. begin
  2325. current_scanner.skipspace;
  2326. hs:=current_scanner.readid;
  2327. mac:=tmacro(search_macro(hs));
  2328. if not assigned(mac) or
  2329. (mac.owner <> current_module.localmacrosymtable) then
  2330. begin
  2331. mac:=tmacro.create(hs);
  2332. mac.defined:=false;
  2333. current_module.localmacrosymtable.insertsym(mac);
  2334. end
  2335. else
  2336. begin
  2337. mac.defined:=false;
  2338. mac.is_compiler_var:=false;
  2339. { delete old definition }
  2340. if assigned(mac.buftext) then
  2341. begin
  2342. freemem(mac.buftext,mac.buflen);
  2343. mac.buftext:=nil;
  2344. end;
  2345. end;
  2346. Message1(parser_c_macro_undefined,mac.name);
  2347. mac.is_used:=true;
  2348. end;
  2349. procedure dir_include;
  2350. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  2351. var
  2352. found : boolean;
  2353. hpath : TCmdStr;
  2354. begin
  2355. (* look for the include file
  2356. If path was absolute and specified as part of {$I } then
  2357. 1. specified path
  2358. else
  2359. 1. path of current inputfile,current dir
  2360. 2. local includepath
  2361. 3. global includepath
  2362. -- Check mantis #13461 before changing this *)
  2363. found:=false;
  2364. foundfile:='';
  2365. hpath:='';
  2366. if path_absolute(path) then
  2367. begin
  2368. found:=FindFile(name,path,true,foundfile);
  2369. end
  2370. else
  2371. begin
  2372. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  2373. found:=FindFile(path+name, hpath,true,foundfile);
  2374. if not found then
  2375. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  2376. if not found then
  2377. found:=includesearchpath.FindFile(path+name,true,foundfile);
  2378. end;
  2379. result:=found;
  2380. end;
  2381. var
  2382. foundfile : TCmdStr;
  2383. path,
  2384. name,
  2385. hs : tpathstr;
  2386. args : string;
  2387. hp : tinputfile;
  2388. found : boolean;
  2389. macroIsString : boolean;
  2390. begin
  2391. current_scanner.skipspace;
  2392. args:=current_scanner.readcomment;
  2393. hs:=GetToken(args,' ');
  2394. if hs='' then
  2395. exit;
  2396. if (hs[1]='%') then
  2397. begin
  2398. { case insensitive }
  2399. hs:=upper(hs);
  2400. { remove %'s }
  2401. Delete(hs,1,1);
  2402. if hs[length(hs)]='%' then
  2403. Delete(hs,length(hs),1);
  2404. { save old }
  2405. path:=hs;
  2406. { first check for internal macros }
  2407. macroIsString:=true;
  2408. case hs of
  2409. 'TIME':
  2410. if timestr<>'' then
  2411. hs:=timestr
  2412. else
  2413. hs:=gettimestr;
  2414. 'DATE':
  2415. if datestr<>'' then
  2416. hs:=datestr
  2417. else
  2418. hs:=getdatestr;
  2419. 'DATEYEAR':
  2420. begin
  2421. hs:=tostr(startsystime.Year);
  2422. macroIsString:=false;
  2423. end;
  2424. 'DATEMONTH':
  2425. begin
  2426. hs:=tostr(startsystime.Month);
  2427. macroIsString:=false;
  2428. end;
  2429. 'DATEDAY':
  2430. begin
  2431. hs:=tostr(startsystime.Day);
  2432. macroIsString:=false;
  2433. end;
  2434. 'TIMEHOUR':
  2435. begin
  2436. hs:=tostr(startsystime.Hour);
  2437. macroIsString:=false;
  2438. end;
  2439. 'TIMEMINUTE':
  2440. begin
  2441. hs:=tostr(startsystime.Minute);
  2442. macroIsString:=false;
  2443. end;
  2444. 'TIMESECOND':
  2445. begin
  2446. hs:=tostr(startsystime.Second);
  2447. macroIsString:=false;
  2448. end;
  2449. 'FILE':
  2450. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2451. 'LINE':
  2452. hs:=tostr(current_filepos.line);
  2453. 'LINENUM':
  2454. begin
  2455. hs:=tostr(current_filepos.line);
  2456. macroIsString:=false;
  2457. end;
  2458. 'FPCVERSION':
  2459. hs:=version_string;
  2460. 'FPCDATE':
  2461. hs:=date_string;
  2462. 'FPCTARGET':
  2463. hs:=target_cpu_string;
  2464. 'FPCTARGETCPU':
  2465. hs:=target_cpu_string;
  2466. 'FPCTARGETOS':
  2467. hs:=target_info.shortname;
  2468. 'CURRENTROUTINE':
  2469. hs:=current_procinfo.procdef.procsym.RealName;
  2470. else
  2471. hs:=GetEnvironmentVariable(hs);
  2472. end;
  2473. if hs='' then
  2474. Message1(scan_w_include_env_not_found,path);
  2475. { make it a stringconst }
  2476. if macroIsString then
  2477. hs:=''''+hs+'''';
  2478. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2479. current_scanner.line_no,current_scanner.inputfile.ref_index,false);
  2480. end
  2481. else
  2482. begin
  2483. hs:=FixFileName(hs);
  2484. path:=ExtractFilePath(hs);
  2485. name:=ExtractFileName(hs);
  2486. { Special case for Delphi compatibility: '*' has to be replaced
  2487. by the file name of the current source file. }
  2488. if (length(name)>=1) and
  2489. (name[1]='*') then
  2490. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2491. { try to find the file }
  2492. found:=findincludefile(path,name,foundfile);
  2493. if (not found) and (ExtractFileExt(name)='') then
  2494. begin
  2495. { try default extensions .inc , .pp and .pas }
  2496. if (not found) then
  2497. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2498. if (not found) then
  2499. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2500. if (not found) then
  2501. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2502. end;
  2503. { if the name ends in dot, try without the dot }
  2504. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2505. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2506. if current_scanner.inputfilecount<max_include_nesting then
  2507. begin
  2508. inc(current_scanner.inputfilecount);
  2509. { we need to reread the current char }
  2510. dec(current_scanner.inputpointer);
  2511. { reset c }
  2512. c:=#0;
  2513. { shutdown current file }
  2514. current_scanner.tempcloseinputfile;
  2515. { load new file }
  2516. hp:=do_openinputfile(foundfile);
  2517. hp.inc_path:=path;
  2518. current_scanner.addfile(hp);
  2519. current_module.sourcefiles.register_file(hp);
  2520. if (not found) then
  2521. Message1(scan_f_cannot_open_includefile,hs);
  2522. if (not current_scanner.openinputfile) then
  2523. Message1(scan_f_cannot_open_includefile,hs);
  2524. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2525. current_scanner.reload;
  2526. end
  2527. else
  2528. Message(scan_f_include_deep_ten);
  2529. end;
  2530. end;
  2531. {*****************************************************************************
  2532. Preprocessor writing
  2533. *****************************************************************************}
  2534. {$ifdef PREPROCWRITE}
  2535. constructor tpreprocfile.create(const fn:string);
  2536. begin
  2537. inherited create;
  2538. { open outputfile }
  2539. assign(f,fn);
  2540. {$push}{$I-}
  2541. rewrite(f);
  2542. {$pop}
  2543. if ioresult<>0 then
  2544. Comment(V_Fatal,'can''t create file '+fn);
  2545. getmem(buf,preprocbufsize);
  2546. settextbuf(f,buf^,preprocbufsize);
  2547. { reset }
  2548. eolfound:=false;
  2549. spacefound:=false;
  2550. end;
  2551. destructor tpreprocfile.destroy;
  2552. begin
  2553. close(f);
  2554. freemem(buf,preprocbufsize);
  2555. end;
  2556. procedure tpreprocfile.add(const s:string);
  2557. begin
  2558. write(f,s);
  2559. end;
  2560. procedure tpreprocfile.addspace;
  2561. begin
  2562. if eolfound then
  2563. begin
  2564. writeln(f,'');
  2565. eolfound:=false;
  2566. spacefound:=false;
  2567. end
  2568. else
  2569. if spacefound then
  2570. begin
  2571. write(f,' ');
  2572. spacefound:=false;
  2573. end;
  2574. end;
  2575. {$endif PREPROCWRITE}
  2576. {*****************************************************************************
  2577. TPreProcStack
  2578. *****************************************************************************}
  2579. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2580. begin
  2581. accept:=a;
  2582. typ:=atyp;
  2583. next:=n;
  2584. end;
  2585. {*****************************************************************************
  2586. TReplayStack
  2587. *****************************************************************************}
  2588. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2589. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2590. apatternw:pcompilerwidestring;asettings:tsettings;
  2591. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2592. begin
  2593. token:=atoken;
  2594. idtoken:=aidtoken;
  2595. orgpattern:=aorgpattern;
  2596. pattern:=apattern;
  2597. cstringpattern:=acstringpattern;
  2598. initwidestring(patternw);
  2599. if assigned(apatternw) then
  2600. begin
  2601. setlengthwidestring(patternw,apatternw^.len);
  2602. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2603. end;
  2604. settings:=asettings;
  2605. tokenbuf:=atokenbuf;
  2606. tokenbuf_needs_swapping:=change_endian;
  2607. next:=anext;
  2608. end;
  2609. destructor treplaystack.destroy;
  2610. begin
  2611. donewidestring(patternw);
  2612. end;
  2613. {*****************************************************************************
  2614. TDirectiveItem
  2615. *****************************************************************************}
  2616. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2617. begin
  2618. inherited Create(AList,n);
  2619. is_conditional:=false;
  2620. proc:=p;
  2621. end;
  2622. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2623. begin
  2624. inherited Create(AList,n);
  2625. is_conditional:=true;
  2626. proc:=p;
  2627. end;
  2628. {****************************************************************************
  2629. TSCANNERFILE
  2630. ****************************************************************************}
  2631. constructor tscannerfile.create(const fn:string; is_macro: boolean = false);
  2632. begin
  2633. inputfile:=do_openinputfile(fn);
  2634. if is_macro then
  2635. inputfile.is_macro:=true;
  2636. if assigned(current_module) then
  2637. current_module.sourcefiles.register_file(inputfile);
  2638. { reset localinput }
  2639. c:=#0;
  2640. inputbuffer:=nil;
  2641. inputpointer:=nil;
  2642. inputstart:=0;
  2643. { reset scanner }
  2644. preprocstack:=nil;
  2645. replaystack:=nil;
  2646. comment_level:=0;
  2647. yylexcount:=0;
  2648. block_type:=bt_general;
  2649. line_no:=0;
  2650. lastlinepos:=0;
  2651. lasttokenpos:=0;
  2652. nexttokenpos:=0;
  2653. lasttoken:=NOTOKEN;
  2654. nexttoken:=NOTOKEN;
  2655. ignoredirectives:=TFPHashList.Create;
  2656. change_endian_for_replay:=false;
  2657. end;
  2658. procedure tscannerfile.firstfile;
  2659. begin
  2660. { load block }
  2661. if not openinputfile then
  2662. Message1(scan_f_cannot_open_input,inputfile.name);
  2663. reload;
  2664. end;
  2665. destructor tscannerfile.destroy;
  2666. begin
  2667. if assigned(current_module) and
  2668. (current_module.state=ms_compiled) and
  2669. (status.errorcount=0) then
  2670. checkpreprocstack
  2671. else
  2672. begin
  2673. while assigned(preprocstack) do
  2674. poppreprocstack;
  2675. end;
  2676. while assigned(replaystack) do
  2677. popreplaystack;
  2678. if not inputfile.closed then
  2679. closeinputfile;
  2680. if inputfile.is_macro then
  2681. inputfile.free;
  2682. ignoredirectives.free;
  2683. end;
  2684. function tscannerfile.openinputfile:boolean;
  2685. begin
  2686. openinputfile:=inputfile.open;
  2687. { load buffer }
  2688. inputbuffer:=inputfile.buf;
  2689. inputpointer:=inputfile.buf;
  2690. inputstart:=inputfile.bufstart;
  2691. { line }
  2692. line_no:=0;
  2693. lastlinepos:=0;
  2694. lasttokenpos:=0;
  2695. nexttokenpos:=0;
  2696. end;
  2697. procedure tscannerfile.closeinputfile;
  2698. begin
  2699. inputfile.close;
  2700. { reset buffer }
  2701. inputbuffer:=nil;
  2702. inputpointer:=nil;
  2703. inputstart:=0;
  2704. { reset line }
  2705. line_no:=0;
  2706. lastlinepos:=0;
  2707. lasttokenpos:=0;
  2708. nexttokenpos:=0;
  2709. end;
  2710. function tscannerfile.tempopeninputfile:boolean;
  2711. begin
  2712. tempopeninputfile:=false;
  2713. if inputfile.is_macro then
  2714. exit;
  2715. tempopeninputfile:=inputfile.tempopen;
  2716. { reload buffer }
  2717. inputbuffer:=inputfile.buf;
  2718. inputpointer:=inputfile.buf;
  2719. inputstart:=inputfile.bufstart;
  2720. end;
  2721. procedure tscannerfile.tempcloseinputfile;
  2722. begin
  2723. if inputfile.closed or inputfile.is_macro then
  2724. exit;
  2725. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2726. inputfile.tempclose;
  2727. { reset buffer }
  2728. inputbuffer:=nil;
  2729. inputpointer:=nil;
  2730. inputstart:=0;
  2731. end;
  2732. procedure tscannerfile.saveinputfile;
  2733. begin
  2734. inputfile.saveinputpointer:=inputpointer;
  2735. inputfile.savelastlinepos:=lastlinepos;
  2736. inputfile.saveline_no:=line_no;
  2737. end;
  2738. procedure tscannerfile.restoreinputfile;
  2739. begin
  2740. inputbuffer:=inputfile.buf;
  2741. inputpointer:=inputfile.saveinputpointer;
  2742. lastlinepos:=inputfile.savelastlinepos;
  2743. line_no:=inputfile.saveline_no;
  2744. if not inputfile.is_macro then
  2745. parser_current_file:=inputfile.name;
  2746. end;
  2747. procedure tscannerfile.nextfile;
  2748. var
  2749. to_dispose : tinputfile;
  2750. begin
  2751. if assigned(inputfile.next) then
  2752. begin
  2753. if inputfile.is_macro then
  2754. begin
  2755. to_dispose:=inputfile;
  2756. dec(macro_nesting_depth);
  2757. end
  2758. else
  2759. begin
  2760. to_dispose:=nil;
  2761. dec(inputfilecount);
  2762. end;
  2763. { we can allways close the file, no ? }
  2764. inputfile.close;
  2765. inputfile:=inputfile.next;
  2766. if assigned(to_dispose) then
  2767. to_dispose.free;
  2768. restoreinputfile;
  2769. end;
  2770. end;
  2771. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  2772. begin
  2773. if not assigned(buf) then
  2774. internalerror(200511172);
  2775. if assigned(recordtokenbuf) then
  2776. internalerror(200511173);
  2777. recordtokenbuf:=buf;
  2778. fillchar(last_settings,sizeof(last_settings),0);
  2779. last_message:=nil;
  2780. fillchar(last_filepos,sizeof(last_filepos),0);
  2781. end;
  2782. procedure tscannerfile.stoprecordtokens;
  2783. begin
  2784. if not assigned(recordtokenbuf) then
  2785. internalerror(200511174);
  2786. recordtokenbuf:=nil;
  2787. end;
  2788. function tscannerfile.is_recording_tokens: boolean;
  2789. begin
  2790. result:=assigned(recordtokenbuf);
  2791. end;
  2792. procedure tscannerfile.writetoken(t : ttoken);
  2793. var
  2794. b : byte;
  2795. begin
  2796. if ord(t)>$7f then
  2797. begin
  2798. b:=(ord(t) shr 8) or $80;
  2799. recordtokenbuf.write(b,1);
  2800. end;
  2801. b:=ord(t) and $ff;
  2802. recordtokenbuf.write(b,1);
  2803. end;
  2804. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  2805. begin
  2806. recordtokenbuf.write(val,sizeof(asizeint));
  2807. end;
  2808. procedure tscannerfile.tokenwritelongint(val : longint);
  2809. begin
  2810. recordtokenbuf.write(val,sizeof(longint));
  2811. end;
  2812. procedure tscannerfile.tokenwriteshortint(val : shortint);
  2813. begin
  2814. recordtokenbuf.write(val,sizeof(shortint));
  2815. end;
  2816. procedure tscannerfile.tokenwriteword(val : word);
  2817. begin
  2818. recordtokenbuf.write(val,sizeof(word));
  2819. end;
  2820. procedure tscannerfile.tokenwritelongword(val : longword);
  2821. begin
  2822. recordtokenbuf.write(val,sizeof(longword));
  2823. end;
  2824. function tscannerfile.tokenreadsizeint : asizeint;
  2825. var
  2826. val : asizeint;
  2827. begin
  2828. replaytokenbuf.read(val,sizeof(asizeint));
  2829. if change_endian_for_replay then
  2830. val:=swapendian(val);
  2831. result:=val;
  2832. end;
  2833. function tscannerfile.tokenreadlongword : longword;
  2834. var
  2835. val : longword;
  2836. begin
  2837. replaytokenbuf.read(val,sizeof(longword));
  2838. if change_endian_for_replay then
  2839. val:=swapendian(val);
  2840. result:=val;
  2841. end;
  2842. function tscannerfile.tokenreadlongint : longint;
  2843. var
  2844. val : longint;
  2845. begin
  2846. replaytokenbuf.read(val,sizeof(longint));
  2847. if change_endian_for_replay then
  2848. val:=swapendian(val);
  2849. result:=val;
  2850. end;
  2851. function tscannerfile.tokenreadshortint : shortint;
  2852. var
  2853. val : shortint;
  2854. begin
  2855. replaytokenbuf.read(val,sizeof(shortint));
  2856. result:=val;
  2857. end;
  2858. function tscannerfile.tokenreadbyte : byte;
  2859. var
  2860. val : byte;
  2861. begin
  2862. replaytokenbuf.read(val,sizeof(byte));
  2863. result:=val;
  2864. end;
  2865. function tscannerfile.tokenreadsmallint : smallint;
  2866. var
  2867. val : smallint;
  2868. begin
  2869. replaytokenbuf.read(val,sizeof(smallint));
  2870. if change_endian_for_replay then
  2871. val:=swapendian(val);
  2872. result:=val;
  2873. end;
  2874. function tscannerfile.tokenreadword : word;
  2875. var
  2876. val : word;
  2877. begin
  2878. replaytokenbuf.read(val,sizeof(word));
  2879. if change_endian_for_replay then
  2880. val:=swapendian(val);
  2881. result:=val;
  2882. end;
  2883. function tscannerfile.tokenreadenum(size : longint) : longword;
  2884. begin
  2885. if size=1 then
  2886. result:=tokenreadbyte
  2887. else if size=2 then
  2888. result:=tokenreadword
  2889. else if size=4 then
  2890. result:=tokenreadlongword
  2891. else
  2892. internalerror(2013112901);
  2893. end;
  2894. procedure tscannerfile.tokenreadset(var b;size : longint);
  2895. var
  2896. i : longint;
  2897. begin
  2898. replaytokenbuf.read(b,size);
  2899. if change_endian_for_replay then
  2900. for i:=0 to size-1 do
  2901. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  2902. end;
  2903. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  2904. begin
  2905. recordtokenbuf.write(b,size);
  2906. end;
  2907. procedure tscannerfile.tokenwriteset(var b;size : longint);
  2908. begin
  2909. recordtokenbuf.write(b,size);
  2910. end;
  2911. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  2912. { This procedure
  2913. needs to be changed whenever
  2914. globals.tsettings type is changed,
  2915. the problem is that no error will appear
  2916. before tests with generics are tested. PM }
  2917. var
  2918. startpos, endpos : longword;
  2919. begin
  2920. { WARNING all those fields need to be in the correct
  2921. order otherwise cross_endian PPU reading will fail }
  2922. startpos:=replaytokenbuf.pos;
  2923. with asettings do
  2924. begin
  2925. alignment.procalign:=tokenreadlongint;
  2926. alignment.loopalign:=tokenreadlongint;
  2927. alignment.jumpalign:=tokenreadlongint;
  2928. alignment.jumpalignskipmax:=tokenreadlongint;
  2929. alignment.coalescealign:=tokenreadlongint;
  2930. alignment.coalescealignskipmax:=tokenreadlongint;
  2931. alignment.constalignmin:=tokenreadlongint;
  2932. alignment.constalignmax:=tokenreadlongint;
  2933. alignment.varalignmin:=tokenreadlongint;
  2934. alignment.varalignmax:=tokenreadlongint;
  2935. alignment.localalignmin:=tokenreadlongint;
  2936. alignment.localalignmax:=tokenreadlongint;
  2937. alignment.recordalignmin:=tokenreadlongint;
  2938. alignment.recordalignmax:=tokenreadlongint;
  2939. alignment.maxCrecordalign:=tokenreadlongint;
  2940. tokenreadset(globalswitches,sizeof(globalswitches));
  2941. tokenreadset(targetswitches,sizeof(targetswitches));
  2942. tokenreadset(moduleswitches,sizeof(moduleswitches));
  2943. tokenreadset(localswitches,sizeof(localswitches));
  2944. tokenreadset(modeswitches,sizeof(modeswitches));
  2945. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  2946. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  2947. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  2948. tokenreadset(debugswitches,sizeof(debugswitches));
  2949. { 0: old behaviour for sets <=256 elements
  2950. >0: round to this size }
  2951. setalloc:=tokenreadshortint;
  2952. packenum:=tokenreadshortint;
  2953. packrecords:=tokenreadshortint;
  2954. maxfpuregisters:=tokenreadshortint;
  2955. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2956. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  2957. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  2958. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  2959. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  2960. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  2961. { tstringencoding is word type,
  2962. thus this should be OK here }
  2963. sourcecodepage:=tstringEncoding(tokenreadword);
  2964. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  2965. disabledircache:=boolean(tokenreadbyte);
  2966. tlsmodel:=ttlsmodel(tokenreadenum(sizeof(ttlsmodel)));
  2967. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  2968. { While adding ControllerSupport constant, I decided not to store ct_none }
  2969. { on targets not supporting controllers, but this might be changed here and }
  2970. { in tokenwritesettings in the future to unify the PPU structure and handling }
  2971. { of this field in the compiler. }
  2972. {$PUSH}
  2973. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  2974. if ControllerSupport then
  2975. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  2976. else
  2977. ControllerType:=ct_none;
  2978. {$POP}
  2979. endpos:=replaytokenbuf.pos;
  2980. if endpos-startpos<>expected_size then
  2981. Comment(V_Error,'Wrong size of Settings read-in');
  2982. end;
  2983. end;
  2984. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  2985. { This procedure
  2986. needs to be changed whenever
  2987. globals.tsettings type is changed,
  2988. the problem is that no error will appear
  2989. before tests with generics are tested. PM }
  2990. var
  2991. sizepos, startpos, endpos : longword;
  2992. begin
  2993. { WARNING all those fields need to be in the correct
  2994. order otherwise cross_endian PPU reading will fail }
  2995. sizepos:=recordtokenbuf.pos;
  2996. size:=0;
  2997. tokenwritesizeint(size);
  2998. startpos:=recordtokenbuf.pos;
  2999. with asettings do
  3000. begin
  3001. tokenwritelongint(alignment.procalign);
  3002. tokenwritelongint(alignment.loopalign);
  3003. tokenwritelongint(alignment.jumpalign);
  3004. tokenwritelongint(alignment.jumpalignskipmax);
  3005. tokenwritelongint(alignment.coalescealign);
  3006. tokenwritelongint(alignment.coalescealignskipmax);
  3007. tokenwritelongint(alignment.constalignmin);
  3008. tokenwritelongint(alignment.constalignmax);
  3009. tokenwritelongint(alignment.varalignmin);
  3010. tokenwritelongint(alignment.varalignmax);
  3011. tokenwritelongint(alignment.localalignmin);
  3012. tokenwritelongint(alignment.localalignmax);
  3013. tokenwritelongint(alignment.recordalignmin);
  3014. tokenwritelongint(alignment.recordalignmax);
  3015. tokenwritelongint(alignment.maxCrecordalign);
  3016. tokenwriteset(globalswitches,sizeof(globalswitches));
  3017. tokenwriteset(targetswitches,sizeof(targetswitches));
  3018. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  3019. tokenwriteset(localswitches,sizeof(localswitches));
  3020. tokenwriteset(modeswitches,sizeof(modeswitches));
  3021. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  3022. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3023. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3024. tokenwriteset(debugswitches,sizeof(debugswitches));
  3025. { 0: old behaviour for sets <=256 elements
  3026. >0: round to this size }
  3027. tokenwriteshortint(setalloc);
  3028. tokenwriteshortint(packenum);
  3029. tokenwriteshortint(packrecords);
  3030. tokenwriteshortint(maxfpuregisters);
  3031. tokenwriteenum(cputype,sizeof(tcputype));
  3032. tokenwriteenum(optimizecputype,sizeof(tcputype));
  3033. tokenwriteenum(fputype,sizeof(tfputype));
  3034. tokenwriteenum(asmmode,sizeof(tasmmode));
  3035. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  3036. tokenwriteenum(defproccall,sizeof(tproccalloption));
  3037. { tstringencoding is word type,
  3038. thus this should be OK here }
  3039. tokenwriteword(sourcecodepage);
  3040. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  3041. recordtokenbuf.write(byte(disabledircache),1);
  3042. tokenwriteenum(tlsmodel,sizeof(tlsmodel));
  3043. { TH: See note about controllertype field in tokenreadsettings. }
  3044. {$PUSH}
  3045. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3046. if ControllerSupport then
  3047. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  3048. {$POP}
  3049. endpos:=recordtokenbuf.pos;
  3050. size:=endpos-startpos;
  3051. recordtokenbuf.seek(sizepos);
  3052. tokenwritesizeint(size);
  3053. recordtokenbuf.seek(endpos);
  3054. end;
  3055. end;
  3056. procedure tscannerfile.recordtoken;
  3057. var
  3058. t : ttoken;
  3059. s : tspecialgenerictoken;
  3060. len,msgnb,copy_size : asizeint;
  3061. val : longint;
  3062. b : byte;
  3063. pmsg : pmessagestaterecord;
  3064. begin
  3065. if not assigned(recordtokenbuf) then
  3066. internalerror(200511176);
  3067. t:=_GENERICSPECIALTOKEN;
  3068. { settings changed? }
  3069. { last field pmessage is handled separately below in
  3070. ST_LOADMESSAGES }
  3071. if CompareByte(current_settings,last_settings,
  3072. sizeof(current_settings)-sizeof(pointer))<>0 then
  3073. begin
  3074. { use a special token to record it }
  3075. s:=ST_LOADSETTINGS;
  3076. writetoken(t);
  3077. recordtokenbuf.write(s,1);
  3078. copy_size:=sizeof(current_settings)-sizeof(pointer);
  3079. tokenwritesettings(current_settings,copy_size);
  3080. last_settings:=current_settings;
  3081. end;
  3082. if current_settings.pmessage<>last_message then
  3083. begin
  3084. { use a special token to record it }
  3085. s:=ST_LOADMESSAGES;
  3086. writetoken(t);
  3087. recordtokenbuf.write(s,1);
  3088. msgnb:=0;
  3089. pmsg:=current_settings.pmessage;
  3090. while assigned(pmsg) do
  3091. begin
  3092. if msgnb=high(asizeint) then
  3093. { Too many messages }
  3094. internalerror(2011090401);
  3095. inc(msgnb);
  3096. pmsg:=pmsg^.next;
  3097. end;
  3098. tokenwritesizeint(msgnb);
  3099. pmsg:=current_settings.pmessage;
  3100. while assigned(pmsg) do
  3101. begin
  3102. { What about endianess here?}
  3103. { SB: this is handled by tokenreadlongint }
  3104. val:=pmsg^.value;
  3105. tokenwritelongint(val);
  3106. val:=ord(pmsg^.state);
  3107. tokenwritelongint(val);
  3108. pmsg:=pmsg^.next;
  3109. end;
  3110. last_message:=current_settings.pmessage;
  3111. end;
  3112. { file pos changes? }
  3113. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3114. begin
  3115. s:=ST_FILEINDEX;
  3116. writetoken(t);
  3117. recordtokenbuf.write(s,1);
  3118. tokenwriteword(current_tokenpos.fileindex);
  3119. last_filepos.fileindex:=current_tokenpos.fileindex;
  3120. last_filepos.line:=0;
  3121. end;
  3122. if current_tokenpos.line<>last_filepos.line then
  3123. begin
  3124. s:=ST_LINE;
  3125. writetoken(t);
  3126. recordtokenbuf.write(s,1);
  3127. tokenwritelongint(current_tokenpos.line);
  3128. last_filepos.line:=current_tokenpos.line;
  3129. last_filepos.column:=0;
  3130. end;
  3131. if current_tokenpos.column<>last_filepos.column then
  3132. begin
  3133. s:=ST_COLUMN;
  3134. writetoken(t);
  3135. { can the column be written packed? }
  3136. if current_tokenpos.column<$80 then
  3137. begin
  3138. b:=$80 or current_tokenpos.column;
  3139. recordtokenbuf.write(b,1);
  3140. end
  3141. else
  3142. begin
  3143. recordtokenbuf.write(s,1);
  3144. tokenwriteword(current_tokenpos.column);
  3145. end;
  3146. last_filepos.column:=current_tokenpos.column;
  3147. end;
  3148. writetoken(token);
  3149. if token<>_GENERICSPECIALTOKEN then
  3150. writetoken(idtoken);
  3151. case token of
  3152. _CWCHAR,
  3153. _CWSTRING :
  3154. begin
  3155. tokenwritesizeint(patternw^.len);
  3156. if patternw^.len>0 then
  3157. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3158. end;
  3159. _CSTRING:
  3160. begin
  3161. len:=length(cstringpattern);
  3162. tokenwritesizeint(len);
  3163. if len>0 then
  3164. recordtokenbuf.write(cstringpattern[1],len);
  3165. end;
  3166. _CCHAR,
  3167. _INTCONST,
  3168. _REALNUMBER :
  3169. begin
  3170. { pexpr.pas messes with pattern in case of negative integer consts,
  3171. see around line 2562 the comment of JM; remove the - before recording it
  3172. (FK)
  3173. }
  3174. if (token=_INTCONST) and (pattern[1]='-') then
  3175. delete(pattern,1,1);
  3176. recordtokenbuf.write(pattern[0],1);
  3177. recordtokenbuf.write(pattern[1],length(pattern));
  3178. end;
  3179. _ID :
  3180. begin
  3181. recordtokenbuf.write(orgpattern[0],1);
  3182. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3183. end;
  3184. else
  3185. ;
  3186. end;
  3187. end;
  3188. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3189. begin
  3190. if not assigned(buf) then
  3191. internalerror(200511175);
  3192. { save current scanner state }
  3193. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3194. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3195. if assigned(inputpointer) then
  3196. dec(inputpointer);
  3197. { install buffer }
  3198. replaytokenbuf:=buf;
  3199. { Initialize value of change_endian_for_replay variable }
  3200. change_endian_for_replay:=change_endian;
  3201. { reload next token }
  3202. replaytokenbuf.seek(0);
  3203. replaytoken;
  3204. end;
  3205. function tscannerfile.readtoken: ttoken;
  3206. var
  3207. b,b2 : byte;
  3208. begin
  3209. replaytokenbuf.read(b,1);
  3210. if (b and $80)<>0 then
  3211. begin
  3212. replaytokenbuf.read(b2,1);
  3213. result:=ttoken(((b and $7f) shl 8) or b2);
  3214. end
  3215. else
  3216. result:=ttoken(b);
  3217. end;
  3218. procedure tscannerfile.replaytoken;
  3219. var
  3220. wlen,mesgnb,copy_size : asizeint;
  3221. specialtoken : tspecialgenerictoken;
  3222. i : byte;
  3223. pmsg,prevmsg : pmessagestaterecord;
  3224. begin
  3225. if not assigned(replaytokenbuf) then
  3226. internalerror(200511177);
  3227. { End of replay buffer? Then load the next char from the file again }
  3228. if replaytokenbuf.pos>=replaytokenbuf.size then
  3229. begin
  3230. token:=replaystack.token;
  3231. idtoken:=replaystack.idtoken;
  3232. pattern:=replaystack.pattern;
  3233. orgpattern:=replaystack.orgpattern;
  3234. setlengthwidestring(patternw,replaystack.patternw^.len);
  3235. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3236. cstringpattern:=replaystack.cstringpattern;
  3237. replaytokenbuf:=replaystack.tokenbuf;
  3238. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3239. { restore compiler settings }
  3240. current_settings:=replaystack.settings;
  3241. popreplaystack;
  3242. if assigned(inputpointer) then
  3243. begin
  3244. c:=inputpointer^;
  3245. inc(inputpointer);
  3246. end;
  3247. exit;
  3248. end;
  3249. repeat
  3250. { load token from the buffer }
  3251. token:=readtoken;
  3252. if token<>_GENERICSPECIALTOKEN then
  3253. idtoken:=readtoken
  3254. else
  3255. idtoken:=_NOID;
  3256. case token of
  3257. _CWCHAR,
  3258. _CWSTRING :
  3259. begin
  3260. wlen:=tokenreadsizeint;
  3261. setlengthwidestring(patternw,wlen);
  3262. if wlen>0 then
  3263. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3264. orgpattern:='';
  3265. pattern:='';
  3266. cstringpattern:='';
  3267. end;
  3268. _CSTRING:
  3269. begin
  3270. wlen:=tokenreadsizeint;
  3271. if wlen>0 then
  3272. begin
  3273. setlength(cstringpattern,wlen);
  3274. replaytokenbuf.read(cstringpattern[1],wlen);
  3275. end
  3276. else
  3277. cstringpattern:='';
  3278. orgpattern:='';
  3279. pattern:='';
  3280. end;
  3281. _CCHAR,
  3282. _INTCONST,
  3283. _REALNUMBER :
  3284. begin
  3285. replaytokenbuf.read(pattern[0],1);
  3286. replaytokenbuf.read(pattern[1],length(pattern));
  3287. orgpattern:='';
  3288. end;
  3289. _ID :
  3290. begin
  3291. replaytokenbuf.read(orgpattern[0],1);
  3292. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3293. pattern:=upper(orgpattern);
  3294. end;
  3295. _GENERICSPECIALTOKEN:
  3296. begin
  3297. replaytokenbuf.read(specialtoken,1);
  3298. { packed column? }
  3299. if (ord(specialtoken) and $80)<>0 then
  3300. begin
  3301. current_tokenpos.column:=ord(specialtoken) and $7f;
  3302. current_filepos:=current_tokenpos;
  3303. end
  3304. else
  3305. case specialtoken of
  3306. ST_LOADSETTINGS:
  3307. begin
  3308. copy_size:=tokenreadsizeint;
  3309. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3310. // internalerror(2011090501);
  3311. {
  3312. replaytokenbuf.read(current_settings,copy_size);
  3313. }
  3314. tokenreadsettings(current_settings,copy_size);
  3315. end;
  3316. ST_LOADMESSAGES:
  3317. begin
  3318. current_settings.pmessage:=nil;
  3319. mesgnb:=tokenreadsizeint;
  3320. prevmsg:=nil;
  3321. for i:=1 to mesgnb do
  3322. begin
  3323. new(pmsg);
  3324. if i=1 then
  3325. current_settings.pmessage:=pmsg
  3326. else
  3327. prevmsg^.next:=pmsg;
  3328. pmsg^.value:=tokenreadlongint;
  3329. pmsg^.state:=tmsgstate(tokenreadlongint);
  3330. pmsg^.next:=nil;
  3331. prevmsg:=pmsg;
  3332. end;
  3333. end;
  3334. ST_LINE:
  3335. begin
  3336. current_tokenpos.line:=tokenreadlongint;
  3337. current_filepos:=current_tokenpos;
  3338. end;
  3339. ST_COLUMN:
  3340. begin
  3341. current_tokenpos.column:=tokenreadword;
  3342. current_filepos:=current_tokenpos;
  3343. end;
  3344. ST_FILEINDEX:
  3345. begin
  3346. current_tokenpos.fileindex:=tokenreadword;
  3347. current_filepos:=current_tokenpos;
  3348. end;
  3349. end;
  3350. continue;
  3351. end;
  3352. else
  3353. ;
  3354. end;
  3355. break;
  3356. until false;
  3357. end;
  3358. procedure tscannerfile.addfile(hp:tinputfile);
  3359. begin
  3360. saveinputfile;
  3361. { add to list }
  3362. hp.next:=inputfile;
  3363. inputfile:=hp;
  3364. { load new inputfile }
  3365. restoreinputfile;
  3366. end;
  3367. procedure tscannerfile.reload;
  3368. var
  3369. wasmacro: Boolean;
  3370. begin
  3371. with inputfile do
  3372. begin
  3373. { when nothing more to read then leave immediatly, so we
  3374. don't change the current_filepos and leave it point to the last
  3375. char }
  3376. if (c=#26) and (not assigned(next)) then
  3377. exit;
  3378. repeat
  3379. { still more to read?, then change the #0 to a space so its seen
  3380. as a separator, this can't be used for macro's which can change
  3381. the place of the #0 in the buffer with tempopen }
  3382. if (c=#0) and (bufsize>0) and
  3383. not(inputfile.is_macro) and
  3384. (inputpointer-inputbuffer<bufsize) then
  3385. begin
  3386. c:=' ';
  3387. inc(inputpointer);
  3388. exit;
  3389. end;
  3390. { can we read more from this file ? }
  3391. if (c<>#26) and (not endoffile) then
  3392. begin
  3393. readbuf;
  3394. inputpointer:=buf;
  3395. inputbuffer:=buf;
  3396. inputstart:=bufstart;
  3397. { first line? }
  3398. if line_no=0 then
  3399. begin
  3400. c:=inputpointer^;
  3401. { eat utf-8 signature? }
  3402. if (ord(inputpointer^)=$ef) and
  3403. (ord((inputpointer+1)^)=$bb) and
  3404. (ord((inputpointer+2)^)=$bf) then
  3405. begin
  3406. (* we don't support including files with an UTF-8 bom
  3407. inside another file that wasn't encoded as UTF-8
  3408. already (we don't support {$codepage xxx} switches in
  3409. the middle of a file either) *)
  3410. if (current_settings.sourcecodepage<>CP_UTF8) and
  3411. not current_module.in_global then
  3412. Message(scanner_f_illegal_utf8_bom);
  3413. inc(inputpointer,3);
  3414. message(scan_c_switching_to_utf8);
  3415. current_settings.sourcecodepage:=CP_UTF8;
  3416. exclude(current_settings.moduleswitches,cs_system_codepage);
  3417. include(current_settings.moduleswitches,cs_explicit_codepage);
  3418. end;
  3419. line_no:=1;
  3420. if cs_asm_source in current_settings.globalswitches then
  3421. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3422. end;
  3423. end
  3424. else
  3425. begin
  3426. wasmacro:=inputfile.is_macro;
  3427. { load eof position in tokenpos/current_filepos }
  3428. gettokenpos;
  3429. { close file }
  3430. closeinputfile;
  3431. { no next module, than EOF }
  3432. if not assigned(inputfile.next) then
  3433. begin
  3434. c:=#26;
  3435. exit;
  3436. end;
  3437. { load next file and reopen it }
  3438. nextfile;
  3439. tempopeninputfile;
  3440. { status }
  3441. Message1(scan_t_back_in,inputfile.name);
  3442. { end of include file is like a line break which ends e.g. also // style comments }
  3443. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3444. begin
  3445. c:=#10;
  3446. { ... but we have to decrease the line number first because it is increased due to this
  3447. inserted line break later on }
  3448. dec(line_no);
  3449. exit;
  3450. end;
  3451. end;
  3452. { load next char }
  3453. c:=inputpointer^;
  3454. inc(inputpointer);
  3455. until c<>#0; { if also end, then reload again }
  3456. end;
  3457. end;
  3458. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3459. var
  3460. hp : tinputfile;
  3461. begin
  3462. { save old postion }
  3463. dec(inputpointer);
  3464. tempcloseinputfile;
  3465. { create macro 'file' }
  3466. { use special name to dispose after !! }
  3467. hp:=do_openinputfile('_Macro_.'+macname);
  3468. addfile(hp);
  3469. with inputfile do
  3470. begin
  3471. inc(macro_nesting_depth);
  3472. setmacro(p,len);
  3473. { local buffer }
  3474. inputbuffer:=buf;
  3475. inputpointer:=buf;
  3476. inputstart:=bufstart;
  3477. ref_index:=fileindex;
  3478. internally_generated_macro:=internally_generated;
  3479. end;
  3480. { reset line }
  3481. line_no:=line;
  3482. lastlinepos:=0;
  3483. lasttokenpos:=0;
  3484. nexttokenpos:=0;
  3485. { load new c }
  3486. c:=inputpointer^;
  3487. inc(inputpointer);
  3488. end;
  3489. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3490. begin
  3491. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3492. filepos.line:=line_no;
  3493. filepos.column:=tokenpos-lastlinepos;
  3494. filepos.fileindex:=inputfile.ref_index;
  3495. filepos.moduleindex:=current_module.unit_index;
  3496. end;
  3497. procedure tscannerfile.gettokenpos;
  3498. { load the values of tokenpos and lasttokenpos }
  3499. begin
  3500. do_gettokenpos(lasttokenpos,current_tokenpos);
  3501. current_filepos:=current_tokenpos;
  3502. end;
  3503. procedure tscannerfile.cachenexttokenpos;
  3504. begin
  3505. do_gettokenpos(nexttokenpos,next_filepos);
  3506. end;
  3507. procedure tscannerfile.setnexttoken;
  3508. begin
  3509. token:=nexttoken;
  3510. nexttoken:=NOTOKEN;
  3511. lasttokenpos:=nexttokenpos;
  3512. current_tokenpos:=next_filepos;
  3513. current_filepos:=current_tokenpos;
  3514. nexttokenpos:=0;
  3515. end;
  3516. procedure tscannerfile.savetokenpos;
  3517. begin
  3518. oldlasttokenpos:=lasttokenpos;
  3519. oldcurrent_filepos:=current_filepos;
  3520. oldcurrent_tokenpos:=current_tokenpos;
  3521. end;
  3522. procedure tscannerfile.restoretokenpos;
  3523. begin
  3524. lasttokenpos:=oldlasttokenpos;
  3525. current_filepos:=oldcurrent_filepos;
  3526. current_tokenpos:=oldcurrent_tokenpos;
  3527. end;
  3528. procedure tscannerfile.inc_comment_level;
  3529. begin
  3530. if (m_nested_comment in current_settings.modeswitches) then
  3531. inc(comment_level)
  3532. else
  3533. comment_level:=1;
  3534. if (comment_level>1) then
  3535. begin
  3536. savetokenpos;
  3537. gettokenpos; { update for warning }
  3538. Message1(scan_w_comment_level,tostr(comment_level));
  3539. restoretokenpos;
  3540. end;
  3541. end;
  3542. procedure tscannerfile.dec_comment_level;
  3543. begin
  3544. if (m_nested_comment in current_settings.modeswitches) then
  3545. dec(comment_level)
  3546. else
  3547. comment_level:=0;
  3548. end;
  3549. procedure tscannerfile.linebreak;
  3550. var
  3551. cur : char;
  3552. begin
  3553. with inputfile do
  3554. begin
  3555. if (byte(inputpointer^)=0) and not(endoffile) then
  3556. begin
  3557. cur:=c;
  3558. reload;
  3559. if byte(cur)+byte(c)<>23 then
  3560. dec(inputpointer);
  3561. end
  3562. else
  3563. begin
  3564. { Support all combination of #10 and #13 as line break }
  3565. if (byte(inputpointer^)+byte(c)=23) then
  3566. inc(inputpointer);
  3567. end;
  3568. { Always return #10 as line break }
  3569. c:=#10;
  3570. { increase line counters }
  3571. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3572. inc(line_no);
  3573. { update linebuffer }
  3574. if cs_asm_source in current_settings.globalswitches then
  3575. inputfile.setline(line_no,lastlinepos);
  3576. { update for status and call the show status routine,
  3577. but don't touch current_filepos ! }
  3578. savetokenpos;
  3579. gettokenpos; { update for v_status }
  3580. inc(status.compiledlines);
  3581. ShowStatus;
  3582. restoretokenpos;
  3583. end;
  3584. end;
  3585. procedure tscannerfile.illegal_char(c:char);
  3586. var
  3587. s : string;
  3588. begin
  3589. if c in [#32..#255] then
  3590. s:=''''+c+''''
  3591. else
  3592. s:='#'+tostr(ord(c));
  3593. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3594. end;
  3595. procedure tscannerfile.end_of_file;
  3596. begin
  3597. checkpreprocstack;
  3598. Message(scan_f_end_of_file);
  3599. end;
  3600. {-------------------------------------------
  3601. IF Conditional Handling
  3602. -------------------------------------------}
  3603. procedure tscannerfile.checkpreprocstack;
  3604. begin
  3605. { check for missing ifdefs }
  3606. while assigned(preprocstack) do
  3607. begin
  3608. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3609. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3610. tostr(preprocstack.line_nb));
  3611. poppreprocstack;
  3612. end;
  3613. end;
  3614. procedure tscannerfile.poppreprocstack;
  3615. var
  3616. hp : tpreprocstack;
  3617. begin
  3618. if assigned(preprocstack) then
  3619. begin
  3620. Message1(scan_c_endif_found,preprocstack.name);
  3621. hp:=preprocstack.next;
  3622. preprocstack.free;
  3623. preprocstack:=hp;
  3624. end
  3625. else
  3626. Message(scan_e_endif_without_if);
  3627. end;
  3628. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3629. var
  3630. condition: Boolean;
  3631. valuedescr: String;
  3632. begin
  3633. if (preprocstack=nil) or preprocstack.accept then
  3634. condition:=compile_time_predicate(valuedescr)
  3635. else
  3636. begin
  3637. condition:= false;
  3638. valuedescr:= '';
  3639. end;
  3640. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3641. preprocstack.name:=valuedescr;
  3642. preprocstack.line_nb:=line_no;
  3643. preprocstack.fileindex:=current_filepos.fileindex;
  3644. if preprocstack.accept then
  3645. Message2(messid,preprocstack.name,'accepted')
  3646. else
  3647. Message2(messid,preprocstack.name,'rejected');
  3648. end;
  3649. procedure tscannerfile.elsepreprocstack;
  3650. begin
  3651. if assigned(preprocstack) and
  3652. (preprocstack.typ<>pp_else) then
  3653. begin
  3654. if (preprocstack.typ=pp_elseif) then
  3655. preprocstack.accept:=false
  3656. else
  3657. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3658. preprocstack.accept:=not preprocstack.accept;
  3659. preprocstack.iftyp:=preprocstack.typ;
  3660. preprocstack.typ:=pp_else;
  3661. preprocstack.line_nb:=line_no;
  3662. preprocstack.fileindex:=current_filepos.fileindex;
  3663. if preprocstack.accept then
  3664. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3665. else
  3666. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3667. end
  3668. else
  3669. Message(scan_e_endif_without_if);
  3670. end;
  3671. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3672. var
  3673. valuedescr: String;
  3674. begin
  3675. if assigned(preprocstack) and
  3676. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  3677. begin
  3678. { when the branch is accepted we use pp_elseif so we know that
  3679. all the next branches need to be rejected. when this branch is still
  3680. not accepted then leave it at pp_if }
  3681. if (preprocstack.typ=pp_elseif) then
  3682. preprocstack.accept:=false
  3683. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  3684. begin
  3685. preprocstack.accept:=false;
  3686. preprocstack.typ:=pp_elseif;
  3687. end
  3688. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  3689. and compile_time_predicate(valuedescr) then
  3690. begin
  3691. preprocstack.name:=valuedescr;
  3692. preprocstack.accept:=true;
  3693. preprocstack.typ:=pp_elseif;
  3694. end;
  3695. preprocstack.line_nb:=line_no;
  3696. preprocstack.fileindex:=current_filepos.fileindex;
  3697. if preprocstack.accept then
  3698. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3699. else
  3700. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3701. end
  3702. else
  3703. Message(scan_e_endif_without_if);
  3704. end;
  3705. procedure tscannerfile.popreplaystack;
  3706. var
  3707. hp : treplaystack;
  3708. begin
  3709. if assigned(replaystack) then
  3710. begin
  3711. hp:=replaystack.next;
  3712. replaystack.free;
  3713. replaystack:=hp;
  3714. end;
  3715. end;
  3716. function tscannerfile.replay_stack_depth:longint;
  3717. var
  3718. tmp: treplaystack;
  3719. begin
  3720. result:=0;
  3721. tmp:=replaystack;
  3722. while assigned(tmp) do
  3723. begin
  3724. inc(result);
  3725. tmp:=tmp.next;
  3726. end;
  3727. end;
  3728. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  3729. begin
  3730. savetokenpos;
  3731. repeat
  3732. current_scanner.gettokenpos;
  3733. Message1(scan_d_handling_switch,'$'+p.name);
  3734. p.proc();
  3735. { accept the text ? }
  3736. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  3737. break
  3738. else
  3739. begin
  3740. current_scanner.gettokenpos;
  3741. Message(scan_c_skipping_until);
  3742. repeat
  3743. current_scanner.skipuntildirective;
  3744. if not (m_mac in current_settings.modeswitches) then
  3745. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  3746. else
  3747. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  3748. until assigned(p) and (p.is_conditional);
  3749. current_scanner.gettokenpos;
  3750. end;
  3751. until false;
  3752. restoretokenpos;
  3753. end;
  3754. procedure tscannerfile.handledirectives;
  3755. var
  3756. t : tdirectiveitem;
  3757. hs : string;
  3758. begin
  3759. gettokenpos;
  3760. readchar; {Remove the $}
  3761. hs:=readid;
  3762. { handle empty directive }
  3763. if hs='' then
  3764. begin
  3765. Message1(scan_w_illegal_switch,'$');
  3766. exit;
  3767. end;
  3768. {$ifdef PREPROCWRITE}
  3769. if parapreprocess then
  3770. begin
  3771. if not (m_mac in current_settings.modeswitches) then
  3772. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3773. else
  3774. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3775. if assigned(t) and not(t.is_conditional) then
  3776. begin
  3777. preprocfile.AddSpace;
  3778. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  3779. exit;
  3780. end;
  3781. end;
  3782. {$endif PREPROCWRITE}
  3783. { skip this directive? }
  3784. if (ignoredirectives.find(hs)<>nil) then
  3785. begin
  3786. if (comment_level>0) then
  3787. readcomment;
  3788. { we've read the whole comment }
  3789. current_commentstyle:=comment_none;
  3790. exit;
  3791. end;
  3792. { Check for compiler switches }
  3793. while (length(hs)=1) and (c in ['-','+']) do
  3794. begin
  3795. Message1(scan_d_handling_switch,'$'+hs+c);
  3796. HandleSwitch(hs[1],c);
  3797. current_scanner.readchar; {Remove + or -}
  3798. if c=',' then
  3799. begin
  3800. current_scanner.readchar; {Remove , }
  3801. { read next switch, support $v+,$+}
  3802. hs:=current_scanner.readid;
  3803. if (hs='') then
  3804. begin
  3805. if (c='$') and (m_fpc in current_settings.modeswitches) then
  3806. begin
  3807. current_scanner.readchar; { skip $ }
  3808. hs:=current_scanner.readid;
  3809. end;
  3810. if (hs='') then
  3811. Message1(scan_w_illegal_directive,'$'+c);
  3812. end;
  3813. end
  3814. else
  3815. hs:='';
  3816. end;
  3817. { directives may follow switches after a , }
  3818. if hs<>'' then
  3819. begin
  3820. if not (m_mac in current_settings.modeswitches) then
  3821. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  3822. else
  3823. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  3824. if assigned(t) then
  3825. begin
  3826. if t.is_conditional then
  3827. handleconditional(t)
  3828. else
  3829. begin
  3830. Message1(scan_d_handling_switch,'$'+hs);
  3831. t.proc();
  3832. end;
  3833. end
  3834. else
  3835. begin
  3836. current_scanner.ignoredirectives.Add(hs,nil);
  3837. Message1(scan_w_illegal_directive,'$'+hs);
  3838. end;
  3839. { conditionals already read the comment }
  3840. if (current_scanner.comment_level>0) then
  3841. current_scanner.readcomment;
  3842. { we've read the whole comment }
  3843. current_commentstyle:=comment_none;
  3844. end;
  3845. end;
  3846. procedure tscannerfile.readchar;
  3847. begin
  3848. c:=inputpointer^;
  3849. if c=#0 then
  3850. reload
  3851. else
  3852. inc(inputpointer);
  3853. end;
  3854. procedure tscannerfile.readstring;
  3855. var
  3856. i : longint;
  3857. err : boolean;
  3858. begin
  3859. err:=false;
  3860. i:=0;
  3861. repeat
  3862. case c of
  3863. '_',
  3864. '0'..'9',
  3865. 'A'..'Z',
  3866. 'a'..'z' :
  3867. begin
  3868. if i<255 then
  3869. begin
  3870. inc(i);
  3871. orgpattern[i]:=c;
  3872. if c in ['a'..'z'] then
  3873. pattern[i]:=chr(ord(c)-32)
  3874. else
  3875. pattern[i]:=c;
  3876. end
  3877. else
  3878. begin
  3879. if not err then
  3880. begin
  3881. Message(scan_e_string_exceeds_255_chars);
  3882. err:=true;
  3883. end;
  3884. end;
  3885. c:=inputpointer^;
  3886. inc(inputpointer);
  3887. end;
  3888. #0 :
  3889. reload;
  3890. else if inputfile.internally_generated_macro and
  3891. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  3892. begin
  3893. if i<255 then
  3894. begin
  3895. inc(i);
  3896. orgpattern[i]:=c;
  3897. pattern[i]:=c;
  3898. end
  3899. else
  3900. begin
  3901. if not err then
  3902. begin
  3903. Message(scan_e_string_exceeds_255_chars);
  3904. err:=true;
  3905. end;
  3906. end;
  3907. c:=inputpointer^;
  3908. inc(inputpointer);
  3909. end
  3910. else
  3911. break;
  3912. end;
  3913. until false;
  3914. orgpattern[0]:=chr(i);
  3915. pattern[0]:=chr(i);
  3916. end;
  3917. procedure tscannerfile.readnumber;
  3918. var
  3919. base,
  3920. i : longint;
  3921. firstdigitread: Boolean;
  3922. begin
  3923. case c of
  3924. '%' :
  3925. begin
  3926. readchar;
  3927. base:=2;
  3928. pattern[1]:='%';
  3929. i:=1;
  3930. end;
  3931. '&' :
  3932. begin
  3933. readchar;
  3934. base:=8;
  3935. pattern[1]:='&';
  3936. i:=1;
  3937. end;
  3938. '$' :
  3939. begin
  3940. readchar;
  3941. base:=16;
  3942. pattern[1]:='$';
  3943. i:=1;
  3944. end;
  3945. else
  3946. begin
  3947. base:=10;
  3948. i:=0;
  3949. end;
  3950. end;
  3951. firstdigitread:=false;
  3952. while ((base>=10) and (c in ['0'..'9'])) or
  3953. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  3954. ((base=8) and (c in ['0'..'7'])) or
  3955. ((base=2) and (c in ['0'..'1'])) or
  3956. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  3957. begin
  3958. if (i<255) and (c<>'_') then
  3959. begin
  3960. inc(i);
  3961. pattern[i]:=c;
  3962. end;
  3963. readchar;
  3964. firstdigitread:=true;
  3965. end;
  3966. pattern[0]:=chr(i);
  3967. end;
  3968. function tscannerfile.readid:string;
  3969. begin
  3970. readstring;
  3971. readid:=pattern;
  3972. end;
  3973. function tscannerfile.readval:longint;
  3974. var
  3975. l : longint;
  3976. w : integer;
  3977. begin
  3978. readnumber;
  3979. val(pattern,l,w);
  3980. readval:=l;
  3981. end;
  3982. function tscannerfile.readcomment(include_special_char: boolean):string;
  3983. var
  3984. i : longint;
  3985. begin
  3986. i:=0;
  3987. repeat
  3988. case c of
  3989. '{' :
  3990. begin
  3991. if (include_special_char) and (i<255) then
  3992. begin
  3993. inc(i);
  3994. readcomment[i]:=c;
  3995. end;
  3996. if current_commentstyle=comment_tp then
  3997. inc_comment_level;
  3998. end;
  3999. '}' :
  4000. begin
  4001. if (include_special_char) and (i<255) then
  4002. begin
  4003. inc(i);
  4004. readcomment[i]:=c;
  4005. end;
  4006. if current_commentstyle=comment_tp then
  4007. begin
  4008. readchar;
  4009. dec_comment_level;
  4010. if comment_level=0 then
  4011. break
  4012. else
  4013. continue;
  4014. end;
  4015. end;
  4016. '*' :
  4017. begin
  4018. if current_commentstyle=comment_oldtp then
  4019. begin
  4020. readchar;
  4021. if c=')' then
  4022. begin
  4023. readchar;
  4024. dec_comment_level;
  4025. break;
  4026. end
  4027. else
  4028. { Add both characters !!}
  4029. if (i<255) then
  4030. begin
  4031. inc(i);
  4032. readcomment[i]:='*';
  4033. if (i<255) then
  4034. begin
  4035. inc(i);
  4036. readcomment[i]:=c;
  4037. end;
  4038. end;
  4039. end
  4040. else
  4041. { Not old TP comment, so add...}
  4042. begin
  4043. if (i<255) then
  4044. begin
  4045. inc(i);
  4046. readcomment[i]:='*';
  4047. end;
  4048. end;
  4049. end;
  4050. #10,#13 :
  4051. linebreak;
  4052. #26 :
  4053. end_of_file;
  4054. else
  4055. begin
  4056. if (i<255) then
  4057. begin
  4058. inc(i);
  4059. readcomment[i]:=c;
  4060. end;
  4061. end;
  4062. end;
  4063. readchar;
  4064. until false;
  4065. readcomment[0]:=chr(i);
  4066. end;
  4067. function tscannerfile.readquotedstring:string;
  4068. var
  4069. i : longint;
  4070. msgwritten : boolean;
  4071. begin
  4072. i:=0;
  4073. msgwritten:=false;
  4074. if (c='''') then
  4075. begin
  4076. repeat
  4077. readchar;
  4078. case c of
  4079. #26 :
  4080. end_of_file;
  4081. #10,#13 :
  4082. Message(scan_f_string_exceeds_line);
  4083. '''' :
  4084. begin
  4085. readchar;
  4086. if c<>'''' then
  4087. break;
  4088. end;
  4089. end;
  4090. if i<255 then
  4091. begin
  4092. inc(i);
  4093. result[i]:=c;
  4094. end
  4095. else
  4096. begin
  4097. if not msgwritten then
  4098. begin
  4099. Message(scan_e_string_exceeds_255_chars);
  4100. msgwritten:=true;
  4101. end;
  4102. end;
  4103. until false;
  4104. end;
  4105. result[0]:=chr(i);
  4106. end;
  4107. function tscannerfile.readstate:char;
  4108. var
  4109. state : char;
  4110. begin
  4111. state:=' ';
  4112. if c=' ' then
  4113. begin
  4114. current_scanner.skipspace;
  4115. current_scanner.readid;
  4116. if pattern='ON' then
  4117. state:='+'
  4118. else
  4119. if pattern='OFF' then
  4120. state:='-';
  4121. end
  4122. else
  4123. state:=c;
  4124. if not (state in ['+','-']) then
  4125. Message(scan_e_wrong_switch_toggle);
  4126. readstate:=state;
  4127. end;
  4128. function tscannerfile.readoptionalstate(fallback:char):char;
  4129. var
  4130. state : char;
  4131. begin
  4132. state:=' ';
  4133. if c=' ' then
  4134. begin
  4135. current_scanner.skipspace;
  4136. if c in ['*','}'] then
  4137. state:=fallback
  4138. else
  4139. begin
  4140. current_scanner.readid;
  4141. if pattern='ON' then
  4142. state:='+'
  4143. else
  4144. if pattern='OFF' then
  4145. state:='-';
  4146. end;
  4147. end
  4148. else
  4149. if c in ['*','}'] then
  4150. state:=fallback
  4151. else
  4152. state:=c;
  4153. if not (state in ['+','-']) then
  4154. Message(scan_e_wrong_switch_toggle);
  4155. readoptionalstate:=state;
  4156. end;
  4157. function tscannerfile.readstatedefault:char;
  4158. var
  4159. state : char;
  4160. begin
  4161. state:=' ';
  4162. if c=' ' then
  4163. begin
  4164. current_scanner.skipspace;
  4165. current_scanner.readid;
  4166. if pattern='ON' then
  4167. state:='+'
  4168. else
  4169. if pattern='OFF' then
  4170. state:='-'
  4171. else
  4172. if pattern='DEFAULT' then
  4173. state:='*';
  4174. end
  4175. else
  4176. state:=c;
  4177. if not (state in ['+','-','*']) then
  4178. Message(scan_e_wrong_switch_toggle_default);
  4179. readstatedefault:=state;
  4180. end;
  4181. procedure tscannerfile.skipspace;
  4182. begin
  4183. repeat
  4184. case c of
  4185. #26 :
  4186. begin
  4187. reload;
  4188. if (c=#26) and not assigned(inputfile.next) then
  4189. break;
  4190. continue;
  4191. end;
  4192. #10,
  4193. #13 :
  4194. linebreak;
  4195. #9,#11,#12,' ' :
  4196. ;
  4197. else
  4198. break;
  4199. end;
  4200. readchar;
  4201. until false;
  4202. end;
  4203. procedure tscannerfile.skipuntildirective;
  4204. var
  4205. found : longint;
  4206. next_char_loaded : boolean;
  4207. begin
  4208. found:=0;
  4209. next_char_loaded:=false;
  4210. repeat
  4211. case c of
  4212. #10,
  4213. #13 :
  4214. linebreak;
  4215. #26 :
  4216. begin
  4217. reload;
  4218. if (c=#26) and not assigned(inputfile.next) then
  4219. end_of_file;
  4220. continue;
  4221. end;
  4222. '{' :
  4223. begin
  4224. if (current_commentstyle in [comment_tp,comment_none]) then
  4225. begin
  4226. current_commentstyle:=comment_tp;
  4227. if (comment_level=0) then
  4228. found:=1;
  4229. inc_comment_level;
  4230. end;
  4231. end;
  4232. '*' :
  4233. begin
  4234. if (current_commentstyle=comment_oldtp) then
  4235. begin
  4236. readchar;
  4237. if c=')' then
  4238. begin
  4239. dec_comment_level;
  4240. found:=0;
  4241. current_commentstyle:=comment_none;
  4242. end
  4243. else
  4244. next_char_loaded:=true;
  4245. end
  4246. else
  4247. found := 0;
  4248. end;
  4249. '}' :
  4250. begin
  4251. if (current_commentstyle=comment_tp) then
  4252. begin
  4253. dec_comment_level;
  4254. if (comment_level=0) then
  4255. current_commentstyle:=comment_none;
  4256. found:=0;
  4257. end;
  4258. end;
  4259. '$' :
  4260. begin
  4261. if found=1 then
  4262. found:=2;
  4263. end;
  4264. '''' :
  4265. if (current_commentstyle=comment_none) then
  4266. begin
  4267. repeat
  4268. readchar;
  4269. case c of
  4270. #26 :
  4271. end_of_file;
  4272. #10,#13 :
  4273. break;
  4274. '''' :
  4275. begin
  4276. readchar;
  4277. if c<>'''' then
  4278. begin
  4279. next_char_loaded:=true;
  4280. break;
  4281. end;
  4282. end;
  4283. end;
  4284. until false;
  4285. end;
  4286. '(' :
  4287. begin
  4288. if (current_commentstyle=comment_none) then
  4289. begin
  4290. readchar;
  4291. if c='*' then
  4292. begin
  4293. readchar;
  4294. if c='$' then
  4295. begin
  4296. found:=2;
  4297. inc_comment_level;
  4298. current_commentstyle:=comment_oldtp;
  4299. end
  4300. else
  4301. begin
  4302. skipoldtpcomment(false);
  4303. next_char_loaded:=true;
  4304. end;
  4305. end
  4306. else
  4307. next_char_loaded:=true;
  4308. end
  4309. else
  4310. found:=0;
  4311. end;
  4312. '/' :
  4313. begin
  4314. if (current_commentstyle=comment_none) then
  4315. begin
  4316. readchar;
  4317. if c='/' then
  4318. skipdelphicomment;
  4319. next_char_loaded:=true;
  4320. end
  4321. else
  4322. found:=0;
  4323. end;
  4324. else
  4325. found:=0;
  4326. end;
  4327. if next_char_loaded then
  4328. next_char_loaded:=false
  4329. else
  4330. readchar;
  4331. until (found=2);
  4332. end;
  4333. {****************************************************************************
  4334. Comment Handling
  4335. ****************************************************************************}
  4336. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4337. begin
  4338. current_commentstyle:=comment_tp;
  4339. if read_first_char then
  4340. readchar;
  4341. inc_comment_level;
  4342. { handle compiler switches }
  4343. if (c='$') then
  4344. handledirectives;
  4345. { handle_switches can dec comment_level, }
  4346. while (comment_level>0) do
  4347. begin
  4348. case c of
  4349. '{' :
  4350. inc_comment_level;
  4351. '}' :
  4352. dec_comment_level;
  4353. '*' :
  4354. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4355. if m_iso in current_settings.modeswitches then
  4356. begin
  4357. readchar;
  4358. if c=')' then
  4359. dec_comment_level
  4360. else
  4361. continue;
  4362. end;
  4363. #10,#13 :
  4364. linebreak;
  4365. #26 :
  4366. begin
  4367. reload;
  4368. if (c=#26) and not assigned(inputfile.next) then
  4369. end_of_file;
  4370. continue;
  4371. end;
  4372. end;
  4373. readchar;
  4374. end;
  4375. current_commentstyle:=comment_none;
  4376. end;
  4377. procedure tscannerfile.skipdelphicomment;
  4378. begin
  4379. current_commentstyle:=comment_delphi;
  4380. inc_comment_level;
  4381. readchar;
  4382. { this is not supported }
  4383. if c='$' then
  4384. Message(scan_w_wrong_styled_switch);
  4385. { skip comment }
  4386. while not (c in [#10,#13,#26]) do
  4387. readchar;
  4388. dec_comment_level;
  4389. current_commentstyle:=comment_none;
  4390. end;
  4391. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4392. var
  4393. found : longint;
  4394. begin
  4395. current_commentstyle:=comment_oldtp;
  4396. inc_comment_level;
  4397. { only load a char if last already processed,
  4398. was cause of bug1634 PM }
  4399. if read_first_char then
  4400. readchar;
  4401. { this is now supported }
  4402. if (c='$') then
  4403. handledirectives;
  4404. { skip comment }
  4405. while (comment_level>0) do
  4406. begin
  4407. found:=0;
  4408. repeat
  4409. case c of
  4410. #26 :
  4411. begin
  4412. reload;
  4413. if (c=#26) and not assigned(inputfile.next) then
  4414. end_of_file;
  4415. continue;
  4416. end;
  4417. #10,#13 :
  4418. begin
  4419. if found=4 then
  4420. inc_comment_level;
  4421. linebreak;
  4422. found:=0;
  4423. end;
  4424. '*' :
  4425. begin
  4426. if found=3 then
  4427. found:=4
  4428. else
  4429. begin
  4430. if found=4 then
  4431. inc_comment_level;
  4432. found:=1;
  4433. end;
  4434. end;
  4435. ')' :
  4436. begin
  4437. if found in [1,4] then
  4438. begin
  4439. dec_comment_level;
  4440. if comment_level=0 then
  4441. found:=2
  4442. else
  4443. found:=0;
  4444. end
  4445. else
  4446. found:=0;
  4447. end;
  4448. '}' :
  4449. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  4450. if m_iso in current_settings.modeswitches then
  4451. begin
  4452. dec_comment_level;
  4453. if comment_level=0 then
  4454. found:=2
  4455. else
  4456. found:=0;
  4457. end;
  4458. '(' :
  4459. begin
  4460. if found=4 then
  4461. inc_comment_level;
  4462. found:=3;
  4463. end;
  4464. else
  4465. begin
  4466. if found=4 then
  4467. inc_comment_level;
  4468. found:=0;
  4469. end;
  4470. end;
  4471. readchar;
  4472. until (found=2);
  4473. end;
  4474. current_commentstyle:=comment_none;
  4475. end;
  4476. {****************************************************************************
  4477. Token Scanner
  4478. ****************************************************************************}
  4479. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4480. var
  4481. code : integer;
  4482. d : cardinal;
  4483. len,
  4484. low,high,mid : longint;
  4485. w : word;
  4486. m : longint;
  4487. mac : tmacro;
  4488. asciinr : string[33];
  4489. iswidestring , firstdigitread: boolean;
  4490. label
  4491. exit_label;
  4492. begin
  4493. flushpendingswitchesstate;
  4494. { record tokens? }
  4495. if allowrecordtoken and
  4496. assigned(recordtokenbuf) then
  4497. recordtoken;
  4498. { replay tokens? }
  4499. if assigned(replaytokenbuf) then
  4500. begin
  4501. replaytoken;
  4502. goto exit_label;
  4503. end;
  4504. { was there already a token read, then return that token }
  4505. if nexttoken<>NOTOKEN then
  4506. begin
  4507. setnexttoken;
  4508. goto exit_label;
  4509. end;
  4510. { Skip all spaces and comments }
  4511. repeat
  4512. case c of
  4513. '{' :
  4514. skipcomment(true);
  4515. #26 :
  4516. begin
  4517. reload;
  4518. if (c=#26) and not assigned(inputfile.next) then
  4519. break;
  4520. end;
  4521. ' ',#9..#13 :
  4522. begin
  4523. {$ifdef PREPROCWRITE}
  4524. if parapreprocess then
  4525. begin
  4526. if c=#10 then
  4527. preprocfile.eolfound:=true
  4528. else
  4529. preprocfile.spacefound:=true;
  4530. end;
  4531. {$endif PREPROCWRITE}
  4532. skipspace;
  4533. end
  4534. else
  4535. break;
  4536. end;
  4537. until false;
  4538. { Save current token position, for EOF its already loaded }
  4539. if c<>#26 then
  4540. gettokenpos;
  4541. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4542. if c in ['A'..'Z','a'..'z','_'] then
  4543. begin
  4544. readstring;
  4545. token:=_ID;
  4546. idtoken:=_ID;
  4547. { keyword or any other known token,
  4548. pattern is always uppercased }
  4549. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4550. begin
  4551. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4552. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4553. while low<high do
  4554. begin
  4555. mid:=(high+low+1) shr 1;
  4556. if pattern<tokeninfo^[ttoken(mid)].str then
  4557. high:=mid-1
  4558. else
  4559. low:=mid;
  4560. end;
  4561. with tokeninfo^[ttoken(high)] do
  4562. if pattern=str then
  4563. begin
  4564. if (keyword*current_settings.modeswitches)<>[] then
  4565. if op=NOTOKEN then
  4566. token:=ttoken(high)
  4567. else
  4568. token:=op;
  4569. idtoken:=ttoken(high);
  4570. end;
  4571. end;
  4572. { Only process identifiers and not keywords }
  4573. if token=_ID then
  4574. begin
  4575. { this takes some time ... }
  4576. if (cs_support_macro in current_settings.moduleswitches) then
  4577. begin
  4578. mac:=tmacro(search_macro(pattern));
  4579. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4580. begin
  4581. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  4582. begin
  4583. mac.is_used:=true;
  4584. inc(yylexcount);
  4585. substitutemacro(pattern,mac.buftext,mac.buflen,
  4586. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  4587. { handle empty macros }
  4588. if c=#0 then
  4589. begin
  4590. reload;
  4591. { avoid macro nesting error in case of
  4592. a sequence of empty macros, see #38802 }
  4593. dec(yylexcount);
  4594. readtoken(false);
  4595. end
  4596. else
  4597. begin
  4598. readtoken(false);
  4599. { that's all folks }
  4600. dec(yylexcount);
  4601. end;
  4602. exit;
  4603. end
  4604. else
  4605. Message(scan_w_macro_too_deep);
  4606. end;
  4607. end;
  4608. end;
  4609. { return token }
  4610. goto exit_label;
  4611. end
  4612. else
  4613. begin
  4614. idtoken:=_NOID;
  4615. case c of
  4616. '$' :
  4617. begin
  4618. readnumber;
  4619. token:=_INTCONST;
  4620. goto exit_label;
  4621. end;
  4622. '%' :
  4623. begin
  4624. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  4625. Illegal_Char(c)
  4626. else
  4627. begin
  4628. readnumber;
  4629. token:=_INTCONST;
  4630. goto exit_label;
  4631. end;
  4632. end;
  4633. '&' :
  4634. begin
  4635. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4636. begin
  4637. readnumber;
  4638. if length(pattern)=1 then
  4639. begin
  4640. { does really an identifier follow? }
  4641. if not (c in ['_','A'..'Z','a'..'z']) then
  4642. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4643. readstring;
  4644. token:=_ID;
  4645. idtoken:=_ID;
  4646. end
  4647. else
  4648. token:=_INTCONST;
  4649. goto exit_label;
  4650. end
  4651. else if m_mac in current_settings.modeswitches then
  4652. begin
  4653. readchar;
  4654. token:=_AMPERSAND;
  4655. goto exit_label;
  4656. end
  4657. else
  4658. Illegal_Char(c);
  4659. end;
  4660. '0'..'9' :
  4661. begin
  4662. readnumber;
  4663. if (c in ['.','e','E']) then
  4664. begin
  4665. { first check for a . }
  4666. if c='.' then
  4667. begin
  4668. cachenexttokenpos;
  4669. readchar;
  4670. { is it a .. from a range? }
  4671. case c of
  4672. '.' :
  4673. begin
  4674. readchar;
  4675. token:=_INTCONST;
  4676. nexttoken:=_POINTPOINT;
  4677. goto exit_label;
  4678. end;
  4679. ')' :
  4680. begin
  4681. readchar;
  4682. token:=_INTCONST;
  4683. nexttoken:=_RECKKLAMMER;
  4684. goto exit_label;
  4685. end;
  4686. '0'..'9' :
  4687. begin
  4688. { insert the number after the . }
  4689. pattern:=pattern+'.';
  4690. firstdigitread:=false;
  4691. while (c in ['0'..'9']) or
  4692. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4693. begin
  4694. if c<>'_' then
  4695. pattern:=pattern+c;
  4696. readchar;
  4697. firstdigitread:=true;
  4698. end;
  4699. end;
  4700. else
  4701. begin
  4702. token:=_INTCONST;
  4703. nexttoken:=_POINT;
  4704. goto exit_label;
  4705. end;
  4706. end;
  4707. end;
  4708. { E can also follow after a point is scanned }
  4709. if c in ['e','E'] then
  4710. begin
  4711. pattern:=pattern+'E';
  4712. readchar;
  4713. if c in ['-','+'] then
  4714. begin
  4715. pattern:=pattern+c;
  4716. readchar;
  4717. end;
  4718. if not(c in ['0'..'9']) then
  4719. Illegal_Char(c);
  4720. firstdigitread:=false;
  4721. while (c in ['0'..'9']) or
  4722. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4723. begin
  4724. if c<>'_' then
  4725. pattern:=pattern+c;
  4726. readchar;
  4727. firstdigitread:=true;
  4728. end;
  4729. end;
  4730. token:=_REALNUMBER;
  4731. goto exit_label;
  4732. end;
  4733. token:=_INTCONST;
  4734. goto exit_label;
  4735. end;
  4736. ';' :
  4737. begin
  4738. readchar;
  4739. token:=_SEMICOLON;
  4740. goto exit_label;
  4741. end;
  4742. '[' :
  4743. begin
  4744. readchar;
  4745. token:=_LECKKLAMMER;
  4746. goto exit_label;
  4747. end;
  4748. ']' :
  4749. begin
  4750. readchar;
  4751. token:=_RECKKLAMMER;
  4752. goto exit_label;
  4753. end;
  4754. '(' :
  4755. begin
  4756. readchar;
  4757. case c of
  4758. '*' :
  4759. begin
  4760. skipoldtpcomment(true);
  4761. readtoken(false);
  4762. exit;
  4763. end;
  4764. '.' :
  4765. begin
  4766. readchar;
  4767. token:=_LECKKLAMMER;
  4768. goto exit_label;
  4769. end;
  4770. end;
  4771. token:=_LKLAMMER;
  4772. goto exit_label;
  4773. end;
  4774. ')' :
  4775. begin
  4776. readchar;
  4777. token:=_RKLAMMER;
  4778. goto exit_label;
  4779. end;
  4780. '+' :
  4781. begin
  4782. readchar;
  4783. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4784. begin
  4785. readchar;
  4786. token:=_PLUSASN;
  4787. goto exit_label;
  4788. end;
  4789. token:=_PLUS;
  4790. goto exit_label;
  4791. end;
  4792. '-' :
  4793. begin
  4794. readchar;
  4795. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4796. begin
  4797. readchar;
  4798. token:=_MINUSASN;
  4799. goto exit_label;
  4800. end;
  4801. token:=_MINUS;
  4802. goto exit_label;
  4803. end;
  4804. ':' :
  4805. begin
  4806. readchar;
  4807. if c='=' then
  4808. begin
  4809. readchar;
  4810. token:=_ASSIGNMENT;
  4811. goto exit_label;
  4812. end;
  4813. token:=_COLON;
  4814. goto exit_label;
  4815. end;
  4816. '*' :
  4817. begin
  4818. readchar;
  4819. if (c='=') and (cs_support_c_operators in current_settings.moduleswitches) then
  4820. begin
  4821. readchar;
  4822. token:=_STARASN;
  4823. end
  4824. else
  4825. if c='*' then
  4826. begin
  4827. readchar;
  4828. token:=_STARSTAR;
  4829. end
  4830. else
  4831. token:=_STAR;
  4832. goto exit_label;
  4833. end;
  4834. '/' :
  4835. begin
  4836. readchar;
  4837. case c of
  4838. '=' :
  4839. begin
  4840. if (cs_support_c_operators in current_settings.moduleswitches) then
  4841. begin
  4842. readchar;
  4843. token:=_SLASHASN;
  4844. goto exit_label;
  4845. end;
  4846. end;
  4847. '/' :
  4848. begin
  4849. skipdelphicomment;
  4850. readtoken(false);
  4851. exit;
  4852. end;
  4853. end;
  4854. token:=_SLASH;
  4855. goto exit_label;
  4856. end;
  4857. '|' :
  4858. if m_mac in current_settings.modeswitches then
  4859. begin
  4860. readchar;
  4861. token:=_PIPE;
  4862. goto exit_label;
  4863. end
  4864. else
  4865. Illegal_Char(c);
  4866. '=' :
  4867. begin
  4868. readchar;
  4869. token:=_EQ;
  4870. goto exit_label;
  4871. end;
  4872. '.' :
  4873. begin
  4874. readchar;
  4875. case c of
  4876. '.' :
  4877. begin
  4878. readchar;
  4879. case c of
  4880. '.' :
  4881. begin
  4882. readchar;
  4883. token:=_POINTPOINTPOINT;
  4884. goto exit_label;
  4885. end;
  4886. else
  4887. begin
  4888. token:=_POINTPOINT;
  4889. goto exit_label;
  4890. end;
  4891. end;
  4892. end;
  4893. ')' :
  4894. begin
  4895. readchar;
  4896. token:=_RECKKLAMMER;
  4897. goto exit_label;
  4898. end;
  4899. end;
  4900. token:=_POINT;
  4901. goto exit_label;
  4902. end;
  4903. '@' :
  4904. begin
  4905. readchar;
  4906. token:=_KLAMMERAFFE;
  4907. goto exit_label;
  4908. end;
  4909. ',' :
  4910. begin
  4911. readchar;
  4912. token:=_COMMA;
  4913. goto exit_label;
  4914. end;
  4915. '''','#','^' :
  4916. begin
  4917. len:=0;
  4918. cstringpattern:='';
  4919. iswidestring:=false;
  4920. if c='^' then
  4921. begin
  4922. readchar;
  4923. c:=upcase(c);
  4924. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  4925. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  4926. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  4927. begin
  4928. token:=_CARET;
  4929. goto exit_label;
  4930. end
  4931. else
  4932. begin
  4933. inc(len);
  4934. setlength(cstringpattern,256);
  4935. if c<#64 then
  4936. cstringpattern[len]:=chr(ord(c)+64)
  4937. else
  4938. cstringpattern[len]:=chr(ord(c)-64);
  4939. readchar;
  4940. end;
  4941. end;
  4942. repeat
  4943. case c of
  4944. '#' :
  4945. begin
  4946. readchar; { read # }
  4947. case c of
  4948. '$':
  4949. begin
  4950. readchar; { read leading $ }
  4951. asciinr:='$';
  4952. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  4953. begin
  4954. asciinr:=asciinr+c;
  4955. readchar;
  4956. end;
  4957. end;
  4958. '&':
  4959. begin
  4960. readchar; { read leading $ }
  4961. asciinr:='&';
  4962. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  4963. begin
  4964. asciinr:=asciinr+c;
  4965. readchar;
  4966. end;
  4967. end;
  4968. '%':
  4969. begin
  4970. readchar; { read leading $ }
  4971. asciinr:='%';
  4972. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  4973. begin
  4974. asciinr:=asciinr+c;
  4975. readchar;
  4976. end;
  4977. end;
  4978. else
  4979. begin
  4980. asciinr:='';
  4981. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  4982. begin
  4983. asciinr:=asciinr+c;
  4984. readchar;
  4985. end;
  4986. end;
  4987. end;
  4988. val(asciinr,m,code);
  4989. if (asciinr='') or (code<>0) then
  4990. Message(scan_e_illegal_char_const)
  4991. else if (m<0) or (m>255) or (length(asciinr)>3) then
  4992. begin
  4993. if (m>=0) and (m<=$10FFFF) then
  4994. begin
  4995. if not iswidestring then
  4996. begin
  4997. if len>0 then
  4998. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  4999. else
  5000. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5001. iswidestring:=true;
  5002. len:=0;
  5003. end;
  5004. if m<=$FFFF then
  5005. concatwidestringchar(patternw,tcompilerwidechar(m))
  5006. else
  5007. begin
  5008. { split into surrogate pair }
  5009. dec(m,$10000);
  5010. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5011. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5012. end;
  5013. end
  5014. else
  5015. Message(scan_e_illegal_char_const)
  5016. end
  5017. else if iswidestring then
  5018. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5019. else
  5020. begin
  5021. if len>=length(cstringpattern) then
  5022. setlength(cstringpattern,length(cstringpattern)+256);
  5023. inc(len);
  5024. cstringpattern[len]:=chr(m);
  5025. end;
  5026. end;
  5027. '''' :
  5028. begin
  5029. repeat
  5030. readchar;
  5031. case c of
  5032. #26 :
  5033. end_of_file;
  5034. #10,#13 :
  5035. Message(scan_f_string_exceeds_line);
  5036. '''' :
  5037. begin
  5038. readchar;
  5039. if c<>'''' then
  5040. break;
  5041. end;
  5042. end;
  5043. { interpret as utf-8 string? }
  5044. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5045. begin
  5046. { convert existing string to an utf-8 string }
  5047. if not iswidestring then
  5048. begin
  5049. if len>0 then
  5050. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5051. else
  5052. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5053. iswidestring:=true;
  5054. len:=0;
  5055. end;
  5056. { four chars }
  5057. if (ord(c) and $f0)=$f0 then
  5058. begin
  5059. { this always represents a surrogate pair, so
  5060. read as 32-bit value and then split into
  5061. the corresponding pair of two wchars }
  5062. d:=ord(c) and $f;
  5063. readchar;
  5064. if (ord(c) and $c0)<>$80 then
  5065. message(scan_e_utf8_malformed);
  5066. d:=(d shl 6) or (ord(c) and $3f);
  5067. readchar;
  5068. if (ord(c) and $c0)<>$80 then
  5069. message(scan_e_utf8_malformed);
  5070. d:=(d shl 6) or (ord(c) and $3f);
  5071. readchar;
  5072. if (ord(c) and $c0)<>$80 then
  5073. message(scan_e_utf8_malformed);
  5074. d:=(d shl 6) or (ord(c) and $3f);
  5075. if d<$10000 then
  5076. message(scan_e_utf8_malformed);
  5077. d:=d-$10000;
  5078. { high surrogate }
  5079. w:=$d800+(d shr 10);
  5080. concatwidestringchar(patternw,w);
  5081. { low surrogate }
  5082. w:=$dc00+(d and $3ff);
  5083. concatwidestringchar(patternw,w);
  5084. end
  5085. { three chars }
  5086. else if (ord(c) and $e0)=$e0 then
  5087. begin
  5088. w:=ord(c) and $f;
  5089. readchar;
  5090. if (ord(c) and $c0)<>$80 then
  5091. message(scan_e_utf8_malformed);
  5092. w:=(w shl 6) or (ord(c) and $3f);
  5093. readchar;
  5094. if (ord(c) and $c0)<>$80 then
  5095. message(scan_e_utf8_malformed);
  5096. w:=(w shl 6) or (ord(c) and $3f);
  5097. concatwidestringchar(patternw,w);
  5098. end
  5099. { two chars }
  5100. else if (ord(c) and $c0)<>0 then
  5101. begin
  5102. w:=ord(c) and $1f;
  5103. readchar;
  5104. if (ord(c) and $c0)<>$80 then
  5105. message(scan_e_utf8_malformed);
  5106. w:=(w shl 6) or (ord(c) and $3f);
  5107. concatwidestringchar(patternw,w);
  5108. end
  5109. { illegal }
  5110. else if (ord(c) and $80)<>0 then
  5111. message(scan_e_utf8_malformed)
  5112. else
  5113. concatwidestringchar(patternw,tcompilerwidechar(c))
  5114. end
  5115. else if iswidestring then
  5116. begin
  5117. if current_settings.sourcecodepage=CP_UTF8 then
  5118. concatwidestringchar(patternw,ord(c))
  5119. else
  5120. concatwidestringchar(patternw,asciichar2unicode(c))
  5121. end
  5122. else
  5123. begin
  5124. if len>=length(cstringpattern) then
  5125. setlength(cstringpattern,length(cstringpattern)+256);
  5126. inc(len);
  5127. cstringpattern[len]:=c;
  5128. end;
  5129. until false;
  5130. end;
  5131. '^' :
  5132. begin
  5133. readchar;
  5134. c:=upcase(c);
  5135. if c<#64 then
  5136. c:=chr(ord(c)+64)
  5137. else
  5138. c:=chr(ord(c)-64);
  5139. if iswidestring then
  5140. concatwidestringchar(patternw,asciichar2unicode(c))
  5141. else
  5142. begin
  5143. if len>=length(cstringpattern) then
  5144. setlength(cstringpattern,length(cstringpattern)+256);
  5145. inc(len);
  5146. cstringpattern[len]:=c;
  5147. end;
  5148. readchar;
  5149. end;
  5150. else
  5151. break;
  5152. end;
  5153. until false;
  5154. { strings with length 1 become const chars }
  5155. if iswidestring then
  5156. begin
  5157. if patternw^.len=1 then
  5158. token:=_CWCHAR
  5159. else
  5160. token:=_CWSTRING;
  5161. end
  5162. else
  5163. begin
  5164. setlength(cstringpattern,len);
  5165. if length(cstringpattern)=1 then
  5166. begin
  5167. token:=_CCHAR;
  5168. pattern:=cstringpattern;
  5169. end
  5170. else
  5171. token:=_CSTRING;
  5172. end;
  5173. goto exit_label;
  5174. end;
  5175. '>' :
  5176. begin
  5177. readchar;
  5178. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5179. token:=_RSHARPBRACKET
  5180. else
  5181. begin
  5182. case c of
  5183. '=' :
  5184. begin
  5185. readchar;
  5186. token:=_GTE;
  5187. goto exit_label;
  5188. end;
  5189. '>' :
  5190. begin
  5191. readchar;
  5192. token:=_OP_SHR;
  5193. goto exit_label;
  5194. end;
  5195. '<' :
  5196. begin { >< is for a symetric diff for sets }
  5197. readchar;
  5198. token:=_SYMDIF;
  5199. goto exit_label;
  5200. end;
  5201. end;
  5202. token:=_GT;
  5203. end;
  5204. goto exit_label;
  5205. end;
  5206. '<' :
  5207. begin
  5208. readchar;
  5209. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5210. token:=_LSHARPBRACKET
  5211. else
  5212. begin
  5213. case c of
  5214. '>' :
  5215. begin
  5216. readchar;
  5217. token:=_NE;
  5218. goto exit_label;
  5219. end;
  5220. '=' :
  5221. begin
  5222. readchar;
  5223. token:=_LTE;
  5224. goto exit_label;
  5225. end;
  5226. '<' :
  5227. begin
  5228. readchar;
  5229. token:=_OP_SHL;
  5230. goto exit_label;
  5231. end;
  5232. end;
  5233. token:=_LT;
  5234. end;
  5235. goto exit_label;
  5236. end;
  5237. #26 :
  5238. begin
  5239. token:=_EOF;
  5240. checkpreprocstack;
  5241. goto exit_label;
  5242. end;
  5243. else if inputfile.internally_generated_macro and
  5244. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  5245. begin
  5246. token:=_ID;
  5247. readstring;
  5248. end
  5249. else
  5250. Illegal_Char(c);
  5251. end;
  5252. end;
  5253. exit_label:
  5254. lasttoken:=token;
  5255. end;
  5256. function tscannerfile.readpreproc:ttoken;
  5257. var
  5258. low,high,mid: longint;
  5259. optoken: ttoken;
  5260. begin
  5261. skipspace;
  5262. case c of
  5263. '_',
  5264. 'A'..'Z',
  5265. 'a'..'z' :
  5266. begin
  5267. readstring;
  5268. optoken:=_ID;
  5269. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5270. begin
  5271. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5272. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5273. while low<high do
  5274. begin
  5275. mid:=(high+low+1) shr 1;
  5276. if pattern<tokeninfo^[ttoken(mid)].str then
  5277. high:=mid-1
  5278. else
  5279. low:=mid;
  5280. end;
  5281. with tokeninfo^[ttoken(high)] do
  5282. if pattern=str then
  5283. begin
  5284. if (keyword*current_settings.modeswitches)<>[] then
  5285. if op=NOTOKEN then
  5286. optoken:=ttoken(high)
  5287. else
  5288. optoken:=op;
  5289. end;
  5290. if not (optoken in preproc_operators) then
  5291. optoken:=_ID;
  5292. end;
  5293. current_scanner.preproc_pattern:=pattern;
  5294. readpreproc:=optoken;
  5295. end;
  5296. '''' :
  5297. begin
  5298. readquotedstring;
  5299. current_scanner.preproc_pattern:=cstringpattern;
  5300. readpreproc:=_CSTRING;
  5301. end;
  5302. '0'..'9' :
  5303. begin
  5304. readnumber;
  5305. if (c in ['.','e','E']) then
  5306. begin
  5307. { first check for a . }
  5308. if c='.' then
  5309. begin
  5310. readchar;
  5311. if c in ['0'..'9'] then
  5312. begin
  5313. { insert the number after the . }
  5314. pattern:=pattern+'.';
  5315. while c in ['0'..'9'] do
  5316. begin
  5317. pattern:=pattern+c;
  5318. readchar;
  5319. end;
  5320. end
  5321. else
  5322. Illegal_Char(c);
  5323. end;
  5324. { E can also follow after a point is scanned }
  5325. if c in ['e','E'] then
  5326. begin
  5327. pattern:=pattern+'E';
  5328. readchar;
  5329. if c in ['-','+'] then
  5330. begin
  5331. pattern:=pattern+c;
  5332. readchar;
  5333. end;
  5334. if not(c in ['0'..'9']) then
  5335. Illegal_Char(c);
  5336. while c in ['0'..'9'] do
  5337. begin
  5338. pattern:=pattern+c;
  5339. readchar;
  5340. end;
  5341. end;
  5342. readpreproc:=_REALNUMBER;
  5343. end
  5344. else
  5345. readpreproc:=_INTCONST;
  5346. current_scanner.preproc_pattern:=pattern;
  5347. end;
  5348. '$','%':
  5349. begin
  5350. readnumber;
  5351. current_scanner.preproc_pattern:=pattern;
  5352. readpreproc:=_INTCONST;
  5353. end;
  5354. '&' :
  5355. begin
  5356. readnumber;
  5357. if length(pattern)=1 then
  5358. begin
  5359. readstring;
  5360. readpreproc:=_ID;
  5361. end
  5362. else
  5363. readpreproc:=_INTCONST;
  5364. current_scanner.preproc_pattern:=pattern;
  5365. end;
  5366. '.' :
  5367. begin
  5368. readchar;
  5369. readpreproc:=_POINT;
  5370. end;
  5371. ',' :
  5372. begin
  5373. readchar;
  5374. readpreproc:=_COMMA;
  5375. end;
  5376. '}' :
  5377. begin
  5378. readpreproc:=_END;
  5379. end;
  5380. '(' :
  5381. begin
  5382. readchar;
  5383. readpreproc:=_LKLAMMER;
  5384. end;
  5385. ')' :
  5386. begin
  5387. readchar;
  5388. readpreproc:=_RKLAMMER;
  5389. end;
  5390. '[' :
  5391. begin
  5392. readchar;
  5393. readpreproc:=_LECKKLAMMER;
  5394. end;
  5395. ']' :
  5396. begin
  5397. readchar;
  5398. readpreproc:=_RECKKLAMMER;
  5399. end;
  5400. '+' :
  5401. begin
  5402. readchar;
  5403. readpreproc:=_PLUS;
  5404. end;
  5405. '-' :
  5406. begin
  5407. readchar;
  5408. readpreproc:=_MINUS;
  5409. end;
  5410. '*' :
  5411. begin
  5412. readchar;
  5413. readpreproc:=_STAR;
  5414. end;
  5415. '/' :
  5416. begin
  5417. readchar;
  5418. readpreproc:=_SLASH;
  5419. end;
  5420. '=' :
  5421. begin
  5422. readchar;
  5423. readpreproc:=_EQ;
  5424. end;
  5425. '>' :
  5426. begin
  5427. readchar;
  5428. if c='=' then
  5429. begin
  5430. readchar;
  5431. readpreproc:=_GTE;
  5432. end
  5433. else
  5434. readpreproc:=_GT;
  5435. end;
  5436. '<' :
  5437. begin
  5438. readchar;
  5439. case c of
  5440. '>' :
  5441. begin
  5442. readchar;
  5443. readpreproc:=_NE;
  5444. end;
  5445. '=' :
  5446. begin
  5447. readchar;
  5448. readpreproc:=_LTE;
  5449. end;
  5450. else
  5451. readpreproc:=_LT;
  5452. end;
  5453. end;
  5454. #26 :
  5455. begin
  5456. readpreproc:=_EOF;
  5457. checkpreprocstack;
  5458. end;
  5459. else
  5460. begin
  5461. Illegal_Char(c);
  5462. readpreproc:=NOTOKEN;
  5463. end;
  5464. end;
  5465. end;
  5466. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5467. var
  5468. hs : texprvalue;
  5469. begin
  5470. hs:=preproc_comp_expr;
  5471. if hs.isInt then
  5472. begin
  5473. value:=hs.asInt64;
  5474. result:=true;
  5475. end
  5476. else
  5477. begin
  5478. hs.error('Integer',place);
  5479. result:=false;
  5480. end;
  5481. hs.free;
  5482. end;
  5483. function tscannerfile.asmgetchar : char;
  5484. begin
  5485. readchar;
  5486. repeat
  5487. case c of
  5488. #26 :
  5489. begin
  5490. reload;
  5491. if (c=#26) and not assigned(inputfile.next) then
  5492. end_of_file;
  5493. continue;
  5494. end;
  5495. else
  5496. begin
  5497. asmgetchar:=c;
  5498. exit;
  5499. end;
  5500. end;
  5501. until false;
  5502. end;
  5503. {*****************************************************************************
  5504. Helpers
  5505. *****************************************************************************}
  5506. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5507. begin
  5508. if dm in [directive_all, directive_turbo] then
  5509. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5510. if dm in [directive_all, directive_mac] then
  5511. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5512. end;
  5513. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5514. begin
  5515. if dm in [directive_all, directive_turbo] then
  5516. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5517. if dm in [directive_all, directive_mac] then
  5518. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5519. end;
  5520. {*****************************************************************************
  5521. Initialization
  5522. *****************************************************************************}
  5523. procedure InitScanner;
  5524. begin
  5525. InitWideString(patternw);
  5526. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5527. mac_scannerdirectives:=TFPHashObjectList.Create;
  5528. { Common directives and conditionals }
  5529. AddDirective('I',directive_all, @dir_include);
  5530. AddDirective('DEFINE',directive_all, @dir_define);
  5531. AddDirective('UNDEF',directive_all, @dir_undef);
  5532. AddConditional('IF',directive_all, @dir_if);
  5533. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5534. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5535. AddConditional('ELSE',directive_all, @dir_else);
  5536. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5537. AddConditional('ENDIF',directive_all, @dir_endif);
  5538. { Directives and conditionals for all modes except mode macpas}
  5539. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5540. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5541. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5542. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5543. AddConditional('IFEND',directive_turbo, @dir_ifend);
  5544. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5545. { Directives and conditionals for mode macpas: }
  5546. AddDirective('SETC',directive_mac, @dir_setc);
  5547. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5548. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5549. AddConditional('IFC',directive_mac, @dir_if);
  5550. AddConditional('ELSEC',directive_mac, @dir_else);
  5551. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5552. AddConditional('ENDC',directive_mac, @dir_endif);
  5553. end;
  5554. procedure DoneScanner;
  5555. begin
  5556. turbo_scannerdirectives.Free;
  5557. mac_scannerdirectives.Free;
  5558. DoneWideString(patternw);
  5559. end;
  5560. end.