scanner.pas 218 KB

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