scanner.pas 220 KB

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