scanner.pas 218 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366
  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: 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. if (not found) and (ExtractFileExt(name)='') then
  1768. begin
  1769. { try default extensions .inc , .pp and .pas }
  1770. if (not found) then
  1771. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  1772. if (not found) then
  1773. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  1774. if (not found) then
  1775. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  1776. end;
  1777. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  1778. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  1779. result:=texprvalue.create_bool(found);
  1780. current_scanner.skipspace;
  1781. preproc_consume(_RKLAMMER);
  1782. end
  1783. else
  1784. if current_scanner.preproc_pattern='DEFINED' then
  1785. begin
  1786. preproc_consume(_ID);
  1787. current_scanner.skipspace;
  1788. if current_scanner.preproc_token =_LKLAMMER then
  1789. begin
  1790. preproc_consume(_LKLAMMER);
  1791. current_scanner.skipspace;
  1792. hasKlammer:= true;
  1793. end
  1794. else if (m_mac in current_settings.modeswitches) then
  1795. hasKlammer:= false
  1796. else
  1797. Message(scan_e_error_in_preproc_expr);
  1798. if current_scanner.preproc_token =_ID then
  1799. begin
  1800. hs := current_scanner.preproc_pattern;
  1801. mac := tmacro(search_macro(hs));
  1802. if assigned(mac) and mac.defined then
  1803. begin
  1804. result:=texprvalue.create_bool(true);
  1805. mac.is_used:=true;
  1806. end
  1807. else
  1808. result:=texprvalue.create_bool(false);
  1809. preproc_consume(_ID);
  1810. current_scanner.skipspace;
  1811. end
  1812. else
  1813. Message(scan_e_error_in_preproc_expr);
  1814. if hasKlammer then
  1815. if current_scanner.preproc_token =_RKLAMMER then
  1816. preproc_consume(_RKLAMMER)
  1817. else
  1818. Message(scan_e_error_in_preproc_expr);
  1819. end
  1820. else
  1821. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1822. begin
  1823. preproc_consume(_ID);
  1824. current_scanner.skipspace;
  1825. if current_scanner.preproc_token =_ID then
  1826. begin
  1827. hs := current_scanner.preproc_pattern;
  1828. mac := tmacro(search_macro(hs));
  1829. if assigned(mac) then
  1830. begin
  1831. result:=texprvalue.create_bool(false);
  1832. mac.is_used:=true;
  1833. end
  1834. else
  1835. result:=texprvalue.create_bool(true);
  1836. preproc_consume(_ID);
  1837. current_scanner.skipspace;
  1838. end
  1839. else
  1840. Message(scan_e_error_in_preproc_expr);
  1841. end
  1842. else
  1843. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1844. begin
  1845. preproc_consume(_ID);
  1846. current_scanner.skipspace;
  1847. if current_scanner.preproc_token =_LKLAMMER then
  1848. begin
  1849. preproc_consume(_LKLAMMER);
  1850. current_scanner.skipspace;
  1851. end
  1852. else
  1853. Message(scan_e_error_in_preproc_expr);
  1854. if not (current_scanner.preproc_token = _ID) then
  1855. Message(scan_e_error_in_preproc_expr);
  1856. hs:=current_scanner.preproc_pattern;
  1857. if (length(hs) > 1) then
  1858. {This is allowed in Metrowerks Pascal}
  1859. Message(scan_e_error_in_preproc_expr)
  1860. else
  1861. begin
  1862. if CheckSwitch(hs[1],'+') then
  1863. result:=texprvalue.create_bool(true)
  1864. else
  1865. result:=texprvalue.create_bool(false);
  1866. end;
  1867. preproc_consume(_ID);
  1868. current_scanner.skipspace;
  1869. if current_scanner.preproc_token =_RKLAMMER then
  1870. preproc_consume(_RKLAMMER)
  1871. else
  1872. Message(scan_e_error_in_preproc_expr);
  1873. end
  1874. else
  1875. if current_scanner.preproc_pattern='SIZEOF' then
  1876. begin
  1877. preproc_consume(_ID);
  1878. current_scanner.skipspace;
  1879. if current_scanner.preproc_token =_LKLAMMER then
  1880. begin
  1881. preproc_consume(_LKLAMMER);
  1882. current_scanner.skipspace;
  1883. end
  1884. else
  1885. Message(scan_e_preproc_syntax_error);
  1886. storedpattern:=current_scanner.preproc_pattern;
  1887. preproc_consume(_ID);
  1888. current_scanner.skipspace;
  1889. if eval then
  1890. if searchsym(storedpattern,srsym,srsymtable) then
  1891. begin
  1892. try_consume_nestedsym(srsym,srsymtable);
  1893. l:=0;
  1894. if assigned(srsym) then
  1895. case srsym.typ of
  1896. staticvarsym,
  1897. localvarsym,
  1898. paravarsym :
  1899. begin
  1900. l:=tabstractvarsym(srsym).getsize;
  1901. MarkSymbolAsUsed(srsym);
  1902. end;
  1903. typesym:
  1904. begin
  1905. l:=ttypesym(srsym).typedef.size;
  1906. MarkSymbolAsUsed(srsym);
  1907. end;
  1908. else
  1909. Message(scan_e_error_in_preproc_expr);
  1910. end;
  1911. result:=texprvalue.create_int(l);
  1912. end
  1913. else
  1914. Message1(sym_e_id_not_found,storedpattern);
  1915. if current_scanner.preproc_token =_RKLAMMER then
  1916. preproc_consume(_RKLAMMER)
  1917. else
  1918. Message(scan_e_preproc_syntax_error);
  1919. end
  1920. else
  1921. if current_scanner.preproc_pattern='HIGH' then
  1922. begin
  1923. preproc_consume(_ID);
  1924. current_scanner.skipspace;
  1925. if current_scanner.preproc_token =_LKLAMMER then
  1926. begin
  1927. preproc_consume(_LKLAMMER);
  1928. current_scanner.skipspace;
  1929. end
  1930. else
  1931. Message(scan_e_preproc_syntax_error);
  1932. storedpattern:=current_scanner.preproc_pattern;
  1933. preproc_consume(_ID);
  1934. current_scanner.skipspace;
  1935. if eval then
  1936. if searchsym(storedpattern,srsym,srsymtable) then
  1937. begin
  1938. try_consume_nestedsym(srsym,srsymtable);
  1939. hdef:=nil;
  1940. hs:='';
  1941. l:=0;
  1942. if assigned(srsym) then
  1943. case srsym.typ of
  1944. staticvarsym,
  1945. localvarsym,
  1946. paravarsym :
  1947. begin
  1948. hdef:=tabstractvarsym(srsym).vardef;
  1949. MarkSymbolAsUsed(srsym);
  1950. end;
  1951. typesym:
  1952. begin
  1953. hdef:=ttypesym(srsym).typedef;
  1954. MarkSymbolAsUsed(srsym);
  1955. end;
  1956. else
  1957. Message(scan_e_error_in_preproc_expr);
  1958. end;
  1959. if assigned(hdef) then
  1960. begin
  1961. if hdef.typ=setdef then
  1962. hdef:=tsetdef(hdef).elementdef;
  1963. case hdef.typ of
  1964. orddef:
  1965. with torddef(hdef).high do
  1966. if signed then
  1967. result:=texprvalue.create_int(svalue)
  1968. else
  1969. result:=texprvalue.create_uint(uvalue);
  1970. enumdef:
  1971. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1972. arraydef:
  1973. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1974. Message(type_e_mismatch)
  1975. else
  1976. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1977. stringdef:
  1978. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1979. Message(type_e_mismatch)
  1980. else
  1981. result:=texprvalue.create_int(tstringdef(hdef).len);
  1982. else
  1983. Message(type_e_mismatch);
  1984. end;
  1985. end;
  1986. end
  1987. else
  1988. Message1(sym_e_id_not_found,storedpattern);
  1989. if current_scanner.preproc_token =_RKLAMMER then
  1990. preproc_consume(_RKLAMMER)
  1991. else
  1992. Message(scan_e_preproc_syntax_error);
  1993. end
  1994. else
  1995. if current_scanner.preproc_pattern='DECLARED' then
  1996. begin
  1997. preproc_consume(_ID);
  1998. current_scanner.skipspace;
  1999. if current_scanner.preproc_token =_LKLAMMER then
  2000. begin
  2001. preproc_consume(_LKLAMMER);
  2002. current_scanner.skipspace;
  2003. end
  2004. else
  2005. Message(scan_e_error_in_preproc_expr);
  2006. if current_scanner.preproc_token =_ID then
  2007. begin
  2008. hs := upper(current_scanner.preproc_pattern);
  2009. preproc_consume(_ID);
  2010. current_scanner.skipspace;
  2011. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  2012. begin
  2013. l:=1;
  2014. preproc_consume(current_scanner.preproc_token);
  2015. current_scanner.skipspace;
  2016. while current_scanner.preproc_token=_COMMA do
  2017. begin
  2018. inc(l);
  2019. preproc_consume(_COMMA);
  2020. current_scanner.skipspace;
  2021. end;
  2022. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  2023. Message(scan_e_error_in_preproc_expr)
  2024. else
  2025. preproc_consume(current_scanner.preproc_token);
  2026. str(l,countstr);
  2027. hs:=hs+'$'+countstr;
  2028. end
  2029. else
  2030. { special case: <> }
  2031. if current_scanner.preproc_token=_NE then
  2032. begin
  2033. hs:=hs+'$1';
  2034. preproc_consume(_NE);
  2035. end;
  2036. current_scanner.skipspace;
  2037. if searchsym(hs,srsym,srsymtable) then
  2038. begin
  2039. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  2040. if (sp_generic_dummy in srsym.symoptions) and
  2041. (srsym.typ=typesym) and
  2042. (
  2043. { mode delphi}
  2044. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  2045. { non-delphi modes }
  2046. (df_generic in ttypesym(srsym).typedef.defoptions)
  2047. ) then
  2048. result:=texprvalue.create_bool(false)
  2049. else
  2050. result:=texprvalue.create_bool(true);
  2051. MarkSymbolAsUsed(srsym);
  2052. end
  2053. else
  2054. result:=texprvalue.create_bool(false);
  2055. end
  2056. else
  2057. Message(scan_e_error_in_preproc_expr);
  2058. if current_scanner.preproc_token =_RKLAMMER then
  2059. preproc_consume(_RKLAMMER)
  2060. else
  2061. Message(scan_e_error_in_preproc_expr);
  2062. end
  2063. else
  2064. if current_scanner.preproc_pattern='ORD' then
  2065. begin
  2066. preproc_consume(_ID);
  2067. current_scanner.skipspace;
  2068. if current_scanner.preproc_token =_LKLAMMER then
  2069. begin
  2070. preproc_consume(_LKLAMMER);
  2071. current_scanner.skipspace;
  2072. end
  2073. else
  2074. Message(scan_e_preproc_syntax_error);
  2075. exprvalue:=preproc_factor(eval);
  2076. if eval then
  2077. begin
  2078. if is_ordinal(exprvalue.def) then
  2079. result:=texprvalue.create_int(exprvalue.asInt)
  2080. else
  2081. begin
  2082. exprvalue.error('Ordinal','ORD');
  2083. result:=texprvalue.create_int(0);
  2084. end;
  2085. end
  2086. else
  2087. result:=texprvalue.create_int(0);
  2088. exprvalue.free;
  2089. if current_scanner.preproc_token =_RKLAMMER then
  2090. preproc_consume(_RKLAMMER)
  2091. else
  2092. Message(scan_e_error_in_preproc_expr);
  2093. end
  2094. else
  2095. if current_scanner.preproc_pattern='NOT' then
  2096. begin
  2097. preproc_consume(_ID);
  2098. exprvalue:=preproc_factor(eval);
  2099. if eval then
  2100. result:=exprvalue.evaluate(nil,_OP_NOT)
  2101. else
  2102. result:=texprvalue.create_bool(false); {Just to have something}
  2103. exprvalue.free;
  2104. end
  2105. else
  2106. if (current_scanner.preproc_pattern='TRUE') then
  2107. begin
  2108. result:=texprvalue.create_bool(true);
  2109. preproc_consume(_ID);
  2110. end
  2111. else
  2112. if (current_scanner.preproc_pattern='FALSE') then
  2113. begin
  2114. result:=texprvalue.create_bool(false);
  2115. preproc_consume(_ID);
  2116. end
  2117. else
  2118. begin
  2119. storedpattern:=current_scanner.preproc_pattern;
  2120. preproc_consume(_ID);
  2121. current_scanner.skipspace;
  2122. { first look for a macros/int/float }
  2123. result:=preproc_substitutedtoken(storedpattern,eval);
  2124. if eval and (result.consttyp=conststring) then
  2125. begin
  2126. if searchsym(storedpattern,srsym,srsymtable) then
  2127. begin
  2128. try_consume_nestedsym(srsym,srsymtable);
  2129. if assigned(srsym) then
  2130. begin
  2131. MarkSymbolAsUsed(srsym);
  2132. case srsym.typ of
  2133. constsym:
  2134. begin
  2135. { const def must conform to the set type }
  2136. if (conform_to<>nil) and
  2137. (conform_to.typ=setdef) and
  2138. (tconstsym(srsym).constdef.typ=setdef) and
  2139. (compare_defs(tsetdef(tconstsym(srsym).constdef).elementdef,tsetdef(conform_to).elementdef,nothingn)<>te_exact) then
  2140. begin
  2141. result.free;
  2142. result:=nil;
  2143. // TODO(ryan): better error?
  2144. Message(scan_e_error_in_preproc_expr);
  2145. end;
  2146. if result<>nil then
  2147. begin
  2148. result.free;
  2149. result:=texprvalue.create_const(tconstsym(srsym));
  2150. end;
  2151. end;
  2152. enumsym:
  2153. begin
  2154. { enum definition must conform to the set type }
  2155. if (conform_to<>nil) and
  2156. (conform_to.typ=setdef) and
  2157. (compare_defs(tenumsym(srsym).definition,tsetdef(conform_to).elementdef,nothingn)<>te_exact) then
  2158. begin
  2159. result.free;
  2160. result:=nil;
  2161. // TODO(ryan): better error?
  2162. Message(scan_e_error_in_preproc_expr);
  2163. end;
  2164. if result<>nil then
  2165. begin
  2166. result.free;
  2167. result:=texprvalue.create_int(tenumsym(srsym).value);
  2168. end;
  2169. end;
  2170. else
  2171. ;
  2172. end;
  2173. end;
  2174. end
  2175. { the id must be belong to the set type }
  2176. else if (conform_to<>nil) and (conform_to.typ=setdef) then
  2177. begin
  2178. result.free;
  2179. result:=nil;
  2180. // TODO(ryan): better error?
  2181. Message(scan_e_error_in_preproc_expr);
  2182. end;
  2183. end
  2184. { skip id(<expr>) if expression must not be evaluated }
  2185. else if not(eval) and (result.consttyp=conststring) then
  2186. begin
  2187. if current_scanner.preproc_token =_LKLAMMER then
  2188. begin
  2189. preproc_consume(_LKLAMMER);
  2190. current_scanner.skipspace;
  2191. result:=preproc_factor(false);
  2192. if current_scanner.preproc_token =_RKLAMMER then
  2193. preproc_consume(_RKLAMMER)
  2194. else
  2195. Message(scan_e_error_in_preproc_expr);
  2196. end;
  2197. end;
  2198. end
  2199. end
  2200. else if current_scanner.preproc_token =_LKLAMMER then
  2201. begin
  2202. preproc_consume(_LKLAMMER);
  2203. result:=preproc_sub_expr(opcompare,eval);
  2204. preproc_consume(_RKLAMMER);
  2205. end
  2206. else if current_scanner.preproc_token = _LECKKLAMMER then
  2207. begin
  2208. preproc_consume(_LECKKLAMMER);
  2209. ns:=[];
  2210. read_next:=false;
  2211. while (current_scanner.preproc_token in [_ID,_INTCONST]) or read_next do
  2212. begin
  2213. read_next:=false;
  2214. exprvalue:=preproc_factor(eval);
  2215. { the const set does not conform to the set def }
  2216. if (conform_to<>nil) and
  2217. (conform_to.typ=setdef) and
  2218. (exprvalue.consttyp=constnone) then
  2219. begin
  2220. result:=texprvalue.create_error;
  2221. break;
  2222. end;
  2223. { reject duplicate enums in the set }
  2224. if exprvalue.asInt in ns then
  2225. begin
  2226. Message1(sym_e_duplicate_id,current_scanner.preproc_pattern);
  2227. result:=texprvalue.create_error;
  2228. break;
  2229. end;
  2230. include(ns,exprvalue.asInt);
  2231. if current_scanner.preproc_token = _COMMA then
  2232. begin
  2233. preproc_consume(_COMMA);
  2234. read_next:=true;
  2235. end
  2236. end;
  2237. preproc_consume(_RECKKLAMMER);
  2238. if result=nil then
  2239. result:=texprvalue.create_set(ns);
  2240. end
  2241. else if current_scanner.preproc_token = _INTCONST then
  2242. begin
  2243. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  2244. if not assigned(result) then
  2245. begin
  2246. Message(parser_e_invalid_integer);
  2247. result:=texprvalue.create_int(1);
  2248. end;
  2249. preproc_consume(_INTCONST);
  2250. end
  2251. else if current_scanner.preproc_token = _CSTRING then
  2252. begin
  2253. result:=texprvalue.create_str(current_scanner.preproc_pattern);
  2254. preproc_consume(_CSTRING);
  2255. end
  2256. else if current_scanner.preproc_token = _REALNUMBER then
  2257. begin
  2258. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  2259. if not assigned(result) then
  2260. begin
  2261. Message(parser_e_error_in_real);
  2262. result:=texprvalue.create_real(1.0);
  2263. end;
  2264. preproc_consume(_REALNUMBER);
  2265. end
  2266. else if current_scanner.preproc_token = _MINUS then
  2267. begin
  2268. preproc_consume(_MINUS);
  2269. exprvalue:=preproc_factor(eval);
  2270. if eval then
  2271. result:=exprvalue.evaluate(nil,_MINUS)
  2272. else if exprvalue.isInt then
  2273. result:=texprvalue.create_int(-exprvalue.asInt)
  2274. else if is_fpu(exprvalue.def) then
  2275. result:=texprvalue.create_real(-pbestreal(exprvalue.value.valueptr)^)
  2276. else
  2277. Message(scan_e_error_in_preproc_expr);
  2278. exprvalue.free;
  2279. end
  2280. else
  2281. Message(scan_e_error_in_preproc_expr);
  2282. if not assigned(result) then
  2283. result:=texprvalue.create_error;
  2284. end;
  2285. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  2286. var
  2287. hs1,hs2: texprvalue;
  2288. op: ttoken;
  2289. begin
  2290. if pred_level=highest_precedence then
  2291. result:=preproc_factor(eval)
  2292. else
  2293. result:=preproc_sub_expr(succ(pred_level),eval);
  2294. repeat
  2295. op:=current_scanner.preproc_token;
  2296. if (op in preproc_operators) and
  2297. (op in operator_levels[pred_level]) then
  2298. begin
  2299. hs1:=result;
  2300. preproc_consume(op);
  2301. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  2302. begin
  2303. { stop evaluation the rest of expression }
  2304. result:=texprvalue.create_bool(true);
  2305. if pred_level=highest_precedence then
  2306. hs2:=preproc_factor(false)
  2307. else
  2308. hs2:=preproc_sub_expr(succ(pred_level),false);
  2309. end
  2310. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  2311. begin
  2312. { stop evaluation the rest of expression }
  2313. result:=texprvalue.create_bool(false);
  2314. if pred_level=highest_precedence then
  2315. hs2:=preproc_factor(false)
  2316. else
  2317. hs2:=preproc_sub_expr(succ(pred_level),false);
  2318. end
  2319. else
  2320. begin
  2321. if pred_level=highest_precedence then
  2322. hs2:=preproc_factor(eval)
  2323. else
  2324. hs2:=preproc_sub_expr(succ(pred_level),eval);
  2325. if eval then
  2326. result:=hs1.evaluate(hs2,op)
  2327. else
  2328. result:=texprvalue.create_bool(false); {Just to have something}
  2329. end;
  2330. hs1.free;
  2331. hs2.free;
  2332. end
  2333. else
  2334. break;
  2335. until false;
  2336. end;
  2337. begin
  2338. current_scanner.in_preproc_comp_expr:=true;
  2339. current_scanner.skipspace;
  2340. { start preproc expression scanner }
  2341. current_scanner.preproc_token:=current_scanner.readpreproc;
  2342. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  2343. current_scanner.in_preproc_comp_expr:=false;
  2344. end;
  2345. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  2346. var
  2347. hs: texprvalue;
  2348. begin
  2349. hs:=preproc_comp_expr(nil);
  2350. if hs.isBoolean then
  2351. result:=hs.asBool
  2352. else
  2353. begin
  2354. hs.error('Boolean', 'IF or ELSEIF');
  2355. result:=false;
  2356. end;
  2357. valuedescr:=hs.asStr;
  2358. hs.free;
  2359. end;
  2360. procedure dir_if;
  2361. begin
  2362. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  2363. end;
  2364. procedure dir_elseif;
  2365. begin
  2366. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  2367. end;
  2368. procedure dir_define_impl(macstyle: boolean);
  2369. var
  2370. hs : string;
  2371. bracketcount : longint;
  2372. mac : tmacro;
  2373. macropos : SizeInt;
  2374. macrobuffer : array[0..maxmacrolen-1] of char;
  2375. begin
  2376. current_scanner.skipspace;
  2377. hs:=current_scanner.readid;
  2378. if hs='' then
  2379. begin
  2380. Message(scan_e_emptymacroname);
  2381. exit;
  2382. end;
  2383. mac:=tmacro(search_macro(hs));
  2384. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  2385. begin
  2386. mac:=tmacro.create(hs);
  2387. mac.defined:=true;
  2388. current_module.localmacrosymtable.insertsym(mac);
  2389. end
  2390. else
  2391. begin
  2392. mac.defined:=true;
  2393. mac.is_compiler_var:=false;
  2394. { delete old definition }
  2395. mac.free_buftext;
  2396. end;
  2397. Message1(parser_c_macro_defined,mac.name);
  2398. mac.is_used:=true;
  2399. if (cs_support_macro in current_settings.moduleswitches) then
  2400. begin
  2401. current_scanner.skipspace;
  2402. if not macstyle then
  2403. begin
  2404. { may be a macro? }
  2405. if c <> ':' then
  2406. exit;
  2407. current_scanner.readchar;
  2408. if c <> '=' then
  2409. exit;
  2410. current_scanner.readchar;
  2411. current_scanner.skipspace;
  2412. end;
  2413. { key words are never substituted }
  2414. if is_keyword(hs) then
  2415. Message(scan_e_keyword_cant_be_a_macro);
  2416. current_scanner.gettokenpos;
  2417. mac.fileinfo:=current_tokenpos;
  2418. macropos:=0;
  2419. { parse macro, brackets are counted so it's possible
  2420. to have a $ifdef etc. in the macro }
  2421. bracketcount:=0;
  2422. repeat
  2423. case c of
  2424. '}' :
  2425. if (bracketcount=0) then
  2426. break
  2427. else
  2428. dec(bracketcount);
  2429. '{' :
  2430. inc(bracketcount);
  2431. #10,#13 :
  2432. current_scanner.linebreak;
  2433. #26 :
  2434. current_scanner.end_of_file;
  2435. end;
  2436. if macropos>=maxmacrolen then
  2437. Message(scan_f_macro_buffer_overflow);
  2438. macrobuffer[macropos]:=c;
  2439. inc(macropos);
  2440. current_scanner.readchar;
  2441. until false;
  2442. { copy the text }
  2443. move(pchar(@macrobuffer[0])^,mac.allocate_buftext(macropos)^,macropos);
  2444. end
  2445. else
  2446. begin
  2447. { check if there is an assignment, then we need to give a
  2448. warning }
  2449. current_scanner.skipspace;
  2450. if c=':' then
  2451. begin
  2452. current_scanner.readchar;
  2453. if c='=' then
  2454. Message(scan_w_macro_support_turned_off);
  2455. end;
  2456. end;
  2457. end;
  2458. procedure dir_define;
  2459. begin
  2460. dir_define_impl(false);
  2461. end;
  2462. procedure dir_definec;
  2463. begin
  2464. dir_define_impl(true);
  2465. end;
  2466. procedure dir_setc;
  2467. var
  2468. hs : string;
  2469. mac : tmacro;
  2470. exprvalue: texprvalue;
  2471. begin
  2472. current_scanner.skipspace;
  2473. hs:=current_scanner.readid;
  2474. mac:=tmacro(search_macro(hs));
  2475. if not assigned(mac) or
  2476. (mac.owner <> current_module.localmacrosymtable) then
  2477. begin
  2478. mac:=tmacro.create(hs);
  2479. mac.defined:=true;
  2480. mac.is_compiler_var:=true;
  2481. current_module.localmacrosymtable.insertsym(mac);
  2482. end
  2483. else
  2484. begin
  2485. mac.defined:=true;
  2486. mac.is_compiler_var:=true;
  2487. { delete old definition }
  2488. mac.free_buftext;
  2489. end;
  2490. Message1(parser_c_macro_defined,mac.name);
  2491. mac.is_used:=true;
  2492. { key words are never substituted }
  2493. if is_keyword(hs) then
  2494. Message(scan_e_keyword_cant_be_a_macro);
  2495. { macro assignment can be both := and = }
  2496. current_scanner.skipspace;
  2497. if c=':' then
  2498. current_scanner.readchar;
  2499. if c='=' then
  2500. begin
  2501. current_scanner.readchar;
  2502. exprvalue:=preproc_comp_expr(nil);
  2503. if not is_boolean(exprvalue.def) and
  2504. not is_integer(exprvalue.def) then
  2505. exprvalue.error('Boolean, Integer', 'SETC');
  2506. hs:=exprvalue.asStr;
  2507. if length(hs) <> 0 then
  2508. begin
  2509. {If we are absolutely shure it is boolean, translate
  2510. to TRUE/FALSE to increase possibility to do future type check}
  2511. if exprvalue.isBoolean then
  2512. begin
  2513. if exprvalue.asBool then
  2514. hs:='TRUE'
  2515. else
  2516. hs:='FALSE';
  2517. end;
  2518. Message2(parser_c_macro_set_to,mac.name,hs);
  2519. { copy the text }
  2520. move(hs[1],mac.allocate_buftext(length(hs))^,length(hs));
  2521. end
  2522. else
  2523. Message(scan_e_preproc_syntax_error);
  2524. exprvalue.free;
  2525. end
  2526. else
  2527. Message(scan_e_preproc_syntax_error);
  2528. end;
  2529. procedure dir_undef;
  2530. var
  2531. hs : string;
  2532. mac : tmacro;
  2533. begin
  2534. current_scanner.skipspace;
  2535. hs:=current_scanner.readid;
  2536. mac:=tmacro(search_macro(hs));
  2537. if not assigned(mac) or
  2538. (mac.owner <> current_module.localmacrosymtable) then
  2539. begin
  2540. mac:=tmacro.create(hs);
  2541. mac.defined:=false;
  2542. current_module.localmacrosymtable.insertsym(mac);
  2543. end
  2544. else
  2545. begin
  2546. mac.defined:=false;
  2547. mac.is_compiler_var:=false;
  2548. { delete old definition }
  2549. mac.free_buftext;
  2550. end;
  2551. Message1(parser_c_macro_undefined,mac.name);
  2552. mac.is_used:=true;
  2553. end;
  2554. procedure dir_include;
  2555. var
  2556. foundfile : TCmdStr;
  2557. path,
  2558. name,
  2559. hs : tpathstr;
  2560. args : string;
  2561. hp : tinputfile;
  2562. found : boolean;
  2563. macroIsString : boolean;
  2564. begin
  2565. current_scanner.skipspace;
  2566. args:=current_scanner.readcomment;
  2567. hs:=GetToken(args,' ');
  2568. if hs='' then
  2569. exit;
  2570. if (hs[1]='%') then
  2571. begin
  2572. { case insensitive }
  2573. hs:=upper(hs);
  2574. { remove %'s }
  2575. Delete(hs,1,1);
  2576. if hs[length(hs)]='%' then
  2577. Delete(hs,length(hs),1);
  2578. { save old }
  2579. path:=hs;
  2580. { first check for internal macros }
  2581. macroIsString:=true;
  2582. case hs of
  2583. 'TIME':
  2584. if timestr<>'' then
  2585. hs:=timestr
  2586. else
  2587. hs:=gettimestr;
  2588. 'DATE':
  2589. if datestr<>'' then
  2590. hs:=datestr
  2591. else
  2592. hs:=getdatestr;
  2593. 'DATEYEAR':
  2594. begin
  2595. hs:=tostr(startsystime.Year);
  2596. macroIsString:=false;
  2597. end;
  2598. 'DATEMONTH':
  2599. begin
  2600. hs:=tostr(startsystime.Month);
  2601. macroIsString:=false;
  2602. end;
  2603. 'DATEDAY':
  2604. begin
  2605. hs:=tostr(startsystime.Day);
  2606. macroIsString:=false;
  2607. end;
  2608. 'TIMEHOUR':
  2609. begin
  2610. hs:=tostr(startsystime.Hour);
  2611. macroIsString:=false;
  2612. end;
  2613. 'TIMEMINUTE':
  2614. begin
  2615. hs:=tostr(startsystime.Minute);
  2616. macroIsString:=false;
  2617. end;
  2618. 'TIMESECOND':
  2619. begin
  2620. hs:=tostr(startsystime.Second);
  2621. macroIsString:=false;
  2622. end;
  2623. 'FILE':
  2624. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2625. 'LINE':
  2626. hs:=tostr(current_filepos.line);
  2627. 'LINENUM':
  2628. begin
  2629. hs:=tostr(current_filepos.line);
  2630. macroIsString:=false;
  2631. end;
  2632. 'FPCVERSION':
  2633. hs:=version_string;
  2634. 'FPCDATE':
  2635. hs:=date_string;
  2636. 'FPCTARGET':
  2637. hs:=target_cpu_string;
  2638. 'FPCTARGETCPU':
  2639. hs:=target_cpu_string;
  2640. 'FPCTARGETOS':
  2641. hs:=target_info.shortname;
  2642. 'CURRENTROUTINE':
  2643. hs:=current_procinfo.procdef.procsym.RealName;
  2644. else
  2645. hs:=GetEnvironmentVariable(hs);
  2646. end;
  2647. if hs='' then
  2648. Message1(scan_w_include_env_not_found,path);
  2649. { make it a stringconst }
  2650. if macroIsString then
  2651. hs:=''''+hs+'''';
  2652. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2653. current_scanner.line_no,current_scanner.inputfile.ref_index,false);
  2654. end
  2655. else
  2656. begin
  2657. hs:=FixFileName(hs);
  2658. path:=ExtractFilePath(hs);
  2659. name:=ExtractFileName(hs);
  2660. { Special case for Delphi compatibility: '*' has to be replaced
  2661. by the file name of the current source file. }
  2662. if (length(name)>=1) and
  2663. (name[1]='*') then
  2664. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2665. { try to find the file }
  2666. found:=findincludefile(path,name,foundfile);
  2667. if (not found) and (ExtractFileExt(name)='') then
  2668. begin
  2669. { try default extensions .inc , .pp and .pas }
  2670. if (not found) then
  2671. found:=findincludefile(path,ChangeFileExt(name,'.inc'),foundfile);
  2672. if (not found) then
  2673. found:=findincludefile(path,ChangeFileExt(name,sourceext),foundfile);
  2674. if (not found) then
  2675. found:=findincludefile(path,ChangeFileExt(name,pasext),foundfile);
  2676. end;
  2677. { if the name ends in dot, try without the dot }
  2678. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2679. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2680. if current_scanner.inputfilecount<max_include_nesting then
  2681. begin
  2682. inc(current_scanner.inputfilecount);
  2683. { we need to reread the current char }
  2684. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2685. current_scanner.dec_inputpointer;
  2686. {$else not CHECK_INPUTPOINTER_LIMITS}
  2687. dec(current_scanner.inputpointer);
  2688. {$endif CHECK_INPUTPOINTER_LIMITS}
  2689. { reset c }
  2690. c:=#0;
  2691. { shutdown current file }
  2692. current_scanner.tempcloseinputfile;
  2693. { load new file }
  2694. hp:=do_openinputfile(foundfile);
  2695. hp.inc_path:=path;
  2696. current_scanner.addfile(hp);
  2697. current_module.sourcefiles.register_file(hp);
  2698. if (not found) then
  2699. Message1(scan_f_cannot_open_includefile,hs);
  2700. if (not current_scanner.openinputfile) then
  2701. Message1(scan_f_cannot_open_includefile,hs);
  2702. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2703. current_scanner.reload;
  2704. end
  2705. else
  2706. Message(scan_f_include_deep_ten);
  2707. end;
  2708. end;
  2709. {*****************************************************************************
  2710. Preprocessor writing
  2711. *****************************************************************************}
  2712. {$ifdef PREPROCWRITE}
  2713. constructor tpreprocfile.create(const fn:string);
  2714. begin
  2715. inherited create;
  2716. { open outputfile }
  2717. assign(f,fn);
  2718. {$push}{$I-}
  2719. rewrite(f);
  2720. {$pop}
  2721. if ioresult<>0 then
  2722. Comment(V_Fatal,'can''t create file '+fn);
  2723. getmem(buf,preprocbufsize);
  2724. settextbuf(f,buf^,preprocbufsize);
  2725. { reset }
  2726. eolfound:=false;
  2727. spacefound:=false;
  2728. end;
  2729. destructor tpreprocfile.destroy;
  2730. begin
  2731. close(f);
  2732. freemem(buf,preprocbufsize);
  2733. end;
  2734. procedure tpreprocfile.add(const s:string);
  2735. begin
  2736. write(f,s);
  2737. end;
  2738. procedure tpreprocfile.addspace;
  2739. begin
  2740. if eolfound then
  2741. begin
  2742. writeln(f,'');
  2743. eolfound:=false;
  2744. spacefound:=false;
  2745. end
  2746. else
  2747. if spacefound then
  2748. begin
  2749. write(f,' ');
  2750. spacefound:=false;
  2751. end;
  2752. end;
  2753. {$endif PREPROCWRITE}
  2754. {*****************************************************************************
  2755. TPreProcStack
  2756. *****************************************************************************}
  2757. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2758. begin
  2759. accept:=a;
  2760. typ:=atyp;
  2761. next:=n;
  2762. end;
  2763. {*****************************************************************************
  2764. TReplayStack
  2765. *****************************************************************************}
  2766. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2767. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2768. apatternw:pcompilerwidestring;asettings:tsettings;
  2769. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2770. begin
  2771. token:=atoken;
  2772. idtoken:=aidtoken;
  2773. orgpattern:=aorgpattern;
  2774. pattern:=apattern;
  2775. cstringpattern:=acstringpattern;
  2776. initwidestring(patternw);
  2777. if assigned(apatternw) then
  2778. begin
  2779. setlengthwidestring(patternw,apatternw^.len);
  2780. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2781. end;
  2782. settings:=asettings;
  2783. tokenbuf:=atokenbuf;
  2784. tokenbuf_needs_swapping:=change_endian;
  2785. next:=anext;
  2786. end;
  2787. destructor treplaystack.destroy;
  2788. begin
  2789. donewidestring(patternw);
  2790. end;
  2791. {*****************************************************************************
  2792. TDirectiveItem
  2793. *****************************************************************************}
  2794. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2795. begin
  2796. inherited Create(AList,n);
  2797. is_conditional:=false;
  2798. proc:=p;
  2799. end;
  2800. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2801. begin
  2802. inherited Create(AList,n);
  2803. is_conditional:=true;
  2804. proc:=p;
  2805. end;
  2806. {****************************************************************************
  2807. TSCANNERFILE
  2808. ****************************************************************************}
  2809. constructor tscannerfile.Create(const fn: string; is_macro: boolean);
  2810. begin
  2811. inputfile:=do_openinputfile(fn);
  2812. if is_macro then
  2813. inputfile.is_macro:=true;
  2814. if assigned(current_module) then
  2815. current_module.sourcefiles.register_file(inputfile);
  2816. { reset localinput }
  2817. c:=#0;
  2818. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2819. hidden_inputbuffer:=nil;
  2820. hidden_inputpointer:=nil;
  2821. {$else not CHECK_INPUTPOINTER_LIMITS}
  2822. inputbuffer:=nil;
  2823. inputpointer:=nil;
  2824. {$endif CHECK_INPUTPOINTER_LIMITS}
  2825. inputstart:=0;
  2826. { reset scanner }
  2827. preprocstack:=nil;
  2828. replaystack:=nil;
  2829. comment_level:=0;
  2830. yylexcount:=0;
  2831. block_type:=bt_general;
  2832. line_no:=0;
  2833. lastlinepos:=0;
  2834. lasttokenpos:=0;
  2835. nexttokenpos:=0;
  2836. lasttoken:=NOTOKEN;
  2837. nexttoken:=NOTOKEN;
  2838. ignoredirectives:=TFPHashList.Create;
  2839. change_endian_for_replay:=false;
  2840. end;
  2841. procedure tscannerfile.firstfile;
  2842. begin
  2843. { load block }
  2844. if not openinputfile then
  2845. Message1(scan_f_cannot_open_input,inputfile.name);
  2846. reload;
  2847. end;
  2848. destructor tscannerfile.Destroy;
  2849. begin
  2850. if assigned(onfreescanner) then
  2851. onfreescanner(self);
  2852. if assigned(current_module) and
  2853. (current_module.state in [ms_processed,ms_compiled]) and
  2854. (status.errorcount=0) then
  2855. checkpreprocstack
  2856. else
  2857. begin
  2858. while assigned(preprocstack) do
  2859. poppreprocstack;
  2860. end;
  2861. while assigned(replaystack) do
  2862. popreplaystack;
  2863. if not inputfile.closed then
  2864. closeinputfile;
  2865. if inputfile.is_macro then
  2866. inputfile.free;
  2867. ignoredirectives.free;
  2868. end;
  2869. function tscannerfile.openinputfile:boolean;
  2870. begin
  2871. openinputfile:=inputfile.open;
  2872. { load buffer }
  2873. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2874. hidden_inputbuffer:=inputfile.buf;
  2875. hidden_inputpointer:=inputfile.buf;
  2876. {$else not CHECK_INPUTPOINTER_LIMITS}
  2877. inputbuffer:=inputfile.buf;
  2878. inputpointer:=inputfile.buf;
  2879. {$endif CHECK_INPUTPOINTER_LIMITS}
  2880. inputstart:=inputfile.bufstart;
  2881. { line }
  2882. line_no:=0;
  2883. lastlinepos:=0;
  2884. lasttokenpos:=0;
  2885. nexttokenpos:=0;
  2886. end;
  2887. procedure tscannerfile.closeinputfile;
  2888. begin
  2889. inputfile.close;
  2890. { reset buffer }
  2891. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2892. hidden_inputbuffer:=nil;
  2893. hidden_inputpointer:=nil;
  2894. {$else not CHECK_INPUTPOINTER_LIMITS}
  2895. inputbuffer:=nil;
  2896. inputpointer:=nil;
  2897. {$endif CHECK_INPUTPOINTER_LIMITS}
  2898. inputstart:=0;
  2899. { reset line }
  2900. line_no:=0;
  2901. lastlinepos:=0;
  2902. lasttokenpos:=0;
  2903. nexttokenpos:=0;
  2904. end;
  2905. function tscannerfile.tempopeninputfile:boolean;
  2906. begin
  2907. tempopeninputfile:=false;
  2908. if inputfile.is_macro then
  2909. exit;
  2910. tempopeninputfile:=inputfile.tempopen;
  2911. { reload buffer }
  2912. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2913. hidden_inputbuffer:=inputfile.buf;
  2914. hidden_inputpointer:=inputfile.buf;
  2915. {$else not CHECK_INPUTPOINTER_LIMITS}
  2916. inputbuffer:=inputfile.buf;
  2917. inputpointer:=inputfile.buf;
  2918. {$endif CHECK_INPUTPOINTER_LIMITS}
  2919. inputstart:=inputfile.bufstart;
  2920. end;
  2921. procedure tscannerfile.tempcloseinputfile;
  2922. begin
  2923. if inputfile.closed or inputfile.is_macro then
  2924. exit;
  2925. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2926. inputfile.setpos(inputstart+(hidden_inputpointer-hidden_inputbuffer));
  2927. {$else not CHECK_INPUTPOINTER_LIMITS}
  2928. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2929. {$endif CHECK_INPUTPOINTER_LIMITS}
  2930. inputfile.tempclose;
  2931. { reset buffer }
  2932. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2933. hidden_inputbuffer:=nil;
  2934. hidden_inputpointer:=nil;
  2935. {$else not CHECK_INPUTPOINTER_LIMITS}
  2936. inputbuffer:=nil;
  2937. inputpointer:=nil;
  2938. {$endif CHECK_INPUTPOINTER_LIMITS}
  2939. inputstart:=0;
  2940. end;
  2941. procedure tscannerfile.saveinputfile;
  2942. begin
  2943. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2944. inputfile.saveinputpointer:=hidden_inputpointer;
  2945. {$else not CHECK_INPUTPOINTER_LIMITS}
  2946. inputfile.saveinputpointer:=inputpointer;
  2947. {$endif CHECK_INPUTPOINTER_LIMITS}
  2948. inputfile.savelastlinepos:=lastlinepos;
  2949. inputfile.saveline_no:=line_no;
  2950. end;
  2951. procedure tscannerfile.restoreinputfile;
  2952. begin
  2953. {$ifdef check_inputpointer_limits}
  2954. hidden_inputbuffer:=inputfile.buf;
  2955. hidden_inputpointer:=inputfile.saveinputpointer;
  2956. {$else not check_inputpointer_limits}
  2957. inputbuffer:=inputfile.buf;
  2958. inputpointer:=inputfile.saveinputpointer;
  2959. {$endif check_inputpointer_limits}
  2960. lastlinepos:=inputfile.savelastlinepos;
  2961. line_no:=inputfile.saveline_no;
  2962. if not inputfile.is_macro then
  2963. parser_current_file:=inputfile.name;
  2964. end;
  2965. procedure tscannerfile.nextfile;
  2966. var
  2967. to_dispose : tinputfile;
  2968. begin
  2969. if assigned(inputfile.next) then
  2970. begin
  2971. if inputfile.is_macro then
  2972. begin
  2973. to_dispose:=inputfile;
  2974. dec(macro_nesting_depth);
  2975. end
  2976. else
  2977. begin
  2978. to_dispose:=nil;
  2979. dec(inputfilecount);
  2980. end;
  2981. { we can allways close the file, no ? }
  2982. inputfile.close;
  2983. inputfile:=inputfile.next;
  2984. if assigned(to_dispose) then
  2985. to_dispose.free;
  2986. restoreinputfile;
  2987. end;
  2988. end;
  2989. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2990. function tscannerfile.get_inputpointer_char(offset : longint = 0) : char;
  2991. begin
  2992. assert(hidden_inputpointer-hidden_inputbuffer+offset<=inputfile.bufsize);
  2993. get_inputpointer_char:=(hidden_inputpointer+offset)^;
  2994. end;
  2995. procedure tscannerfile.inc_inputpointer(amount : longint = 1);
  2996. begin
  2997. assert(hidden_inputpointer-hidden_inputbuffer+amount<=inputfile.bufsize);
  2998. inc(hidden_inputpointer,amount);
  2999. end;
  3000. procedure tscannerfile.dec_inputpointer;
  3001. begin
  3002. assert(hidden_inputpointer>hidden_inputbuffer);
  3003. dec(hidden_inputpointer);
  3004. end;
  3005. {$endif}
  3006. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  3007. begin
  3008. if not assigned(buf) then
  3009. internalerror(200511172);
  3010. if assigned(recordtokenbuf) then
  3011. internalerror(200511173);
  3012. recordtokenbuf:=buf;
  3013. fillchar(last_settings,sizeof(last_settings),0);
  3014. last_message:=nil;
  3015. fillchar(last_filepos,sizeof(last_filepos),0);
  3016. end;
  3017. procedure tscannerfile.stoprecordtokens;
  3018. begin
  3019. if not assigned(recordtokenbuf) then
  3020. internalerror(200511174);
  3021. recordtokenbuf:=nil;
  3022. end;
  3023. function tscannerfile.is_recording_tokens: boolean;
  3024. begin
  3025. result:=assigned(recordtokenbuf);
  3026. end;
  3027. procedure tscannerfile.writetoken(t : ttoken);
  3028. var
  3029. b : byte;
  3030. begin
  3031. if ord(t)>$7f then
  3032. begin
  3033. b:=(ord(t) shr 8) or $80;
  3034. recordtokenbuf.write(b,1);
  3035. end;
  3036. b:=ord(t) and $ff;
  3037. recordtokenbuf.write(b,1);
  3038. end;
  3039. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  3040. begin
  3041. recordtokenbuf.write(val,sizeof(asizeint));
  3042. end;
  3043. procedure tscannerfile.tokenwritelongint(val : longint);
  3044. begin
  3045. recordtokenbuf.write(val,sizeof(longint));
  3046. end;
  3047. procedure tscannerfile.tokenwriteshortint(val : shortint);
  3048. begin
  3049. recordtokenbuf.write(val,sizeof(shortint));
  3050. end;
  3051. procedure tscannerfile.tokenwriteword(val : word);
  3052. begin
  3053. recordtokenbuf.write(val,sizeof(word));
  3054. end;
  3055. procedure tscannerfile.tokenwritelongword(val : longword);
  3056. begin
  3057. recordtokenbuf.write(val,sizeof(longword));
  3058. end;
  3059. function tscannerfile.tokenreadsizeint : asizeint;
  3060. var
  3061. val : asizeint;
  3062. begin
  3063. replaytokenbuf.read(val,sizeof(asizeint));
  3064. if change_endian_for_replay then
  3065. val:=swapendian(val);
  3066. result:=val;
  3067. end;
  3068. function tscannerfile.tokenreadlongword : longword;
  3069. var
  3070. val : longword;
  3071. begin
  3072. replaytokenbuf.read(val,sizeof(longword));
  3073. if change_endian_for_replay then
  3074. val:=swapendian(val);
  3075. result:=val;
  3076. end;
  3077. function tscannerfile.tokenreadlongint : longint;
  3078. var
  3079. val : longint;
  3080. begin
  3081. replaytokenbuf.read(val,sizeof(longint));
  3082. if change_endian_for_replay then
  3083. val:=swapendian(val);
  3084. result:=val;
  3085. end;
  3086. function tscannerfile.tokenreadshortint : shortint;
  3087. var
  3088. val : shortint;
  3089. begin
  3090. replaytokenbuf.read(val,sizeof(shortint));
  3091. result:=val;
  3092. end;
  3093. function tscannerfile.tokenreadbyte : byte;
  3094. var
  3095. val : byte;
  3096. begin
  3097. replaytokenbuf.read(val,sizeof(byte));
  3098. result:=val;
  3099. end;
  3100. function tscannerfile.tokenreadsmallint : smallint;
  3101. var
  3102. val : smallint;
  3103. begin
  3104. replaytokenbuf.read(val,sizeof(smallint));
  3105. if change_endian_for_replay then
  3106. val:=swapendian(val);
  3107. result:=val;
  3108. end;
  3109. function tscannerfile.tokenreadword : word;
  3110. var
  3111. val : word;
  3112. begin
  3113. replaytokenbuf.read(val,sizeof(word));
  3114. if change_endian_for_replay then
  3115. val:=swapendian(val);
  3116. result:=val;
  3117. end;
  3118. function tscannerfile.tokenreadenum(size : longint) : longword;
  3119. begin
  3120. if size=1 then
  3121. result:=tokenreadbyte
  3122. else if size=2 then
  3123. result:=tokenreadword
  3124. else if size=4 then
  3125. result:=tokenreadlongword
  3126. else
  3127. internalerror(2013112901);
  3128. end;
  3129. procedure tscannerfile.tokenreadset(var b;size : longint);
  3130. var
  3131. i : longint;
  3132. begin
  3133. replaytokenbuf.read(b,size);
  3134. if change_endian_for_replay then
  3135. for i:=0 to size-1 do
  3136. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  3137. end;
  3138. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  3139. begin
  3140. recordtokenbuf.write(b,size);
  3141. end;
  3142. procedure tscannerfile.tokenwriteset(var b;size : longint);
  3143. begin
  3144. recordtokenbuf.write(b,size);
  3145. end;
  3146. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  3147. { This procedure
  3148. needs to be changed whenever
  3149. globals.tsettings type is changed,
  3150. the problem is that no error will appear
  3151. before tests with generics are tested. PM }
  3152. var
  3153. startpos, endpos : longword;
  3154. begin
  3155. { WARNING all those fields need to be in the correct
  3156. order otherwise cross_endian PPU reading will fail }
  3157. startpos:=replaytokenbuf.pos;
  3158. with asettings do
  3159. begin
  3160. alignment.procalign:=tokenreadlongint;
  3161. alignment.loopalign:=tokenreadlongint;
  3162. alignment.jumpalign:=tokenreadlongint;
  3163. alignment.jumpalignskipmax:=tokenreadlongint;
  3164. alignment.coalescealign:=tokenreadlongint;
  3165. alignment.coalescealignskipmax:=tokenreadlongint;
  3166. alignment.constalignmin:=tokenreadlongint;
  3167. alignment.constalignmax:=tokenreadlongint;
  3168. alignment.varalignmin:=tokenreadlongint;
  3169. alignment.varalignmax:=tokenreadlongint;
  3170. alignment.localalignmin:=tokenreadlongint;
  3171. alignment.localalignmax:=tokenreadlongint;
  3172. alignment.recordalignmin:=tokenreadlongint;
  3173. alignment.recordalignmax:=tokenreadlongint;
  3174. alignment.maxCrecordalign:=tokenreadlongint;
  3175. tokenreadset(globalswitches,sizeof(globalswitches));
  3176. tokenreadset(targetswitches,sizeof(targetswitches));
  3177. tokenreadset(moduleswitches,sizeof(moduleswitches));
  3178. tokenreadset(localswitches,sizeof(localswitches));
  3179. tokenreadset(modeswitches,sizeof(modeswitches));
  3180. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  3181. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3182. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3183. tokenreadset(debugswitches,sizeof(debugswitches));
  3184. { 0: old behaviour for sets <=256 elements
  3185. >0: round to this size }
  3186. setalloc:=tokenreadshortint;
  3187. packenum:=tokenreadshortint;
  3188. packrecords:=tokenreadshortint;
  3189. maxfpuregisters:=tokenreadshortint;
  3190. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3191. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3192. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  3193. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  3194. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  3195. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  3196. { tstringencoding is word type,
  3197. thus this should be OK here }
  3198. sourcecodepage:=tstringEncoding(tokenreadword);
  3199. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  3200. disabledircache:=boolean(tokenreadbyte);
  3201. tlsmodel:=ttlsmodel(tokenreadenum(sizeof(ttlsmodel)));
  3202. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  3203. { While adding ControllerSupport constant, I decided not to store ct_none }
  3204. { on targets not supporting controllers, but this might be changed here and }
  3205. { in tokenwritesettings in the future to unify the PPU structure and handling }
  3206. { of this field in the compiler. }
  3207. {$PUSH}
  3208. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3209. if ControllerSupport then
  3210. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  3211. else
  3212. ControllerType:=ct_none;
  3213. {$POP}
  3214. endpos:=replaytokenbuf.pos;
  3215. if endpos-startpos<>expected_size then
  3216. Comment(V_Error,'Wrong size of Settings read-in');
  3217. end;
  3218. end;
  3219. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  3220. { This procedure
  3221. needs to be changed whenever
  3222. globals.tsettings type is changed,
  3223. the problem is that no error will appear
  3224. before tests with generics are tested. PM }
  3225. var
  3226. sizepos, startpos, endpos : longword;
  3227. begin
  3228. { WARNING all those fields need to be in the correct
  3229. order otherwise cross_endian PPU reading will fail }
  3230. sizepos:=recordtokenbuf.pos;
  3231. size:=0;
  3232. tokenwritesizeint(size);
  3233. startpos:=recordtokenbuf.pos;
  3234. with asettings do
  3235. begin
  3236. tokenwritelongint(alignment.procalign);
  3237. tokenwritelongint(alignment.loopalign);
  3238. tokenwritelongint(alignment.jumpalign);
  3239. tokenwritelongint(alignment.jumpalignskipmax);
  3240. tokenwritelongint(alignment.coalescealign);
  3241. tokenwritelongint(alignment.coalescealignskipmax);
  3242. tokenwritelongint(alignment.constalignmin);
  3243. tokenwritelongint(alignment.constalignmax);
  3244. tokenwritelongint(alignment.varalignmin);
  3245. tokenwritelongint(alignment.varalignmax);
  3246. tokenwritelongint(alignment.localalignmin);
  3247. tokenwritelongint(alignment.localalignmax);
  3248. tokenwritelongint(alignment.recordalignmin);
  3249. tokenwritelongint(alignment.recordalignmax);
  3250. tokenwritelongint(alignment.maxCrecordalign);
  3251. tokenwriteset(globalswitches,sizeof(globalswitches));
  3252. tokenwriteset(targetswitches,sizeof(targetswitches));
  3253. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  3254. tokenwriteset(localswitches,sizeof(localswitches));
  3255. tokenwriteset(modeswitches,sizeof(modeswitches));
  3256. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  3257. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3258. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3259. tokenwriteset(debugswitches,sizeof(debugswitches));
  3260. { 0: old behaviour for sets <=256 elements
  3261. >0: round to this size }
  3262. tokenwriteshortint(setalloc);
  3263. tokenwriteshortint(packenum);
  3264. tokenwriteshortint(packrecords);
  3265. tokenwriteshortint(maxfpuregisters);
  3266. tokenwriteenum(cputype,sizeof(tcputype));
  3267. tokenwriteenum(optimizecputype,sizeof(tcputype));
  3268. tokenwriteenum(fputype,sizeof(tfputype));
  3269. tokenwriteenum(asmmode,sizeof(tasmmode));
  3270. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  3271. tokenwriteenum(defproccall,sizeof(tproccalloption));
  3272. { tstringencoding is word type,
  3273. thus this should be OK here }
  3274. tokenwriteword(sourcecodepage);
  3275. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  3276. recordtokenbuf.write(byte(disabledircache),1);
  3277. tokenwriteenum(tlsmodel,sizeof(tlsmodel));
  3278. { TH: See note about controllertype field in tokenreadsettings. }
  3279. {$PUSH}
  3280. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3281. if ControllerSupport then
  3282. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  3283. {$POP}
  3284. endpos:=recordtokenbuf.pos;
  3285. size:=endpos-startpos;
  3286. recordtokenbuf.seek(sizepos);
  3287. tokenwritesizeint(size);
  3288. recordtokenbuf.seek(endpos);
  3289. end;
  3290. end;
  3291. procedure tscannerfile.recordtoken;
  3292. var
  3293. t : ttoken;
  3294. s : tspecialgenerictoken;
  3295. len,msgnb,copy_size : asizeint;
  3296. val : longint;
  3297. b : byte;
  3298. pmsg : pmessagestaterecord;
  3299. begin
  3300. if not assigned(recordtokenbuf) then
  3301. internalerror(200511176);
  3302. t:=_GENERICSPECIALTOKEN;
  3303. { settings changed? }
  3304. { last field pmessage is handled separately below in
  3305. ST_LOADMESSAGES }
  3306. if CompareByte(current_settings,last_settings,
  3307. sizeof(current_settings)-sizeof(pointer))<>0 then
  3308. begin
  3309. { use a special token to record it }
  3310. s:=ST_LOADSETTINGS;
  3311. writetoken(t);
  3312. recordtokenbuf.write(s,1);
  3313. copy_size:=sizeof(current_settings)-sizeof(pointer);
  3314. tokenwritesettings(current_settings,copy_size);
  3315. last_settings:=current_settings;
  3316. end;
  3317. if current_settings.pmessage<>last_message then
  3318. begin
  3319. { use a special token to record it }
  3320. s:=ST_LOADMESSAGES;
  3321. writetoken(t);
  3322. recordtokenbuf.write(s,1);
  3323. msgnb:=0;
  3324. pmsg:=current_settings.pmessage;
  3325. while assigned(pmsg) do
  3326. begin
  3327. if msgnb=high(asizeint) then
  3328. { Too many messages }
  3329. internalerror(2011090401);
  3330. inc(msgnb);
  3331. pmsg:=pmsg^.next;
  3332. end;
  3333. tokenwritesizeint(msgnb);
  3334. pmsg:=current_settings.pmessage;
  3335. while assigned(pmsg) do
  3336. begin
  3337. { What about endianess here?}
  3338. { SB: this is handled by tokenreadlongint }
  3339. val:=pmsg^.value;
  3340. tokenwritelongint(val);
  3341. val:=ord(pmsg^.state);
  3342. tokenwritelongint(val);
  3343. pmsg:=pmsg^.next;
  3344. end;
  3345. last_message:=current_settings.pmessage;
  3346. end;
  3347. { file pos changes? }
  3348. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3349. begin
  3350. s:=ST_FILEINDEX;
  3351. writetoken(t);
  3352. recordtokenbuf.write(s,1);
  3353. tokenwriteword(current_tokenpos.fileindex);
  3354. last_filepos.fileindex:=current_tokenpos.fileindex;
  3355. last_filepos.line:=0;
  3356. end;
  3357. if current_tokenpos.line<>last_filepos.line then
  3358. begin
  3359. s:=ST_LINE;
  3360. writetoken(t);
  3361. recordtokenbuf.write(s,1);
  3362. tokenwritelongint(current_tokenpos.line);
  3363. last_filepos.line:=current_tokenpos.line;
  3364. last_filepos.column:=0;
  3365. end;
  3366. if current_tokenpos.column<>last_filepos.column then
  3367. begin
  3368. s:=ST_COLUMN;
  3369. writetoken(t);
  3370. { can the column be written packed? }
  3371. if current_tokenpos.column<$80 then
  3372. begin
  3373. b:=$80 or current_tokenpos.column;
  3374. recordtokenbuf.write(b,1);
  3375. end
  3376. else
  3377. begin
  3378. recordtokenbuf.write(s,1);
  3379. tokenwriteword(current_tokenpos.column);
  3380. end;
  3381. last_filepos.column:=current_tokenpos.column;
  3382. end;
  3383. writetoken(token);
  3384. if token<>_GENERICSPECIALTOKEN then
  3385. writetoken(idtoken);
  3386. case token of
  3387. _CWCHAR,
  3388. _CWSTRING :
  3389. begin
  3390. tokenwritesizeint(patternw^.len);
  3391. if patternw^.len>0 then
  3392. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3393. end;
  3394. _CSTRING:
  3395. begin
  3396. len:=length(cstringpattern);
  3397. tokenwritesizeint(len);
  3398. if len>0 then
  3399. recordtokenbuf.write(cstringpattern[1],len);
  3400. end;
  3401. _CCHAR,
  3402. _INTCONST,
  3403. _REALNUMBER :
  3404. begin
  3405. { pexpr.pas messes with pattern in case of negative integer consts,
  3406. see around line 2562 the comment of JM; remove the - before recording it
  3407. (FK)
  3408. }
  3409. if (token=_INTCONST) and (pattern[1]='-') then
  3410. delete(pattern,1,1);
  3411. recordtokenbuf.write(pattern[0],1);
  3412. recordtokenbuf.write(pattern[1],length(pattern));
  3413. end;
  3414. _ID :
  3415. begin
  3416. recordtokenbuf.write(orgpattern[0],1);
  3417. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3418. end;
  3419. else
  3420. ;
  3421. end;
  3422. end;
  3423. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3424. begin
  3425. if not assigned(buf) then
  3426. internalerror(200511175);
  3427. { save current scanner state }
  3428. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3429. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3430. {$ifdef check_inputpointer_limits}
  3431. if assigned(hidden_inputpointer) then
  3432. dec_inputpointer;
  3433. {$else not check_inputpointer_limits}
  3434. if assigned(inputpointer) then
  3435. dec(inputpointer);
  3436. {$endif check_inputpointer_limits}
  3437. { install buffer }
  3438. replaytokenbuf:=buf;
  3439. { Initialize value of change_endian_for_replay variable }
  3440. change_endian_for_replay:=change_endian;
  3441. { reload next token }
  3442. replaytokenbuf.seek(0);
  3443. replaytoken;
  3444. end;
  3445. function tscannerfile.readtoken: ttoken;
  3446. var
  3447. b,b2 : byte;
  3448. begin
  3449. replaytokenbuf.read(b,1);
  3450. if (b and $80)<>0 then
  3451. begin
  3452. replaytokenbuf.read(b2,1);
  3453. result:=ttoken(((b and $7f) shl 8) or b2);
  3454. end
  3455. else
  3456. result:=ttoken(b);
  3457. end;
  3458. procedure tscannerfile.replaytoken;
  3459. var
  3460. wlen,mesgnb,copy_size : asizeint;
  3461. specialtoken : tspecialgenerictoken;
  3462. i : byte;
  3463. pmsg,prevmsg : pmessagestaterecord;
  3464. begin
  3465. if not assigned(replaytokenbuf) then
  3466. internalerror(200511177);
  3467. { End of replay buffer? Then load the next char from the file again }
  3468. if replaytokenbuf.pos>=replaytokenbuf.size then
  3469. begin
  3470. token:=replaystack.token;
  3471. idtoken:=replaystack.idtoken;
  3472. pattern:=replaystack.pattern;
  3473. orgpattern:=replaystack.orgpattern;
  3474. setlengthwidestring(patternw,replaystack.patternw^.len);
  3475. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3476. cstringpattern:=replaystack.cstringpattern;
  3477. replaytokenbuf:=replaystack.tokenbuf;
  3478. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3479. { restore compiler settings }
  3480. current_settings:=replaystack.settings;
  3481. popreplaystack;
  3482. {$ifdef check_inputpointer_limits}
  3483. if assigned(hidden_inputpointer) then
  3484. begin
  3485. c:=get_inputpointer_char;
  3486. inc_inputpointer;
  3487. end;
  3488. {$else not check_inputpointer_limits}
  3489. if assigned(inputpointer) then
  3490. begin
  3491. c:=inputpointer^;
  3492. inc(inputpointer);
  3493. end;
  3494. {$endif check_inputpointer_limits}
  3495. exit;
  3496. end;
  3497. repeat
  3498. { load token from the buffer }
  3499. token:=readtoken;
  3500. if token<>_GENERICSPECIALTOKEN then
  3501. idtoken:=readtoken
  3502. else
  3503. idtoken:=_NOID;
  3504. case token of
  3505. _CWCHAR,
  3506. _CWSTRING :
  3507. begin
  3508. wlen:=tokenreadsizeint;
  3509. setlengthwidestring(patternw,wlen);
  3510. if wlen>0 then
  3511. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3512. orgpattern:='';
  3513. pattern:='';
  3514. cstringpattern:='';
  3515. end;
  3516. _CSTRING:
  3517. begin
  3518. wlen:=tokenreadsizeint;
  3519. if wlen>0 then
  3520. begin
  3521. setlength(cstringpattern,wlen);
  3522. replaytokenbuf.read(cstringpattern[1],wlen);
  3523. end
  3524. else
  3525. cstringpattern:='';
  3526. orgpattern:='';
  3527. pattern:='';
  3528. end;
  3529. _CCHAR,
  3530. _INTCONST,
  3531. _REALNUMBER :
  3532. begin
  3533. replaytokenbuf.read(pattern[0],1);
  3534. replaytokenbuf.read(pattern[1],length(pattern));
  3535. orgpattern:='';
  3536. end;
  3537. _ID :
  3538. begin
  3539. replaytokenbuf.read(orgpattern[0],1);
  3540. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3541. pattern:=upper(orgpattern);
  3542. end;
  3543. _GENERICSPECIALTOKEN:
  3544. begin
  3545. replaytokenbuf.read(specialtoken,1);
  3546. { packed column? }
  3547. if (ord(specialtoken) and $80)<>0 then
  3548. begin
  3549. current_tokenpos.column:=ord(specialtoken) and $7f;
  3550. current_filepos:=current_tokenpos;
  3551. end
  3552. else
  3553. case specialtoken of
  3554. ST_LOADSETTINGS:
  3555. begin
  3556. copy_size:=tokenreadsizeint;
  3557. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3558. // internalerror(2011090501);
  3559. {
  3560. replaytokenbuf.read(current_settings,copy_size);
  3561. }
  3562. tokenreadsettings(current_settings,copy_size);
  3563. end;
  3564. ST_LOADMESSAGES:
  3565. begin
  3566. current_settings.pmessage:=nil;
  3567. mesgnb:=tokenreadsizeint;
  3568. prevmsg:=nil;
  3569. for i:=1 to mesgnb do
  3570. begin
  3571. new(pmsg);
  3572. if i=1 then
  3573. current_settings.pmessage:=pmsg
  3574. else
  3575. prevmsg^.next:=pmsg;
  3576. pmsg^.value:=tokenreadlongint;
  3577. pmsg^.state:=tmsgstate(tokenreadlongint);
  3578. pmsg^.next:=nil;
  3579. prevmsg:=pmsg;
  3580. end;
  3581. end;
  3582. ST_LINE:
  3583. begin
  3584. current_tokenpos.line:=tokenreadlongint;
  3585. current_filepos:=current_tokenpos;
  3586. end;
  3587. ST_COLUMN:
  3588. begin
  3589. current_tokenpos.column:=tokenreadword;
  3590. current_filepos:=current_tokenpos;
  3591. end;
  3592. ST_FILEINDEX:
  3593. begin
  3594. current_tokenpos.fileindex:=tokenreadword;
  3595. current_filepos:=current_tokenpos;
  3596. end;
  3597. end;
  3598. continue;
  3599. end;
  3600. else
  3601. ;
  3602. end;
  3603. break;
  3604. until false;
  3605. end;
  3606. procedure tscannerfile.addfile(hp:tinputfile);
  3607. begin
  3608. saveinputfile;
  3609. { add to list }
  3610. hp.next:=inputfile;
  3611. inputfile:=hp;
  3612. { load new inputfile }
  3613. restoreinputfile;
  3614. end;
  3615. procedure tscannerfile.reload;
  3616. var
  3617. wasmacro: Boolean;
  3618. begin
  3619. with inputfile do
  3620. begin
  3621. { when nothing more to read then leave immediatly, so we
  3622. don't change the current_filepos and leave it point to the last
  3623. char }
  3624. if (c=#26) and (not assigned(next)) then
  3625. exit;
  3626. repeat
  3627. { still more to read?, then change the #0 to a space so its seen
  3628. as a separator, this can't be used for macro's which can change
  3629. the place of the #0 in the buffer with tempopen }
  3630. if (c=#0) and (bufsize>0) and
  3631. not(inputfile.is_macro) and
  3632. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3633. (hidden_inputpointer-hidden_inputbuffer<bufsize) then
  3634. {$else not CHECK_INPUTPOINTER_LIMITS}
  3635. (inputpointer-inputbuffer<bufsize) then
  3636. {$endif CHECK_INPUTPOINTER_LIMITS}
  3637. begin
  3638. c:=' ';
  3639. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3640. inc_inputpointer;
  3641. {$else not CHECK_INPUTPOINTER_LIMITS}
  3642. inc(inputpointer);
  3643. {$endif CHECK_INPUTPOINTER_LIMITS}
  3644. exit;
  3645. end;
  3646. { can we read more from this file ? }
  3647. if (c<>#26) and (not endoffile) then
  3648. begin
  3649. readbuf;
  3650. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3651. hidden_inputpointer:=buf;
  3652. hidden_inputbuffer:=buf;
  3653. {$else not CHECK_INPUTPOINTER_LIMITS}
  3654. inputpointer:=buf;
  3655. inputbuffer:=buf;
  3656. {$endif CHECK_INPUTPOINTER_LIMITS}
  3657. inputstart:=bufstart;
  3658. { first line? }
  3659. if line_no=0 then
  3660. begin
  3661. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3662. c:=get_inputpointer_char;
  3663. { eat utf-8 signature? }
  3664. if (bufsize>2) and
  3665. (ord(get_inputpointer_char)=$ef) and
  3666. (ord(get_inputpointer_char(1))=$bb) and
  3667. (ord(get_inputpointer_char(2))=$bf) then
  3668. begin
  3669. {$else not CHECK_INPUTPOINTER_LIMITS}
  3670. c:=inputpointer^;
  3671. { eat utf-8 signature? }
  3672. if (bufsize>2) and
  3673. (ord(inputpointer^)=$ef) and
  3674. (ord((inputpointer+1)^)=$bb) and
  3675. (ord((inputpointer+2)^)=$bf) then
  3676. begin
  3677. {$endif CHECK_INPUTPOINTER_LIMITS}
  3678. (* we don't support including files with an UTF-8 bom
  3679. inside another file that wasn't encoded as UTF-8
  3680. already (we don't support {$codepage xxx} switches in
  3681. the middle of a file either) *)
  3682. if (current_settings.sourcecodepage<>CP_UTF8) and
  3683. not current_module.in_global then
  3684. Message(scanner_f_illegal_utf8_bom);
  3685. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3686. inc_inputpointer(3);
  3687. {$else not CHECK_INPUTPOINTER_LIMITS}
  3688. inc(inputpointer,3);
  3689. {$endif CHECK_INPUTPOINTER_LIMITS}
  3690. message(scan_c_switching_to_utf8);
  3691. current_settings.sourcecodepage:=CP_UTF8;
  3692. exclude(current_settings.moduleswitches,cs_system_codepage);
  3693. include(current_settings.moduleswitches,cs_explicit_codepage);
  3694. end;
  3695. line_no:=1;
  3696. if cs_asm_source in current_settings.globalswitches then
  3697. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3698. inputfile.setline(line_no,inputstart+hidden_inputpointer-hidden_inputbuffer);
  3699. {$else not CHECK_INPUTPOINTER_LIMITS}
  3700. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3701. {$endif CHECK_INPUTPOINTER_LIMITS}
  3702. end;
  3703. end
  3704. else
  3705. begin
  3706. wasmacro:=inputfile.is_macro;
  3707. { load eof position in tokenpos/current_filepos }
  3708. gettokenpos;
  3709. { close file }
  3710. closeinputfile;
  3711. { no next module, than EOF }
  3712. if not assigned(inputfile.next) then
  3713. begin
  3714. c:=#26;
  3715. exit;
  3716. end;
  3717. { load next file and reopen it }
  3718. nextfile;
  3719. tempopeninputfile;
  3720. { status }
  3721. Message1(scan_t_back_in,inputfile.name);
  3722. { end of include file is like a line break which ends e.g. also // style comments }
  3723. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3724. begin
  3725. c:=#10;
  3726. { ... but we have to decrease the line number first because it is increased due to this
  3727. inserted line break later on }
  3728. dec(line_no);
  3729. exit;
  3730. end;
  3731. end;
  3732. { load next char }
  3733. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3734. c:=get_inputpointer_char;
  3735. inc_inputpointer;
  3736. {$else not CHECK_INPUTPOINTER_LIMITS}
  3737. c:=inputpointer^;
  3738. inc(inputpointer);
  3739. {$endif CHECK_INPUTPOINTER_LIMITS}
  3740. until c<>#0; { if also end, then reload again }
  3741. end;
  3742. end;
  3743. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3744. var
  3745. hp : tinputfile;
  3746. begin
  3747. { save old postion }
  3748. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3749. dec_inputpointer;
  3750. {$else not CHECK_INPUTPOINTER_LIMITS}
  3751. dec(inputpointer);
  3752. {$endif CHECK_INPUTPOINTER_LIMITS}
  3753. tempcloseinputfile;
  3754. { create macro 'file' }
  3755. { use special name to dispose after !! }
  3756. hp:=do_openinputfile('_Macro_.'+macname);
  3757. addfile(hp);
  3758. with inputfile do
  3759. begin
  3760. inc(macro_nesting_depth);
  3761. setmacro(p,len);
  3762. { local buffer }
  3763. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3764. hidden_inputbuffer:=buf;
  3765. hidden_inputpointer:=buf;
  3766. {$else not CHECK_INPUTPOINTER_LIMITS}
  3767. inputbuffer:=buf;
  3768. inputpointer:=buf;
  3769. {$endif CHECK_INPUTPOINTER_LIMITS}
  3770. inputstart:=bufstart;
  3771. ref_index:=fileindex;
  3772. internally_generated_macro:=internally_generated;
  3773. end;
  3774. { reset line }
  3775. line_no:=line;
  3776. lastlinepos:=0;
  3777. lasttokenpos:=0;
  3778. nexttokenpos:=0;
  3779. { load new c }
  3780. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3781. c:=get_inputpointer_char;
  3782. inc_inputpointer;
  3783. {$else not CHECK_INPUTPOINTER_LIMITS}
  3784. c:=inputpointer^;
  3785. inc(inputpointer);
  3786. {$endif CHECK_INPUTPOINTER_LIMITS}
  3787. end;
  3788. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3789. begin
  3790. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3791. tokenpos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3792. {$else not CHECK_INPUTPOINTER_LIMITS}
  3793. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3794. {$endif CHECK_INPUTPOINTER_LIMITS}
  3795. filepos.line:=line_no;
  3796. filepos.column:=tokenpos-lastlinepos;
  3797. filepos.fileindex:=inputfile.ref_index;
  3798. filepos.moduleindex:=current_module.unit_index;
  3799. end;
  3800. procedure tscannerfile.gettokenpos;
  3801. { load the values of tokenpos and lasttokenpos }
  3802. begin
  3803. do_gettokenpos(lasttokenpos,current_tokenpos);
  3804. current_filepos:=current_tokenpos;
  3805. end;
  3806. procedure tscannerfile.cachenexttokenpos;
  3807. begin
  3808. do_gettokenpos(nexttokenpos,next_filepos);
  3809. end;
  3810. procedure tscannerfile.setnexttoken;
  3811. begin
  3812. token:=nexttoken;
  3813. nexttoken:=NOTOKEN;
  3814. lasttokenpos:=nexttokenpos;
  3815. current_tokenpos:=next_filepos;
  3816. current_filepos:=current_tokenpos;
  3817. nexttokenpos:=0;
  3818. end;
  3819. procedure tscannerfile.savetokenpos;
  3820. begin
  3821. oldlasttokenpos:=lasttokenpos;
  3822. oldcurrent_filepos:=current_filepos;
  3823. oldcurrent_tokenpos:=current_tokenpos;
  3824. end;
  3825. procedure tscannerfile.restoretokenpos;
  3826. begin
  3827. lasttokenpos:=oldlasttokenpos;
  3828. current_filepos:=oldcurrent_filepos;
  3829. current_tokenpos:=oldcurrent_tokenpos;
  3830. end;
  3831. procedure tscannerfile.inc_comment_level;
  3832. begin
  3833. if (m_nested_comment in current_settings.modeswitches) then
  3834. inc(comment_level)
  3835. else
  3836. comment_level:=1;
  3837. if (comment_level>1) then
  3838. begin
  3839. savetokenpos;
  3840. gettokenpos; { update for warning }
  3841. Message1(scan_w_comment_level,tostr(comment_level));
  3842. restoretokenpos;
  3843. end;
  3844. end;
  3845. procedure tscannerfile.dec_comment_level;
  3846. begin
  3847. if (m_nested_comment in current_settings.modeswitches) then
  3848. dec(comment_level)
  3849. else
  3850. comment_level:=0;
  3851. end;
  3852. procedure tscannerfile.linebreak;
  3853. var
  3854. cur : char;
  3855. begin
  3856. with inputfile do
  3857. begin
  3858. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3859. if (byte(get_inputpointer_char)=0) and not(endoffile) then
  3860. {$else not CHECK_INPUTPOINTER_LIMITS}
  3861. if (byte(inputpointer^)=0) and not(endoffile) then
  3862. {$endif CHECK_INPUTPOINTER_LIMITS}
  3863. begin
  3864. cur:=c;
  3865. reload;
  3866. if byte(cur)+byte(c)<>23 then
  3867. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3868. dec_inputpointer;
  3869. {$else not CHECK_INPUTPOINTER_LIMITS}
  3870. dec(inputpointer);
  3871. {$endif CHECK_INPUTPOINTER_LIMITS}
  3872. end
  3873. else
  3874. begin
  3875. { Support all combination of #10 and #13 as line break }
  3876. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3877. if (byte(get_inputpointer_char)+byte(c)=23) then
  3878. inc_inputpointer;
  3879. {$else not CHECK_INPUTPOINTER_LIMITS}
  3880. if (byte(inputpointer^)+byte(c)=23) then
  3881. inc(inputpointer);
  3882. {$endif CHECK_INPUTPOINTER_LIMITS}
  3883. end;
  3884. { Always return #10 as line break }
  3885. c:=#10;
  3886. { increase line counters }
  3887. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3888. lastlinepos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3889. {$else not CHECK_INPUTPOINTER_LIMITS}
  3890. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3891. {$endif CHECK_INPUTPOINTER_LIMITS}
  3892. inc(line_no);
  3893. { update linebuffer }
  3894. if cs_asm_source in current_settings.globalswitches then
  3895. inputfile.setline(line_no,lastlinepos);
  3896. { update for status and call the show status routine,
  3897. but don't touch current_filepos ! }
  3898. savetokenpos;
  3899. gettokenpos; { update for v_status }
  3900. inc(status.compiledlines);
  3901. ShowStatus;
  3902. restoretokenpos;
  3903. end;
  3904. end;
  3905. procedure tscannerfile.illegal_char(c:char);
  3906. var
  3907. s : string;
  3908. begin
  3909. if c in [#32..#255] then
  3910. s:=''''+c+''''
  3911. else
  3912. s:='#'+tostr(ord(c));
  3913. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3914. end;
  3915. procedure tscannerfile.end_of_file;
  3916. begin
  3917. checkpreprocstack;
  3918. Message(scan_f_end_of_file);
  3919. end;
  3920. {-------------------------------------------
  3921. IF Conditional Handling
  3922. -------------------------------------------}
  3923. procedure tscannerfile.checkpreprocstack;
  3924. begin
  3925. { check for missing ifdefs }
  3926. while assigned(preprocstack) do
  3927. begin
  3928. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3929. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3930. tostr(preprocstack.line_nb));
  3931. poppreprocstack;
  3932. end;
  3933. end;
  3934. procedure tscannerfile.poppreprocstack;
  3935. var
  3936. hp : tpreprocstack;
  3937. begin
  3938. if assigned(preprocstack) then
  3939. begin
  3940. Message1(scan_c_endif_found,preprocstack.name);
  3941. hp:=preprocstack.next;
  3942. preprocstack.free;
  3943. preprocstack:=hp;
  3944. end
  3945. else
  3946. Message(scan_e_endif_without_if);
  3947. end;
  3948. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3949. var
  3950. condition: Boolean;
  3951. valuedescr: String;
  3952. begin
  3953. if (preprocstack=nil) or preprocstack.accept then
  3954. condition:=compile_time_predicate(valuedescr)
  3955. else
  3956. begin
  3957. condition:= false;
  3958. valuedescr:= '';
  3959. end;
  3960. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3961. preprocstack.name:=valuedescr;
  3962. preprocstack.line_nb:=line_no;
  3963. preprocstack.fileindex:=current_filepos.fileindex;
  3964. if preprocstack.accept then
  3965. Message2(messid,preprocstack.name,'accepted')
  3966. else
  3967. Message2(messid,preprocstack.name,'rejected');
  3968. end;
  3969. procedure tscannerfile.elsepreprocstack;
  3970. begin
  3971. if assigned(preprocstack) and
  3972. (preprocstack.typ<>pp_else) then
  3973. begin
  3974. if (preprocstack.typ=pp_elseif) then
  3975. preprocstack.accept:=false
  3976. else
  3977. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3978. preprocstack.accept:=not preprocstack.accept;
  3979. preprocstack.iftyp:=preprocstack.typ;
  3980. preprocstack.typ:=pp_else;
  3981. preprocstack.line_nb:=line_no;
  3982. preprocstack.fileindex:=current_filepos.fileindex;
  3983. if preprocstack.accept then
  3984. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3985. else
  3986. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3987. end
  3988. else
  3989. Message(scan_e_endif_without_if);
  3990. end;
  3991. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  3992. var
  3993. valuedescr: String;
  3994. begin
  3995. if assigned(preprocstack) and
  3996. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  3997. begin
  3998. { when the branch is accepted we use pp_elseif so we know that
  3999. all the next branches need to be rejected. when this branch is still
  4000. not accepted then leave it at pp_if }
  4001. if (preprocstack.typ=pp_elseif) then
  4002. preprocstack.accept:=false
  4003. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  4004. begin
  4005. preprocstack.accept:=false;
  4006. preprocstack.typ:=pp_elseif;
  4007. end
  4008. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  4009. and compile_time_predicate(valuedescr) then
  4010. begin
  4011. preprocstack.name:=valuedescr;
  4012. preprocstack.accept:=true;
  4013. preprocstack.typ:=pp_elseif;
  4014. end;
  4015. preprocstack.line_nb:=line_no;
  4016. preprocstack.fileindex:=current_filepos.fileindex;
  4017. if preprocstack.accept then
  4018. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4019. else
  4020. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4021. end
  4022. else
  4023. Message(scan_e_endif_without_if);
  4024. end;
  4025. procedure tscannerfile.popreplaystack;
  4026. var
  4027. hp : treplaystack;
  4028. begin
  4029. if assigned(replaystack) then
  4030. begin
  4031. hp:=replaystack.next;
  4032. replaystack.free;
  4033. replaystack:=hp;
  4034. end;
  4035. end;
  4036. function tscannerfile.replay_stack_depth:longint;
  4037. var
  4038. tmp: treplaystack;
  4039. begin
  4040. result:=0;
  4041. tmp:=replaystack;
  4042. while assigned(tmp) do
  4043. begin
  4044. inc(result);
  4045. tmp:=tmp.next;
  4046. end;
  4047. end;
  4048. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  4049. begin
  4050. savetokenpos;
  4051. repeat
  4052. current_scanner.gettokenpos;
  4053. Message1(scan_d_handling_switch,'$'+p.name);
  4054. p.proc();
  4055. { accept the text ? }
  4056. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  4057. break
  4058. else
  4059. begin
  4060. current_scanner.gettokenpos;
  4061. Message(scan_c_skipping_until);
  4062. repeat
  4063. current_scanner.skipuntildirective;
  4064. if not (m_mac in current_settings.modeswitches) then
  4065. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  4066. else
  4067. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  4068. until assigned(p) and (p.is_conditional);
  4069. current_scanner.gettokenpos;
  4070. end;
  4071. until false;
  4072. restoretokenpos;
  4073. end;
  4074. procedure tscannerfile.handledirectives;
  4075. var
  4076. t : tdirectiveitem;
  4077. hs : string;
  4078. begin
  4079. gettokenpos;
  4080. readchar; {Remove the $}
  4081. hs:=readid;
  4082. { handle empty directive }
  4083. if hs='' then
  4084. begin
  4085. Message1(scan_w_illegal_switch,'$');
  4086. exit;
  4087. end;
  4088. {$ifdef PREPROCWRITE}
  4089. if parapreprocess then
  4090. begin
  4091. if not (m_mac in current_settings.modeswitches) then
  4092. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4093. else
  4094. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4095. if assigned(t) and not(t.is_conditional) then
  4096. begin
  4097. preprocfile.AddSpace;
  4098. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  4099. exit;
  4100. end;
  4101. end;
  4102. {$endif PREPROCWRITE}
  4103. { skip this directive? }
  4104. if (ignoredirectives.find(hs)<>nil) then
  4105. begin
  4106. if (comment_level>0) then
  4107. readcomment;
  4108. { we've read the whole comment }
  4109. current_commentstyle:=comment_none;
  4110. exit;
  4111. end;
  4112. { Check for compiler switches }
  4113. while (length(hs)=1) and (c in ['-','+']) do
  4114. begin
  4115. Message1(scan_d_handling_switch,'$'+hs+c);
  4116. HandleSwitch(hs[1],c);
  4117. current_scanner.readchar; {Remove + or -}
  4118. if c=',' then
  4119. begin
  4120. current_scanner.readchar; {Remove , }
  4121. { read next switch, support $v+,$+}
  4122. hs:=current_scanner.readid;
  4123. if (hs='') then
  4124. begin
  4125. if (c='$') and (m_fpc in current_settings.modeswitches) then
  4126. begin
  4127. current_scanner.readchar; { skip $ }
  4128. hs:=current_scanner.readid;
  4129. end;
  4130. if (hs='') then
  4131. Message1(scan_w_illegal_directive,'$'+c);
  4132. end;
  4133. end
  4134. else
  4135. hs:='';
  4136. end;
  4137. { directives may follow switches after a , }
  4138. if hs<>'' then
  4139. begin
  4140. if not (m_mac in current_settings.modeswitches) then
  4141. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4142. else
  4143. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4144. if assigned(t) then
  4145. begin
  4146. if t.is_conditional then
  4147. handleconditional(t)
  4148. else
  4149. begin
  4150. Message1(scan_d_handling_switch,'$'+hs);
  4151. t.proc();
  4152. end;
  4153. end
  4154. else
  4155. begin
  4156. current_scanner.ignoredirectives.Add(hs,nil);
  4157. Message1(scan_w_illegal_directive,'$'+hs);
  4158. end;
  4159. { conditionals already read the comment }
  4160. if (current_scanner.comment_level>0) then
  4161. current_scanner.readcomment;
  4162. { we've read the whole comment }
  4163. current_commentstyle:=comment_none;
  4164. end;
  4165. end;
  4166. procedure tscannerfile.readchar;
  4167. begin
  4168. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4169. c:=get_inputpointer_char;
  4170. {$else not CHECK_INPUTPOINTER_LIMITS}
  4171. c:=inputpointer^;
  4172. {$endif CHECK_INPUTPOINTER_LIMITS}
  4173. if c=#0 then
  4174. reload
  4175. else
  4176. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4177. inc_inputpointer;
  4178. {$else not CHECK_INPUTPOINTER_LIMITS}
  4179. inc(inputpointer);
  4180. {$endif CHECK_INPUTPOINTER_LIMITS}
  4181. end;
  4182. procedure tscannerfile.readstring;
  4183. var
  4184. i : longint;
  4185. err : boolean;
  4186. begin
  4187. err:=false;
  4188. i:=0;
  4189. repeat
  4190. case c of
  4191. '_','$',
  4192. '0'..'9',
  4193. 'A'..'Z',
  4194. 'a'..'z' :
  4195. begin
  4196. if (c='$') and not allowgenericid then
  4197. break;
  4198. if i<255 then
  4199. begin
  4200. inc(i);
  4201. orgpattern[i]:=c;
  4202. if c in ['a'..'z'] then
  4203. pattern[i]:=chr(ord(c)-32)
  4204. else
  4205. pattern[i]:=c;
  4206. end
  4207. else
  4208. begin
  4209. if not err then
  4210. begin
  4211. Message(scan_e_string_exceeds_255_chars);
  4212. err:=true;
  4213. end;
  4214. end;
  4215. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4216. c:=get_inputpointer_char;
  4217. inc_inputpointer;
  4218. {$else not CHECK_INPUTPOINTER_LIMITS}
  4219. c:=inputpointer^;
  4220. inc(inputpointer);
  4221. {$endif CHECK_INPUTPOINTER_LIMITS}
  4222. end;
  4223. #0 :
  4224. reload;
  4225. else if inputfile.internally_generated_macro and
  4226. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  4227. begin
  4228. if i<255 then
  4229. begin
  4230. inc(i);
  4231. orgpattern[i]:=c;
  4232. pattern[i]:=c;
  4233. end
  4234. else
  4235. begin
  4236. if not err then
  4237. begin
  4238. Message(scan_e_string_exceeds_255_chars);
  4239. err:=true;
  4240. end;
  4241. end;
  4242. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4243. c:=get_inputpointer_char;
  4244. inc_inputpointer;
  4245. {$else not CHECK_INPUTPOINTER_LIMITS}
  4246. c:=inputpointer^;
  4247. inc(inputpointer);
  4248. {$endif CHECK_INPUTPOINTER_LIMITS}
  4249. end
  4250. else
  4251. break;
  4252. end;
  4253. until false;
  4254. orgpattern[0]:=chr(i);
  4255. pattern[0]:=chr(i);
  4256. end;
  4257. procedure tscannerfile.readnumber;
  4258. var
  4259. base,
  4260. i : longint;
  4261. firstdigitread: Boolean;
  4262. begin
  4263. case c of
  4264. '%' :
  4265. begin
  4266. readchar;
  4267. base:=2;
  4268. pattern[1]:='%';
  4269. i:=1;
  4270. end;
  4271. '&' :
  4272. begin
  4273. readchar;
  4274. base:=8;
  4275. pattern[1]:='&';
  4276. i:=1;
  4277. end;
  4278. '$' :
  4279. begin
  4280. readchar;
  4281. base:=16;
  4282. pattern[1]:='$';
  4283. i:=1;
  4284. end;
  4285. else
  4286. begin
  4287. base:=10;
  4288. i:=0;
  4289. end;
  4290. end;
  4291. firstdigitread:=false;
  4292. while ((base>=10) and (c in ['0'..'9'])) or
  4293. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  4294. ((base=8) and (c in ['0'..'7'])) or
  4295. ((base=2) and (c in ['0'..'1'])) or
  4296. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4297. begin
  4298. if (i<255) and (c<>'_') then
  4299. begin
  4300. inc(i);
  4301. pattern[i]:=c;
  4302. end;
  4303. readchar;
  4304. firstdigitread:=true;
  4305. end;
  4306. pattern[0]:=chr(i);
  4307. end;
  4308. function tscannerfile.readid:string;
  4309. begin
  4310. readstring;
  4311. readid:=pattern;
  4312. end;
  4313. function tscannerfile.readval:longint;
  4314. var
  4315. l : longint;
  4316. w : integer;
  4317. begin
  4318. readnumber;
  4319. val(pattern,l,w);
  4320. readval:=l;
  4321. end;
  4322. function tscannerfile.readval64:int64;
  4323. var
  4324. l : int64;
  4325. w : integer;
  4326. begin
  4327. readnumber;
  4328. val(pattern,l,w);
  4329. readval64:=l;
  4330. end;
  4331. function tscannerfile.readcomment(include_special_char: boolean):string;
  4332. var
  4333. i : longint;
  4334. begin
  4335. i:=0;
  4336. repeat
  4337. case c of
  4338. '{' :
  4339. begin
  4340. if (include_special_char) and (i<255) then
  4341. begin
  4342. inc(i);
  4343. readcomment[i]:=c;
  4344. end;
  4345. if current_commentstyle=comment_tp then
  4346. inc_comment_level;
  4347. end;
  4348. '}' :
  4349. begin
  4350. if (include_special_char) and (i<255) then
  4351. begin
  4352. inc(i);
  4353. readcomment[i]:=c;
  4354. end;
  4355. if current_commentstyle=comment_tp then
  4356. begin
  4357. readchar;
  4358. dec_comment_level;
  4359. if comment_level=0 then
  4360. break
  4361. else
  4362. continue;
  4363. end;
  4364. end;
  4365. '*' :
  4366. begin
  4367. if current_commentstyle=comment_oldtp then
  4368. begin
  4369. readchar;
  4370. if c=')' then
  4371. begin
  4372. readchar;
  4373. dec_comment_level;
  4374. break;
  4375. end
  4376. else
  4377. { Add both characters !!}
  4378. if (i<255) then
  4379. begin
  4380. inc(i);
  4381. readcomment[i]:='*';
  4382. if (i<255) then
  4383. begin
  4384. inc(i);
  4385. readcomment[i]:=c;
  4386. end;
  4387. end;
  4388. end
  4389. else
  4390. { Not old TP comment, so add...}
  4391. begin
  4392. if (i<255) then
  4393. begin
  4394. inc(i);
  4395. readcomment[i]:='*';
  4396. end;
  4397. end;
  4398. end;
  4399. #10,#13 :
  4400. linebreak;
  4401. #26 :
  4402. end_of_file;
  4403. else
  4404. begin
  4405. if (i<255) then
  4406. begin
  4407. inc(i);
  4408. readcomment[i]:=c;
  4409. end;
  4410. end;
  4411. end;
  4412. readchar;
  4413. until false;
  4414. readcomment[0]:=chr(i);
  4415. end;
  4416. function tscannerfile.readquotedstring:string;
  4417. var
  4418. i : longint;
  4419. msgwritten : boolean;
  4420. begin
  4421. i:=0;
  4422. msgwritten:=false;
  4423. if (c='''') then
  4424. begin
  4425. repeat
  4426. readchar;
  4427. case c of
  4428. #26 :
  4429. end_of_file;
  4430. #10,#13 :
  4431. Message(scan_f_string_exceeds_line);
  4432. '''' :
  4433. begin
  4434. readchar;
  4435. if c<>'''' then
  4436. break;
  4437. end;
  4438. end;
  4439. if i<255 then
  4440. begin
  4441. inc(i);
  4442. result[i]:=c;
  4443. end
  4444. else
  4445. begin
  4446. if not msgwritten then
  4447. begin
  4448. Message(scan_e_string_exceeds_255_chars);
  4449. msgwritten:=true;
  4450. end;
  4451. end;
  4452. until false;
  4453. end;
  4454. result[0]:=chr(i);
  4455. end;
  4456. function tscannerfile.readstate:char;
  4457. var
  4458. state : char;
  4459. begin
  4460. state:=' ';
  4461. if c=' ' then
  4462. begin
  4463. current_scanner.skipspace;
  4464. current_scanner.readid;
  4465. if pattern='ON' then
  4466. state:='+'
  4467. else
  4468. if pattern='OFF' then
  4469. state:='-';
  4470. end
  4471. else
  4472. state:=c;
  4473. if not (state in ['+','-']) then
  4474. Message(scan_e_wrong_switch_toggle);
  4475. readstate:=state;
  4476. end;
  4477. function tscannerfile.readoptionalstate(fallback:char):char;
  4478. var
  4479. state : char;
  4480. begin
  4481. state:=' ';
  4482. if c=' ' then
  4483. begin
  4484. current_scanner.skipspace;
  4485. if c in ['*','}'] then
  4486. state:=fallback
  4487. else
  4488. begin
  4489. current_scanner.readid;
  4490. if pattern='ON' then
  4491. state:='+'
  4492. else
  4493. if pattern='OFF' then
  4494. state:='-';
  4495. end;
  4496. end
  4497. else
  4498. if c in ['*','}'] then
  4499. state:=fallback
  4500. else
  4501. state:=c;
  4502. if not (state in ['+','-']) then
  4503. Message(scan_e_wrong_switch_toggle);
  4504. readoptionalstate:=state;
  4505. end;
  4506. function tscannerfile.readstatedefault:char;
  4507. var
  4508. state : char;
  4509. begin
  4510. state:=' ';
  4511. if c=' ' then
  4512. begin
  4513. current_scanner.skipspace;
  4514. current_scanner.readid;
  4515. if pattern='ON' then
  4516. state:='+'
  4517. else
  4518. if pattern='OFF' then
  4519. state:='-'
  4520. else
  4521. if pattern='DEFAULT' then
  4522. state:='*';
  4523. end
  4524. else
  4525. state:=c;
  4526. if not (state in ['+','-','*']) then
  4527. Message(scan_e_wrong_switch_toggle_default);
  4528. readstatedefault:=state;
  4529. end;
  4530. procedure tscannerfile.skipspace;
  4531. begin
  4532. repeat
  4533. case c of
  4534. #26 :
  4535. begin
  4536. reload;
  4537. if (c=#26) and not assigned(inputfile.next) then
  4538. break;
  4539. continue;
  4540. end;
  4541. #10,
  4542. #13 :
  4543. linebreak;
  4544. #9,#11,#12,' ' :
  4545. ;
  4546. else
  4547. break;
  4548. end;
  4549. readchar;
  4550. until false;
  4551. end;
  4552. procedure tscannerfile.skipuntildirective;
  4553. var
  4554. found : longint;
  4555. next_char_loaded : boolean;
  4556. begin
  4557. found:=0;
  4558. next_char_loaded:=false;
  4559. repeat
  4560. case c of
  4561. #10,
  4562. #13 :
  4563. linebreak;
  4564. #26 :
  4565. begin
  4566. reload;
  4567. if (c=#26) and not assigned(inputfile.next) then
  4568. end_of_file;
  4569. continue;
  4570. end;
  4571. '{' :
  4572. begin
  4573. if (current_commentstyle in [comment_tp,comment_none]) then
  4574. begin
  4575. current_commentstyle:=comment_tp;
  4576. if (comment_level=0) then
  4577. found:=1;
  4578. inc_comment_level;
  4579. end;
  4580. end;
  4581. '*' :
  4582. begin
  4583. if (current_commentstyle=comment_oldtp) then
  4584. begin
  4585. readchar;
  4586. if c=')' then
  4587. begin
  4588. dec_comment_level;
  4589. found:=0;
  4590. current_commentstyle:=comment_none;
  4591. end
  4592. else
  4593. next_char_loaded:=true;
  4594. end
  4595. else
  4596. found := 0;
  4597. end;
  4598. '}' :
  4599. begin
  4600. if (current_commentstyle=comment_tp) then
  4601. begin
  4602. dec_comment_level;
  4603. if (comment_level=0) then
  4604. current_commentstyle:=comment_none;
  4605. found:=0;
  4606. end;
  4607. end;
  4608. '$' :
  4609. begin
  4610. if found=1 then
  4611. found:=2;
  4612. end;
  4613. '''' :
  4614. if (current_commentstyle=comment_none) then
  4615. begin
  4616. repeat
  4617. readchar;
  4618. case c of
  4619. #26 :
  4620. end_of_file;
  4621. #10,#13 :
  4622. break;
  4623. '''' :
  4624. begin
  4625. readchar;
  4626. if c<>'''' then
  4627. begin
  4628. next_char_loaded:=true;
  4629. break;
  4630. end;
  4631. end;
  4632. end;
  4633. until false;
  4634. end;
  4635. '(' :
  4636. begin
  4637. if (current_commentstyle=comment_none) then
  4638. begin
  4639. readchar;
  4640. if c='*' then
  4641. begin
  4642. readchar;
  4643. if c='$' then
  4644. begin
  4645. found:=2;
  4646. inc_comment_level;
  4647. current_commentstyle:=comment_oldtp;
  4648. end
  4649. else
  4650. begin
  4651. skipoldtpcomment(false);
  4652. next_char_loaded:=true;
  4653. end;
  4654. end
  4655. else
  4656. next_char_loaded:=true;
  4657. end
  4658. else
  4659. found:=0;
  4660. end;
  4661. '/' :
  4662. begin
  4663. if (current_commentstyle=comment_none) then
  4664. begin
  4665. readchar;
  4666. if c='/' then
  4667. skipdelphicomment;
  4668. next_char_loaded:=true;
  4669. end
  4670. else
  4671. found:=0;
  4672. end;
  4673. else
  4674. found:=0;
  4675. end;
  4676. if next_char_loaded then
  4677. next_char_loaded:=false
  4678. else
  4679. readchar;
  4680. until (found=2);
  4681. end;
  4682. {****************************************************************************
  4683. Comment Handling
  4684. ****************************************************************************}
  4685. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4686. begin
  4687. current_commentstyle:=comment_tp;
  4688. if read_first_char then
  4689. readchar;
  4690. inc_comment_level;
  4691. { handle compiler switches }
  4692. if (c='$') then
  4693. handledirectives;
  4694. { handle_switches can dec comment_level, }
  4695. while (comment_level>0) do
  4696. begin
  4697. case c of
  4698. '{' :
  4699. inc_comment_level;
  4700. '}' :
  4701. dec_comment_level;
  4702. '*' :
  4703. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4704. if m_iso in current_settings.modeswitches then
  4705. begin
  4706. readchar;
  4707. if c=')' then
  4708. dec_comment_level
  4709. else
  4710. continue;
  4711. end;
  4712. #10,#13 :
  4713. linebreak;
  4714. #26 :
  4715. begin
  4716. reload;
  4717. if (c=#26) and not assigned(inputfile.next) then
  4718. end_of_file;
  4719. continue;
  4720. end;
  4721. end;
  4722. readchar;
  4723. end;
  4724. current_commentstyle:=comment_none;
  4725. end;
  4726. procedure tscannerfile.skipdelphicomment;
  4727. begin
  4728. current_commentstyle:=comment_delphi;
  4729. inc_comment_level;
  4730. readchar;
  4731. { this is not supported }
  4732. if c='$' then
  4733. Message(scan_w_wrong_styled_switch);
  4734. { skip comment }
  4735. while not (c in [#10,#13,#26]) do
  4736. readchar;
  4737. dec_comment_level;
  4738. current_commentstyle:=comment_none;
  4739. end;
  4740. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4741. var
  4742. found : longint;
  4743. begin
  4744. current_commentstyle:=comment_oldtp;
  4745. inc_comment_level;
  4746. { only load a char if last already processed,
  4747. was cause of bug1634 PM }
  4748. if read_first_char then
  4749. readchar;
  4750. { this is now supported }
  4751. if (c='$') then
  4752. handledirectives;
  4753. { skip comment }
  4754. while (comment_level>0) do
  4755. begin
  4756. found:=0;
  4757. repeat
  4758. case c of
  4759. #26 :
  4760. begin
  4761. reload;
  4762. if (c=#26) and not assigned(inputfile.next) then
  4763. end_of_file;
  4764. continue;
  4765. end;
  4766. #10,#13 :
  4767. begin
  4768. if found=4 then
  4769. inc_comment_level;
  4770. linebreak;
  4771. found:=0;
  4772. end;
  4773. '*' :
  4774. begin
  4775. if found=3 then
  4776. found:=4
  4777. else
  4778. begin
  4779. if found=4 then
  4780. inc_comment_level;
  4781. found:=1;
  4782. end;
  4783. end;
  4784. ')' :
  4785. begin
  4786. if found in [1,4] then
  4787. begin
  4788. dec_comment_level;
  4789. if comment_level=0 then
  4790. found:=2
  4791. else
  4792. found:=0;
  4793. end
  4794. else
  4795. found:=0;
  4796. end;
  4797. '}' :
  4798. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  4799. if m_iso in current_settings.modeswitches then
  4800. begin
  4801. dec_comment_level;
  4802. if comment_level=0 then
  4803. found:=2
  4804. else
  4805. found:=0;
  4806. end;
  4807. '(' :
  4808. begin
  4809. if found=4 then
  4810. inc_comment_level;
  4811. found:=3;
  4812. end;
  4813. else
  4814. begin
  4815. if found=4 then
  4816. inc_comment_level;
  4817. found:=0;
  4818. end;
  4819. end;
  4820. readchar;
  4821. until (found=2);
  4822. end;
  4823. current_commentstyle:=comment_none;
  4824. end;
  4825. {****************************************************************************
  4826. Token Scanner
  4827. ****************************************************************************}
  4828. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4829. var
  4830. code : integer;
  4831. d : cardinal;
  4832. len,
  4833. low,high,mid : longint;
  4834. w : word;
  4835. m : longint;
  4836. mac : tmacro;
  4837. asciinr : string[33];
  4838. iswidestring , firstdigitread: boolean;
  4839. label
  4840. exit_label;
  4841. begin
  4842. flushpendingswitchesstate;
  4843. { record tokens? }
  4844. if allowrecordtoken and
  4845. assigned(recordtokenbuf) then
  4846. recordtoken;
  4847. { replay tokens? }
  4848. if assigned(replaytokenbuf) then
  4849. begin
  4850. replaytoken;
  4851. goto exit_label;
  4852. end;
  4853. { was there already a token read, then return that token }
  4854. if nexttoken<>NOTOKEN then
  4855. begin
  4856. setnexttoken;
  4857. goto exit_label;
  4858. end;
  4859. { Skip all spaces and comments }
  4860. repeat
  4861. case c of
  4862. '{' :
  4863. skipcomment(true);
  4864. #26 :
  4865. begin
  4866. reload;
  4867. if (c=#26) and not assigned(inputfile.next) then
  4868. break;
  4869. end;
  4870. ' ',#9..#13 :
  4871. begin
  4872. {$ifdef PREPROCWRITE}
  4873. if parapreprocess then
  4874. begin
  4875. if c=#10 then
  4876. preprocfile.eolfound:=true
  4877. else
  4878. preprocfile.spacefound:=true;
  4879. end;
  4880. {$endif PREPROCWRITE}
  4881. skipspace;
  4882. end
  4883. else
  4884. break;
  4885. end;
  4886. until false;
  4887. { Save current token position, for EOF its already loaded }
  4888. if c<>#26 then
  4889. gettokenpos;
  4890. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4891. if c in ['A'..'Z','a'..'z','_'] then
  4892. begin
  4893. readstring;
  4894. token:=_ID;
  4895. idtoken:=_ID;
  4896. { keyword or any other known token,
  4897. pattern is always uppercased }
  4898. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4899. begin
  4900. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4901. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4902. while low<high do
  4903. begin
  4904. mid:=(high+low+1) shr 1;
  4905. if pattern<tokeninfo^[ttoken(mid)].str then
  4906. high:=mid-1
  4907. else
  4908. low:=mid;
  4909. end;
  4910. with tokeninfo^[ttoken(high)] do
  4911. if pattern=str then
  4912. begin
  4913. if (keyword*current_settings.modeswitches)<>[] then
  4914. if op=NOTOKEN then
  4915. token:=ttoken(high)
  4916. else
  4917. token:=op;
  4918. idtoken:=ttoken(high);
  4919. end;
  4920. end;
  4921. { Only process identifiers and not keywords }
  4922. if token=_ID then
  4923. begin
  4924. { this takes some time ... }
  4925. if (cs_support_macro in current_settings.moduleswitches) then
  4926. begin
  4927. mac:=tmacro(search_macro(pattern));
  4928. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4929. begin
  4930. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  4931. begin
  4932. mac.is_used:=true;
  4933. inc(yylexcount);
  4934. substitutemacro(pattern,mac.buftext,mac.buflen,
  4935. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  4936. { handle empty macros }
  4937. if c=#0 then
  4938. begin
  4939. reload;
  4940. { avoid macro nesting error in case of
  4941. a sequence of empty macros, see #38802 }
  4942. dec(yylexcount);
  4943. readtoken(false);
  4944. end
  4945. else
  4946. begin
  4947. readtoken(false);
  4948. { that's all folks }
  4949. dec(yylexcount);
  4950. end;
  4951. exit;
  4952. end
  4953. else
  4954. Message(scan_w_macro_too_deep);
  4955. end;
  4956. end;
  4957. end;
  4958. { return token }
  4959. goto exit_label;
  4960. end
  4961. else
  4962. begin
  4963. idtoken:=_NOID;
  4964. case c of
  4965. '$' :
  4966. begin
  4967. readnumber;
  4968. token:=_INTCONST;
  4969. goto exit_label;
  4970. end;
  4971. '%' :
  4972. begin
  4973. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  4974. Illegal_Char(c)
  4975. else
  4976. begin
  4977. readnumber;
  4978. token:=_INTCONST;
  4979. goto exit_label;
  4980. end;
  4981. end;
  4982. '&' :
  4983. begin
  4984. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4985. begin
  4986. readnumber;
  4987. if length(pattern)=1 then
  4988. begin
  4989. { does really an identifier follow? }
  4990. if not (c in ['_','A'..'Z','a'..'z']) then
  4991. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  4992. readstring;
  4993. token:=_ID;
  4994. idtoken:=_ID;
  4995. end
  4996. else
  4997. token:=_INTCONST;
  4998. goto exit_label;
  4999. end
  5000. else if m_mac in current_settings.modeswitches then
  5001. begin
  5002. readchar;
  5003. token:=_AMPERSAND;
  5004. goto exit_label;
  5005. end
  5006. else
  5007. Illegal_Char(c);
  5008. end;
  5009. '0'..'9' :
  5010. begin
  5011. readnumber;
  5012. if (c in ['.','e','E']) then
  5013. begin
  5014. { first check for a . }
  5015. if c='.' then
  5016. begin
  5017. cachenexttokenpos;
  5018. readchar;
  5019. { is it a .. from a range? }
  5020. case c of
  5021. '.' :
  5022. begin
  5023. readchar;
  5024. token:=_INTCONST;
  5025. nexttoken:=_POINTPOINT;
  5026. goto exit_label;
  5027. end;
  5028. ')' :
  5029. begin
  5030. readchar;
  5031. token:=_INTCONST;
  5032. nexttoken:=_RECKKLAMMER;
  5033. goto exit_label;
  5034. end;
  5035. '0'..'9' :
  5036. begin
  5037. { insert the number after the . }
  5038. pattern:=pattern+'.';
  5039. firstdigitread:=false;
  5040. while (c in ['0'..'9']) or
  5041. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5042. begin
  5043. if c<>'_' then
  5044. pattern:=pattern+c;
  5045. readchar;
  5046. firstdigitread:=true;
  5047. end;
  5048. end;
  5049. else
  5050. begin
  5051. token:=_INTCONST;
  5052. nexttoken:=_POINT;
  5053. goto exit_label;
  5054. end;
  5055. end;
  5056. end;
  5057. { E can also follow after a point is scanned }
  5058. if c in ['e','E'] then
  5059. begin
  5060. pattern:=pattern+'E';
  5061. readchar;
  5062. if c in ['-','+'] then
  5063. begin
  5064. pattern:=pattern+c;
  5065. readchar;
  5066. end;
  5067. if not(c in ['0'..'9']) then
  5068. Illegal_Char(c);
  5069. firstdigitread:=false;
  5070. while (c in ['0'..'9']) or
  5071. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5072. begin
  5073. if c<>'_' then
  5074. pattern:=pattern+c;
  5075. readchar;
  5076. firstdigitread:=true;
  5077. end;
  5078. end;
  5079. token:=_REALNUMBER;
  5080. goto exit_label;
  5081. end;
  5082. token:=_INTCONST;
  5083. goto exit_label;
  5084. end;
  5085. ';' :
  5086. begin
  5087. readchar;
  5088. token:=_SEMICOLON;
  5089. goto exit_label;
  5090. end;
  5091. '[' :
  5092. begin
  5093. readchar;
  5094. token:=_LECKKLAMMER;
  5095. goto exit_label;
  5096. end;
  5097. ']' :
  5098. begin
  5099. readchar;
  5100. token:=_RECKKLAMMER;
  5101. goto exit_label;
  5102. end;
  5103. '(' :
  5104. begin
  5105. readchar;
  5106. case c of
  5107. '*' :
  5108. begin
  5109. skipoldtpcomment(true);
  5110. readtoken(false);
  5111. exit;
  5112. end;
  5113. '.' :
  5114. begin
  5115. readchar;
  5116. token:=_LECKKLAMMER;
  5117. goto exit_label;
  5118. end;
  5119. end;
  5120. token:=_LKLAMMER;
  5121. goto exit_label;
  5122. end;
  5123. ')' :
  5124. begin
  5125. readchar;
  5126. token:=_RKLAMMER;
  5127. goto exit_label;
  5128. end;
  5129. '+' :
  5130. begin
  5131. readchar;
  5132. if c='=' then
  5133. begin
  5134. readchar;
  5135. token:=_PLUSASN;
  5136. goto exit_label;
  5137. end;
  5138. token:=_PLUS;
  5139. goto exit_label;
  5140. end;
  5141. '-' :
  5142. begin
  5143. readchar;
  5144. if c='=' then
  5145. begin
  5146. readchar;
  5147. token:=_MINUSASN;
  5148. goto exit_label;
  5149. end;
  5150. token:=_MINUS;
  5151. goto exit_label;
  5152. end;
  5153. ':' :
  5154. begin
  5155. readchar;
  5156. if c='=' then
  5157. begin
  5158. readchar;
  5159. token:=_ASSIGNMENT;
  5160. goto exit_label;
  5161. end;
  5162. token:=_COLON;
  5163. goto exit_label;
  5164. end;
  5165. '*' :
  5166. begin
  5167. readchar;
  5168. if c='=' then
  5169. begin
  5170. readchar;
  5171. token:=_STARASN;
  5172. end
  5173. else
  5174. if c='*' then
  5175. begin
  5176. readchar;
  5177. token:=_STARSTAR;
  5178. end
  5179. else
  5180. token:=_STAR;
  5181. goto exit_label;
  5182. end;
  5183. '/' :
  5184. begin
  5185. readchar;
  5186. case c of
  5187. '=' :
  5188. begin
  5189. readchar;
  5190. token:=_SLASHASN;
  5191. goto exit_label;
  5192. end;
  5193. '/' :
  5194. begin
  5195. skipdelphicomment;
  5196. readtoken(false);
  5197. exit;
  5198. end;
  5199. end;
  5200. token:=_SLASH;
  5201. goto exit_label;
  5202. end;
  5203. '|' :
  5204. if m_mac in current_settings.modeswitches then
  5205. begin
  5206. readchar;
  5207. token:=_PIPE;
  5208. goto exit_label;
  5209. end
  5210. else
  5211. Illegal_Char(c);
  5212. '=' :
  5213. begin
  5214. readchar;
  5215. token:=_EQ;
  5216. goto exit_label;
  5217. end;
  5218. '.' :
  5219. begin
  5220. readchar;
  5221. case c of
  5222. '.' :
  5223. begin
  5224. readchar;
  5225. case c of
  5226. '.' :
  5227. begin
  5228. readchar;
  5229. token:=_POINTPOINTPOINT;
  5230. goto exit_label;
  5231. end;
  5232. else
  5233. begin
  5234. token:=_POINTPOINT;
  5235. goto exit_label;
  5236. end;
  5237. end;
  5238. end;
  5239. ')' :
  5240. begin
  5241. readchar;
  5242. token:=_RECKKLAMMER;
  5243. goto exit_label;
  5244. end;
  5245. end;
  5246. token:=_POINT;
  5247. goto exit_label;
  5248. end;
  5249. '@' :
  5250. begin
  5251. readchar;
  5252. token:=_KLAMMERAFFE;
  5253. goto exit_label;
  5254. end;
  5255. ',' :
  5256. begin
  5257. readchar;
  5258. token:=_COMMA;
  5259. goto exit_label;
  5260. end;
  5261. '''','#','^' :
  5262. begin
  5263. len:=0;
  5264. cstringpattern:='';
  5265. iswidestring:=false;
  5266. if c='^' then
  5267. begin
  5268. readchar;
  5269. c:=upcase(c);
  5270. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  5271. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  5272. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  5273. begin
  5274. token:=_CARET;
  5275. goto exit_label;
  5276. end
  5277. else
  5278. begin
  5279. inc(len);
  5280. setlength(cstringpattern,256);
  5281. if c<#64 then
  5282. cstringpattern[len]:=chr(ord(c)+64)
  5283. else
  5284. cstringpattern[len]:=chr(ord(c)-64);
  5285. readchar;
  5286. end;
  5287. end;
  5288. repeat
  5289. case c of
  5290. '#' :
  5291. begin
  5292. readchar; { read # }
  5293. case c of
  5294. '$':
  5295. begin
  5296. readchar; { read leading $ }
  5297. asciinr:='$';
  5298. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  5299. begin
  5300. asciinr:=asciinr+c;
  5301. readchar;
  5302. end;
  5303. end;
  5304. '&':
  5305. begin
  5306. readchar; { read leading $ }
  5307. asciinr:='&';
  5308. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  5309. begin
  5310. asciinr:=asciinr+c;
  5311. readchar;
  5312. end;
  5313. end;
  5314. '%':
  5315. begin
  5316. readchar; { read leading $ }
  5317. asciinr:='%';
  5318. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  5319. begin
  5320. asciinr:=asciinr+c;
  5321. readchar;
  5322. end;
  5323. end;
  5324. else
  5325. begin
  5326. asciinr:='';
  5327. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  5328. begin
  5329. asciinr:=asciinr+c;
  5330. readchar;
  5331. end;
  5332. end;
  5333. end;
  5334. val(asciinr,m,code);
  5335. if (asciinr='') or (code<>0) then
  5336. Message(scan_e_illegal_char_const)
  5337. else if (m<0) or (m>255) or (length(asciinr)>3) then
  5338. begin
  5339. if (m>=0) and (m<=$10FFFF) then
  5340. begin
  5341. if not iswidestring then
  5342. begin
  5343. if len>0 then
  5344. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5345. else
  5346. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5347. iswidestring:=true;
  5348. len:=0;
  5349. end;
  5350. if m<=$FFFF then
  5351. concatwidestringchar(patternw,tcompilerwidechar(m))
  5352. else
  5353. begin
  5354. { split into surrogate pair }
  5355. dec(m,$10000);
  5356. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5357. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5358. end;
  5359. end
  5360. else
  5361. Message(scan_e_illegal_char_const)
  5362. end
  5363. else if iswidestring then
  5364. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5365. else
  5366. begin
  5367. if len>=length(cstringpattern) then
  5368. setlength(cstringpattern,length(cstringpattern)+256);
  5369. inc(len);
  5370. cstringpattern[len]:=chr(m);
  5371. end;
  5372. end;
  5373. '''' :
  5374. begin
  5375. repeat
  5376. readchar;
  5377. case c of
  5378. #26 :
  5379. end_of_file;
  5380. #10,#13 :
  5381. Message(scan_f_string_exceeds_line);
  5382. '''' :
  5383. begin
  5384. readchar;
  5385. if c<>'''' then
  5386. break;
  5387. end;
  5388. end;
  5389. { interpret as utf-8 string? }
  5390. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5391. begin
  5392. { convert existing string to an utf-8 string }
  5393. if not iswidestring then
  5394. begin
  5395. if len>0 then
  5396. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5397. else
  5398. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5399. iswidestring:=true;
  5400. len:=0;
  5401. end;
  5402. { four chars }
  5403. if (ord(c) and $f0)=$f0 then
  5404. begin
  5405. { this always represents a surrogate pair, so
  5406. read as 32-bit value and then split into
  5407. the corresponding pair of two wchars }
  5408. d:=ord(c) and $f;
  5409. readchar;
  5410. if (ord(c) and $c0)<>$80 then
  5411. message(scan_e_utf8_malformed);
  5412. d:=(d shl 6) or (ord(c) and $3f);
  5413. readchar;
  5414. if (ord(c) and $c0)<>$80 then
  5415. message(scan_e_utf8_malformed);
  5416. d:=(d shl 6) or (ord(c) and $3f);
  5417. readchar;
  5418. if (ord(c) and $c0)<>$80 then
  5419. message(scan_e_utf8_malformed);
  5420. d:=(d shl 6) or (ord(c) and $3f);
  5421. if d<$10000 then
  5422. message(scan_e_utf8_malformed);
  5423. d:=d-$10000;
  5424. { high surrogate }
  5425. w:=$d800+(d shr 10);
  5426. concatwidestringchar(patternw,w);
  5427. { low surrogate }
  5428. w:=$dc00+(d and $3ff);
  5429. concatwidestringchar(patternw,w);
  5430. end
  5431. { three chars }
  5432. else if (ord(c) and $e0)=$e0 then
  5433. begin
  5434. w:=ord(c) and $f;
  5435. readchar;
  5436. if (ord(c) and $c0)<>$80 then
  5437. message(scan_e_utf8_malformed);
  5438. w:=(w shl 6) or (ord(c) and $3f);
  5439. readchar;
  5440. if (ord(c) and $c0)<>$80 then
  5441. message(scan_e_utf8_malformed);
  5442. w:=(w shl 6) or (ord(c) and $3f);
  5443. concatwidestringchar(patternw,w);
  5444. end
  5445. { two chars }
  5446. else if (ord(c) and $c0)<>0 then
  5447. begin
  5448. w:=ord(c) and $1f;
  5449. readchar;
  5450. if (ord(c) and $c0)<>$80 then
  5451. message(scan_e_utf8_malformed);
  5452. w:=(w shl 6) or (ord(c) and $3f);
  5453. concatwidestringchar(patternw,w);
  5454. end
  5455. { illegal }
  5456. else if (ord(c) and $80)<>0 then
  5457. message(scan_e_utf8_malformed)
  5458. else
  5459. concatwidestringchar(patternw,tcompilerwidechar(c))
  5460. end
  5461. else if iswidestring then
  5462. begin
  5463. if current_settings.sourcecodepage=CP_UTF8 then
  5464. concatwidestringchar(patternw,ord(c))
  5465. else
  5466. concatwidestringchar(patternw,asciichar2unicode(c))
  5467. end
  5468. else
  5469. begin
  5470. if len>=length(cstringpattern) then
  5471. setlength(cstringpattern,length(cstringpattern)+256);
  5472. inc(len);
  5473. cstringpattern[len]:=c;
  5474. end;
  5475. until false;
  5476. end;
  5477. '^' :
  5478. begin
  5479. readchar;
  5480. c:=upcase(c);
  5481. if c<#64 then
  5482. c:=chr(ord(c)+64)
  5483. else
  5484. c:=chr(ord(c)-64);
  5485. if iswidestring then
  5486. concatwidestringchar(patternw,asciichar2unicode(c))
  5487. else
  5488. begin
  5489. if len>=length(cstringpattern) then
  5490. setlength(cstringpattern,length(cstringpattern)+256);
  5491. inc(len);
  5492. cstringpattern[len]:=c;
  5493. end;
  5494. readchar;
  5495. end;
  5496. else
  5497. break;
  5498. end;
  5499. until false;
  5500. { strings with length 1 become const chars }
  5501. if iswidestring then
  5502. begin
  5503. if patternw^.len=1 then
  5504. token:=_CWCHAR
  5505. else
  5506. token:=_CWSTRING;
  5507. end
  5508. else
  5509. begin
  5510. setlength(cstringpattern,len);
  5511. if length(cstringpattern)=1 then
  5512. begin
  5513. token:=_CCHAR;
  5514. pattern:=cstringpattern;
  5515. end
  5516. else
  5517. token:=_CSTRING;
  5518. end;
  5519. goto exit_label;
  5520. end;
  5521. '>' :
  5522. begin
  5523. readchar;
  5524. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5525. token:=_RSHARPBRACKET
  5526. else
  5527. begin
  5528. case c of
  5529. '=' :
  5530. begin
  5531. readchar;
  5532. token:=_GTE;
  5533. goto exit_label;
  5534. end;
  5535. '>' :
  5536. begin
  5537. readchar;
  5538. token:=_OP_SHR;
  5539. goto exit_label;
  5540. end;
  5541. '<' :
  5542. begin { >< is for a symetric diff for sets }
  5543. readchar;
  5544. token:=_SYMDIF;
  5545. goto exit_label;
  5546. end;
  5547. end;
  5548. token:=_GT;
  5549. end;
  5550. goto exit_label;
  5551. end;
  5552. '<' :
  5553. begin
  5554. readchar;
  5555. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5556. token:=_LSHARPBRACKET
  5557. else
  5558. begin
  5559. case c of
  5560. '>' :
  5561. begin
  5562. readchar;
  5563. token:=_NE;
  5564. goto exit_label;
  5565. end;
  5566. '=' :
  5567. begin
  5568. readchar;
  5569. token:=_LTE;
  5570. goto exit_label;
  5571. end;
  5572. '<' :
  5573. begin
  5574. readchar;
  5575. token:=_OP_SHL;
  5576. goto exit_label;
  5577. end;
  5578. end;
  5579. token:=_LT;
  5580. end;
  5581. goto exit_label;
  5582. end;
  5583. #26 :
  5584. begin
  5585. token:=_EOF;
  5586. checkpreprocstack;
  5587. goto exit_label;
  5588. end;
  5589. else if inputfile.internally_generated_macro and
  5590. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  5591. begin
  5592. token:=_ID;
  5593. readstring;
  5594. end
  5595. else
  5596. Illegal_Char(c);
  5597. end;
  5598. end;
  5599. exit_label:
  5600. lasttoken:=token;
  5601. end;
  5602. function tscannerfile.readpreproc:ttoken;
  5603. var
  5604. low,high,mid: longint;
  5605. optoken: ttoken;
  5606. begin
  5607. skipspace;
  5608. case c of
  5609. '_',
  5610. 'A'..'Z',
  5611. 'a'..'z' :
  5612. begin
  5613. readstring;
  5614. optoken:=_ID;
  5615. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5616. begin
  5617. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5618. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5619. while low<high do
  5620. begin
  5621. mid:=(high+low+1) shr 1;
  5622. if pattern<tokeninfo^[ttoken(mid)].str then
  5623. high:=mid-1
  5624. else
  5625. low:=mid;
  5626. end;
  5627. with tokeninfo^[ttoken(high)] do
  5628. if pattern=str then
  5629. begin
  5630. if (keyword*current_settings.modeswitches)<>[] then
  5631. if op=NOTOKEN then
  5632. optoken:=ttoken(high)
  5633. else
  5634. optoken:=op;
  5635. end;
  5636. if not (optoken in preproc_operators) then
  5637. optoken:=_ID;
  5638. end;
  5639. current_scanner.preproc_pattern:=pattern;
  5640. readpreproc:=optoken;
  5641. end;
  5642. '''' :
  5643. begin
  5644. current_scanner.preproc_pattern:=readquotedstring;
  5645. readpreproc:=_CSTRING;
  5646. end;
  5647. '0'..'9' :
  5648. begin
  5649. readnumber;
  5650. if (c in ['.','e','E']) then
  5651. begin
  5652. { first check for a . }
  5653. if c='.' then
  5654. begin
  5655. readchar;
  5656. if c in ['0'..'9'] then
  5657. begin
  5658. { insert the number after the . }
  5659. pattern:=pattern+'.';
  5660. while c in ['0'..'9'] do
  5661. begin
  5662. pattern:=pattern+c;
  5663. readchar;
  5664. end;
  5665. end
  5666. else
  5667. Illegal_Char(c);
  5668. end;
  5669. { E can also follow after a point is scanned }
  5670. if c in ['e','E'] then
  5671. begin
  5672. pattern:=pattern+'E';
  5673. readchar;
  5674. if c in ['-','+'] then
  5675. begin
  5676. pattern:=pattern+c;
  5677. readchar;
  5678. end;
  5679. if not(c in ['0'..'9']) then
  5680. Illegal_Char(c);
  5681. while c in ['0'..'9'] do
  5682. begin
  5683. pattern:=pattern+c;
  5684. readchar;
  5685. end;
  5686. end;
  5687. readpreproc:=_REALNUMBER;
  5688. end
  5689. else
  5690. readpreproc:=_INTCONST;
  5691. current_scanner.preproc_pattern:=pattern;
  5692. end;
  5693. '$','%':
  5694. begin
  5695. readnumber;
  5696. current_scanner.preproc_pattern:=pattern;
  5697. readpreproc:=_INTCONST;
  5698. end;
  5699. '&' :
  5700. begin
  5701. readnumber;
  5702. if length(pattern)=1 then
  5703. begin
  5704. readstring;
  5705. readpreproc:=_ID;
  5706. end
  5707. else
  5708. readpreproc:=_INTCONST;
  5709. current_scanner.preproc_pattern:=pattern;
  5710. end;
  5711. '.' :
  5712. begin
  5713. readchar;
  5714. readpreproc:=_POINT;
  5715. end;
  5716. ',' :
  5717. begin
  5718. readchar;
  5719. readpreproc:=_COMMA;
  5720. end;
  5721. '}' :
  5722. begin
  5723. readpreproc:=_END;
  5724. end;
  5725. '(' :
  5726. begin
  5727. readchar;
  5728. readpreproc:=_LKLAMMER;
  5729. end;
  5730. ')' :
  5731. begin
  5732. readchar;
  5733. readpreproc:=_RKLAMMER;
  5734. end;
  5735. '[' :
  5736. begin
  5737. readchar;
  5738. readpreproc:=_LECKKLAMMER;
  5739. end;
  5740. ']' :
  5741. begin
  5742. readchar;
  5743. readpreproc:=_RECKKLAMMER;
  5744. end;
  5745. '+' :
  5746. begin
  5747. readchar;
  5748. readpreproc:=_PLUS;
  5749. end;
  5750. '-' :
  5751. begin
  5752. readchar;
  5753. readpreproc:=_MINUS;
  5754. end;
  5755. '*' :
  5756. begin
  5757. readchar;
  5758. readpreproc:=_STAR;
  5759. end;
  5760. '/' :
  5761. begin
  5762. readchar;
  5763. readpreproc:=_SLASH;
  5764. end;
  5765. '=' :
  5766. begin
  5767. readchar;
  5768. readpreproc:=_EQ;
  5769. end;
  5770. '>' :
  5771. begin
  5772. readchar;
  5773. if c='=' then
  5774. begin
  5775. readchar;
  5776. readpreproc:=_GTE;
  5777. end
  5778. else
  5779. readpreproc:=_GT;
  5780. end;
  5781. '<' :
  5782. begin
  5783. readchar;
  5784. case c of
  5785. '>' :
  5786. begin
  5787. readchar;
  5788. readpreproc:=_NE;
  5789. end;
  5790. '=' :
  5791. begin
  5792. readchar;
  5793. readpreproc:=_LTE;
  5794. end;
  5795. else
  5796. readpreproc:=_LT;
  5797. end;
  5798. end;
  5799. #26 :
  5800. begin
  5801. readpreproc:=_EOF;
  5802. checkpreprocstack;
  5803. end;
  5804. else
  5805. begin
  5806. Illegal_Char(c);
  5807. readpreproc:=NOTOKEN;
  5808. end;
  5809. end;
  5810. end;
  5811. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5812. var
  5813. hs : texprvalue;
  5814. begin
  5815. hs:=preproc_comp_expr(nil);
  5816. if hs.isInt then
  5817. begin
  5818. value:=hs.asInt64;
  5819. result:=true;
  5820. end
  5821. else
  5822. begin
  5823. hs.error('Integer',place);
  5824. result:=false;
  5825. end;
  5826. hs.free;
  5827. end;
  5828. function tscannerfile.readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  5829. var
  5830. hs : texprvalue;
  5831. begin
  5832. hs:=preproc_comp_expr(conform_to);
  5833. if hs.def.typ=setdef then
  5834. begin
  5835. value:=hs.asSet;
  5836. result:=true;
  5837. end
  5838. else
  5839. begin
  5840. hs.error('Set',place);
  5841. result:=false;
  5842. end;
  5843. hs.free;
  5844. end;
  5845. function tscannerfile.asmgetchar : char;
  5846. begin
  5847. readchar;
  5848. repeat
  5849. case c of
  5850. #26 :
  5851. begin
  5852. reload;
  5853. if (c=#26) and not assigned(inputfile.next) then
  5854. end_of_file;
  5855. continue;
  5856. end;
  5857. else
  5858. begin
  5859. asmgetchar:=c;
  5860. exit;
  5861. end;
  5862. end;
  5863. until false;
  5864. end;
  5865. {$ifdef EXTDEBUG}
  5866. function tscannerfile.DumpPointer: string;
  5867. var
  5868. i: Integer;
  5869. begin
  5870. Result:='';
  5871. if inputpointer=nil then exit;
  5872. i:=0;
  5873. While (inputpointer[I]<>#0) and (i<200) do
  5874. inc(i);
  5875. Setlength(result,I);
  5876. move(inputpointer^,Result[1],I);
  5877. result:='<'+inttostr(inputstart)+'>'+result;
  5878. end;
  5879. {$endif EXTDEBUG}
  5880. {*****************************************************************************
  5881. Helpers
  5882. *****************************************************************************}
  5883. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5884. begin
  5885. if dm in [directive_all, directive_turbo] then
  5886. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5887. if dm in [directive_all, directive_mac] then
  5888. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5889. end;
  5890. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5891. begin
  5892. if dm in [directive_all, directive_turbo] then
  5893. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5894. if dm in [directive_all, directive_mac] then
  5895. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5896. end;
  5897. {*****************************************************************************
  5898. Initialization
  5899. *****************************************************************************}
  5900. procedure InitScanner;
  5901. begin
  5902. InitWideString(patternw);
  5903. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5904. mac_scannerdirectives:=TFPHashObjectList.Create;
  5905. { Common directives and conditionals }
  5906. AddDirective('I',directive_all, @dir_include);
  5907. AddDirective('DEFINE',directive_all, @dir_define);
  5908. AddDirective('UNDEF',directive_all, @dir_undef);
  5909. AddConditional('IF',directive_all, @dir_if);
  5910. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5911. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5912. AddConditional('ELSE',directive_all, @dir_else);
  5913. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5914. AddConditional('ENDIF',directive_all, @dir_endif);
  5915. { Directives and conditionals for all modes except mode macpas}
  5916. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5917. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5918. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5919. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5920. AddConditional('IFEND',directive_turbo, @dir_ifend);
  5921. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5922. { Directives and conditionals for mode macpas: }
  5923. AddDirective('SETC',directive_mac, @dir_setc);
  5924. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5925. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5926. AddConditional('IFC',directive_mac, @dir_if);
  5927. AddConditional('ELSEC',directive_mac, @dir_else);
  5928. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5929. AddConditional('ENDC',directive_mac, @dir_endif);
  5930. end;
  5931. procedure DoneScanner;
  5932. begin
  5933. turbo_scannerdirectives.Free;
  5934. mac_scannerdirectives.Free;
  5935. DoneWideString(patternw);
  5936. end;
  5937. end.