scanner.pas 236 KB

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