scanner.pas 220 KB

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