scanner.pas 217 KB

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