FoxSemanticChecker.Mod 391 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Parser := FoxParser,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, ActiveCells := FoxActiveCells, SYSTEM, Streams, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. (* temporary variables for the visitors
  76. they replace variables on a stack during use of the visitor pattern and may only be
  77. - set in AcceptXXX procedures
  78. - set and read in ResolveXXX procedures
  79. *)
  80. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  81. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  82. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  83. currentScope-: SyntaxTree.Scope;
  84. currentIsRealtime: BOOLEAN;
  85. currentIsUnreachable: BOOLEAN;
  86. currentIsCellNet: BOOLEAN;
  87. currentIsBodyProcedure: BOOLEAN;
  88. currentIsExclusive: BOOLEAN;
  89. global: SyntaxTree.ModuleScope;
  90. withEntries: WithEntry;
  91. activeCellsStatement: BOOLEAN;
  92. activeCellsSpecification: ActiveCells.Specification;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; activeCellsSpecification: ActiveCells.Specification; VAR importCache: SyntaxTree.ModuleScope);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. SELF.activeCellsSpecification := activeCellsSpecification;
  104. error := FALSE;
  105. NEW(typeFixes);
  106. NEW(pointerFixes);
  107. resolvedType := NIL;
  108. resolvedExpression := NIL;
  109. resolvedStatement := NIL;
  110. currentScope := NIL;
  111. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  112. SELF.importCache := importCache;
  113. arrayBaseImported := FALSE;
  114. SELF.VerboseErrorMessage := verboseErrorMessage;
  115. global := NIL;
  116. phase := UndefinedPhase;
  117. currentIsRealtime := FALSE;
  118. currentIsUnreachable := FALSE;
  119. currentIsCellNet := FALSE;
  120. currentIsBodyProcedure := FALSE;
  121. currentIsExclusive := FALSE;
  122. withEntries := NIL;
  123. SELF.cellsAreObjects := system.cellsAreObjects;
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. END;
  182. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  183. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  184. ELSE
  185. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  186. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  187. symbol.MarkUsed;
  188. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  189. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  190. base := RecordBase(ownerRecord);
  191. IF (base # NIL) THEN
  192. baseScope := base.recordScope;
  193. symbol := Find(baseScope,name,FALSE);
  194. ELSE
  195. symbol := NIL;
  196. END;
  197. ELSE
  198. symbol := NIL;
  199. END;
  200. END;
  201. END;
  202. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  203. END;
  204. IF (symbol # NIL) THEN
  205. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  206. ASSERT(phase = DeclarationPhase);
  207. ResolveSymbol(symbol)
  208. END;
  209. symbol.MarkUsed;
  210. END;
  211. RETURN symbol
  212. END Find;
  213. (*** types ***)
  214. (** find type declaration with name qualifiedIdentifier and return resolved type
  215. - check qualified identifier prefix, set scope to module scope if appropriate
  216. - check suffix in scope
  217. **)
  218. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  219. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  220. BEGIN
  221. result := NIL;
  222. prevScope := currentScope;
  223. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  224. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  225. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  226. IF symbol(SyntaxTree.Import).module = NIL THEN
  227. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  228. result := SyntaxTree.invalidType;
  229. symbol := NIL;
  230. ELSE
  231. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  232. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  233. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)") END;
  234. END;
  235. ELSE
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  237. symbol := NIL;
  238. END;
  239. ELSE
  240. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  241. IF symbol = NIL THEN
  242. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  243. IF VerboseErrorMessage THEN
  244. Printout.Info("Qualident",qualifiedIdentifier);
  245. Printout.Info("in scope",currentScope) ;
  246. END;
  247. END;
  248. END;
  249. IF symbol = NIL THEN (* error already handled *)
  250. typeDeclaration := NIL;
  251. result := SyntaxTree.invalidType;
  252. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  253. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSE
  257. currentScope := symbol.scope;
  258. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  259. result := ResolveType(typeDeclaration.declaredType);
  260. symbol.MarkUsed;
  261. ASSERT(result # NIL);
  262. END;
  263. currentScope := prevScope;
  264. RETURN result
  265. END ResolveNamedType;
  266. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  267. If node is currently being resolved then emit a cyclic definition error.
  268. Return TRUE only if node is fully resolved.
  269. **)
  270. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  271. VAR result: BOOLEAN;
  272. BEGIN
  273. IF SyntaxTree.Resolved IN x.state THEN
  274. result := FALSE
  275. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  276. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  277. result := FALSE;
  278. ELSE
  279. result := TRUE;
  280. x.SetState(SyntaxTree.BeingResolved)
  281. END;
  282. RETURN result
  283. END TypeNeedsResolution;
  284. (** Return invalid type if x is currently being resolved, return x otherwise**)
  285. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  286. BEGIN
  287. IF SyntaxTree.Resolved IN x.state THEN
  288. RETURN x
  289. ELSE
  290. RETURN SyntaxTree.invalidType
  291. END;
  292. END ResolvedType;
  293. PROCEDURE VisitType(x: SyntaxTree.Type);
  294. BEGIN
  295. ASSERT(x = SyntaxTree.invalidType);
  296. END VisitType;
  297. (** resolve basic type **)
  298. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  299. BEGIN
  300. IF TypeNeedsResolution(x) THEN
  301. x.SetState(SyntaxTree.Resolved);
  302. END;
  303. resolvedType := ResolvedType(x)
  304. END VisitBasicType;
  305. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  306. BEGIN
  307. VisitBasicType(x);
  308. END VisitByteType;
  309. (** resolve character type **)
  310. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitCharacterType;
  314. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitBooleanType;
  318. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitSetType;
  322. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitAddressType;
  326. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitSizeType;
  330. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitAnyType;
  334. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitObjectType;
  338. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitNilType;
  342. (** resolve integer type **)
  343. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitIntegerType;
  347. (** resolve real type **)
  348. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitFloatType;
  352. (** resolve complex type **)
  353. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitComplexType;
  357. (**
  358. resolve string type: nothing to be done
  359. **)
  360. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  361. BEGIN
  362. IF TypeNeedsResolution(x) THEN
  363. x.SetState(SyntaxTree.Resolved);
  364. END;
  365. resolvedType := ResolvedType(x)
  366. END VisitStringType;
  367. (**
  368. check enumeration scope: enter symbols and check for duplicate names
  369. **)
  370. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  371. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  372. BEGIN
  373. prevScope := currentScope;
  374. currentScope := x;
  375. e := x.firstConstant;
  376. WHILE (e # NIL) DO
  377. Register(e,x,FALSE);
  378. IF SymbolNeedsResolution(e) THEN
  379. IF e.value # NIL THEN
  380. value := ConstantExpression(e.value);
  381. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  382. ELSE
  383. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  384. value.SetType(x.ownerEnumeration);
  385. END;
  386. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  387. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  388. IF nextHighest > highest THEN highest := nextHighest END;
  389. END;
  390. e.SetValue(value);
  391. CheckSymbolVisibility(e);
  392. e.SetType(x.ownerEnumeration);
  393. e.SetState(SyntaxTree.Resolved);
  394. END;
  395. e := e.nextConstant;
  396. END;
  397. currentScope := prevScope;
  398. END CheckEnumerationScope;
  399. (**
  400. resolve enumeration type: check enumeration scope
  401. **)
  402. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  403. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  404. lowest, highest: LONGINT;
  405. BEGIN
  406. IF TypeNeedsResolution(x) THEN
  407. IF x.enumerationBase # NIL THEN
  408. position := x.enumerationBase.position;
  409. baseType := ResolveType(x.enumerationBase);
  410. resolved := baseType.resolved;
  411. baseScope := NIL;
  412. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  413. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  414. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  415. ELSE
  416. enumerationBase := resolved(SyntaxTree.EnumerationType);
  417. lowest := enumerationBase.rangeHighest+1;
  418. END;
  419. x.SetEnumerationBase(baseType);
  420. ELSE lowest := 0;
  421. END;
  422. highest := lowest-1;
  423. CheckEnumerationScope(x.enumerationScope, highest);
  424. x.SetRange(lowest, highest);
  425. x.SetState(SyntaxTree.Resolved);
  426. END;
  427. resolvedType := ResolvedType(x);
  428. END VisitEnumerationType;
  429. (**
  430. resolve range type: nothing to be done
  431. **)
  432. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  433. BEGIN
  434. IF TypeNeedsResolution(x) THEN
  435. x.SetState(SyntaxTree.Resolved);
  436. END;
  437. resolvedType := ResolvedType(x)
  438. END VisitRangeType;
  439. (**
  440. resolve qualified type
  441. - find and resolve named type and set resolved type
  442. **)
  443. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  444. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  445. BEGIN
  446. IF TypeNeedsResolution(x) THEN
  447. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  448. x.SetResolved(type.resolved);
  449. x.SetState(SyntaxTree.Resolved);
  450. x.SetTypeDeclaration (typeDeclaration);
  451. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  452. x.SetResolved(SyntaxTree.invalidType);
  453. END;
  454. resolvedType := x;
  455. END VisitQualifiedType;
  456. (**
  457. resolve array type
  458. - check base type
  459. - array of math array forbidden
  460. - static array of open array forbidden
  461. **)
  462. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  463. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  464. BEGIN
  465. IF TypeNeedsResolution(x) THEN
  466. x.SetArrayBase(ResolveType(x.arrayBase));
  467. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  468. arrayBase := x.arrayBase.resolved;
  469. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  470. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  471. pointerType.SetPointerBase(arrayBase);
  472. pointerType.SetHidden(TRUE);
  473. x.SetArrayBase(pointerType);
  474. END;
  475. IF x.length # NIL THEN
  476. variableAccessed := FALSE;
  477. e := ResolveExpression(x.length);
  478. IF (e.resolved = NIL) THEN
  479. IF variableAccessed THEN
  480. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  481. END;
  482. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  483. ELSE
  484. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  485. END;
  486. END;
  487. IF arrayBase IS SyntaxTree.ArrayType THEN
  488. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  489. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  490. END;
  491. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  492. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  493. END;
  494. x.SetHasPointers(arrayBase.hasPointers);
  495. x.SetState(SyntaxTree.Resolved);
  496. END;
  497. resolvedType := ResolvedType(x);
  498. END VisitArrayType;
  499. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  500. VAR module: SyntaxTree.Module; import: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  501. BEGIN
  502. module := currentScope.ownerModule;
  503. IF module.name=name THEN
  504. (* do nothing *)
  505. ELSE
  506. moduleScope := module.moduleScope;
  507. import := moduleScope.FindImport(name);
  508. IF import = NIL THEN
  509. import := SyntaxTree.NewImport(position,name,name,TRUE);
  510. moduleScope.AddImport(import);
  511. Register(import,moduleScope,FALSE);
  512. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  513. VisitImport(import);
  514. ELSIF import.direct=FALSE THEN
  515. import.SetScope(module.moduleScope);
  516. import.SetDirect(TRUE);
  517. END;
  518. import.MarkUsed
  519. END;
  520. END ImportModule;
  521. (**
  522. resolve math array type
  523. - check base type
  524. - open math array of array forbidden
  525. - math array of tensor forbidden
  526. - static array of open array forbidden
  527. **)
  528. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  529. VAR arrayBase: SyntaxTree.Type;
  530. BEGIN
  531. IF TypeNeedsResolution(x) THEN
  532. x.SetArrayBase(ResolveType(x.arrayBase));
  533. IF x.length # NIL THEN
  534. x.SetLength(ConstantIntegerGeq0(x.length));
  535. END;
  536. arrayBase := x.arrayBase;
  537. IF arrayBase # NIL THEN
  538. arrayBase := arrayBase.resolved;
  539. IF arrayBase = SyntaxTree.invalidType THEN
  540. (* error already handled *)
  541. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  542. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  543. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  544. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  545. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  546. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  547. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  548. END;
  549. END;
  550. IF x.form = SyntaxTree.Static THEN
  551. x.SetIncrement(system.SizeOf(arrayBase));
  552. END;
  553. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  554. END;
  555. x.SetState(SyntaxTree.Resolved);
  556. END;
  557. resolvedType := ResolvedType(x);
  558. END VisitMathArrayType;
  559. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  560. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  561. (1) Rec = RECORD ... END; Ptr <---> Rec
  562. Ptr = POINTER TO Rec; ^ |
  563. | |
  564. TypeDesc TypeDesc
  565. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  566. ^ /
  567. | /
  568. TypeDesc <-- /
  569. *)
  570. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  571. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  572. BEGIN
  573. Strings.IntToStr(x.position,number);
  574. COPY(prefix,name);
  575. Strings.Append(name,"@");
  576. Strings.Append(name,number);
  577. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  578. typeDeclaration.SetDeclaredType(x);
  579. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  580. x.SetTypeDeclaration(typeDeclaration);
  581. currentScope.AddTypeDeclaration(typeDeclaration);
  582. typeDeclaration.SetScope(currentScope);
  583. END AnonymousTypeDeclaration;
  584. (**
  585. deferred pointer type resolving
  586. - resolve base type
  587. - check that base type is a record or array type
  588. - if error then set base type to invalid type
  589. **)
  590. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  591. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  592. BEGIN
  593. ASSERT(type.pointerBase # NIL);
  594. position := type.pointerBase.position;
  595. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  596. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  597. (* not for pointers, a type is needed for the records only
  598. IF type.typeDeclaration = NIL THEN
  599. AnonymousTypeDeclaration(type);
  600. END;
  601. *)
  602. END;
  603. resolved := ResolveType(type.pointerBase);
  604. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  605. type.SetPointerBase(resolved);
  606. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  607. recordType := resolved.resolved(SyntaxTree.RecordType);
  608. IF recordType.isObject & (recordType.baseType # NIL) THEN
  609. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  610. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  611. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  612. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  613. END;
  614. END;
  615. END;
  616. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  617. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  618. END;
  619. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  620. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  621. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  622. ELS
  623. *)
  624. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  625. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  626. END;
  627. END;
  628. ELSE
  629. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  630. type.SetPointerBase(SyntaxTree.invalidType)
  631. END
  632. END FixPointerType;
  633. (**
  634. resolve pointer type
  635. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  636. **)
  637. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  638. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  639. modifiers: SyntaxTree.Modifier; position: LONGINT;
  640. BEGIN
  641. IF TypeNeedsResolution(x) THEN
  642. modifiers := x.modifiers;
  643. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  644. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  645. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  646. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  647. (* inheritance cycle check
  648. example:
  649. A=POINTER TO RECORD(B) END;
  650. B=POINTER TO RECORD(A) END;
  651. *)
  652. IF x.pointerBase IS SyntaxTree.RecordType THEN
  653. recordType := x.pointerBase(SyntaxTree.RecordType);
  654. recordBaseType := ResolveType(recordType.baseType);
  655. recordType.SetBaseType(recordBaseType);
  656. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  657. END;
  658. CheckModifiers(modifiers, TRUE);
  659. typeFixes.Add(x,currentScope);
  660. x.SetState(SyntaxTree.Resolved);
  661. END;
  662. resolvedType := ResolvedType(x)
  663. END VisitPointerType;
  664. (**
  665. resolve port type
  666. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  667. **)
  668. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  669. VAR value: LONGINT;
  670. BEGIN
  671. IF TypeNeedsResolution(x) THEN
  672. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  673. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  674. x.SetSize(value)
  675. ELSE
  676. x.SetSize(system.SizeOf(system.longintType));
  677. END;
  678. x.SetState(SyntaxTree.Resolved);
  679. END;
  680. resolvedType := ResolvedType(x)
  681. END VisitPortType;
  682. (**
  683. deferred procedure type resolving
  684. - resolve return type
  685. - traverse and resolve parameters
  686. **)
  687. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  688. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  689. BEGIN
  690. resolved := ResolveType(procedureType.returnType);
  691. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  692. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  693. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  694. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  695. END;
  696. procedureType.SetReturnType(resolved);
  697. IF (resolved # NIL) THEN
  698. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  699. parameter.SetType(procedureType.returnType);
  700. parameter.SetAccess(SyntaxTree.Hidden);
  701. parameter.SetUntraced(procedureType.hasUntracedReturn);
  702. VisitParameter(parameter);
  703. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  704. END;
  705. (* process parameters *)
  706. parameter :=procedureType.firstParameter;
  707. WHILE (parameter # NIL) DO
  708. VisitParameter(parameter);
  709. parameter := parameter.nextParameter;
  710. END;
  711. END FixProcedureType;
  712. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  713. VAR prev,this: SyntaxTree.Modifier;
  714. BEGIN
  715. this := modifiers;prev := NIL;
  716. WHILE (this # NIL) & (this.identifier # name) DO
  717. prev := this; this := this.nextModifier;
  718. END;
  719. IF this # NIL THEN
  720. IF this.expression # NIL THEN
  721. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  722. END;
  723. this.Resolved;
  724. position := this.position;
  725. RETURN TRUE
  726. ELSE
  727. RETURN FALSE
  728. END;
  729. END HasFlag;
  730. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  731. VAR prev,this: SyntaxTree.Modifier;
  732. BEGIN
  733. this := modifiers;prev := NIL;
  734. WHILE (this # NIL) & (this.identifier # name) DO
  735. prev := this; this := this.nextModifier;
  736. END;
  737. IF this # NIL THEN
  738. IF this.expression = NIL THEN
  739. Error(this.position,Diagnostics.Invalid,"expected expression value");
  740. ELSE
  741. this.SetExpression(ConstantExpression(this.expression));
  742. IF CheckIntegerValue(this.expression,value) THEN END;
  743. END;
  744. this.Resolved;
  745. position := this.position;
  746. RETURN TRUE
  747. ELSE RETURN FALSE
  748. END;
  749. END HasValue;
  750. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  751. VAR this: SyntaxTree.Modifier;
  752. BEGIN
  753. this := modifiers;
  754. WHILE this # NIL DO
  755. IF ~this.resolved THEN
  756. IF checkUse OR (this.expression = NIL) THEN
  757. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  758. ELSE
  759. this.SetExpression(ResolveExpression(this.expression));
  760. this.Resolved;
  761. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  762. END;
  763. END;
  764. this := this.nextModifier
  765. END;
  766. END CheckModifiers;
  767. (**
  768. resolve procedure type
  769. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  770. **)
  771. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  772. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  773. BEGIN
  774. IF TypeNeedsResolution(procedureType) THEN
  775. modifiers := procedureType.modifiers;
  776. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  777. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  778. procedureType.SetInterrupt(TRUE);
  779. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  780. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  781. IF useDarwinCCalls THEN (*fld*)
  782. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  783. ELSE
  784. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  785. END
  786. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  787. procedureType.SetNoReturn(TRUE);
  788. END;
  789. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  790. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  791. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  792. CheckModifiers(modifiers, TRUE);
  793. modifiers := procedureType.returnTypeModifiers;
  794. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  795. CheckModifiers(modifiers, TRUE);
  796. typeFixes.Add(procedureType,currentScope);
  797. procedureType.SetHasPointers(procedureType.isDelegate);
  798. procedureType.SetState(SyntaxTree.Resolved);
  799. END;
  800. resolvedType := ResolvedType(procedureType)
  801. END VisitProcedureType;
  802. (** check and resolve record type
  803. - check base type: must be record, math array or array-structured object type
  804. - check declarations
  805. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  806. **)
  807. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  808. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  809. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  810. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  811. hasPointers: BOOLEAN;
  812. modifiers: SyntaxTree.Modifier;
  813. value: LONGINT;
  814. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  815. BEGIN
  816. type := type.resolved;
  817. IF (type IS SyntaxTree.PointerType) &
  818. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  819. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  820. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  821. RETURN TRUE
  822. ELSE
  823. RETURN FALSE
  824. END;
  825. END IsPointerToRecord;
  826. BEGIN
  827. IF TypeNeedsResolution(x) THEN
  828. hasPointers := FALSE;
  829. modifiers := x.modifiers;
  830. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  831. CheckModifiers(modifiers, TRUE);
  832. IF x.baseType # NIL THEN
  833. position := x.baseType.position;
  834. baseType := ResolveType(x.baseType);
  835. resolved := baseType.resolved;
  836. hasPointers := hasPointers OR resolved.hasPointers;
  837. IF x.isObject THEN (* object *)
  838. ASSERT(x.pointerType # NIL);
  839. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  840. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  841. baseType := NIL
  842. ELSIF IsPointerToRecord(resolved,recordType) THEN
  843. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  844. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  845. ELSE
  846. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  847. END;
  848. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  849. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  850. ELSIF IsPointerToRecord(resolved,recordType) THEN
  851. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  852. ELSIF resolved IS SyntaxTree.RecordType THEN
  853. ELSE
  854. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  855. END;
  856. ELSE
  857. IF resolved IS SyntaxTree.RecordType THEN
  858. ELSE
  859. Error(position, Diagnostics.Invalid,"record does not extend record")
  860. END;
  861. END;
  862. x.SetBaseType(baseType);
  863. IF x.Level() > 15 THEN
  864. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  865. (* note:
  866. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  867. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  868. inheritance history of a type.
  869. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  870. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  871. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  872. *)
  873. END;
  874. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  875. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  876. END;
  877. END;
  878. Declarations(x.recordScope);
  879. ResolveArrayStructure(x);
  880. (* computation of sizes and offsets skipped -> done in backend / system *)
  881. recordBase := x.GetBaseRecord();
  882. IF recordBase = NIL THEN numberMethods := 0
  883. ELSE numberMethods := recordBase.recordScope.numberMethods
  884. END;
  885. isRealtime := TRUE;
  886. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  887. WHILE symbol # NIL DO
  888. IF symbol IS SyntaxTree.Variable THEN
  889. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  890. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  891. END;
  892. IF symbol IS SyntaxTree.Procedure THEN
  893. procedure := symbol(SyntaxTree.Procedure);
  894. IF procedure.super # NIL THEN
  895. procedure.SetMethodNumber(procedure.super.methodNumber)
  896. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  897. procedure.SetMethodNumber(numberMethods);
  898. INC(numberMethods);
  899. END;
  900. END;
  901. symbol := symbol.nextSymbol;
  902. END;
  903. IF isRealtime THEN x.SetRealtime(TRUE) END;
  904. x.recordScope.SetNumberMethods(numberMethods);
  905. (* TODO: is this needed anymore? *)
  906. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  907. Error(x.position,Diagnostics.Invalid,"object extends a record")
  908. END;
  909. IF (x.typeDeclaration = NIL) THEN
  910. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  911. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  912. (*
  913. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  914. AnonymousTypeDeclaration(x,name);
  915. *)
  916. ELSE
  917. AnonymousTypeDeclaration(x,"Anonymous");
  918. END;
  919. END;
  920. x.SetHasPointers(hasPointers);
  921. x.SetState(SyntaxTree.Resolved);
  922. END;
  923. resolvedType := ResolvedType(x);
  924. END VisitRecordType;
  925. (** check and resolve cell type
  926. - check base type: must be cell
  927. - check declarations
  928. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  929. **)
  930. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  931. VAR
  932. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  933. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  934. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  935. recordBase: SyntaxTree.RecordType;
  936. numberMethods, int: LONGINT;
  937. variable: SyntaxTree.Variable;
  938. v: SyntaxTree.Expression;
  939. str: Scanner.StringType;
  940. atype: SyntaxTree.ArrayType;
  941. prev: SyntaxTree.Scope;
  942. BEGIN
  943. IF TypeNeedsResolution(x) THEN
  944. recordBase := NIL;
  945. IF cellsAreObjects THEN
  946. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  947. ImportModule(qualifiedIdentifier.prefix, x.position);
  948. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  949. x.SetBaseType(ResolveType(x.baseType));
  950. recordBase := x.GetBaseRecord();
  951. IF recordBase = NIL THEN
  952. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  953. END;
  954. END;
  955. IF recordBase = NIL THEN numberMethods := 0
  956. ELSE numberMethods := recordBase.recordScope.numberMethods
  957. END;
  958. modifier := x.modifiers;
  959. (*IF ~x.isCellNet THEN*)
  960. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  961. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  962. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  963. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  964. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  965. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  966. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  967. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  968. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  969. symbol := system.activeCellsCapabilities;
  970. WHILE symbol # NIL DO
  971. IF HasFlag(modifier, symbol.name, position) THEN END;
  972. symbol := symbol.nextSymbol;
  973. END;
  974. modifier := x.modifiers;
  975. WHILE (modifier # NIL) DO
  976. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  977. IF modifier.expression # NIL THEN
  978. v := ConstantExpression(modifier.expression);
  979. property.SetValue(v);
  980. IF IsIntegerValue(modifier.expression, int) THEN
  981. (*property.SetValue(modifier.expression);*)
  982. property.SetType(system.longintType);
  983. ELSIF IsStringValue(modifier.expression, str) THEN
  984. (*property.SetValue(modifier.expression);*)
  985. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  986. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  987. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  988. property.SetType(atype);
  989. ELSE
  990. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  991. END;
  992. ELSE (* flag property *)
  993. ASSERT(modifier.resolved);
  994. property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));
  995. property.SetType(system.booleanType);
  996. END;
  997. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  998. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  999. x.AddProperty(property);
  1000. modifier := modifier.nextModifier;
  1001. END;
  1002. (*ELSE
  1003. (* no: this should not be allowed on cell net types or check against global scope ...
  1004. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1005. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1006. END;
  1007. END
  1008. *)
  1009. END;*)
  1010. CheckModifiers(modifier, FALSE);
  1011. Declarations(x.cellScope);
  1012. (* process parameters *)
  1013. prev := currentScope;
  1014. currentScope := x.cellScope;
  1015. parameter :=x.firstParameter;
  1016. WHILE (parameter # NIL) DO
  1017. VisitParameter(parameter);
  1018. type := parameter.type.resolved;
  1019. IF ~(type IS SyntaxTree.PortType) THEN
  1020. WHILE IsStaticArray(type, type, len) DO
  1021. END;
  1022. WHILE IsDynamicArray(type, type) DO END;
  1023. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1024. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1025. END;
  1026. END;
  1027. parameter := parameter.nextParameter;
  1028. END;
  1029. currentScope := prev;
  1030. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1031. WHILE symbol # NIL DO
  1032. IF symbol IS SyntaxTree.Variable THEN
  1033. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1034. END;
  1035. symbol := symbol.nextSymbol;
  1036. END;
  1037. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1038. IF (x.typeDeclaration = NIL) THEN
  1039. AnonymousTypeDeclaration(x,"Anonymous");
  1040. END;
  1041. x.SetState(SyntaxTree.Resolved);
  1042. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1043. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1044. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1045. Warning(x.position, "Non-empty body for an engine?");
  1046. END;
  1047. END;
  1048. resolvedType := ResolvedType(x);
  1049. END VisitCellType;
  1050. (* check if an object is an array-structured object type
  1051. - determine the array structure
  1052. - collect operators from top to bottom in the inheritance hierarchy
  1053. - check if LEN operator is declared
  1054. - determine number of possible index operators
  1055. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1056. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1057. *)
  1058. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1059. VAR
  1060. indexOperatorCount, i: LONGINT;
  1061. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1062. isTensor: BOOLEAN;
  1063. BEGIN
  1064. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1065. (* determine array structure *)
  1066. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1067. END;
  1068. IF recordType.HasArrayStructure() THEN
  1069. (* the object is an ASOT *)
  1070. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1071. (* reset array access operators *)
  1072. arrayAccessOperators.len := NIL;
  1073. arrayAccessOperators.generalRead := NIL;
  1074. arrayAccessOperators.generalWrite := NIL;
  1075. IF isTensor THEN
  1076. (* all operators of dimensionalities 1 to max *)
  1077. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1078. ELSE
  1079. (* all operators of certain dimensionality *)
  1080. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1081. END;
  1082. NEW(arrayAccessOperators.read, indexOperatorCount);
  1083. NEW(arrayAccessOperators.write, indexOperatorCount);
  1084. FOR i := 0 TO indexOperatorCount - 1 DO
  1085. arrayAccessOperators.read[i] := NIL;
  1086. arrayAccessOperators.write[i] := NIL
  1087. END;
  1088. (* collect access operators in the record scope *)
  1089. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1090. IF arrayAccessOperators.len = NIL THEN
  1091. (* TODO: think about making this operator optional for static array structures *)
  1092. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1093. END;
  1094. (* show error messages *)
  1095. IF isTensor THEN
  1096. (* require ARRAY [*] OF RANGE *)
  1097. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1098. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1099. ELSE
  1100. (* forbid ARRAY [*] OF RANGE *)
  1101. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1102. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1103. (* require RANGE, RANGE, ... RANGE *)
  1104. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1105. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1106. END;
  1107. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1108. ELSE
  1109. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1110. IF recordType.recordScope.firstOperator # NIL THEN
  1111. RETURN;
  1112. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1113. END
  1114. END
  1115. END ResolveArrayStructure;
  1116. (** collect array access operators in a record scope **)
  1117. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1118. VAR
  1119. baseType: SyntaxTree.Type;
  1120. operator: SyntaxTree.Operator;
  1121. isReadOperator, isGeneralOperator: BOOLEAN;
  1122. indexListSize, indexListKind, hashValue: LONGINT;
  1123. BEGIN
  1124. (* if a parent record scope exists, collect the operators there first *)
  1125. baseType := recordScope.ownerRecord.baseType;
  1126. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1127. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1128. END;
  1129. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1130. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1131. END;
  1132. (* go through all operators in the current record scope *)
  1133. operator := recordScope.firstOperator;
  1134. WHILE operator # NIL DO
  1135. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1136. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1137. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1138. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1139. IF isGeneralOperator THEN
  1140. IF isReadOperator THEN
  1141. arrayAccessOperators.generalRead := operator
  1142. ELSE
  1143. arrayAccessOperators.generalWrite := operator
  1144. END
  1145. ELSE
  1146. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1147. IF isReadOperator THEN
  1148. arrayAccessOperators.read[hashValue] := operator
  1149. ELSE
  1150. arrayAccessOperators.write[hashValue] := operator
  1151. END
  1152. END
  1153. END
  1154. ELSE
  1155. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1156. END;
  1157. operator := operator.nextOperator
  1158. END
  1159. END CollectArrayAccessOperators;
  1160. (** the hash value of an index operator **)
  1161. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1162. VAR result: LONGINT;
  1163. BEGIN
  1164. IF isTensor THEN
  1165. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1166. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1167. ELSE
  1168. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1169. END
  1170. ELSE
  1171. result := indexListKind
  1172. END;
  1173. RETURN result
  1174. END IndexOperatorHash;
  1175. (** 2 to the power of exponent **)
  1176. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1177. VAR result, i: LONGINT;
  1178. BEGIN
  1179. result := 1;
  1180. FOR i := 1 TO exponent DO
  1181. result := result * 2;
  1182. END;
  1183. RETURN result
  1184. END TwoToThePowerOf;
  1185. (** check if a LEN operator has a correct signature. i.e.
  1186. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1187. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1188. **)
  1189. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1190. VAR
  1191. procedureType: SyntaxTree.ProcedureType;
  1192. returnedArrayType: SyntaxTree.MathArrayType;
  1193. result: BOOLEAN;
  1194. BEGIN
  1195. result := FALSE;
  1196. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1197. IF (procedureType.numberParameters = 0) THEN
  1198. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1199. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1200. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1201. IF returnedArrayType.form = SyntaxTree.Open THEN
  1202. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1203. result := TRUE
  1204. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1205. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1206. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1207. result := TRUE
  1208. END
  1209. END
  1210. END
  1211. END
  1212. END;
  1213. IF result THEN
  1214. (* export symbol automatically *)
  1215. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1216. ELSE
  1217. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1218. END;
  1219. RETURN result
  1220. END CheckLenOperator;
  1221. (** check if an index operator has a correct signature. i.e.
  1222. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1223. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1224. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1225. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1226. - determine if it is a read or write operator (existance of return type)
  1227. - check index parameters
  1228. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1229. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1230. [LONGINT] -> binary 0 -> 0
  1231. [RANGE] -> binary 1 -> 1
  1232. [LONGINT, LONGINT] -> binary 00 -> 0
  1233. [LONGINT, RANGE] -> binary 01 -> 1
  1234. [RANGE, LONGINT] -> binary 10 -> 2
  1235. [RANGE, RANGE] -> binary 11 -> 3
  1236. etc.
  1237. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1238. - for read operators, check if return type matches the type of data that is read
  1239. - for write operators, check if last parameter type matches the type of data that is written
  1240. **)
  1241. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1242. VAR
  1243. elementType, otherElementType, dataType: SyntaxTree.Type;
  1244. procedureType: SyntaxTree.ProcedureType;
  1245. mathArrayType: SyntaxTree.MathArrayType;
  1246. parameter: SyntaxTree.Parameter;
  1247. parameterCount, rangeCount, i: LONGINT;
  1248. hasTypeError: BOOLEAN;
  1249. BEGIN
  1250. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1251. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1252. (* determine if it is a read or write operator *)
  1253. isReadOperator := (procedureType.returnType # NIL);
  1254. IF isReadOperator THEN
  1255. indexListSize := parameterCount;
  1256. ELSE
  1257. indexListSize := parameterCount - 1;
  1258. END;
  1259. IF indexListSize < 1 THEN
  1260. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1261. RETURN FALSE
  1262. END;
  1263. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1264. (* general operator *)
  1265. isGeneralOperator := TRUE;
  1266. IF indexListSize > 1 THEN
  1267. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1268. RETURN FALSE
  1269. END;
  1270. (* ARRAY [*] OF RANGE*)
  1271. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1272. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1273. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1274. RETURN FALSE
  1275. END;
  1276. parameter := procedureType.firstParameter.nextParameter
  1277. ELSE
  1278. (* fixed-dim. operator *)
  1279. isGeneralOperator := FALSE;
  1280. (* check number of index parameters *)
  1281. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1282. (* for tensors, limited to a certain size *)
  1283. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1284. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1285. RETURN FALSE
  1286. END
  1287. ELSE
  1288. (* for non-tensors, depends on dimensionality *)
  1289. IF indexListSize # arrayStructure.Dimensionality() THEN
  1290. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1291. RETURN FALSE
  1292. END
  1293. END;
  1294. (* go through all index parameters
  1295. - count the number of ranges
  1296. - determine the index list kind number
  1297. *)
  1298. indexListKind := 0;
  1299. rangeCount := 0;
  1300. parameter := procedureType.firstParameter;
  1301. FOR i := 1 TO indexListSize DO
  1302. indexListKind := indexListKind * 2;
  1303. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1304. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1305. INC(indexListKind);
  1306. INC(rangeCount)
  1307. ELSE
  1308. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1309. RETURN FALSE
  1310. END;
  1311. parameter := parameter.nextParameter
  1312. END;
  1313. END;
  1314. (*
  1315. - for read operators: check type of last parameter
  1316. - for write operators: check return type
  1317. *)
  1318. IF isReadOperator THEN
  1319. dataType := procedureType.returnType (* the return type *)
  1320. ELSE
  1321. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1322. END;
  1323. elementType := arrayStructure.ElementType();
  1324. hasTypeError := FALSE;
  1325. IF isGeneralOperator THEN
  1326. (* ARRAY [?] OF <Element> *)
  1327. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1328. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1329. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1330. hasTypeError := TRUE
  1331. END
  1332. ELSE
  1333. hasTypeError := TRUE
  1334. END
  1335. ELSE
  1336. IF rangeCount = 0 THEN
  1337. (* <Element> *)
  1338. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1339. ELSE
  1340. (* ARRAY [*, *, ..., *] OF <Element> *)
  1341. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1342. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1343. IF mathArrayType.IsFullyDynamic() THEN
  1344. IF mathArrayType.Dimensionality() = rangeCount THEN
  1345. otherElementType := mathArrayType.ElementType();
  1346. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1347. ELSE
  1348. hasTypeError := TRUE
  1349. END
  1350. ELSE
  1351. hasTypeError := TRUE
  1352. END
  1353. ELSE
  1354. hasTypeError := TRUE
  1355. END
  1356. END
  1357. END;
  1358. IF hasTypeError THEN
  1359. IF isReadOperator THEN
  1360. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1361. ELSE
  1362. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1363. END;
  1364. RETURN FALSE
  1365. END;
  1366. (* export symbol automatically *)
  1367. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1368. RETURN TRUE
  1369. END CheckIndexOperator;
  1370. (** resolve all pending types (late resolving).
  1371. - type fixes are resolved at the end of the declaration phase
  1372. - type fixes may imply new type fixes that are also entered at the end of the list
  1373. **)
  1374. PROCEDURE FixTypes;
  1375. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1376. BEGIN
  1377. prevScope := currentScope;
  1378. p := typeFixes.Get(currentScope);
  1379. WHILE p # NIL DO
  1380. ASSERT(currentScope # NIL);
  1381. ASSERT(p IS SyntaxTree.Type);
  1382. IF p IS SyntaxTree.PointerType THEN
  1383. FixPointerType(p(SyntaxTree.PointerType))
  1384. ELSIF p IS SyntaxTree.ProcedureType THEN
  1385. FixProcedureType(p(SyntaxTree.ProcedureType))
  1386. ELSE
  1387. HALT(100);
  1388. END;
  1389. p := typeFixes.Get(currentScope);
  1390. END;
  1391. currentScope :=prevScope;
  1392. END FixTypes;
  1393. (**
  1394. resolve type x
  1395. - if x is nil then return nil
  1396. - if x cannot be resolved then the result is invalidType else the result is x
  1397. - the resolved type is entered into x.resolved
  1398. **)
  1399. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1400. VAR prev,resolved: SyntaxTree.Type;
  1401. BEGIN
  1402. prev := resolvedType;
  1403. resolvedType := SyntaxTree.invalidType;
  1404. IF x = NIL THEN resolvedType := NIL
  1405. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1406. END;
  1407. resolved := resolvedType;
  1408. resolvedType := prev;
  1409. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1410. RETURN resolved
  1411. END ResolveType;
  1412. (*** compatibility rules ***)
  1413. (**
  1414. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1415. **)
  1416. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1417. VAR result: SyntaxTree.Type;
  1418. BEGIN
  1419. result := SyntaxTree.invalidType;
  1420. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1421. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1422. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1423. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1424. ELSE result := type.resolved
  1425. END;
  1426. RETURN result
  1427. END RegularType;
  1428. (** returns signature compatibility of procedure types this and to
  1429. - if not compatible then error is reported
  1430. - compatibility means type equality
  1431. **)
  1432. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1433. VAR result: BOOLEAN;
  1434. BEGIN
  1435. result := SameType(to,this);
  1436. IF ~result THEN
  1437. Error(position,Diagnostics.Invalid,"signature incompatible");
  1438. IF VerboseErrorMessage THEN
  1439. Printout.Info("this",this);
  1440. Printout.Info("to",to);
  1441. END;
  1442. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1443. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1444. END;
  1445. RETURN result
  1446. END SignatureCompatible;
  1447. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1448. - for var parameters compatibility means same type except for
  1449. - formal is of open array of system byte
  1450. - formal is of record type
  1451. - formal is of open array type
  1452. - formal is of open math array type
  1453. - for value parameters compatibllity means assignment compatibility except for
  1454. - formal is of open array type
  1455. if compatible the return true else report error and return false
  1456. **)
  1457. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1458. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1459. BEGIN
  1460. formalType := RegularType(formal.position,formal.type);
  1461. actualType := RegularType(actual.position,actual.type);
  1462. error := FALSE;
  1463. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1464. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1465. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1466. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1467. END;
  1468. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1469. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1470. ELSIF ~IsVariable(actual) THEN
  1471. result := FALSE; error := TRUE;
  1472. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1473. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1474. ELSE
  1475. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1476. END;
  1477. IF VerboseErrorMessage THEN
  1478. Printout.Info("actual",actual);
  1479. Printout.Info("formal",formal);
  1480. END;
  1481. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1482. result := CompatibleTo(system,actualType,formalType);
  1483. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1484. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1485. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1486. IF IsArrayStructuredObjectType(actualType) THEN
  1487. actualType := MathArrayStructureOfType(actualType)
  1488. END;
  1489. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1490. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1491. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1492. END;
  1493. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1494. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1495. END;
  1496. ELSE
  1497. result := SameType(actualType,formalType)
  1498. END
  1499. ELSE
  1500. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1501. actualType := system.characterType;
  1502. END;
  1503. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1504. result := TRUE; (* special rule for WINAPI parameters *)
  1505. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1506. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1507. ELSE
  1508. result := CompatibleTo(system,actualType,formalType);
  1509. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1510. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1511. END;
  1512. END;
  1513. END;
  1514. IF ~result & ~error THEN
  1515. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1516. IF VerboseErrorMessage THEN
  1517. Printout.Info("actual",actual);
  1518. Printout.Info("formal",formal);
  1519. END;
  1520. END;
  1521. RETURN result
  1522. END ParameterCompatible;
  1523. (** check compatibility for expressions of the form left := right
  1524. - if compatible then return true else error report and return false
  1525. - check if left is variable
  1526. - check compatiblity
  1527. **)
  1528. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1529. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1530. BEGIN
  1531. result := FALSE;
  1532. leftType := RegularType(left.position,left.type);
  1533. rightType := RegularType(right.position,right.type);
  1534. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1535. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1536. END;
  1537. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1538. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1539. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1540. (* error already handled *)
  1541. result := TRUE;
  1542. ELSIF ~IsVariable(left) THEN
  1543. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1544. IF VerboseErrorMessage THEN
  1545. Printout.Info("left",left);
  1546. Printout.Info("right",right);
  1547. END;
  1548. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1549. result := TRUE;
  1550. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1551. result := TRUE
  1552. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1553. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1554. IF VerboseErrorMessage THEN
  1555. Printout.Info("left",left);
  1556. Printout.Info("right",right);
  1557. END;
  1558. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1559. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1560. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1561. ELSE
  1562. result := TRUE
  1563. END;
  1564. RETURN result
  1565. END AssignmentCompatible;
  1566. (*** values ***)
  1567. (** check and resolve integer value **)
  1568. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1569. VAR hugeint: HUGEINT;
  1570. BEGIN
  1571. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1572. value.SetType(Global.GetIntegerType(system,hugeint));
  1573. resolvedExpression := value
  1574. END VisitIntegerValue;
  1575. (** check and resolve real value **)
  1576. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1577. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1578. BEGIN
  1579. subtype := value(SyntaxTree.RealValue).subtype;
  1580. IF subtype = Scanner.Real THEN
  1581. type := system.realType
  1582. ELSIF subtype = Scanner.Longreal THEN
  1583. type := system.longrealType
  1584. ELSE
  1585. HALT(100)
  1586. END;
  1587. value.SetType(type);
  1588. resolvedExpression := value
  1589. END VisitRealValue;
  1590. (** check and resolve complex value **)
  1591. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1592. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1593. BEGIN
  1594. subtype := value(SyntaxTree.ComplexValue).subtype;
  1595. IF subtype = Scanner.Real THEN
  1596. type := system.complexType
  1597. ELSIF subtype = Scanner.Longreal THEN
  1598. type := system.longcomplexType
  1599. ELSE
  1600. HALT(100)
  1601. END;
  1602. value.SetType(type);
  1603. resolvedExpression := value
  1604. END VisitComplexValue;
  1605. (** check and resolve set value **)
  1606. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1607. BEGIN
  1608. value.SetType(system.setType);
  1609. resolvedExpression := value
  1610. END VisitSetValue;
  1611. (** check and resolve set value **)
  1612. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1613. BEGIN
  1614. value.SetType(SyntaxTree.invalidType);
  1615. resolvedExpression := value
  1616. END VisitMathArrayValue;
  1617. (** check and resolve boolean value **)
  1618. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1619. BEGIN
  1620. value.SetType(system.booleanType);
  1621. resolvedExpression := value
  1622. END VisitBooleanValue;
  1623. (** check and resolve string value **)
  1624. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1625. BEGIN
  1626. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1627. resolvedExpression := value
  1628. END VisitStringValue;
  1629. (** check and resolve character value **)
  1630. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1631. BEGIN
  1632. value.SetType(system.characterType);
  1633. resolvedExpression := value
  1634. END VisitCharacterValue;
  1635. (** check and resolve nil value **)
  1636. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1637. BEGIN
  1638. value.SetType(system.nilType);
  1639. resolvedExpression := value
  1640. END VisitNilValue;
  1641. (** check and resolve enumerator value **)
  1642. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1643. BEGIN
  1644. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1645. ASSERT(value.type # NIL);
  1646. resolvedExpression := value
  1647. END VisitEnumerationValue;
  1648. (*** expressions ***)
  1649. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1650. - check all elements on integer type
  1651. - if element range is constant, then check lower and upper bound
  1652. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1653. if an error occurs then report error and return invalidExpression
  1654. **)
  1655. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1656. VAR
  1657. i: LONGINT;
  1658. element: SyntaxTree.Expression;
  1659. constant: BOOLEAN;
  1660. elements: SyntaxTree.ExpressionList;
  1661. s: SET;
  1662. result: SyntaxTree.Expression;
  1663. value: SyntaxTree.Value;
  1664. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1665. VAR
  1666. left, right: SyntaxTree.Expression;
  1667. elementResult: SyntaxTree.Expression;
  1668. leftInteger, rightInteger: LONGINT;
  1669. BEGIN
  1670. (* set context of range *)
  1671. IF element IS SyntaxTree.RangeExpression THEN
  1672. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1673. END;
  1674. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1675. IF elementResult = SyntaxTree.invalidExpression THEN
  1676. (* error already reported *)
  1677. constant := FALSE
  1678. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1679. (* the element is a range expression *)
  1680. (* extract left and right hand side of range *)
  1681. left := elementResult(SyntaxTree.RangeExpression).first;
  1682. right := elementResult(SyntaxTree.RangeExpression).last;
  1683. (* guaranteed by VisitRangeExpression: *)
  1684. ASSERT((left # NIL) & (right # NIL));
  1685. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1686. ELSE
  1687. (* the element is not a range expression *)
  1688. (* check type and add conversion if needed *)
  1689. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1690. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1691. ELSE
  1692. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1693. elementResult := SyntaxTree.invalidExpression;
  1694. constant := FALSE
  1695. END;
  1696. left := elementResult;
  1697. right := elementResult
  1698. END;
  1699. IF elementResult # SyntaxTree.invalidExpression THEN
  1700. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1701. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1702. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1703. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1704. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1705. END
  1706. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1707. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1708. ELSE
  1709. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1710. s := s + {leftInteger..rightInteger};
  1711. (* same but more explicit:
  1712. WHILE (leftInteger <= rightInteger) DO
  1713. INCL(s,leftInteger);
  1714. INC(leftInteger);
  1715. END;
  1716. *)
  1717. END;
  1718. ELSE
  1719. constant := FALSE;
  1720. END
  1721. END;
  1722. RETURN elementResult
  1723. END CheckElement;
  1724. BEGIN
  1725. result := set; constant := TRUE; s := {}; elements := set.elements;
  1726. IF elements # NIL THEN
  1727. FOR i := 0 TO elements.Length()-1 DO
  1728. element := elements.GetExpression(i);
  1729. element := CheckElement(element);
  1730. IF element = SyntaxTree.invalidExpression THEN
  1731. result := SyntaxTree.invalidExpression
  1732. END;
  1733. elements.SetExpression(i,element);
  1734. END;
  1735. END;
  1736. IF constant THEN
  1737. value := SyntaxTree.NewSetValue(set.position,s);
  1738. value.SetType(system.setType);
  1739. result.SetResolved(value);
  1740. END;
  1741. (* optimization possible
  1742. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1743. left this to the programmer...
  1744. *)
  1745. result.SetType(system.setType);
  1746. resolvedExpression := result;
  1747. END VisitSet;
  1748. (*
  1749. old variant: quite generic but needs better conversion handling, do this?
  1750. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1751. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1752. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1753. BEGIN
  1754. type := NIL;
  1755. numberElements := x.elements.Length();
  1756. FOR i := 0 TO numberElements-1 DO
  1757. expression := x.elements.GetExpression(i);
  1758. position := expression.position;
  1759. expression := ResolveExpression(x.elements.GetExpression(i));
  1760. x.elements.SetExpression(i,de);
  1761. IF type = NIL THEN
  1762. type := expression.type;
  1763. ELSIF CompatibleTo(system,expression.type,type) THEN
  1764. (* ok *)
  1765. ELSIF CompatibleTo(system,type,expression.type) THEN
  1766. type := expression.type
  1767. ELSE
  1768. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1769. type := SyntaxTree.invalidType;
  1770. END;
  1771. END;
  1772. isValue := TRUE;
  1773. FOR i := 0 TO numberElements-1 DO
  1774. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1775. x.elements.SetExpression(i,expression);
  1776. isValue := isValue & (expression.resolved # NIL);
  1777. END;
  1778. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1779. arrayType.SetArrayBase(type);
  1780. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1781. IF isValue THEN
  1782. value := SyntaxTree.NewMathArrayValue(position);
  1783. value.SetElements(x.elements);
  1784. x.SetResolved(value);
  1785. END;
  1786. x.SetType(arrayType);
  1787. resolvedExpression := x;
  1788. END VisitMathArrayExpression;
  1789. *)
  1790. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1791. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1792. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1793. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1794. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1795. BEGIN
  1796. numberElements := x.elements.Length();
  1797. FOR i := 0 TO numberElements-1 DO
  1798. expression := x.elements.GetExpression(i);
  1799. IF expression IS SyntaxTree.MathArrayExpression THEN
  1800. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1801. ELSE
  1802. position := expression.position;
  1803. expression := ResolveExpression(x.elements.GetExpression(i));
  1804. x.elements.SetExpression(i,expression);
  1805. IF type = NIL THEN
  1806. type := expression.type;
  1807. ELSIF CompatibleTo(system,expression.type,type) THEN
  1808. (* ok *)
  1809. ELSIF CompatibleTo(system,type,expression.type) THEN
  1810. type := expression.type
  1811. ELSE
  1812. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1813. type := SyntaxTree.invalidType;
  1814. END;
  1815. END;
  1816. END;
  1817. END RecursivelyFindType;
  1818. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1819. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1820. BEGIN
  1821. numberElements := x.elements.Length();
  1822. FOR i := 0 TO numberElements-1 DO
  1823. expression := x.elements.GetExpression(i);
  1824. IF expression IS SyntaxTree.MathArrayExpression THEN
  1825. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1826. ELSE
  1827. position := expression.position;
  1828. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1829. x.elements.SetExpression(i,expression);
  1830. isValue := isValue & (expression.resolved # NIL);
  1831. END;
  1832. END;
  1833. END RecursivelySetExpression;
  1834. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1835. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1836. arrayType: SyntaxTree.MathArrayType;
  1837. BEGIN
  1838. numberElements := x.elements.Length();
  1839. baseType := NIL;
  1840. gsize := 0;
  1841. FOR i := 0 TO numberElements-1 DO
  1842. expression := x.elements.GetExpression(i);
  1843. IF expression IS SyntaxTree.MathArrayExpression THEN
  1844. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1845. IF i=0 THEN
  1846. gsize := size;
  1847. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1848. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1849. ELSE expression.SetType(baseType)
  1850. END;
  1851. ELSIF baseType = NIL THEN baseType := type;
  1852. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1853. END;
  1854. END;
  1855. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1856. arrayType.SetArrayBase(baseType);
  1857. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1858. RETURN ResolveType(arrayType);
  1859. END RecursivelySetType;
  1860. BEGIN
  1861. type := NIL;
  1862. RecursivelyFindType(x);
  1863. isValue := TRUE;
  1864. RecursivelySetExpression(x);
  1865. arrayType := RecursivelySetType(x);
  1866. x.SetType(arrayType);
  1867. IF isValue THEN
  1868. value := SyntaxTree.NewMathArrayValue(x.position);
  1869. value.SetArray(x);
  1870. x.SetResolved(value);
  1871. value.SetType(arrayType);
  1872. END;
  1873. x.SetType(arrayType);
  1874. resolvedExpression := x;
  1875. END VisitMathArrayExpression;
  1876. (** check and resolve unary expression **)
  1877. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1878. VAR
  1879. left: SyntaxTree.Expression;
  1880. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1881. bool: BOOLEAN;
  1882. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1883. value: SyntaxTree.Value;
  1884. BEGIN
  1885. type := SyntaxTree.invalidType;
  1886. left := ResolveExpression(unaryExpression.left);
  1887. unaryExpression.SetLeft(left);
  1888. operator := unaryExpression.operator;
  1889. result := unaryExpression;
  1890. IF ~system.operatorDefined[operator] THEN
  1891. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1892. RETURN
  1893. ELSIF left.type = NIL THEN
  1894. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1895. resolvedExpression := SyntaxTree.invalidExpression;
  1896. RETURN
  1897. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1898. RETURN
  1899. END;
  1900. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1901. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1902. END;
  1903. IF operatorCall # NIL THEN
  1904. result := operatorCall;
  1905. type := operatorCall.type;
  1906. (* admissible operators
  1907. Minus number, set
  1908. Not boolean
  1909. *)
  1910. ELSE
  1911. CASE unaryExpression.operator OF
  1912. |Scanner.Minus:
  1913. IF IsIntegerType(left.type.resolved) THEN
  1914. IF left.resolved # NIL THEN
  1915. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1916. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1917. result.SetResolved(value);
  1918. type := Global.GetIntegerType(system,int);
  1919. value.SetType(type);
  1920. ELSE
  1921. type := left.type
  1922. END
  1923. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1924. IF IsRealValue(left,real) THEN
  1925. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1926. result.SetResolved(value);
  1927. type := left.type;
  1928. value.SetType(type);
  1929. ELSE
  1930. type := left.type;
  1931. END;
  1932. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1933. IF IsSetValue(left,set) THEN
  1934. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1935. result.SetResolved(value);
  1936. type := left.type;
  1937. value.SetType(type);
  1938. ELSE
  1939. type := left.type;
  1940. END;
  1941. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1942. IF IsComplexValue(left, real, imaginary) THEN
  1943. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1944. result.SetResolved(value);
  1945. type := left.type;
  1946. value.SetType(type);
  1947. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1948. ELSE
  1949. type := left.type;
  1950. END
  1951. ELSE
  1952. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1953. END;
  1954. |Scanner.Not:
  1955. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1956. IF IsBooleanValue(left,bool) THEN
  1957. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1958. result.SetResolved(value);
  1959. type := system.booleanType;
  1960. value.SetType(type);
  1961. ELSE
  1962. type := system.booleanType;
  1963. END;
  1964. ELSE
  1965. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1966. END;
  1967. |Scanner.Plus:
  1968. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  1969. result := left; type := left.type;
  1970. ELSE
  1971. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1972. END;
  1973. (* ADDRESS OF *)
  1974. |Scanner.Address:
  1975. IF HasAddress(left) THEN
  1976. type := system.addressType;
  1977. ELSE
  1978. type := SyntaxTree.invalidType;
  1979. Error(left.position,Diagnostics.Invalid,"has no address");
  1980. Printout.Info("par", left);
  1981. END;
  1982. (* SIZE OF *)
  1983. |Scanner.Size:
  1984. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  1985. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  1986. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  1987. value := SyntaxTree.NewIntegerValue(left.position, int);
  1988. result.SetResolved(value);
  1989. type := Global.GetIntegerType(system,int);
  1990. value.SetType(type)
  1991. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  1992. ELSE
  1993. (* for variables, system sizeof could represent the physically occupied size
  1994. determined via the type descriptor, implement that ? *)
  1995. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  1996. END
  1997. (* ALIAS OF *)
  1998. |Scanner.Alias:
  1999. type := left.type.resolved;
  2000. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2001. type := SyntaxTree.invalidType;
  2002. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2003. END;
  2004. ELSE
  2005. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2006. END;
  2007. END;
  2008. result.SetType(type);
  2009. resolvedExpression := result
  2010. END VisitUnaryExpression;
  2011. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2012. VAR
  2013. result: SyntaxTree.Expression;
  2014. array: SyntaxTree.MathArrayExpression;
  2015. value: SyntaxTree.MathArrayValue;
  2016. isValue: BOOLEAN;
  2017. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2018. BEGIN
  2019. type := type.resolved;
  2020. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2021. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2022. END;
  2023. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2024. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2025. END;
  2026. RETURN type
  2027. END BaseType;
  2028. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2029. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2030. BEGIN
  2031. numberElements := x.elements.Length();
  2032. FOR i := 0 TO numberElements-1 DO
  2033. expression := x.elements.GetExpression(i);
  2034. IF expression IS SyntaxTree.MathArrayExpression THEN
  2035. array := SyntaxTree.NewMathArrayExpression(position);
  2036. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2037. expression := array;
  2038. ELSE
  2039. position := expression.position;
  2040. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2041. isValue := isValue & (expression.resolved # NIL);
  2042. END;
  2043. to.elements.AddExpression(expression);
  2044. END;
  2045. END RecursivelyConvert;
  2046. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2047. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2048. arrayType: SyntaxTree.MathArrayType;
  2049. BEGIN
  2050. numberElements := x.elements.Length();
  2051. baseType := NIL;
  2052. gsize := 0;
  2053. FOR i := 0 TO numberElements-1 DO
  2054. expression := x.elements.GetExpression(i);
  2055. IF expression IS SyntaxTree.MathArrayExpression THEN
  2056. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2057. IF i=0 THEN
  2058. gsize := size;
  2059. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2060. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2061. ELSE expression.SetType(baseType)
  2062. END;
  2063. ELSIF baseType = NIL THEN baseType := type;
  2064. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2065. END;
  2066. END;
  2067. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2068. arrayType.SetArrayBase(baseType);
  2069. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2070. RETURN ResolveType(arrayType);
  2071. END RecursivelySetType;
  2072. BEGIN
  2073. result := SyntaxTree.invalidExpression;
  2074. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2075. result := expression (* do not convert *)
  2076. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2077. isValue := TRUE;
  2078. type := BaseType(type);
  2079. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2080. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2081. value := SyntaxTree.NewMathArrayValue(array.position);
  2082. value.SetArray(array);
  2083. value.SetType(RecursivelySetType(array));
  2084. result := value;
  2085. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2086. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2087. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2088. IF result = NIL THEN
  2089. result := SyntaxTree.invalidExpression;
  2090. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2091. IF VerboseErrorMessage THEN
  2092. Printout.Info("expression",expression);
  2093. Printout.Info("type",type);
  2094. END;
  2095. END;
  2096. END;
  2097. RETURN result
  2098. END MathArrayConversion;
  2099. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2100. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2101. BEGIN
  2102. result := expression; type := type.resolved;
  2103. IF (expression IS SyntaxTree.IntegerValue) THEN
  2104. int := expression(SyntaxTree.IntegerValue).hvalue;
  2105. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2106. int := Global.ConvertSigned(int,system.SizeOf(type));
  2107. result := SyntaxTree.NewIntegerValue(position,int);
  2108. result.SetType(type);
  2109. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2110. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2111. result := SyntaxTree.NewIntegerValue(position,int);
  2112. result.SetType(type);
  2113. ELSIF (type IS SyntaxTree.FloatType) THEN
  2114. result := SyntaxTree.NewRealValue(expression.position,int);
  2115. result.SetType(type);
  2116. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2117. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2118. result.SetType(type);
  2119. ELSIF (type IS SyntaxTree.SetType) THEN
  2120. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2121. result.SetType(type);
  2122. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2123. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2124. result.SetType(type);
  2125. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2126. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2127. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2128. END;
  2129. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2130. result.SetType(type);
  2131. ELSIF (type IS SyntaxTree.PortType) THEN
  2132. result := ConvertValue(position, expression, system.integerType);
  2133. ELSE
  2134. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2135. result := SyntaxTree.invalidExpression;
  2136. IF VerboseErrorMessage THEN
  2137. Printout.Info("expression",expression);
  2138. Printout.Info("type",type);
  2139. END;
  2140. END;
  2141. ELSIF IsRealValue(expression,real) THEN
  2142. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2143. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2144. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2145. result.SetType(type);
  2146. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2147. int := ENTIERH(real);
  2148. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2149. result.SetType(type);
  2150. ELSIF (type IS SyntaxTree.FloatType) THEN
  2151. result := SyntaxTree.NewRealValue(position,real);
  2152. result.SetType(type);
  2153. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2154. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2155. result.SetType(type);
  2156. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2157. ELSIF (type IS SyntaxTree.PortType) THEN
  2158. result := ConvertValue(position, expression, system.integerType);
  2159. ELSE
  2160. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2161. result := SyntaxTree.invalidExpression;
  2162. END
  2163. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2164. IF (type IS SyntaxTree.ComplexType) THEN
  2165. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2166. result.SetType(type);
  2167. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2168. ELSE
  2169. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2170. result := SyntaxTree.invalidExpression;
  2171. END
  2172. ELSIF IsSetValue(expression,set) THEN
  2173. IF (type IS SyntaxTree.IntegerType) THEN
  2174. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2175. result.SetType(type);
  2176. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2177. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2178. result.SetType(type);
  2179. ELSIF (type IS SyntaxTree.PortType) THEN
  2180. result := ConvertValue(position, expression, system.integerType);
  2181. ELSE
  2182. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2183. result := SyntaxTree.invalidExpression;
  2184. END;
  2185. ELSIF IsStringValue(expression,string) THEN
  2186. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2187. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2188. result.SetType(type);
  2189. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2190. ELSE
  2191. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2192. result := SyntaxTree.invalidExpression;
  2193. END;
  2194. ELSIF IsCharacterValue(expression,char) THEN
  2195. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2196. string[0] := char; string[1] := 0X;
  2197. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2198. result := SyntaxTree.NewStringValue(expression.position,string);
  2199. result.SetType(type);
  2200. ELSIF (type IS SyntaxTree.ByteType) THEN
  2201. (* do not simply set the new type as this could invalidate types of constants *)
  2202. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2203. result.SetType(type)
  2204. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2205. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2206. result.SetType(type);
  2207. ELSIF (type IS SyntaxTree.SetType) THEN
  2208. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2209. result.SetType(type);
  2210. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2211. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2212. result.SetType(type);
  2213. ELSIF (type IS SyntaxTree.PortType) THEN
  2214. result := ConvertValue(position, expression, system.integerType);
  2215. ELSE
  2216. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2217. result := SyntaxTree.invalidExpression;
  2218. END;
  2219. ELSIF expression IS SyntaxTree.NilValue THEN
  2220. IF type IS SyntaxTree.AddressType THEN
  2221. result := SyntaxTree.NewIntegerValue(position,0);
  2222. result.SetType(type);
  2223. ELSE
  2224. result := expression;
  2225. END;
  2226. (* nothing to be done *)
  2227. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2228. result := MathArrayConversion(position, expression,type);
  2229. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2230. int := expression(SyntaxTree.EnumerationValue).value;
  2231. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2232. int := Global.ConvertSigned(int,system.SizeOf(type));
  2233. result := SyntaxTree.NewIntegerValue(position,int);
  2234. result.SetType(type);
  2235. ELSE
  2236. result := expression;
  2237. END;
  2238. (* nothing to be done *)
  2239. ELSE
  2240. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2241. IF VerboseErrorMessage THEN
  2242. Printout.Info("expression",expression);
  2243. Printout.Info("type",type);
  2244. END;
  2245. result := SyntaxTree.invalidExpression;
  2246. END;
  2247. RETURN result
  2248. END ConvertValue;
  2249. (**
  2250. return a conversion of an expression to a given type
  2251. - if expression is already of same type then return expression
  2252. - if incompatible conversion then report error and return invalidExpression
  2253. **)
  2254. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2255. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2256. BEGIN
  2257. type := type.resolved;
  2258. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2259. result := expression;
  2260. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2261. ELSIF expression = NIL THEN (* NIL expression *)
  2262. ELSIF expression.type = NIL THEN
  2263. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2264. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2265. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2266. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2267. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2268. ELSIF expression.resolved # NIL THEN (* value *)
  2269. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2270. IF value IS SyntaxTree.Value THEN
  2271. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2272. result.SetResolved(value(SyntaxTree.Value));
  2273. result.SetType(value.type);
  2274. ELSE
  2275. result := value
  2276. END;
  2277. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2278. expressionList := SyntaxTree.NewExpressionList();
  2279. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2280. typeDeclaration.SetDeclaredType(type);
  2281. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2282. typeSymbol.SetType(typeDeclaration.type);
  2283. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2284. expressionList.AddExpression(expression);
  2285. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2286. result.SetType(type);
  2287. ELSIF IsArrayStructuredObjectType(type) THEN
  2288. (* no type can be converted to an array-structured object type *)
  2289. HALT(100)
  2290. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2291. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2292. result := MathArrayConversion(position, expression,type);
  2293. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2294. expression := ConvertToMathArray(expression);
  2295. type := MathArrayStructureOfType(type);
  2296. result := MathArrayConversion(position, expression, type)
  2297. ELSE
  2298. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2299. END;
  2300. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2301. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2302. OR ~(type IS SyntaxTree.ArrayType) THEN
  2303. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2304. END;
  2305. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2306. (*skip, no conversion*)
  2307. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2308. (* skip, no conversion *)
  2309. ELSE
  2310. ASSERT(~(type IS SyntaxTree.RangeType));
  2311. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2312. ASSERT(type # NIL);
  2313. END;
  2314. RETURN result
  2315. END NewConversion;
  2316. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2317. BEGIN
  2318. IF CompatibleTo(system,expression.type, type) THEN
  2319. RETURN NewConversion(position, expression, type, NIL);
  2320. ELSE
  2321. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2322. RETURN SyntaxTree.invalidExpression
  2323. END;
  2324. END CompatibleConversion;
  2325. (**
  2326. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2327. **)
  2328. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2329. VAR leftType,rightType: SyntaxTree.Type;
  2330. BEGIN
  2331. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2332. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2333. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2334. (* error already handled *)
  2335. ELSE
  2336. leftType := left.type.resolved; rightType := right.type.resolved;
  2337. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2338. right := NewConversion(right.position, right, leftType, NIL);
  2339. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2340. left := NewConversion(left.position,left,rightType,NIL);
  2341. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2342. right := NewConversion(right.position, right, leftType, NIL);
  2343. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2344. left := NewConversion(left.position,left,rightType,NIL);
  2345. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2346. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2347. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2348. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2349. ELSIF
  2350. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2351. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2352. (* must be the case LONGREAL / COMPLEX ) *)
  2353. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2354. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2355. ELSE
  2356. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2357. END;
  2358. END;
  2359. END ConvertOperands;
  2360. (** find and return best operator matching to parameter list (nil, if none)
  2361. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2362. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2363. **)
  2364. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2365. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2366. identifier: SyntaxTree.Identifier;
  2367. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2368. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2369. BEGIN
  2370. operator := scope.firstOperator;
  2371. WHILE(operator # NIL) DO
  2372. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2373. procedureType := operator.type(SyntaxTree.ProcedureType);
  2374. distance := Distance(system, procedureType,actualParameters);
  2375. IF (distance < Infinity) THEN
  2376. IF returnType # NIL THEN
  2377. IF procedureType.returnType = NIL THEN
  2378. distance := Infinity
  2379. ELSE
  2380. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2381. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2382. END;
  2383. END;
  2384. END;
  2385. (*
  2386. IF distance < Infinity THEN
  2387. TRACE(distance, operator);
  2388. Printout.Info("potential operator",operator);
  2389. ELSE
  2390. Printout.Info("operator not possible",operator);
  2391. END;
  2392. *)
  2393. IF distance < bestDistance THEN
  2394. bestDistance := distance;
  2395. bestOperator := operator;
  2396. END;
  2397. END;
  2398. operator := operator.nextOperator;
  2399. END;
  2400. (*
  2401. Printout.Info("taken operator",bestOperator);
  2402. *)
  2403. END FindInScope;
  2404. BEGIN
  2405. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2406. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2407. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2408. import := currentScope.ownerModule.moduleScope.firstImport;
  2409. WHILE (bestDistance > 0) & (import # NIL) DO
  2410. IF import.module # NIL THEN
  2411. identifier := Global.GetIdentifier(operator,import.module.case);
  2412. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2413. END;
  2414. import := import.nextImport;
  2415. END;
  2416. RETURN bestOperator
  2417. END FindOperator;
  2418. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2419. BEGIN
  2420. currentScope := scope;
  2421. END SetCurrentScope;
  2422. (**
  2423. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2424. - handle LEN and DIM operator for array-structured object types
  2425. - find operator, if found then
  2426. - if in other module then add import designator
  2427. - create symbol designator for operator
  2428. - if error then return invalidExpression, if no operator then return NIL
  2429. **)
  2430. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2431. VAR
  2432. operator: SyntaxTree.Operator;
  2433. import: SyntaxTree.Import;
  2434. expression, result: SyntaxTree.Expression;
  2435. designator: SyntaxTree.Designator;
  2436. actualParameters, tempList: SyntaxTree.ExpressionList;
  2437. recordType: SyntaxTree.RecordType;
  2438. castReturnType : SyntaxTree.MathArrayType;
  2439. BEGIN
  2440. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2441. result := SyntaxTree.invalidExpression
  2442. ELSIF leftExpression = NIL THEN
  2443. result := NIL
  2444. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2445. (* LEN or DIM operator on array-structured object type *)
  2446. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2447. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2448. IF recordType.arrayAccessOperators.len = NIL THEN
  2449. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2450. result := SyntaxTree.invalidExpression
  2451. ELSE
  2452. ASSERT(leftExpression IS SyntaxTree.Designator);
  2453. designator := leftExpression(SyntaxTree.Designator);
  2454. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2455. ASSERT(expression IS SyntaxTree.Designator);
  2456. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2457. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2458. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2459. result := designator
  2460. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2461. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2462. tempList := SyntaxTree.NewExpressionList();
  2463. tempList.AddExpression(rightExpression);
  2464. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2465. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2466. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2467. tempList := SyntaxTree.NewExpressionList();
  2468. tempList.AddExpression(designator);
  2469. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2470. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2471. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2472. END
  2473. END;
  2474. ELSE
  2475. (* import OCArrayBase if needed *)
  2476. IF ~arrayBaseImported THEN
  2477. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2478. (* operators on complex numbers *)
  2479. ImportModule(Global.ArrayBaseName,position);
  2480. arrayBaseImported := TRUE;
  2481. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2482. IF op = Global.Dim THEN
  2483. (* not existing in OCArrayBase *)
  2484. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2485. (* not existing in OCArrayBase *)
  2486. ELSE
  2487. ImportModule(Global.ArrayBaseName,position);
  2488. arrayBaseImported := TRUE;
  2489. END
  2490. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2491. ImportModule(Global.ArrayBaseName,position);
  2492. arrayBaseImported := TRUE
  2493. END;
  2494. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2495. (* LEN(RANGE) *)
  2496. ImportModule(Global.ArrayBaseName,position);
  2497. arrayBaseImported := TRUE;
  2498. END;
  2499. END;
  2500. actualParameters := SyntaxTree.NewExpressionList();
  2501. actualParameters.AddExpression(leftExpression);
  2502. IF rightExpression # NIL THEN
  2503. actualParameters.AddExpression(rightExpression)
  2504. END;
  2505. operator := FindOperator(system,op,actualParameters,resultType);
  2506. IF operator # NIL THEN
  2507. designator := NIL;
  2508. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2509. import := currentScope.ownerModule.moduleScope.firstImport;
  2510. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2511. import := import.nextImport;
  2512. END;
  2513. expression := NewSymbolDesignator(position,NIL,import);
  2514. designator := expression(SyntaxTree.Designator);
  2515. END;
  2516. expression := NewSymbolDesignator(position,designator,operator);
  2517. designator := expression(SyntaxTree.Designator);
  2518. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2519. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2520. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2521. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2522. result.SetType(castReturnType);
  2523. END;
  2524. ELSE
  2525. result := NIL;
  2526. END;
  2527. END;
  2528. RETURN result
  2529. END NewOperatorCall;
  2530. (** check and resolve binary expression **)
  2531. (*! clean up *)
  2532. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2533. VAR left,right,result: SyntaxTree.Expression;
  2534. leftType, rightType: SyntaxTree.Type;
  2535. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2536. cl,cr: CHAR;
  2537. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2538. type: SyntaxTree.Type;
  2539. value: SyntaxTree.Value;
  2540. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2541. integerConstantFolding: BOOLEAN;
  2542. PROCEDURE NewBool(v: BOOLEAN);
  2543. BEGIN
  2544. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2545. value.SetType(system.booleanType);
  2546. result.SetResolved(value);
  2547. type := system.booleanType
  2548. END NewBool;
  2549. PROCEDURE NewSet(v: SET);
  2550. BEGIN
  2551. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2552. value.SetType(system.setType);
  2553. result.SetResolved(value);
  2554. type := system.setType;
  2555. END NewSet;
  2556. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2557. BEGIN
  2558. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2559. (* type cast to "larger" type only if the value is still in the range *)
  2560. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2561. value.SetType(t);
  2562. END;
  2563. result.SetResolved(value);
  2564. type := value.type;
  2565. END NewInteger;
  2566. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2567. BEGIN
  2568. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2569. value.SetType(t);
  2570. result.SetResolved(value);
  2571. type := t;
  2572. END NewReal;
  2573. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2574. BEGIN
  2575. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2576. value.SetType(t);
  2577. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2578. result.SetResolved(value);
  2579. type := t;
  2580. END NewComplex;
  2581. BEGIN
  2582. type := SyntaxTree.invalidType;
  2583. left := ResolveExpression(binaryExpression.left);
  2584. right := ResolveExpression(binaryExpression.right);
  2585. binaryExpression.SetLeft(left);
  2586. binaryExpression.SetRight(right);
  2587. result := binaryExpression;
  2588. operator := binaryExpression.operator;
  2589. IF ~system.operatorDefined[operator] THEN
  2590. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2591. result := SyntaxTree.invalidExpression;
  2592. RETURN
  2593. END;
  2594. IF left.type = NIL THEN
  2595. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2596. result := SyntaxTree.invalidExpression;
  2597. RETURN;
  2598. END;
  2599. IF right.type = NIL THEN
  2600. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2601. result := SyntaxTree.invalidExpression;
  2602. RETURN;
  2603. END;
  2604. leftType := left.type.resolved; rightType := right.type.resolved;
  2605. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2606. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2607. END;
  2608. IF operatorCall # NIL THEN
  2609. result := operatorCall;
  2610. type := operatorCall.type;
  2611. (* admissible operators:
  2612. Times, Plus, Minus numeric numeric numeric
  2613. set set set
  2614. Slash numeric numeric real /complex
  2615. set set set
  2616. Div , Mod integer integer integer
  2617. And, Or bool bool bool
  2618. Equal, Unequal basic basic bool
  2619. pointer pointer bool
  2620. object object bool
  2621. record record bool
  2622. string string bool
  2623. enumerator enumerator bool
  2624. Less, LessEqual,
  2625. Greater, GreaterEqual integer/real integer/real bool
  2626. enumerator enumerator bool
  2627. In integer set bool
  2628. Is pointer type bool
  2629. object type bool
  2630. record type bool
  2631. Upto: special abbreviation for a..b
  2632. *)
  2633. ELSIF (left.type = NIL) THEN
  2634. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2635. D.Str("nil type in "); D.Type(left); D.Ln;
  2636. result := SyntaxTree.invalidExpression;
  2637. ELSIF (right.type = NIL) THEN
  2638. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2639. result := SyntaxTree.invalidExpression;
  2640. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2641. result := SyntaxTree.invalidExpression;
  2642. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2643. HALT(100);
  2644. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2645. type := system.booleanType;
  2646. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2647. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2648. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2649. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2650. IF VerboseErrorMessage THEN
  2651. Printout.Info("left",left);
  2652. Printout.Info("right",right);
  2653. END;
  2654. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2655. NewBool(TRUE)
  2656. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2657. NewBool(TRUE);
  2658. ELSIF IsUnextensibleRecord(left) THEN
  2659. NewBool(FALSE)
  2660. END
  2661. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2662. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2663. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2664. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2665. ELSIF operator = Scanner.In THEN (* left IN right *)
  2666. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2667. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2668. NewBool(il IN sr);
  2669. ELSE
  2670. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2671. left := NewConversion(left.position, left, system.longintType,NIL);
  2672. binaryExpression.SetLeft(left)
  2673. END;
  2674. type := system.booleanType;
  2675. END
  2676. ELSE
  2677. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2678. END
  2679. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2680. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2681. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2682. END;
  2683. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2684. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2685. END
  2686. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2687. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2688. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2689. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2690. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2691. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2692. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2693. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2694. END;
  2695. type := system.booleanType;
  2696. ELSE
  2697. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2698. END
  2699. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2700. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2701. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2702. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2703. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2704. CASE operator OF
  2705. |Scanner.Equal: NewBool(strl^=strr^);
  2706. |Scanner.Unequal:NewBool(strl^#strr^);
  2707. |Scanner.Less: NewBool(strl^<strr^);
  2708. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2709. |Scanner.Greater: NewBool(strl^>strr^);
  2710. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2711. ELSE
  2712. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2713. END;
  2714. END;
  2715. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2716. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2717. type := system.booleanType
  2718. ELSE
  2719. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2720. END;
  2721. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2722. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2723. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2724. type := system.booleanType;
  2725. ELSE
  2726. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2727. END
  2728. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2729. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2730. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2731. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2732. type := system.booleanType
  2733. ELSE
  2734. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2735. END;
  2736. ELSE
  2737. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2738. END;
  2739. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2740. type := system.booleanType;
  2741. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2742. type := system.booleanType;
  2743. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2744. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2745. THEN
  2746. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2747. IF (leftType # rightType) THEN
  2748. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2749. ConvertOperands(left,right); (* operands must be of the same type here *)
  2750. END;
  2751. binaryExpression.SetLeft(left);
  2752. binaryExpression.SetRight(right);
  2753. leftType := left.type.resolved;
  2754. rightType := right.type.resolved;
  2755. END;
  2756. type := leftType;
  2757. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2758. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2759. IF VerboseErrorMessage THEN
  2760. Printout.Info("left",left);
  2761. Printout.Info("right",right);
  2762. END;
  2763. ELSIF IsIntegerType(leftType) THEN
  2764. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2765. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2766. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2767. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2768. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2769. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2770. END;
  2771. END;
  2772. (* constant folding *)
  2773. (* bootstrap64
  2774. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2775. CASE operator OF
  2776. |Scanner.Plus: NewInteger(il+ir,left.type);
  2777. |Scanner.Minus: NewInteger(il-ir,left.type);
  2778. |Scanner.Times: NewInteger(il*ir,left.type);
  2779. |Scanner.Slash:
  2780. IF ir # 0 THEN
  2781. NewReal(il/ir, system.realType);
  2782. END;
  2783. |Scanner.Mod:
  2784. IF ir > 0 THEN
  2785. NewInteger(il MOD ir,left.type);
  2786. END;
  2787. |Scanner.Div:
  2788. IF ir > 0 THEN
  2789. NewInteger(il DIV ir,left.type);
  2790. END;
  2791. |Scanner.Equal: NewBool(il=ir);
  2792. |Scanner.Unequal:NewBool(il#ir);
  2793. |Scanner.Less: NewBool(il<ir);
  2794. |Scanner.LessEqual: NewBool(il<=ir);
  2795. |Scanner.Greater: NewBool(il>ir);
  2796. |Scanner.GreaterEqual: NewBool(il>=ir);
  2797. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2798. END;
  2799. ELS*)
  2800. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2801. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2802. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2803. CASE operator OF
  2804. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2805. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2806. |Scanner.Times: NewInteger(hl*hr,left.type);
  2807. |Scanner.Slash:
  2808. IF hr = 0 THEN
  2809. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2810. ELSE
  2811. IF type.sizeInBits = 64 THEN
  2812. NewReal(hl/hr,system.longrealType);
  2813. ELSE
  2814. NewReal(hl/hr,system.realType)
  2815. END
  2816. END;
  2817. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2818. |Scanner.Mod:
  2819. IF hr = 0 THEN
  2820. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2821. ELSE
  2822. NewInteger(hl MOD hr, left.type);
  2823. (* bootstrap64
  2824. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2825. *)
  2826. END;
  2827. |Scanner.Div:
  2828. IF hr = 0 THEN
  2829. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2830. ELSE
  2831. NewInteger(hl DIV hr, left.type);
  2832. (* bootstrap64
  2833. NewInteger(Machine.DivH(hl,hr),left.type);
  2834. *)
  2835. END;
  2836. (* *)
  2837. |Scanner.Equal: NewBool(hl=hr);
  2838. |Scanner.Unequal: NewBool(hl#hr);
  2839. |Scanner.Less: NewBool(hl<hr);
  2840. |Scanner.LessEqual: NewBool(hl<=hr);
  2841. |Scanner.Greater: NewBool(hl>hr);
  2842. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2843. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2844. END;
  2845. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2846. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2847. type := left.type
  2848. ELSIF (operator = Scanner.Slash) THEN
  2849. left := NewConversion(left.position,left,system.realType,NIL);
  2850. right := NewConversion(right.position,right,system.realType,NIL);
  2851. binaryExpression.SetLeft(left);
  2852. binaryExpression.SetRight(right);
  2853. type := system.realType
  2854. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2855. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2856. type := system.booleanType
  2857. ELSE
  2858. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2859. END;
  2860. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2861. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2862. CASE operator OF
  2863. |Scanner.Plus: NewReal(rl+rr,leftType);
  2864. |Scanner.Minus: NewReal(rl-rr,leftType);
  2865. |Scanner.Times:NewReal(rl*rr,leftType);
  2866. |Scanner.Slash:
  2867. IF rr = 0 THEN
  2868. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2869. ELSE
  2870. NewReal(rl/rr,leftType);
  2871. END
  2872. |Scanner.Equal: NewBool(rl=rr);
  2873. |Scanner.Unequal: NewBool(rl#rr);
  2874. |Scanner.Less: NewBool(rl<rr);
  2875. |Scanner.LessEqual: NewBool(rl<=rr);
  2876. |Scanner.Greater: NewBool(rl>rr);
  2877. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2878. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2879. END;
  2880. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2881. type := left.type
  2882. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2883. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2884. type := system.booleanType
  2885. ELSE
  2886. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2887. IF VerboseErrorMessage THEN
  2888. Printout.Info("left",left);
  2889. Printout.Info("right",right);
  2890. END;
  2891. END;
  2892. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2893. CASE operator OF
  2894. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2895. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2896. ELSE
  2897. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2898. IF VerboseErrorMessage THEN
  2899. Printout.Info("left", left);
  2900. Printout.Info("right", right)
  2901. END;
  2902. END;
  2903. IF ~error THEN
  2904. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2905. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2906. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2907. (* do constant folding *)
  2908. CASE operator OF
  2909. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2910. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2911. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2912. |Scanner.Slash:
  2913. divisor := c * c + d * d;
  2914. ASSERT(divisor # 0);
  2915. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2916. |Scanner.Equal: NewBool((a = c) & (b = d))
  2917. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2918. END
  2919. END
  2920. END
  2921. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2922. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2923. CASE operator OF
  2924. |Scanner.And: NewBool(bl & br);
  2925. |Scanner.Or: NewBool(bl OR br);
  2926. |Scanner.Equal: NewBool(bl = br);
  2927. |Scanner.Unequal: NewBool(bl # br);
  2928. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  2929. END;
  2930. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2931. type := system.booleanType
  2932. ELSE
  2933. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  2934. END;
  2935. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2936. (* constant folding *)
  2937. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2938. IF operator = Scanner.Equal THEN
  2939. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2940. ELSIF operator = Scanner.Unequal THEN
  2941. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2942. END;
  2943. END;
  2944. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2945. type := system.booleanType;
  2946. ELSE
  2947. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  2948. END;
  2949. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2950. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2951. CASE operator OF
  2952. |Scanner.Plus: NewSet(sl + sr);
  2953. |Scanner.Minus: NewSet(sl - sr);
  2954. |Scanner.Times: NewSet(sl * sr);
  2955. |Scanner.Slash: NewSet(sl / sr);
  2956. |Scanner.Equal: NewBool(sl=sr);
  2957. |Scanner.Unequal: NewBool(sl#sr);
  2958. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  2959. |Scanner.LessEqual: NewBool(sl*sr = sl);
  2960. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  2961. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  2962. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  2963. END;
  2964. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  2965. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2966. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  2967. THEN
  2968. type := system.booleanType
  2969. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2970. type := left.type
  2971. ELSE
  2972. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  2973. END;
  2974. ELSIF IsCharacterType(left.type) THEN
  2975. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  2976. CASE operator OF
  2977. |Scanner.Equal: NewBool(cl=cr);
  2978. |Scanner.Unequal: NewBool(cl#cr);
  2979. |Scanner.Less: NewBool(cl<cr);
  2980. |Scanner.LessEqual: NewBool(cl<=cr);
  2981. |Scanner.Greater: NewBool(cl>cr);
  2982. |Scanner.GreaterEqual: NewBool(cl>=cr);
  2983. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  2984. END;
  2985. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2986. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2987. type := system.booleanType
  2988. ELSE
  2989. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  2990. END;
  2991. ELSE
  2992. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  2993. END;
  2994. ELSE
  2995. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  2996. END;
  2997. IF type = SyntaxTree.invalidType THEN
  2998. result := SyntaxTree.invalidExpression
  2999. ELSE
  3000. result.SetType(type)
  3001. END;
  3002. resolvedExpression := result
  3003. END VisitBinaryExpression;
  3004. (** resolve a range expression of the from <<first .. last BY step>>
  3005. - depending on the context different things are checked:
  3006. ArrayIndex:
  3007. - components must be integers
  3008. - replace missing lower bound with 0
  3009. - replace missing upper bound with MAX(LONGINT)
  3010. - replace missing step size with 1
  3011. SetElement:
  3012. - components must be integers
  3013. - replace missing lower bound with 0
  3014. - replace missing upper bound with MAX(SET)
  3015. - must not have step size
  3016. CaseGuard:
  3017. - components must be constant
  3018. - components must be integers or characters
  3019. - must have lower and upper bound present
  3020. - components are made compatible
  3021. - must not have step size
  3022. - if error: return invalidExpression
  3023. **)
  3024. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3025. VAR
  3026. hasError: BOOLEAN;
  3027. first, last, step: SyntaxTree.Expression;
  3028. BEGIN
  3029. hasError := FALSE;
  3030. first := x.first;
  3031. last := x.last;
  3032. step := x.step;
  3033. (* check lower bound *)
  3034. IF x.context = SyntaxTree.CaseGuard THEN
  3035. IF first = NIL THEN
  3036. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3037. hasError := TRUE
  3038. ELSE
  3039. first := ResolveExpression(first);
  3040. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3041. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3042. hasError := TRUE
  3043. ELSE
  3044. IF first IS SyntaxTree.StringValue THEN
  3045. (* add conversion from string to character *)
  3046. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3047. END
  3048. END;
  3049. (* check if expression is constant *)
  3050. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3051. (* error already reported *)
  3052. hasError := TRUE
  3053. END
  3054. END
  3055. ELSE (* ArrayIndex, SetElement *)
  3056. IF first = NIL THEN
  3057. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3058. END;
  3059. first := ResolveExpression(first);
  3060. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3061. first := NewConversion(first.position, first, system.longintType, NIL)
  3062. ELSE
  3063. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3064. hasError := TRUE
  3065. END
  3066. END;
  3067. (* check upper bound *)
  3068. IF x.context = SyntaxTree.CaseGuard THEN
  3069. IF last = NIL THEN
  3070. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3071. hasError := TRUE
  3072. ELSE
  3073. last := ResolveExpression(last);
  3074. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3075. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3076. hasError := TRUE
  3077. ELSE
  3078. IF last IS SyntaxTree.StringValue THEN
  3079. (* add conversion from string to character *)
  3080. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3081. END
  3082. END;
  3083. (* check if expression is constant *)
  3084. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3085. (* error already reported *)
  3086. hasError := TRUE
  3087. ELSE
  3088. (* try to make lower and upper bound compatible *)
  3089. ConvertOperands(first, last);
  3090. IF first.type.resolved # last.type.resolved THEN
  3091. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3092. hasError := TRUE
  3093. END
  3094. END
  3095. END
  3096. ELSE (* ArrayIndex, SetElement *)
  3097. IF last = NIL THEN
  3098. IF x.context = SyntaxTree.ArrayIndex THEN
  3099. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3100. ELSE
  3101. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3102. END
  3103. END;
  3104. last := ResolveExpression(last);
  3105. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3106. last := NewConversion(last.position, last, system.longintType, NIL)
  3107. ELSE
  3108. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3109. hasError := TRUE
  3110. END
  3111. END;
  3112. (* check step size *)
  3113. IF x.context = SyntaxTree.ArrayIndex THEN
  3114. IF step = NIL THEN
  3115. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3116. END;
  3117. step := ResolveExpression(step);
  3118. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3119. step := NewConversion(step.position, step, system.longintType, NIL)
  3120. ELSE
  3121. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3122. hasError := TRUE
  3123. END
  3124. ELSE (* SetElement, CaseGuard *)
  3125. IF step # NIL THEN
  3126. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3127. hasError := TRUE
  3128. END
  3129. END;
  3130. IF hasError THEN
  3131. resolvedExpression := SyntaxTree.invalidExpression
  3132. ELSE
  3133. x.SetFirst(first);
  3134. x.SetLast(last);
  3135. x.SetStep(step);
  3136. x.SetType(system.rangeType);
  3137. resolvedExpression := x;
  3138. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3139. END
  3140. END VisitRangeExpression;
  3141. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3142. BEGIN
  3143. x.SetType(NIL);
  3144. resolvedExpression := x;
  3145. END VisitTensorRangeExpression;
  3146. (** resolve the expression d and return result as designator
  3147. - resolve expression
  3148. - if expression is a designator then return designator else error message and return invalidDesignator
  3149. **)
  3150. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3151. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3152. BEGIN
  3153. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3154. resolved := ResolveExpression(d);
  3155. IF resolved = SyntaxTree.invalidExpression THEN
  3156. (* error should already have been reported *)
  3157. result := SyntaxTree.invalidDesignator;
  3158. ELSIF resolved IS SyntaxTree.Designator THEN
  3159. result := resolved(SyntaxTree.Designator);
  3160. ELSE
  3161. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3162. result := SyntaxTree.invalidDesignator;
  3163. END;
  3164. (* result.type might be nil. *)
  3165. RETURN result
  3166. END ResolveDesignator;
  3167. (**
  3168. symbol designator generated in this module
  3169. nothing to be resolved
  3170. **)
  3171. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3172. BEGIN
  3173. resolvedExpression := x;
  3174. END VisitSymbolDesignator;
  3175. (**
  3176. self designator generated in this module
  3177. nothing to be resolved
  3178. **)
  3179. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3180. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3181. BEGIN
  3182. (* check if in record scope *)
  3183. scope := currentScope;
  3184. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3185. scope := scope.outerScope;
  3186. END;
  3187. IF scope = NIL THEN (* in module scope *)
  3188. x.SetType(system.anyType);
  3189. ELSIF scope IS SyntaxTree.CellScope THEN
  3190. cell := scope(SyntaxTree.CellScope).ownerCell;
  3191. x.SetType(cell);
  3192. ELSE (* in record scope *)
  3193. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3194. IF (record # NIL) & (record.pointerType # NIL) THEN
  3195. type := ResolveType(record.pointerType);
  3196. x.SetType(type);
  3197. ELSE
  3198. x.SetType(record);
  3199. END;
  3200. END;
  3201. resolvedExpression := x;
  3202. END VisitSelfDesignator;
  3203. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3204. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3205. BEGIN
  3206. scope := currentScope;
  3207. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3208. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3209. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3210. returnType := procedureType.returnType;
  3211. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3212. THEN
  3213. x.SetType(returnType);
  3214. ELSE
  3215. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3216. x.SetType(SyntaxTree.invalidType);
  3217. END;
  3218. ELSE
  3219. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3220. x.SetType(SyntaxTree.invalidType);
  3221. END;
  3222. x.SetAssignable(TRUE);
  3223. resolvedExpression := x;
  3224. END VisitResultDesignator;
  3225. (**
  3226. return symbol designator as an expression
  3227. - if symbol is a constant then return the constant value expression
  3228. - else
  3229. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3230. - if symbol is a guarded variable then return a TypeGuardDesignator
  3231. - else return a symbol designator
  3232. **)
  3233. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3234. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3235. guardType: SyntaxTree.Type;
  3236. BEGIN
  3237. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3238. result := SyntaxTree.invalidExpression;
  3239. ASSERT(symbol # NIL);
  3240. (*
  3241. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3242. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3243. Error(position,Diagnostics.Invalid,"type not allowed here");
  3244. ELS *)
  3245. (* not needed any more as values are stored in the expression
  3246. IF symbol IS SyntaxTree.Constant THEN
  3247. result := symbol(SyntaxTree.Constant).value
  3248. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3249. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3250. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3251. ELSE
  3252. result := symbol(SyntaxTree.Constant).value
  3253. END;
  3254. ELSE
  3255. *)
  3256. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3257. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3258. THEN
  3259. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3260. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3261. left := NewDereferenceDesignator(position,left);
  3262. left.SetHidden(TRUE);
  3263. END;
  3264. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3265. scope := currentScope;
  3266. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3267. scope := scope.outerScope;
  3268. END;
  3269. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3270. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3271. END;
  3272. END;
  3273. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3274. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3275. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3276. ELSE
  3277. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3278. END;
  3279. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3280. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3281. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3282. result.SetType(symbol.type);
  3283. result.SetAssignable(assignable);
  3284. symbol.MarkUsed;
  3285. IF symbol IS SyntaxTree.Constant THEN
  3286. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3287. END;
  3288. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3289. variableAccessed := TRUE
  3290. END;
  3291. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3292. IF GetGuard(symbol,guardType) THEN
  3293. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType);
  3294. END;
  3295. END;
  3296. ASSERT(result.type # NIL);
  3297. RETURN result
  3298. END NewSymbolDesignator;
  3299. (** check and resolve an identifier designator "identifier"
  3300. - if identifier = self then return SelfDesignator
  3301. - else find symbol in current scope
  3302. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3303. **)
  3304. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3305. VAR symbol: SyntaxTree.Symbol;
  3306. BEGIN
  3307. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3308. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3309. IF symbol # NIL THEN
  3310. ResolveSymbol(symbol);
  3311. ASSERT(symbol.type # NIL);
  3312. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3313. ELSE
  3314. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3315. IF VerboseErrorMessage THEN
  3316. Printout.Info("undeclared identifier designator",identifierDesignator);
  3317. END;
  3318. resolvedExpression := SyntaxTree.invalidDesignator;
  3319. END;
  3320. END VisitIdentifierDesignator;
  3321. (** check and resolve a selector designator of the form left.designator
  3322. - if left is a pointer type then do auto dereferenciation
  3323. - left denotes a search scope:
  3324. - if left type is import type then set search scope to respective module
  3325. - if left type is enumeration type then set search scope to respective enumeration scope
  3326. - elsif left type is record type then set search scope to record scope
  3327. - search symbol in computed scope
  3328. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3329. **)
  3330. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3331. VAR
  3332. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3333. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3334. BEGIN
  3335. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3336. left := ResolveDesignator(selectorDesignator.left);
  3337. result := SyntaxTree.invalidDesignator;
  3338. IF left # NIL THEN
  3339. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3340. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3341. END;
  3342. scope := NIL;
  3343. IF left.type = NIL THEN
  3344. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3345. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3346. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3347. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3348. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3349. module := symbol(SyntaxTree.Import).module;
  3350. IF module # NIL THEN
  3351. scope := module.moduleScope
  3352. ELSE
  3353. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3354. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3355. END;
  3356. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3357. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3358. ASSERT(scope # NIL)
  3359. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3360. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3361. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3362. IF type IS SyntaxTree.EnumerationType THEN
  3363. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3364. ELSE
  3365. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3366. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3367. END;
  3368. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3369. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3370. ELSE
  3371. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3372. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3373. END;
  3374. symbol := NIL;
  3375. IF scope # NIL THEN
  3376. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3377. IF symbol # NIL THEN
  3378. ResolveSymbol(symbol);
  3379. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3380. symbol.MarkUsed
  3381. ELSE
  3382. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3383. IF VerboseErrorMessage THEN
  3384. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3385. Printout.Info("scope", scope);
  3386. Printout.Info("left", left);
  3387. Printout.Info("undeclared identifier",selectorDesignator);
  3388. Printout.Info("left resolved designator",left);
  3389. END
  3390. END;
  3391. END;
  3392. END;
  3393. resolvedExpression := result;
  3394. END VisitSelectorDesignator;
  3395. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3396. VAR len,idx: LONGINT;
  3397. BEGIN
  3398. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3399. IF idx < 0 THEN
  3400. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3401. ELSE
  3402. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3403. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3404. END;
  3405. END;
  3406. END;
  3407. END IndexCheck;
  3408. (*
  3409. - if index designator has not type, use newBaseType as its type
  3410. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3411. - special rule: if static array of dynamic array occurs, make it all dynamic
  3412. index designator type: new base type: new index designator type:
  3413. NIL z z
  3414. ARRAY [x, y] z ARRAY [x, y] OF z
  3415. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3416. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3417. *)
  3418. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3419. VAR
  3420. mathArrayType: SyntaxTree.MathArrayType;
  3421. makeDynamic: BOOLEAN;
  3422. BEGIN
  3423. IF indexDesignator.type = NIL THEN
  3424. indexDesignator.SetType(newBaseType)
  3425. ELSE
  3426. (* index designator must be a of math array type *)
  3427. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3428. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3429. (* determine if all arrays have to be made dynamic *)
  3430. makeDynamic :=
  3431. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3432. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3433. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3434. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3435. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3436. END;
  3437. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3438. mathArrayType.SetArrayBase(newBaseType)
  3439. END
  3440. END SetIndexBaseType;
  3441. (** check and append index list element to index designator of math array
  3442. - check validity of single index or array range
  3443. - compute new type
  3444. - if range then create new array type (calculate length of resulting array)
  3445. - otherwise take sourceArray.arrayBase as new type
  3446. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3447. **)
  3448. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3449. VAR
  3450. targetArray: SyntaxTree.MathArrayType;
  3451. first, last, step: SyntaxTree.Expression;
  3452. firstValue, lastValue, stepValue, length: LONGINT;
  3453. rangeExpression: SyntaxTree.RangeExpression;
  3454. BEGIN
  3455. IF indexListItem.type = SyntaxTree.invalidType THEN
  3456. (* error already handled *)
  3457. indexDesignator.parameters.AddExpression(indexListItem)
  3458. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3459. indexDesignator.HasRange;
  3460. indexDesignator.HasTensorRange;
  3461. indexDesignator.parameters.AddExpression(indexListItem);
  3462. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3463. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3464. IndexCheck(indexListItem, sourceArray.length);
  3465. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3466. indexDesignator.parameters.AddExpression(indexListItem)
  3467. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3468. indexDesignator.HasRange;
  3469. (* if the range is given as an array range expression, check the validity of its components *)
  3470. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3471. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3472. first := rangeExpression.first;
  3473. last := rangeExpression.last;
  3474. step := rangeExpression.step;
  3475. (* perform static checks on range components *)
  3476. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3477. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3478. END;
  3479. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3480. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3481. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3482. END
  3483. END;
  3484. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3485. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3486. END;
  3487. (* add conversions to size type *)
  3488. (* TODO: needed? *)
  3489. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3490. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3491. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3492. END;
  3493. IF indexDesignator.hasTensorRange THEN
  3494. (* the index designator's base type is a tensor: leave it as is *)
  3495. ELSE
  3496. (* append a new math array to the index designator's base type *)
  3497. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3498. IF ~error THEN
  3499. (*
  3500. (* optimization: calculate length of target array for static ranges *)
  3501. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3502. IF IsStaticallyOpenRange(rangeExpression) THEN
  3503. (* range is open ('*'): reuse source array length as target array length *)
  3504. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3505. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3506. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3507. IF lastValue = MAX(LONGINT) THEN
  3508. IF IsIntegerValue(sourceArray.length, length) THEN
  3509. lastValue := length - 1;
  3510. isStaticTargetArrayLength := TRUE
  3511. ELSE
  3512. isStaticTargetArrayLength := FALSE
  3513. END
  3514. ELSE
  3515. isStaticTargetArrayLength := TRUE
  3516. END;
  3517. IF isStaticTargetArrayLength THEN
  3518. (* calculate static target array length *)
  3519. IF firstValue > lastValue THEN
  3520. length := 0
  3521. ELSE
  3522. length := 1 + lastValue - firstValue;
  3523. IF length MOD stepValue = 0 THEN
  3524. length := length DIV stepValue
  3525. ELSE
  3526. length := length DIV stepValue + 1
  3527. END
  3528. END;
  3529. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3530. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3531. ASSERT(targetArray.form = SyntaxTree.Static)
  3532. END
  3533. END
  3534. END
  3535. *)
  3536. END;
  3537. SetIndexBaseType(indexDesignator, targetArray)
  3538. END;
  3539. indexDesignator.parameters.AddExpression(indexListItem)
  3540. ELSE
  3541. Error(position, Diagnostics.Invalid,"invalid index list item");
  3542. END;
  3543. END AppendMathIndex;
  3544. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3545. VAR parameters: SyntaxTree.ExpressionList;
  3546. BEGIN
  3547. parameters := index.parameters;
  3548. IF (expression.type = NIL) THEN
  3549. Error(position,Diagnostics.Invalid,"invalid index");
  3550. ELSIF IsIntegerType(expression.type.resolved) THEN
  3551. IF over IS SyntaxTree.ArrayType THEN
  3552. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3553. ELSIF over IS SyntaxTree.StringType THEN
  3554. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3555. END;
  3556. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3557. parameters.AddExpression(expression);
  3558. ELSE
  3559. Error(position,Diagnostics.Invalid,"invalid index");
  3560. END;
  3561. END AppendIndex;
  3562. (** convert an expression to math array type
  3563. if expression is of math array type: return expression itself
  3564. if expression is of array-structured object type: return an index operator call on it
  3565. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3566. otherwise: return invalid expression
  3567. **)
  3568. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3569. VAR
  3570. result: SyntaxTree.Expression;
  3571. mathArrayType: SyntaxTree.MathArrayType;
  3572. BEGIN
  3573. IF expression.type = NIL THEN
  3574. result := SyntaxTree.invalidExpression
  3575. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3576. (* expression of math array type *)
  3577. result := expression
  3578. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3579. (* expression of array-structured object type *)
  3580. mathArrayType := MathArrayStructureOfType(expression.type);
  3581. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3582. ELSE
  3583. result := SyntaxTree.invalidExpression
  3584. END;
  3585. RETURN result
  3586. END ConvertToMathArray;
  3587. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3588. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3589. VAR
  3590. result: SyntaxTree.ExpressionList;
  3591. i: LONGINT;
  3592. BEGIN
  3593. result := SyntaxTree.NewExpressionList();
  3594. FOR i := 1 TO itemCount DO
  3595. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3596. END;
  3597. RETURN result
  3598. END ListOfOpenRanges;
  3599. (** create a procedure call designator for an index operator call on an array-structured object type
  3600. - use given index list as actual parameters
  3601. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3602. **)
  3603. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3604. VAR
  3605. operator: SyntaxTree.Operator;
  3606. expression: SyntaxTree.Expression;
  3607. actualParameters, tempList: SyntaxTree.ExpressionList;
  3608. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3609. result, tempDesignator: SyntaxTree.Designator;
  3610. recordType: SyntaxTree.RecordType;
  3611. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3612. i, hashValue, indexListSize, indexListKind: LONGINT;
  3613. castReturnType: SyntaxTree.MathArrayType;
  3614. BEGIN
  3615. ASSERT(IsArrayStructuredObjectType(left.type));
  3616. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3617. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3618. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3619. indexListSize := indexList.Length();
  3620. indexListKind := 0;
  3621. containsNonRange := FALSE;
  3622. FOR i := 0 TO indexList.Length() - 1 DO
  3623. indexListKind := indexListKind * 2;
  3624. expression := indexList.GetExpression(i);
  3625. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3626. INC(indexListKind)
  3627. ELSE
  3628. containsNonRange := TRUE
  3629. END
  3630. END;
  3631. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3632. (* select applicable index operator
  3633. - try to look up optimal index operator
  3634. - if not present, use operator on ranges
  3635. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3636. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3637. *)
  3638. usesGeneralOperator := FALSE;
  3639. IF rhs # NIL THEN
  3640. (* write operator *)
  3641. IF hashValue = -1 THEN
  3642. operator := NIL
  3643. ELSE
  3644. operator := recordType.arrayAccessOperators.write[hashValue];
  3645. END;
  3646. IF operator = NIL THEN
  3647. usesPureRangeOperator := TRUE;
  3648. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3649. operator := recordType.arrayAccessOperators.generalWrite;
  3650. usesGeneralOperator := TRUE
  3651. ELSE
  3652. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3653. operator := recordType.arrayAccessOperators.write[hashValue];
  3654. END
  3655. END
  3656. ELSE
  3657. (* read operator *)
  3658. IF hashValue = -1 THEN
  3659. operator := NIL
  3660. ELSE
  3661. operator := recordType.arrayAccessOperators.read[hashValue];
  3662. END;
  3663. IF operator = NIL THEN
  3664. usesPureRangeOperator := TRUE;
  3665. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3666. operator := recordType.arrayAccessOperators.generalRead;
  3667. usesGeneralOperator := TRUE
  3668. ELSE
  3669. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3670. operator := recordType.arrayAccessOperators.read[hashValue];
  3671. END
  3672. END
  3673. END;
  3674. IF operator = NIL THEN
  3675. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3676. result := SyntaxTree.invalidDesignator;
  3677. ELSE
  3678. (* determine if reshaping is needed *)
  3679. needsReshaping := containsNonRange & usesPureRangeOperator;
  3680. (* import OCArrayBase if reshaping is needed *)
  3681. IF needsReshaping & ~arrayBaseImported THEN
  3682. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3683. arrayBaseImported := TRUE
  3684. END;
  3685. (* add the index list item to the list of actual parameters
  3686. - for general operators: add a single inline array containing the index list items as parameter
  3687. - otherwise: add all index list items as individual parameters
  3688. *)
  3689. actualParameters := SyntaxTree.NewExpressionList();
  3690. IF usesGeneralOperator THEN
  3691. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3692. END;
  3693. FOR i := 0 TO indexListSize - 1 DO
  3694. expression := indexList.GetExpression(i);
  3695. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3696. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3697. tempList := SyntaxTree.NewExpressionList();
  3698. tempList.AddExpression(expression);
  3699. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3700. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3701. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3702. END;
  3703. IF usesGeneralOperator THEN
  3704. tempMathArrayExpression.elements.AddExpression(expression);
  3705. ELSE
  3706. actualParameters.AddExpression(expression)
  3707. END
  3708. END;
  3709. IF usesGeneralOperator THEN
  3710. actualParameters.AddExpression(tempMathArrayExpression)
  3711. END;
  3712. IF rhs # NIL THEN
  3713. (* add actual parameter for RHS *)
  3714. IF needsReshaping THEN
  3715. (* reshape using OCArrayBase.ExpandDimensions *)
  3716. tempList := SyntaxTree.NewExpressionList();
  3717. (* source array *)
  3718. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3719. tempList.AddExpression(rhs);
  3720. ELSE
  3721. (* convert scalar to one-dimensional array *)
  3722. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3723. tempMathArrayExpression.elements.AddExpression(rhs);
  3724. tempList.AddExpression(tempMathArrayExpression)
  3725. END;
  3726. (* list of kept dimensions *)
  3727. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3728. FOR i := 0 TO indexListSize - 1 DO
  3729. expression := indexList.GetExpression(i);
  3730. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3731. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3732. ELSE
  3733. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3734. END
  3735. END;
  3736. tempList.AddExpression(tempMathArrayExpression);
  3737. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3738. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3739. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3740. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3741. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3742. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3743. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3744. expression.SetType(castReturnType);
  3745. ELSE
  3746. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3747. END;
  3748. actualParameters.AddExpression(expression)
  3749. ELSE
  3750. actualParameters.AddExpression(rhs)
  3751. END
  3752. END;
  3753. (* add dereference operator and create procedure call designator *)
  3754. ASSERT(left IS SyntaxTree.Designator);
  3755. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3756. ASSERT(expression IS SyntaxTree.Designator);
  3757. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3758. IF (rhs = NIL) & needsReshaping THEN
  3759. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3760. tempList := SyntaxTree.NewExpressionList();
  3761. FOR i := 0 TO indexList.Length() - 1 DO
  3762. expression := indexList.GetExpression(i);
  3763. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3764. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3765. ELSE
  3766. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3767. END
  3768. END;
  3769. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3770. END;
  3771. IF rhs = NIL THEN
  3772. (* special rule: index read operator calls are considered to be assignable *)
  3773. result.SetAssignable(TRUE)
  3774. END;
  3775. (* put information about this index operator call into the resulting designator *)
  3776. result.SetRelatedAsot(left);
  3777. result.SetRelatedIndexList(indexList)
  3778. END;
  3779. RETURN result
  3780. END NewIndexOperatorCall;
  3781. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3782. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3783. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3784. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3785. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3786. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3787. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3788. CONST trace = FALSE;
  3789. BEGIN
  3790. IF trace THEN
  3791. FOR i := 0 TO actualParameters.Length()-1 DO
  3792. Printout.Info("par", actualParameters.GetExpression(i));
  3793. END;
  3794. END;
  3795. operator := scope.firstOperator;
  3796. WHILE(operator # NIL) DO
  3797. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3798. procedureType := operator.type(SyntaxTree.ProcedureType);
  3799. distance := Distance(system, procedureType,actualParameters);
  3800. IF trace THEN Printout.Info("check op ",operator) END;
  3801. IF distance < bestDistance THEN
  3802. IF trace THEN Printout.Info("taken op",operator) END;
  3803. bestDistance := distance;
  3804. bestOperator := operator;
  3805. END;
  3806. END;
  3807. operator := operator.nextOperator;
  3808. END;
  3809. END FindInScope;
  3810. BEGIN
  3811. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3812. identifier := SyntaxTree.NewIdentifier("[]");
  3813. WHILE (recordType # NIL) DO
  3814. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3815. recordType := recordType.GetBaseRecord();
  3816. END;
  3817. RETURN bestOperator
  3818. END FindOperator;
  3819. BEGIN
  3820. type := left.type.resolved;
  3821. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3822. actualParameters := SyntaxTree.NewExpressionList();
  3823. FOR i := 0 TO parameters.Length()-1 DO
  3824. expression := ResolveExpression(parameters.GetExpression(i));
  3825. actualParameters.AddExpression(expression);
  3826. END;
  3827. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3828. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3829. IF op # NIL THEN
  3830. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3831. ASSERT(expression IS SyntaxTree.Designator);
  3832. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3833. result.SetRelatedAsot(left);
  3834. result.SetRelatedIndexList(parameters);
  3835. (* check if write operator exists, for var parameters *)
  3836. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3837. actualParameters := SyntaxTree.NewExpressionList();
  3838. FOR i := 0 TO parameters.Length()-1 DO
  3839. expression := ResolveExpression(parameters.GetExpression(i));
  3840. actualParameters.AddExpression(expression);
  3841. END;
  3842. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3843. actualParameters.AddExpression(rhs);
  3844. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3845. IF op = NIL THEN rhs := NIL END;
  3846. END;
  3847. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3848. ELSE
  3849. Error(position,Diagnostics.Invalid,"undefined operator");
  3850. result := SyntaxTree.invalidDesignator
  3851. END;
  3852. RETURN result;
  3853. END NewObjectOperatorCall;
  3854. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3855. 1. convert bracket designator chains into a single one that contains separators
  3856. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3857. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3858. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3859. - if an array or math array is indexed over, create index designator
  3860. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3861. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3862. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3863. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3864. - if an array-structured object type is indexed over, create procedure call designator
  3865. e.g.: a[x, y] -> a^."[]"(x, y)
  3866. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3867. - a[i, *] = a[i][*]
  3868. - a[*, i] # a[*][i]
  3869. Because:
  3870. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3871. - 'i-th column' = a[*, i]
  3872. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3873. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3874. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3875. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3876. **)
  3877. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3878. VAR
  3879. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3880. indexDesignator: SyntaxTree.IndexDesignator;
  3881. designator: SyntaxTree.Designator;
  3882. type: SyntaxTree.Type;
  3883. recordType: SyntaxTree.RecordType;
  3884. expression, rhs: SyntaxTree.Expression;
  3885. indexList: SyntaxTree.ExpressionList;
  3886. i: LONGINT;
  3887. hasError, done: BOOLEAN;
  3888. PROCEDURE FinalizeIndexDesignator;
  3889. BEGIN
  3890. IF indexDesignator # NIL THEN
  3891. (* the end of a tensor has been reached: *)
  3892. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3893. SetIndexBaseType(indexDesignator, type);
  3894. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3895. designator := indexDesignator;
  3896. type := designator.type.resolved;
  3897. indexDesignator := NIL;
  3898. ASSERT(SyntaxTree.Resolved IN type.state)
  3899. END
  3900. END FinalizeIndexDesignator;
  3901. BEGIN
  3902. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3903. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3904. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3905. (* copy all index list entries including a separator to the left bracket designator *)
  3906. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3907. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3908. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3909. END;
  3910. (* propagate the related RHS *)
  3911. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  3912. (* only resolve left bracket designator and use as final result *)
  3913. resolvedExpression := ResolveExpression(leftBracketDesignator)
  3914. ELSE
  3915. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3916. designator := ResolveDesignator(bracketDesignator.left);
  3917. type := designator.type.resolved;
  3918. indexDesignator := NIL;
  3919. (*!!! clean up *)
  3920. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  3921. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3922. RETURN
  3923. END;
  3924. i := 0;
  3925. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3926. expression := bracketDesignator.parameters.GetExpression(i);
  3927. expression := ResolveExpression(expression);
  3928. bracketDesignator.parameters.SetExpression(i, expression);
  3929. IF expression = SyntaxTree.indexListSeparator THEN
  3930. (* finalize an existing index designator if needed *)
  3931. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3932. INC(i)
  3933. ELSE
  3934. (* do auto-dereferencing if needed *)
  3935. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  3936. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  3937. & (i=0)*)
  3938. THEN
  3939. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3940. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3941. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  3942. designator := SyntaxTree.invalidDesignator;
  3943. type := SyntaxTree.invalidType
  3944. ELSE
  3945. FinalizeIndexDesignator;
  3946. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  3947. type := designator.type.resolved
  3948. END
  3949. END;
  3950. (* create a new index designator, if needed *)
  3951. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  3952. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  3953. indexDesignator.SetAssignable(designator.assignable);
  3954. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  3955. (* designator := indexDesignator *)
  3956. END;
  3957. IF type = SyntaxTree.invalidType THEN
  3958. (* error already handled *)
  3959. INC(i)
  3960. ELSIF type IS SyntaxTree.ArrayType THEN
  3961. (* indexing over an array *)
  3962. ASSERT(indexDesignator # NIL);
  3963. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  3964. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3965. INC(i)
  3966. ELSIF type IS SyntaxTree.StringType THEN
  3967. (* indexing over an array *)
  3968. ASSERT(indexDesignator # NIL);
  3969. AppendIndex(expression.position, indexDesignator, expression, type);
  3970. type := type(SyntaxTree.StringType).baseType.resolved;
  3971. INC(i)
  3972. ELSIF type IS SyntaxTree.MathArrayType THEN
  3973. (* indexing over a math array *)
  3974. ASSERT(indexDesignator # NIL);
  3975. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  3976. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3977. INC(i)
  3978. ELSIF IsArrayStructuredObjectType(type) THEN
  3979. (* indexing over ASOTs *)
  3980. FinalizeIndexDesignator;
  3981. ASSERT(type IS SyntaxTree.PointerType);
  3982. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3983. (*
  3984. - collect index list items from bracket designator that belong to ASOT
  3985. - check for errors
  3986. *)
  3987. indexList := SyntaxTree.NewExpressionList();
  3988. hasError := FALSE;
  3989. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3990. (* indexing over tensor ASOT:
  3991. - stop at index list end or separator
  3992. - dimensionality is given by number of index list items
  3993. *)
  3994. done := FALSE;
  3995. WHILE ~done DO
  3996. IF i > bracketDesignator.parameters.Length() - 1 THEN
  3997. done := TRUE;
  3998. ELSE
  3999. expression := bracketDesignator.parameters.GetExpression(i);
  4000. IF expression = SyntaxTree.indexListSeparator THEN
  4001. done := TRUE;
  4002. ELSE
  4003. expression := ResolveExpression(expression);
  4004. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4005. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4006. hasError := TRUE
  4007. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4008. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4009. expression := SyntaxTree.invalidExpression;
  4010. hasError := TRUE
  4011. END;
  4012. indexList.AddExpression(expression)
  4013. END;
  4014. INC(i)
  4015. END
  4016. END
  4017. ELSE
  4018. (* indexing over non-tensor ASOT:
  4019. - ignore separators
  4020. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4021. *)
  4022. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4023. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4024. expression := bracketDesignator.parameters.GetExpression(i);
  4025. ELSE
  4026. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4027. END;
  4028. IF expression # SyntaxTree.indexListSeparator THEN
  4029. expression := ResolveExpression(expression);
  4030. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4031. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4032. expression := SyntaxTree.invalidExpression;
  4033. hasError := TRUE
  4034. END;
  4035. indexList.AddExpression(expression)
  4036. END;
  4037. INC(i)
  4038. END;
  4039. END;
  4040. IF hasError THEN
  4041. designator := SyntaxTree.invalidDesignator;
  4042. type := SyntaxTree.invalidType;
  4043. ELSE
  4044. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4045. and the last entry in the index list belongs to the array-structured object type in question.
  4046. E.g.: for a 2-dimensional array-structured object type:
  4047. - 'lhs := asot[1, 2]' -> read mode
  4048. - 'asot[1, 2] := rhs' -> write mode
  4049. - 'asot[1, 2, 3] := rhs' -> read mode
  4050. *)
  4051. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4052. rhs := bracketDesignator.relatedRhs
  4053. ELSE
  4054. rhs := NIL
  4055. END;
  4056. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4057. type := designator.type
  4058. END
  4059. ELSE
  4060. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4061. designator := SyntaxTree.invalidDesignator;
  4062. type := SyntaxTree.invalidType;
  4063. INC(i)
  4064. END
  4065. END
  4066. END;
  4067. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4068. resolvedExpression := designator
  4069. END
  4070. END VisitBracketDesignator;
  4071. (** check and resolve expression list
  4072. - resolve each expression in an expression list
  4073. - returns true if and only if all statements could have successfully been resolved
  4074. **)
  4075. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4076. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4077. BEGIN
  4078. result := TRUE;
  4079. FOR i := 0 TO expressionList.Length()-1 DO
  4080. expression := ResolveExpression(expressionList.GetExpression(i));
  4081. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4082. expressionList.SetExpression(i,expression);
  4083. END;
  4084. RETURN result
  4085. END ExpressionList;
  4086. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4087. BEGIN
  4088. type := type.resolved;
  4089. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4090. RETURN TRUE
  4091. ELSIF system.CanPassInRegister # NIL THEN
  4092. RETURN system.CanPassInRegister(type);
  4093. ELSE
  4094. RETURN FALSE
  4095. END;
  4096. END CanPassInRegister;
  4097. (** return procedure call designator left(actualParameters)
  4098. - check realtime procedure call in realtime procedure
  4099. - check number of parameters
  4100. - check parameter compatibility
  4101. return invalidDesignator if error
  4102. **)
  4103. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4104. VAR result: SyntaxTree.Designator;
  4105. numberFormalParameters, numberActualParameters: LONGINT;
  4106. formalType: SyntaxTree.ProcedureType;
  4107. formalParameter: SyntaxTree.Parameter;
  4108. actualParameter: SyntaxTree.Expression;
  4109. i: LONGINT;
  4110. BEGIN
  4111. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4112. result := SyntaxTree.invalidDesignator;
  4113. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4114. numberFormalParameters := formalType.numberParameters;
  4115. numberActualParameters := actualParameters.Length();
  4116. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4117. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4118. END;
  4119. IF ~ExpressionList(actualParameters) THEN
  4120. result := SyntaxTree.invalidDesignator
  4121. ELSE
  4122. IF numberActualParameters <= numberFormalParameters THEN
  4123. formalParameter := formalType.firstParameter;
  4124. FOR i := 0 TO numberActualParameters-1 DO
  4125. actualParameter := actualParameters.GetExpression(i);
  4126. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4127. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4128. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4129. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4130. ELSE
  4131. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4132. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4133. END;
  4134. actualParameters.SetExpression(i,actualParameter);
  4135. END;
  4136. formalParameter := formalParameter.nextParameter;
  4137. END;
  4138. WHILE (formalParameter # NIL) DO
  4139. IF formalParameter.defaultValue # NIL THEN
  4140. actualParameters.AddExpression(formalParameter.defaultValue);
  4141. formalParameter := formalParameter.nextParameter
  4142. ELSE
  4143. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4144. formalParameter := NIL;
  4145. END;
  4146. END;
  4147. ELSE
  4148. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4149. END;
  4150. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4151. result.SetAssignable(FALSE);
  4152. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4153. END;
  4154. RETURN result
  4155. END NewProcedureCallDesignator;
  4156. (**
  4157. builtin call designator generated in VisitParameterDesignator
  4158. -> nothing to be resolved
  4159. **)
  4160. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4161. BEGIN
  4162. resolvedExpression := x;
  4163. END VisitTypeGuardDesignator;
  4164. (**
  4165. builtin call designator generated in VisitParameterDesignator
  4166. -> nothing to be resolved
  4167. **)
  4168. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4169. BEGIN
  4170. IF ExpressionList(x.parameters) THEN
  4171. resolvedExpression := x;
  4172. END;
  4173. END VisitBuiltinCallDesignator;
  4174. (**
  4175. procedure call designator generated in VisitParameterDesignator
  4176. -> nothing to be resolved
  4177. **)
  4178. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4179. BEGIN
  4180. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4181. resolvedExpression := x;
  4182. END VisitProcedureCallDesignator;
  4183. (** return true if x is a variable else return false and report error **)
  4184. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4185. VAR result: BOOLEAN;
  4186. BEGIN
  4187. result := TRUE;
  4188. IF x = SyntaxTree.invalidExpression THEN
  4189. result := FALSE;
  4190. ELSIF ~IsVariable(x) THEN
  4191. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4192. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4193. result := FALSE;
  4194. END;
  4195. RETURN result
  4196. END CheckVariable;
  4197. (**
  4198. if expression x is of basic type then return true else report error and return false
  4199. **)
  4200. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4201. VAR result: BOOLEAN;
  4202. BEGIN
  4203. result := FALSE;
  4204. IF x = SyntaxTree.invalidExpression THEN
  4205. ELSIF ~IsBasicType(x.type) THEN
  4206. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4207. result := FALSE
  4208. ELSE result := TRUE
  4209. END;
  4210. RETURN result
  4211. END CheckBasicType;
  4212. (**
  4213. if expression x is of number type then return true else report error and return false
  4214. **)
  4215. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4216. VAR result: BOOLEAN;
  4217. BEGIN
  4218. result := FALSE;
  4219. IF x = SyntaxTree.invalidExpression THEN
  4220. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4221. Error(x.position,Diagnostics.Invalid,"is non number type");
  4222. ELSE result := TRUE
  4223. END;
  4224. RETURN result
  4225. END CheckNumberType;
  4226. (**
  4227. if expression x is of number or size type but not complex then return true else report error and return false
  4228. **)
  4229. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4230. VAR result: BOOLEAN;
  4231. BEGIN
  4232. result := FALSE;
  4233. IF x = SyntaxTree.invalidExpression THEN
  4234. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4235. Error(x.position,Diagnostics.Invalid,"is complex type");
  4236. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4237. Error(x.position,Diagnostics.Invalid,"is non number type");
  4238. ELSE result := TRUE
  4239. END;
  4240. RETURN result
  4241. END CheckNonComplexNumberSizeType;
  4242. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4243. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4244. BEGIN
  4245. result := FALSE; type := x.type.resolved;
  4246. IF x = SyntaxTree.invalidExpression THEN
  4247. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4248. TRACE(type.sizeInBits);
  4249. TRACE(system.addressType.sizeInBits);
  4250. Error(x.position,Diagnostics.Invalid,"is no address type");
  4251. ELSE result := TRUE
  4252. END;
  4253. RETURN result
  4254. END CheckAddressType;
  4255. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4256. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4257. BEGIN
  4258. result := FALSE; type := x.type.resolved;
  4259. IF x = SyntaxTree.invalidExpression THEN
  4260. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4261. Error(x.position,Diagnostics.Invalid,"is no size type");
  4262. ELSE result := TRUE
  4263. END;
  4264. RETURN result
  4265. END CheckSizeType;
  4266. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4267. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4268. BEGIN
  4269. result := FALSE; type := x.type.resolved;
  4270. IF x = SyntaxTree.invalidExpression THEN
  4271. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4272. Error(x.position,Diagnostics.Invalid,"is no object type");
  4273. ELSE result := TRUE
  4274. END;
  4275. RETURN result
  4276. END CheckObjectType;
  4277. (**
  4278. if expression x is of integer type then return true else report error and return false
  4279. **)
  4280. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4281. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4282. BEGIN
  4283. result := FALSE; type := x.type.resolved;
  4284. IF x = SyntaxTree.invalidExpression THEN
  4285. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4286. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4287. ELSE result := TRUE
  4288. END;
  4289. RETURN result
  4290. END CheckIntegerType;
  4291. (**
  4292. if expression x is of character type then return true else report error and return false
  4293. **)
  4294. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4295. VAR result: BOOLEAN;
  4296. BEGIN
  4297. result := FALSE;
  4298. IF x = SyntaxTree.invalidExpression THEN
  4299. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4300. Error(x.position,Diagnostics.Invalid,"is no character type");
  4301. ELSE result := TRUE
  4302. END;
  4303. RETURN result
  4304. END CheckCharacterType;
  4305. (**
  4306. if expression x is of real type then return true else report error and return false
  4307. **)
  4308. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4309. VAR result: BOOLEAN;
  4310. BEGIN
  4311. result := FALSE;
  4312. IF x = SyntaxTree.invalidExpression THEN
  4313. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4314. Error(x.position,Diagnostics.Invalid,"is no float type");
  4315. ELSE result := TRUE
  4316. END;
  4317. RETURN result
  4318. END CheckRealType;
  4319. (**
  4320. if expression x is of range type then return true else report error and return false
  4321. **)
  4322. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4323. VAR result: BOOLEAN;
  4324. BEGIN
  4325. result := FALSE;
  4326. IF x = SyntaxTree.invalidExpression THEN
  4327. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4328. Error(x.position,Diagnostics.Invalid,"is no range type");
  4329. ELSE result := TRUE
  4330. END;
  4331. RETURN result
  4332. END CheckRangeType;
  4333. (**
  4334. if expression x is of boolean type then return true else report error and return false
  4335. **)
  4336. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4337. VAR result: BOOLEAN;
  4338. BEGIN
  4339. result := FALSE;
  4340. IF x = SyntaxTree.invalidExpression THEN
  4341. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4342. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4343. ELSE result := TRUE
  4344. END;
  4345. RETURN result
  4346. END CheckBooleanType;
  4347. (**
  4348. if expression x is of set type then return true else report error and return false
  4349. **)
  4350. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4351. VAR result: BOOLEAN;
  4352. BEGIN
  4353. result := FALSE;
  4354. IF x = SyntaxTree.invalidExpression THEN
  4355. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4356. Error(x.position,Diagnostics.Invalid,"is no set type");
  4357. ELSE result := TRUE
  4358. END;
  4359. RETURN result
  4360. END CheckSetType;
  4361. (**
  4362. if expression x is of string or array of character type then return true else report error and return false
  4363. **)
  4364. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4365. VAR result: BOOLEAN;
  4366. BEGIN
  4367. result := FALSE;
  4368. IF x = SyntaxTree.invalidExpression THEN
  4369. ELSIF ~IsStringType(x.type.resolved) THEN
  4370. Error(x.position,Diagnostics.Invalid,"is no string type");
  4371. ELSE result := TRUE
  4372. END;
  4373. RETURN result
  4374. END CheckStringType;
  4375. (**
  4376. if expression x is a type declaration type return true else report error and return false
  4377. **)
  4378. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4379. VAR result: BOOLEAN;
  4380. BEGIN
  4381. result := FALSE;
  4382. IF x = SyntaxTree.invalidExpression THEN
  4383. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4384. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4385. ELSE result := TRUE
  4386. END;
  4387. RETURN result
  4388. END CheckTypeDeclarationType;
  4389. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4390. VAR result: BOOLEAN;
  4391. BEGIN
  4392. result := FALSE;
  4393. IF x = SyntaxTree.invalidExpression THEN
  4394. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4395. result := TRUE;
  4396. value := x.resolved(SyntaxTree.IntegerValue).value;
  4397. ELSE
  4398. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4399. END;
  4400. RETURN result;
  4401. END CheckIntegerValue;
  4402. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4403. BEGIN
  4404. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4405. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4406. ELSE
  4407. RETURN FALSE
  4408. END;
  4409. END IsUnsignedValue;
  4410. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4411. BEGIN
  4412. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4413. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4414. ELSE
  4415. RETURN FALSE
  4416. END
  4417. END IsAddressValue;
  4418. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4419. BEGIN
  4420. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4421. END IsAddressExpression;
  4422. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4423. BEGIN
  4424. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4425. END IsSizeExpression;
  4426. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4427. VAR result: BOOLEAN;
  4428. BEGIN
  4429. result := FALSE;
  4430. IF x = SyntaxTree.invalidExpression THEN
  4431. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4432. result := TRUE;
  4433. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4434. ELSE
  4435. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4436. END;
  4437. RETURN result;
  4438. END CheckEnumerationValue;
  4439. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4440. VAR result: BOOLEAN;
  4441. BEGIN
  4442. result := FALSE;
  4443. IF x = SyntaxTree.invalidExpression THEN
  4444. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4445. result := TRUE;
  4446. value := x.resolved(SyntaxTree.CharacterValue).value;
  4447. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4448. result := TRUE;
  4449. value := x.resolved(SyntaxTree.StringValue).value[0];
  4450. ELSE
  4451. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4452. END;
  4453. RETURN result;
  4454. END CheckCharacterValue;
  4455. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4456. VAR result: BOOLEAN;
  4457. BEGIN
  4458. result := FALSE;
  4459. IF x = SyntaxTree.invalidExpression THEN
  4460. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4461. value := x.resolved(SyntaxTree.IntegerValue).value;
  4462. IF (value > 0) OR includeZero & (value = 0) THEN
  4463. result := TRUE;
  4464. ELSE
  4465. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4466. END
  4467. ELSE
  4468. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4469. END;
  4470. RETURN result;
  4471. END CheckPositiveIntegerValue;
  4472. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4473. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4474. BEGIN
  4475. result := FALSE;
  4476. IF x = SyntaxTree.invalidExpression THEN
  4477. ELSE
  4478. type := x.type.resolved;
  4479. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4480. portType := type(SyntaxTree.PortType);
  4481. result := TRUE
  4482. ELSE
  4483. Error(x.position,Diagnostics.Invalid,"no port type");
  4484. END;
  4485. END;
  4486. RETURN result
  4487. END CheckPortType;
  4488. (* move to builtin procedure call statement ?
  4489. remove builtin procedure call designator ?
  4490. *)
  4491. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator): SyntaxTree.Expression;
  4492. VAR
  4493. numberActualParameters,numberFormalParameters: LONGINT;
  4494. formalParameter: SyntaxTree.Parameter;
  4495. actualParameter: SyntaxTree.Expression;
  4496. procedureType: SyntaxTree.ProcedureType;
  4497. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4498. inPort, outPort: SyntaxTree.PortType;
  4499. constructor: SyntaxTree.Procedure;
  4500. type0,type1,type2: SyntaxTree.Type;
  4501. type,base,parameterType: SyntaxTree.Type;
  4502. arrayType: SyntaxTree.ArrayType;
  4503. i,i0,i1: LONGINT;
  4504. r,r0,r1,im: LONGREAL;
  4505. c: CHAR;
  4506. id: LONGINT;
  4507. b: BOOLEAN;
  4508. mathArrayType: SyntaxTree.MathArrayType;
  4509. customBuiltin: SyntaxTree.CustomBuiltin;
  4510. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4511. VAR resultB: BOOLEAN;
  4512. BEGIN
  4513. IF numberActualParameters < from THEN
  4514. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4515. result := SyntaxTree.invalidExpression;
  4516. resultB := FALSE;
  4517. ELSIF numberActualParameters > to THEN
  4518. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4519. result := SyntaxTree.invalidExpression;
  4520. resultB := FALSE;
  4521. ELSE
  4522. resultB := TRUE;
  4523. END;
  4524. RETURN resultB
  4525. END CheckArity;
  4526. BEGIN
  4527. type := NIL; result := NIL;
  4528. type0 := NIL; type1 := NIL; type2 := NIL;
  4529. numberActualParameters := actualParameters.Length();
  4530. IF numberActualParameters>0 THEN
  4531. parameter0 := actualParameters.GetExpression(0);
  4532. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4533. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4534. result := SyntaxTree.invalidExpression
  4535. END
  4536. END;
  4537. IF numberActualParameters >1 THEN
  4538. parameter1 := actualParameters.GetExpression(1);
  4539. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4540. ELSE
  4541. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4542. result := SyntaxTree.invalidExpression
  4543. END
  4544. END;
  4545. IF numberActualParameters >2 THEN
  4546. parameter2 := actualParameters.GetExpression(2);
  4547. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4548. ELSE
  4549. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4550. result := SyntaxTree.invalidExpression
  4551. END
  4552. END;
  4553. id := builtin.id;
  4554. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4555. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4556. END;
  4557. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4558. ELSIF result # NIL THEN type := result.type (* operator *)
  4559. ELSE
  4560. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4561. result(SyntaxTree.Designator).SetLeft(left);
  4562. (* ---- ASSERT ----- *)
  4563. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4564. IF CheckBooleanType(parameter0) THEN
  4565. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4566. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4567. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4568. *)
  4569. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4570. Error(position,Diagnostics.Invalid,"assert failed");
  4571. END;
  4572. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4573. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4574. rules imposed by the architecture / current runtime
  4575. *)
  4576. END;
  4577. END;
  4578. (* ---- COPY ----- *)
  4579. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4580. IF~IsStringType(type0) THEN
  4581. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4582. END;
  4583. IF ~IsStringType(type1) THEN
  4584. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4585. ELSIF CheckVariable(parameter1) THEN
  4586. IF (type0 IS SyntaxTree.StringType) THEN
  4587. arrayType := type1(SyntaxTree.ArrayType);
  4588. IF arrayType.form = SyntaxTree.Static THEN
  4589. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4590. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4591. END;
  4592. END;
  4593. END;
  4594. END;
  4595. (* ---- INC, DEC----- *)
  4596. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4597. IF numberActualParameters = 1 THEN
  4598. parameter1 :=Global.NewIntegerValue(system,position,1);
  4599. actualParameters.AddExpression(parameter1);
  4600. END;
  4601. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4602. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4603. Error(position,Diagnostics.Invalid,"incompatible increment");
  4604. ELSE
  4605. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4606. actualParameters.SetExpression(1,parameter1);
  4607. END;
  4608. END;
  4609. (* ---- EXCL, INCL----- *)
  4610. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4611. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4612. IF IsIntegerValue(parameter1,i0) THEN
  4613. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4614. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4615. END;
  4616. END;
  4617. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4618. actualParameters.SetExpression(1,parameter1);
  4619. END;
  4620. (* ---- HALT, SYSTEM.HALT ----- *)
  4621. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4622. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4623. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4624. rules imposed by the architecture / current runtime
  4625. *)
  4626. END;
  4627. (* ---- WAIT ----- *)
  4628. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4629. IF CheckObjectType(parameter0) THEN
  4630. END;
  4631. (* ---- NEW ----- *)
  4632. ELSIF (id = Global.New) & CheckArity(1,Infinity) THEN
  4633. IF currentIsRealtime THEN
  4634. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4635. END;
  4636. (* check constructor *)
  4637. IF CheckVariable(parameter0) THEN
  4638. IF type0 IS SyntaxTree.PointerType THEN
  4639. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4640. END;
  4641. IF type0 IS SyntaxTree.ArrayType THEN
  4642. arrayType := type0(SyntaxTree.ArrayType);
  4643. IF arrayType.form = SyntaxTree.Static THEN
  4644. i := 1
  4645. ELSIF arrayType.form = SyntaxTree.Open THEN
  4646. i := Dimension(arrayType,{SyntaxTree.Open})+1;
  4647. ELSE HALT(100)
  4648. END;
  4649. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4650. i := 1;
  4651. REPEAT
  4652. actualParameter := actualParameters.GetExpression(i);
  4653. IF CheckSizeType(actualParameter) THEN
  4654. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4655. actualParameters.SetExpression(i,actualParameter);
  4656. END;
  4657. INC(i);
  4658. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4659. END;
  4660. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4661. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4662. IF constructor = NIL THEN
  4663. IF CheckArity(1,1) THEN END;
  4664. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4665. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4666. ELSE
  4667. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4668. numberFormalParameters := procedureType.numberParameters;
  4669. DEC(numberActualParameters);
  4670. IF numberActualParameters <= numberFormalParameters THEN
  4671. formalParameter := procedureType.firstParameter;
  4672. FOR i := 1 TO numberActualParameters DO
  4673. actualParameter := actualParameters.GetExpression(i);
  4674. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4675. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4676. ELSE
  4677. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4678. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4679. END;
  4680. actualParameters.SetExpression(i,actualParameter);
  4681. END;
  4682. formalParameter := formalParameter.nextParameter;
  4683. END;
  4684. WHILE (formalParameter # NIL) DO
  4685. IF formalParameter.defaultValue # NIL THEN
  4686. actualParameters.AddExpression(formalParameter.defaultValue);
  4687. formalParameter := formalParameter.nextParameter
  4688. ELSE
  4689. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4690. formalParameter := NIL;
  4691. END;
  4692. END;
  4693. ELSE
  4694. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4695. END;
  4696. END;
  4697. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4698. mathArrayType := type0(SyntaxTree.MathArrayType);
  4699. IF mathArrayType.form = SyntaxTree.Static THEN
  4700. Error(position,Diagnostics.Invalid,"new on static array");
  4701. ELSE
  4702. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4703. i0 := 2; i1 := Infinity;
  4704. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4705. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+1;
  4706. i1 := i0;
  4707. ELSE HALT(100);
  4708. END;
  4709. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4710. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4711. base := ArrayBase(type0,MAX(LONGINT));
  4712. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4713. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4714. IF ~CompatibleTo(system,type0,parameterType) THEN
  4715. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4716. result := SyntaxTree.invalidExpression;
  4717. ELSE
  4718. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4719. END;
  4720. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4721. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4722. IF ~CompatibleTo(system,type1,parameterType) THEN
  4723. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4724. result := SyntaxTree.invalidExpression;
  4725. ELSE
  4726. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4727. END;
  4728. ELSE
  4729. IF CheckArity(i0,i1) & (numberActualParameters >1) THEN
  4730. i := 1;
  4731. REPEAT
  4732. actualParameter := actualParameters.GetExpression(i);
  4733. IF CheckSizeType(actualParameter) THEN
  4734. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4735. actualParameters.SetExpression(i,actualParameter);
  4736. END;
  4737. INC(i);
  4738. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4739. END;
  4740. END;
  4741. END;
  4742. ELSIF type0 IS SyntaxTree.CellType THEN
  4743. IF ~(currentIsCellNet) THEN
  4744. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4745. ELSE
  4746. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4747. IF (constructor = NIL) & CheckArity(1,1) THEN
  4748. (* ok *)
  4749. ELSE
  4750. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4751. numberFormalParameters := procedureType.numberParameters;
  4752. DEC(numberActualParameters);
  4753. IF numberActualParameters <= numberFormalParameters THEN
  4754. formalParameter := procedureType.firstParameter;
  4755. FOR i := 1 TO numberActualParameters DO
  4756. actualParameter := actualParameters.GetExpression(i);
  4757. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4758. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4759. ELSE
  4760. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4761. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4762. END;
  4763. actualParameters.SetExpression(i,actualParameter);
  4764. END;
  4765. formalParameter := formalParameter.nextParameter;
  4766. END;
  4767. WHILE (formalParameter # NIL) DO
  4768. IF formalParameter.defaultValue # NIL THEN
  4769. actualParameters.AddExpression(formalParameter.defaultValue);
  4770. formalParameter := formalParameter.nextParameter
  4771. ELSE
  4772. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4773. formalParameter := NIL;
  4774. END;
  4775. END;
  4776. ELSE
  4777. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4778. END;
  4779. END;
  4780. END;
  4781. activeCellsStatement := TRUE;
  4782. ELSE
  4783. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4784. END;
  4785. END;
  4786. (* ---- DISPOSE ----- *)
  4787. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4788. IF ~IsPointerType(parameter0.type) THEN
  4789. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4790. ELSIF ~IsDisposable(parameter0.type) THEN
  4791. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4792. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4793. END
  4794. (* ---- GETPROCEDURE ----- *)
  4795. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4796. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4797. IF CheckVariable(parameter2) THEN
  4798. IF ~GetProcedureAllowed(parameter2.type) THEN
  4799. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4800. END;
  4801. END;
  4802. END;
  4803. (* ---- ABS ----- *)
  4804. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4805. (* note: ABS on complex numbers is done using overloading *)
  4806. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4807. type := type0;
  4808. IF IsIntegerValue(parameter0,i0) THEN
  4809. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4810. type := Global.GetIntegerType(system,ABS(i0));
  4811. ELSIF IsRealValue(parameter0,r) THEN
  4812. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4813. END;
  4814. ELSE
  4815. type := SyntaxTree.invalidType;
  4816. END;
  4817. (* ---- ASH, ASR ----- *)
  4818. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4819. type := type0;
  4820. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4821. (*
  4822. ConvertOperands(parameter0,parameter1); (* same type *)
  4823. *)
  4824. type := parameter0.type;
  4825. IF IsIntegerValue(parameter0,i0) THEN
  4826. IF IsIntegerValue(parameter1,i1) THEN
  4827. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4828. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4829. result := ResolveExpression(result);
  4830. type := Global.GetIntegerType(system,i0);
  4831. END;
  4832. END;
  4833. IF type.resolved.sizeInBits < 32 THEN
  4834. type := system.longintType;
  4835. END;
  4836. (*!compatibility with release, remove when resolved
  4837. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4838. *)
  4839. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4840. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4841. actualParameters.SetExpression(0,parameter0);
  4842. actualParameters.SetExpression(1,parameter1);
  4843. END;
  4844. (* ---- CAP ----- *)
  4845. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4846. type := system.characterType;
  4847. IF CheckCharacterType (parameter0) THEN
  4848. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4849. actualParameters.SetExpression(0,parameter0);
  4850. IF IsCharacterValue(parameter0,c) THEN
  4851. IF (c <= "z") & (c >= "a") THEN
  4852. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4853. ELSE
  4854. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4855. END;
  4856. END;
  4857. END;
  4858. (* ---- CHR ----- *)
  4859. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4860. IF id = Global.Chr THEN
  4861. type := system.characterType
  4862. ELSE
  4863. type := system.characterType32
  4864. END;
  4865. IF CheckIntegerType(parameter0) THEN
  4866. IF IsIntegerValue(parameter0,i0) THEN
  4867. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4868. result := ResolveExpression(result);
  4869. ELSE
  4870. (*
  4871. result := NewConversion(parameter0.position,parameter0,type);
  4872. *)
  4873. END;
  4874. END
  4875. (* ---- ENTIER ----- *)
  4876. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4877. type := system.longintType;
  4878. IF CheckRealType(parameter0) THEN
  4879. IF IsRealValue(parameter0,r) THEN
  4880. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4881. type := Global.GetIntegerType(system,ENTIER(r));
  4882. END
  4883. END;
  4884. (* ---- ENTIERH ----- *)
  4885. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4886. type := system.hugeintType;
  4887. IF CheckRealType(parameter0) THEN
  4888. IF IsRealValue(parameter0,r) THEN
  4889. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4890. END
  4891. END;
  4892. (* ---- LEN ----- *)
  4893. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4894. type := system.longintType;
  4895. base := type0;
  4896. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4897. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4898. actualParameters.SetExpression(0,parameter0);
  4899. type0 := parameter0.type.resolved;
  4900. base := type0;
  4901. END;
  4902. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4903. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4904. IF i1 < 0 THEN
  4905. Error(position,Diagnostics.Invalid,"invalid dimension");
  4906. base := SyntaxTree.invalidType;
  4907. ELSE
  4908. base := ArrayBase(base,i1);
  4909. IF (base # NIL) & Indexable(base) THEN
  4910. ELSE
  4911. Error(position,Diagnostics.Invalid,"len on no array");
  4912. IF VerboseErrorMessage THEN
  4913. Printout.Info("base",base);
  4914. END;
  4915. base := SyntaxTree.invalidType;
  4916. END;
  4917. END;
  4918. IF numberActualParameters=2 THEN
  4919. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4920. actualParameters.SetExpression(1,parameter1);
  4921. ELSIF base IS SyntaxTree.MathArrayType THEN
  4922. Error(position,Diagnostics.Invalid,"missing dimension specification");
  4923. END;
  4924. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  4925. IF base IS SyntaxTree.ArrayType THEN
  4926. arrayType := base(SyntaxTree.ArrayType);
  4927. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  4928. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  4929. result := Global.NewIntegerValue(system,position,i);
  4930. type := result.type;(* arrayType.length.type;*)
  4931. ASSERT(type # NIL);
  4932. END;
  4933. ELSIF base IS SyntaxTree.MathArrayType THEN
  4934. mathArrayType := base(SyntaxTree.MathArrayType);
  4935. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  4936. result := Global.NewIntegerValue(system,position,i);
  4937. type := result.type;
  4938. (*
  4939. type := mathArrayType.length.type;
  4940. *)
  4941. ASSERT(type # NIL);
  4942. END;
  4943. END;
  4944. END;
  4945. ELSE
  4946. type := system.longintType;
  4947. END;
  4948. (* ---- FIRST ---- *)
  4949. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  4950. type := system.longintType;
  4951. IF CheckRangeType(parameter0) THEN END;
  4952. result.SetAssignable(parameter0.assignable)
  4953. (* ---- LAST ---- *)
  4954. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  4955. type := system.longintType;
  4956. IF CheckRangeType(parameter0) THEN END;
  4957. result.SetAssignable(parameter0.assignable)
  4958. (* ---- STEP ---- *)
  4959. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  4960. type := system.longintType;
  4961. IF CheckRangeType(parameter0) THEN END;
  4962. result.SetAssignable(parameter0.assignable)
  4963. (* ---- RE ---- *)
  4964. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  4965. IF CheckNumberType(parameter0) THEN
  4966. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4967. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4968. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  4969. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  4970. type := parameter0.type
  4971. ELSE
  4972. type := system.realType
  4973. END
  4974. END;
  4975. result.SetAssignable(parameter0.assignable)
  4976. (* ---- IM ---- *)
  4977. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  4978. IF CheckNumberType(parameter0) THEN
  4979. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4980. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4981. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  4982. ELSE
  4983. type := system.realType;
  4984. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  4985. END
  4986. END;
  4987. result.SetAssignable(parameter0.assignable)
  4988. (* ---- MAX ----- *)
  4989. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  4990. IF numberActualParameters = 1 THEN
  4991. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  4992. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  4993. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  4994. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  4995. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  4996. *)
  4997. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  4998. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  4999. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5000. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5001. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5002. END;
  5003. ELSE
  5004. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5005. END
  5006. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5007. ConvertOperands(parameter0,parameter1);
  5008. actualParameters.SetExpression(0,parameter0);
  5009. actualParameters.SetExpression(1,parameter1);
  5010. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5011. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5012. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5013. END;
  5014. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5015. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5016. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5017. END;
  5018. END;
  5019. type := parameter0.type;
  5020. ELSE type := SyntaxTree.invalidType;
  5021. END;
  5022. (* ---- MIN ----- *)
  5023. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5024. IF numberActualParameters = 1 THEN
  5025. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5026. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5027. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5028. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5029. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5030. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5031. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5032. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5033. END;
  5034. ELSE
  5035. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5036. END
  5037. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5038. ConvertOperands(parameter0,parameter1);
  5039. actualParameters.SetExpression(0,parameter0);
  5040. actualParameters.SetExpression(1,parameter1);
  5041. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5042. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5043. ELSE result.SetResolved(parameter1.resolved)
  5044. END;
  5045. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5046. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5047. ELSE result.SetResolved(parameter1.resolved)
  5048. END;
  5049. END;
  5050. type := parameter0.type;
  5051. ELSE type := SyntaxTree.invalidType;
  5052. END;
  5053. (* ---- ODD ----- *)
  5054. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5055. type := system.booleanType;
  5056. IF CheckIntegerType(parameter0) THEN
  5057. IF IsIntegerValue(parameter0,i0) THEN
  5058. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5059. type := system.booleanType;
  5060. END;
  5061. END;
  5062. (* ---- ORD ----- *)
  5063. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5064. IF id = Global.Ord THEN
  5065. type := system.integerType;
  5066. ELSE
  5067. type := system.longintType;
  5068. END;
  5069. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5070. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5071. actualParameters.SetExpression(0,parameter0);
  5072. (* IF CheckCharacterType(parameter0) THEN*)
  5073. IF IsCharacterValue(parameter0,c)THEN
  5074. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5075. type := Global.GetSignedIntegerType(system,ORD(c));
  5076. END;
  5077. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5078. END;
  5079. (* ---- SHORT ----- *)
  5080. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5081. type := type0;
  5082. IF IsSignedIntegerType(type) THEN
  5083. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5084. ELSIF type = system.integerType THEN type := system.shortintType
  5085. ELSIF type = system.longintType THEN type := system.integerType
  5086. ELSIF type = system.hugeintType THEN type:= system.longintType
  5087. ELSE
  5088. CASE type.sizeInBits OF
  5089. 16: type := Global.Integer8
  5090. |32: type := Global.Integer16
  5091. |64: type := Global.Integer32
  5092. END;
  5093. END;
  5094. ELSIF type IS SyntaxTree.FloatType THEN
  5095. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5096. ELSIF type = system.longrealType THEN type := system.realType
  5097. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5098. END;
  5099. ELSIF type IS SyntaxTree.ComplexType THEN
  5100. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5101. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5102. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5103. END;
  5104. ELSE
  5105. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5106. END;
  5107. IF (parameter0.resolved # NIL) THEN
  5108. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5109. IF parameter0 IS SyntaxTree.Value THEN
  5110. result.SetResolved(parameter0(SyntaxTree.Value));
  5111. END;
  5112. END;
  5113. (* ---- LONG ----- *)
  5114. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5115. type := type0;
  5116. IF IsSignedIntegerType(type) THEN
  5117. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5118. ELSIF type = system.longintType THEN type := system.hugeintType
  5119. ELSIF type = system.integerType THEN type := system.longintType
  5120. ELSIF type = system.shortintType THEN type := system.integerType
  5121. ELSE
  5122. CASE type.sizeInBits OF
  5123. 8: type := Global.Integer16
  5124. |16: type := Global.Integer32
  5125. |32: type := Global.Integer64
  5126. END;
  5127. END;
  5128. ELSIF type IS SyntaxTree.FloatType THEN
  5129. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5130. ELSIF type= system.realType THEN type := system.longrealType
  5131. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5132. END;
  5133. ELSIF type IS SyntaxTree.ComplexType THEN
  5134. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5135. ELSIF type = system.complexType THEN type := system.longcomplexType
  5136. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5137. END;
  5138. ELSE
  5139. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5140. END;
  5141. IF (parameter0.resolved # NIL) THEN
  5142. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5143. IF parameter0 IS SyntaxTree.Value THEN
  5144. result.SetResolved(parameter0(SyntaxTree.Value));
  5145. END;
  5146. END;
  5147. (* ---- SIZE OF ----- *)
  5148. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5149. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5150. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5151. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5152. type := system.integerType;
  5153. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5154. ELSE
  5155. (* for variables, system sizeof could represent the physically occupied size
  5156. determined via the type descriptor, implement that ? *)
  5157. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5158. END
  5159. (* ---- SYSTEM.TRACE -----*)
  5160. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5161. FOR i := 0 TO numberActualParameters-1 DO
  5162. parameter0 := actualParameters.GetExpression(i);
  5163. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5164. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5165. END;
  5166. END;
  5167. (* remaining issues can only be tested in backend *)
  5168. (* ---- ADDRESSOF----- *)
  5169. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5170. IF HasAddress(parameter0) THEN
  5171. type := system.addressType;
  5172. ELSE
  5173. type := SyntaxTree.invalidType;
  5174. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5175. END;
  5176. (* ---- BIT ----- *)
  5177. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5178. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5179. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5180. actualParameters.SetExpression(0,parameter0);
  5181. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5182. actualParameters.SetExpression(1,parameter1);
  5183. END;
  5184. type := system.booleanType;
  5185. (* ----- MSK ---- *)
  5186. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5187. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5188. ConvertOperands(parameter0,parameter1);
  5189. actualParameters.SetExpression(0,parameter0);
  5190. actualParameters.SetExpression(1,parameter1);
  5191. END;
  5192. type := parameter0.type;
  5193. (* ---- SYSTEM.GET64 ----- *)
  5194. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5195. IF CheckAddressType(parameter0) THEN
  5196. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5197. actualParameters.SetExpression(0,parameter0);
  5198. END;
  5199. type := system.hugeintType;
  5200. (* ---- SYSTEM.GET32 ----- *)
  5201. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5202. IF CheckAddressType(parameter0) THEN
  5203. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5204. actualParameters.SetExpression(0,parameter0);
  5205. END;
  5206. type := system.longintType;
  5207. (* ---- SYSTEM.GET16 ----- *)
  5208. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5209. IF CheckAddressType(parameter0) THEN
  5210. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5211. actualParameters.SetExpression(0,parameter0);
  5212. END;
  5213. type := system.integerType;
  5214. (* ---- SYSTEM.GET8 ----- *)
  5215. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5216. IF CheckAddressType(parameter0) THEN
  5217. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5218. actualParameters.SetExpression(0,parameter0);
  5219. END;
  5220. type := system.shortintType;
  5221. (* ---- SYSTEM.GetStackPointer ----- *)
  5222. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5223. type := system.addressType;
  5224. (* ---- SYSTEM.GetFramePointer ----- *)
  5225. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5226. type := system.addressType;
  5227. (* ---- SYSTEM.GetActivity ----- *)
  5228. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5229. type := system.objectType;
  5230. (* ---- SYSTEM.SetStackPointer ----- *)
  5231. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5232. IF CheckAddressType(parameter0) THEN
  5233. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5234. actualParameters.SetExpression(0,parameter0);
  5235. END;
  5236. (* ---- SYSTEM.SetFramePointer ----- *)
  5237. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5238. IF CheckAddressType(parameter0) THEN
  5239. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5240. actualParameters.SetExpression(0,parameter0);
  5241. END;
  5242. (* ---- SYSTEM.SetActivity ----- *)
  5243. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5244. IF CheckObjectType(parameter0) THEN
  5245. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5246. actualParameters.SetExpression(0,parameter0);
  5247. END;
  5248. (* ---- LSH, LSL, ROT, ROR ----- *)
  5249. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5250. type := type0;
  5251. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5252. actualParameters.SetExpression(1, parameter1);
  5253. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5254. IF id = Global.Lsh THEN
  5255. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5256. ELSIF id = Global.Rot THEN
  5257. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5258. ELSIF id = Global.Ror THEN
  5259. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5260. END;
  5261. END;
  5262. (* ---- SYSTEM.VAL ----- *)
  5263. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5264. IF CheckTypeDeclarationType(parameter0) THEN
  5265. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5266. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5267. result := SyntaxTree.invalidExpression;
  5268. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5269. ELSE
  5270. IF (parameter1.resolved # NIL) THEN
  5271. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5272. IF parameter0 IS SyntaxTree.Value THEN
  5273. result.SetResolved(parameter0(SyntaxTree.Value));
  5274. END;
  5275. END;
  5276. result.SetAssignable(parameter1.assignable);
  5277. END;
  5278. END;
  5279. (* ---- SYSTEM.GET ----- *)
  5280. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5281. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5282. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5283. actualParameters.SetExpression(0,parameter0);
  5284. END;
  5285. (* ---- SYSTEM.PUT ----- *)
  5286. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5287. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5288. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5289. actualParameters.SetExpression(0,parameter0);
  5290. END;
  5291. (* ---- SYSTEM.PUT64 ----- *)
  5292. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5293. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5294. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5295. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5296. actualParameters.SetExpression(0,parameter0);
  5297. actualParameters.SetExpression(1,parameter1);
  5298. END;
  5299. (* ---- SYSTEM.PUT32 ----- *)
  5300. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5301. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5302. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5303. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5304. actualParameters.SetExpression(0,parameter0);
  5305. actualParameters.SetExpression(1,parameter1);
  5306. END;
  5307. (* ---- SYSTEM.PUT16 ----- *)
  5308. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5309. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5310. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5311. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5312. actualParameters.SetExpression(0,parameter0);
  5313. actualParameters.SetExpression(1,parameter1);
  5314. END;
  5315. (* ---- SYSTEM.PUT8 ----- *)
  5316. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5317. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5318. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5319. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5320. actualParameters.SetExpression(0,parameter0);
  5321. actualParameters.SetExpression(1,parameter1);
  5322. END;
  5323. (* ---- SYSTEM.MOVE ----- *)
  5324. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5325. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5326. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5327. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5328. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5329. actualParameters.SetExpression(0,parameter0);
  5330. actualParameters.SetExpression(1,parameter1);
  5331. actualParameters.SetExpression(2,parameter2);
  5332. END;
  5333. (* ---- SYSTEM.NEW ----- *)
  5334. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5335. IF ~IsPointerType(parameter0.type) THEN
  5336. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5337. ELSIF CheckSizeType(parameter1) THEN
  5338. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5339. actualParameters.SetExpression(1,parameter1);
  5340. END;
  5341. (* ----SYSTEM.REF ---- *)
  5342. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5343. type := system.addressType
  5344. (* ---- INCR ----- *)
  5345. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5346. type := system.sizeType;
  5347. base := type0;
  5348. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5349. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5350. IF i1 < 0 THEN
  5351. Error(position,Diagnostics.Invalid,"invalid dimension");
  5352. base := SyntaxTree.invalidType;
  5353. ELSE
  5354. base := ArrayBase(base,i1);
  5355. IF (base # NIL) & Indexable(base) THEN
  5356. ELSE
  5357. Error(position,Diagnostics.Invalid,"len on no array");
  5358. IF VerboseErrorMessage THEN
  5359. Printout.Info("base",base);
  5360. END;
  5361. base := SyntaxTree.invalidType;
  5362. END;
  5363. END;
  5364. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5365. actualParameters.SetExpression(1,parameter1);
  5366. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5367. mathArrayType := base(SyntaxTree.MathArrayType);
  5368. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5369. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5370. type := system.longintType;
  5371. END;
  5372. END;
  5373. ELSE
  5374. type := system.longintType;
  5375. END;
  5376. (* ---- SUM ----- *)
  5377. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5378. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5379. (* ---- ALL ----- *)
  5380. ELSIF (id = Global.All) & CheckArity(2,3) THEN (* can only be found by overloading *)
  5381. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5382. (* ---- DIM ----- *)
  5383. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5384. type := system.sizeType;
  5385. IF type0 IS SyntaxTree.MathArrayType THEN
  5386. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5387. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5388. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5389. END;
  5390. ELSE
  5391. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5392. END;
  5393. (* ---- CAS ----- *)
  5394. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5395. IF type0.IsComposite () THEN
  5396. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5397. result := SyntaxTree.invalidExpression;
  5398. ELSIF ~IsVariable (parameter0) THEN
  5399. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5400. result := SyntaxTree.invalidExpression;
  5401. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5402. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5403. result := SyntaxTree.invalidExpression;
  5404. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5405. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5406. result := SyntaxTree.invalidExpression;
  5407. ELSE
  5408. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5409. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5410. type := type0;
  5411. END;
  5412. (* ---- RESHAPE ----- *)
  5413. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5414. IF type0 IS SyntaxTree.MathArrayType THEN
  5415. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5416. base := ArrayBase(type0,MAX(LONGINT));
  5417. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5418. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5419. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5420. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5421. IF ~CompatibleTo(system,type0,parameterType) THEN
  5422. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5423. result := SyntaxTree.invalidExpression;
  5424. ELSE
  5425. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5426. END;
  5427. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5428. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5429. IF ~CompatibleTo(system,type1,parameterType) THEN
  5430. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5431. result := SyntaxTree.invalidExpression;
  5432. ELSE
  5433. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5434. END;
  5435. ELSE
  5436. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5437. result := SyntaxTree.invalidExpression;
  5438. END;
  5439. (* ---- SYSTEM.TYPECODE ----- *)
  5440. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5441. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5442. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5443. type := type.resolved;
  5444. IF type IS SyntaxTree.PointerType THEN
  5445. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5446. END;
  5447. IF ~(type IS SyntaxTree.RecordType) THEN
  5448. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5449. END;
  5450. ELSE
  5451. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5452. END;
  5453. type := system.addressType;
  5454. (* -------- FLT --------- *)
  5455. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5456. type := system.realType;
  5457. IF IsRealValue(parameter0, r) THEN
  5458. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5459. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5460. i0 := i; i := ABS(i);
  5461. IF i # 0 THEN
  5462. i1 := 23;
  5463. IF i >= 2*800000H THEN
  5464. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5465. ELSIF i < 800000H THEN
  5466. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5467. END;
  5468. i := (i1 + 127)*800000H - 800000H + i;
  5469. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5470. END;
  5471. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5472. END;
  5473. (* ------- CONNECT -------*)
  5474. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5475. IF ~(currentIsCellNet) THEN
  5476. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5477. END;
  5478. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5479. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5480. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5481. END;
  5482. IF numberActualParameters = 3 THEN
  5483. IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5484. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5485. END;
  5486. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5487. actualParameters.SetExpression(2,parameter2);
  5488. END;
  5489. activeCellsStatement := TRUE;
  5490. (* ---------- DELEGATE --------*)
  5491. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5492. IF ~(currentIsCellNet) THEN
  5493. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5494. END;
  5495. IF ~CheckPortType(parameter1, inPort) THEN
  5496. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5497. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5498. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5499. ELSIF (outPort.direction # inPort.direction) THEN
  5500. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5501. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5502. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5503. END;
  5504. activeCellsStatement := TRUE;
  5505. (* --------- RECEIVE ---------*)
  5506. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5507. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5508. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5509. IF inPort.direction # SyntaxTree.InPort THEN
  5510. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5511. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5512. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5513. END;
  5514. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5515. IF ~SameType(parameter2.type, system.integerType) THEN
  5516. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5517. END;
  5518. END;
  5519. END;
  5520. (* --------- SEND ---------*)
  5521. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5522. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5523. IF CheckPortType(parameter0,outPort) THEN
  5524. IF outPort.direction # SyntaxTree.OutPort THEN
  5525. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5526. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5527. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5528. ELSE
  5529. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5530. actualParameters.SetExpression(1,parameter1);
  5531. END;
  5532. END;
  5533. (* ------- custom builtins ----- *)
  5534. ELSIF id = Global.systemSpecial THEN
  5535. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5536. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5537. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5538. type := procedureType.returnType;
  5539. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5540. (* go through all formal parameters *)
  5541. formalParameter := procedureType.firstParameter;
  5542. FOR i := 0 TO actualParameters.Length() - 1 DO
  5543. actualParameter := actualParameters.GetExpression(i);
  5544. IF actualParameter = SyntaxTree.invalidExpression THEN
  5545. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5546. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5547. ELSE
  5548. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5549. END;
  5550. actualParameters.SetExpression(i, actualParameter);
  5551. formalParameter := formalParameter.nextParameter
  5552. END
  5553. END
  5554. ELSE
  5555. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5556. result := SyntaxTree.invalidExpression;
  5557. END;
  5558. END;
  5559. IF result # SyntaxTree.invalidExpression THEN
  5560. type := ResolveType(type);
  5561. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5562. result.SetType(type);
  5563. END;
  5564. RETURN result
  5565. END NewBuiltinCallDesignator;
  5566. (** return type guard designator left(type)
  5567. - check if type can be extended (i.e. is no static record)
  5568. - check if type is a type extension of left.type
  5569. - returns new type guard designator
  5570. returns invalidDesignator = invalidExpression if error
  5571. **)
  5572. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type): SyntaxTree.Designator;
  5573. VAR result: SyntaxTree.Designator;
  5574. BEGIN
  5575. result := SyntaxTree.invalidDesignator;
  5576. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5577. Error(position,Diagnostics.Invalid,"no type extension of type");
  5578. IF VerboseErrorMessage THEN
  5579. Printout.Info("left",left);
  5580. Printout.Info("type",type);
  5581. END;
  5582. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5583. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5584. ELSE
  5585. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5586. result.SetType(type);
  5587. result.SetAssignable(left.assignable);
  5588. END;
  5589. RETURN result
  5590. END NewTypeGuardDesignator;
  5591. (** check and resolve parameter designator left(expression list)
  5592. - check expression list
  5593. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5594. - elsif left is a procedure type then
  5595. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5596. - else return is a procedure call then return ProcedureCallDesignator
  5597. returns invalidDesignator = invalidExpression if error
  5598. **)
  5599. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5600. VAR
  5601. parameters: SyntaxTree.ExpressionList;
  5602. left: SyntaxTree.Designator;
  5603. result,expression: SyntaxTree.Expression;
  5604. typeDeclaration: SyntaxTree.TypeDeclaration;
  5605. type, expressionType: SyntaxTree.Type;
  5606. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5607. BEGIN
  5608. type := type.resolved;
  5609. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5610. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5611. END;
  5612. RETURN type
  5613. END BaseType;
  5614. BEGIN
  5615. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5616. result := SyntaxTree.invalidDesignator;
  5617. left := ResolveDesignator(designator.left);
  5618. IF left # SyntaxTree.invalidDesignator THEN
  5619. parameters := designator.parameters;
  5620. IF ExpressionList(parameters) THEN
  5621. IF (left.type = NIL) THEN
  5622. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5623. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5624. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5625. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5626. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5627. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5628. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5629. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left);
  5630. ELSE
  5631. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5632. END
  5633. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5634. expression := parameters.GetExpression(0);
  5635. type := typeDeclaration.declaredType.resolved;
  5636. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5637. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5638. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5639. OR (expressionType IS SyntaxTree.EnumerationType)
  5640. ) THEN
  5641. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5642. ELSE
  5643. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5644. END;
  5645. ELSE
  5646. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5647. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5648. result := SyntaxTree.invalidDesignator;
  5649. END;
  5650. ELSE
  5651. result := SyntaxTree.invalidDesignator
  5652. END;
  5653. END;
  5654. resolvedExpression := result;
  5655. END VisitParameterDesignator;
  5656. (** check dereference designator left^
  5657. - check if left is pointer type or left is object type
  5658. - return new dereference designator with type = left.baseType.type (if appropriate)
  5659. with error handling
  5660. returns invalidDesignator = invalidExpression if error
  5661. **)
  5662. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5663. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5664. BEGIN
  5665. result := SyntaxTree.invalidDesignator;
  5666. type := left.type;
  5667. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5668. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5669. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5670. result.SetAssignable(TRUE);
  5671. result.SetType(type);
  5672. result.SetHidden(left.isHidden);
  5673. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5674. type := type.resolved;
  5675. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5676. result.SetAssignable(TRUE);
  5677. result.SetType(type);
  5678. result.SetHidden(left.isHidden);
  5679. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5680. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5681. result.SetAssignable(TRUE);
  5682. result.SetType(type);
  5683. result.SetHidden(left.isHidden);
  5684. ELSE
  5685. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5686. IF VerboseErrorMessage THEN
  5687. Printout.Info("pointer", type);
  5688. Printout.Info("scope", currentScope);
  5689. END;
  5690. END;
  5691. RETURN result
  5692. END NewDereferenceDesignator;
  5693. (** check supercall designator left^
  5694. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5695. - return new supercall designator with type = left.type
  5696. with error handling
  5697. **)
  5698. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5699. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5700. objectScope: SyntaxTree.Scope;
  5701. BEGIN
  5702. result := SyntaxTree.invalidDesignator;
  5703. IF left = SyntaxTree.invalidDesignator THEN
  5704. (* error already handled *)
  5705. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5706. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5707. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5708. IF symbol IS SyntaxTree.Procedure THEN
  5709. procedure := symbol(SyntaxTree.Procedure);
  5710. objectScope := currentScope;
  5711. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5712. objectScope := objectScope.outerScope;
  5713. END;
  5714. IF (left.left = NIL) OR ~
  5715. (
  5716. (left.left IS SyntaxTree.SelfDesignator) OR
  5717. (left.left IS SyntaxTree.DereferenceDesignator)
  5718. & (left.left(SyntaxTree.Designator).left # NIL)
  5719. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5720. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5721. IF VerboseErrorMessage THEN
  5722. Printout.Info("left.left",left.left);
  5723. END;
  5724. ELSIF procedure.super # NIL THEN
  5725. result := SyntaxTree.NewSupercallDesignator(position,left);
  5726. result.SetType(left.type.resolved)
  5727. ELSE
  5728. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5729. END;
  5730. ELSE
  5731. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5732. END;
  5733. ELSE
  5734. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5735. END;
  5736. RETURN result
  5737. END NewSupercallDesignator;
  5738. (** check and semantically resolve arrow designator left^
  5739. - if left is procedure type -> result := SupercallDesignator
  5740. - else result := DereferenceDesignator
  5741. returns result via global variable resolvedExpression
  5742. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5743. **)
  5744. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5745. VAR left: SyntaxTree.Designator;
  5746. BEGIN
  5747. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5748. left := ResolveDesignator(arrowDesignator.left);
  5749. IF left # NIL THEN
  5750. IF (left.type = NIL) THEN
  5751. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5752. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5753. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5754. ELSE
  5755. IF IsPointerToObject(left.type) THEN
  5756. Warning(arrowDesignator.position, "forbidden dereference on object");
  5757. END;
  5758. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5759. END
  5760. END
  5761. END VisitArrowDesignator;
  5762. (** check and return expression
  5763. - if expression has no type then resolve expression
  5764. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5765. - return result
  5766. **)
  5767. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5768. VAR result,prev: SyntaxTree.Expression;
  5769. BEGIN
  5770. IF expression = NIL THEN result := NIL
  5771. ELSIF (expression.type = NIL) THEN
  5772. prev := resolvedExpression;
  5773. resolvedExpression := SyntaxTree.invalidExpression;
  5774. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5775. expression.SetType(SyntaxTree.invalidType);
  5776. END;
  5777. expression.Accept(SELF);
  5778. result := resolvedExpression;
  5779. IF currentIsRealtime THEN
  5780. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5781. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5782. END;
  5783. END;
  5784. (* designator modifiers for backends if they support it ...*)
  5785. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5786. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5787. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5788. END;
  5789. resolvedExpression := prev
  5790. ELSE
  5791. result := expression
  5792. END;
  5793. RETURN result
  5794. END ResolveExpression;
  5795. (**
  5796. check expression to be constant expression
  5797. - resolve expression
  5798. - if valid then check that of value type
  5799. report error and return invalidExpression if anything fails
  5800. **)
  5801. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5802. VAR position: LONGINT;
  5803. BEGIN
  5804. position := expression.position;
  5805. expression := ResolveExpression(expression);
  5806. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5807. ELSIF (expression.resolved = NIL) THEN
  5808. Error(position,Diagnostics.Invalid,"expression is not constant");
  5809. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5810. expression := SyntaxTree.invalidExpression;
  5811. END;
  5812. RETURN expression
  5813. END ConstantExpression;
  5814. (** check expression to be constant integer
  5815. - resolve expresssion
  5816. - if valid then check that of integer value type
  5817. report error and return invalidExpression if anything fails
  5818. **)
  5819. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5820. VAR position: LONGINT;
  5821. BEGIN
  5822. position := expression.position;
  5823. expression := ResolveExpression(expression);
  5824. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5825. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5826. expression := SyntaxTree.invalidExpression;
  5827. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5828. END;
  5829. RETURN expression
  5830. END ConstantInteger;
  5831. (** check expression as positive (>=0) constant integer
  5832. - resolve expression
  5833. - if valid then check that integer value
  5834. - if integer value then check that value >= 0
  5835. report error and return invalidExpression if anything fails
  5836. **)
  5837. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5838. VAR position: LONGINT;
  5839. BEGIN
  5840. position := expression.position;
  5841. expression := ConstantExpression(expression);
  5842. IF expression = SyntaxTree.invalidExpression THEN
  5843. (* error already reported *)
  5844. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5845. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5846. expression := SyntaxTree.invalidExpression
  5847. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5848. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5849. END;
  5850. RETURN expression
  5851. END ConstantIntegerGeq0;
  5852. (** check expression as condition
  5853. - resolve expression
  5854. - if valid expression then check that result type is boolean
  5855. report error and return invalidExpression if anything fails
  5856. **)
  5857. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5858. VAR position: LONGINT;
  5859. BEGIN
  5860. position := expression.position;
  5861. expression := ResolveExpression(expression);
  5862. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5863. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5864. expression := SyntaxTree.invalidExpression;
  5865. Error(position,Diagnostics.Invalid,"expression is not boolean");
  5866. END;
  5867. RETURN expression
  5868. END ResolveCondition;
  5869. (*** symbols ***)
  5870. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  5871. BEGIN
  5872. x.Accept(SELF);
  5873. END ResolveSymbol;
  5874. (** check a symbol
  5875. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5876. **)
  5877. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5878. VAR scope: SyntaxTree.Scope;
  5879. BEGIN
  5880. (* visibility *)
  5881. scope := symbol.scope;
  5882. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5883. scope := scope.outerScope;
  5884. END;
  5885. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5886. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5887. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  5888. IF VerboseErrorMessage THEN
  5889. Printout.Info("symbol",symbol);
  5890. END;
  5891. END;
  5892. END;
  5893. END CheckSymbolVisibility;
  5894. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5895. If node is currently being resolved then emit a cyclic definition error.
  5896. Return TRUE only if node is fully resolved.
  5897. **)
  5898. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5899. VAR result: BOOLEAN;
  5900. BEGIN
  5901. IF SyntaxTree.Resolved IN x.state THEN
  5902. result := FALSE
  5903. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5904. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  5905. result := FALSE;
  5906. ELSE
  5907. result := TRUE;
  5908. x.SetState(SyntaxTree.BeingResolved)
  5909. END;
  5910. RETURN result
  5911. END SymbolNeedsResolution;
  5912. (** check and resolve a type declaration symbol = Type
  5913. - set type to declaration type
  5914. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5915. This is so because the type declaration itself does not have a type but it only stands for a type.
  5916. In the implementation of the compiler this made a lot much easier.
  5917. - resolve and set declared type
  5918. - check symbol
  5919. **)
  5920. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5921. VAR prevScope: SyntaxTree.Scope;
  5922. BEGIN
  5923. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  5924. IF SymbolNeedsResolution(typeDeclaration) THEN
  5925. prevScope := currentScope;
  5926. currentScope := typeDeclaration.scope;
  5927. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  5928. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  5929. CheckSymbolVisibility(typeDeclaration);
  5930. typeDeclaration.SetState(SyntaxTree.Resolved);
  5931. currentScope := prevScope;
  5932. END;
  5933. END VisitTypeDeclaration;
  5934. (** check and resolve a constant declaration symbol = (constant) expression
  5935. - check expression
  5936. - set type and value
  5937. - check symbol
  5938. **)
  5939. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  5940. VAR
  5941. expression: SyntaxTree.Expression;
  5942. type: SyntaxTree.Type;
  5943. name: Basic.SegmentedName;
  5944. replacement: Replacement;
  5945. stringReader: Streams.StringReader;
  5946. scanner: Scanner.Scanner; parser: Parser.Parser;
  5947. BEGIN
  5948. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  5949. IF SymbolNeedsResolution(constant) THEN
  5950. expression := constant.value;
  5951. IF replacements # NIL THEN
  5952. Global.GetSymbolSegmentedName(constant, name);
  5953. replacement := replacements;
  5954. WHILE (replacement # NIL) & (replacement.name # name) DO
  5955. replacement := replacement.next;
  5956. END;
  5957. IF replacement # NIL THEN
  5958. InfoSS(constant.position, "replacing constant", constant.name);
  5959. (*
  5960. NEW(stringReader, Strings.Length(replacement.string^));
  5961. stringReader.Set(replacement.string^);
  5962. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  5963. NEW(parser, scanner, diagnostics);
  5964. expression := parser.Expression();
  5965. *)
  5966. expression := replacement.expression;
  5967. replacement.used := TRUE;
  5968. END;
  5969. END;
  5970. constant.SetType(SyntaxTree.invalidType);
  5971. expression := ConstantExpression(expression);
  5972. ASSERT(expression.type # NIL);
  5973. type := expression.type.resolved;
  5974. constant.SetType(type);
  5975. constant.SetValue(expression);
  5976. CheckSymbolVisibility(constant);
  5977. constant.SetState(SyntaxTree.Resolved);
  5978. END;
  5979. END VisitConstant;
  5980. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  5981. VAR procedureAlignment: LONGINT;
  5982. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  5983. (* least common multiple *)
  5984. VAR a,b: LONGINT;
  5985. BEGIN
  5986. a := a0; b := b0;
  5987. WHILE (a # b) DO
  5988. IF a < b THEN a := a+a0
  5989. ELSE b := b + b0
  5990. END;
  5991. END;
  5992. RETURN a
  5993. END LCM;
  5994. BEGIN
  5995. IF alignment > 1 THEN
  5996. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  5997. IF (procedureAlignment > 1) THEN
  5998. alignment := LCM(alignment, procedureAlignment);
  5999. END;
  6000. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6001. END;
  6002. END AdaptStackAlignment;
  6003. (** check and resolve a variable / field
  6004. - check and set type
  6005. - negative check on open array type
  6006. - check symbol
  6007. **)
  6008. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6009. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6010. BEGIN
  6011. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6012. IF SymbolNeedsResolution(variable) THEN
  6013. modifiers := variable.modifiers;
  6014. (*
  6015. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6016. variable.AddFlags(flags);
  6017. *)
  6018. variable.SetType(ResolveType(variable.type));
  6019. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6020. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6021. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6022. END;
  6023. END;
  6024. CheckSymbolVisibility(variable);
  6025. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6026. variable.SetUntraced(TRUE);
  6027. IF ~ContainsPointer(variable.type) THEN
  6028. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6029. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6030. END;
  6031. END;
  6032. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6033. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6034. IF ~PowerOf2(value) THEN
  6035. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6036. ELSE
  6037. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6038. END;
  6039. END;
  6040. variable.SetAlignment(FALSE,value);
  6041. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6042. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6043. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6044. END;
  6045. variable.SetAlignment(TRUE, value);
  6046. END;
  6047. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6048. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6049. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6050. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6051. END;
  6052. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6053. IF variable.initializer # NIL THEN
  6054. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6055. END;
  6056. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6057. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6058. pointerType.SetPointerBase(variable.type);
  6059. pointerType.SetHidden(TRUE);
  6060. variable.SetType(ResolveType(pointerType));
  6061. END;
  6062. variable.SetState(SyntaxTree.Resolved);
  6063. END;
  6064. END VisitVariable;
  6065. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6066. BEGIN
  6067. VisitVariable(property);
  6068. END VisitProperty;
  6069. (** check and resolve a (procedure) parameter
  6070. - check and set type
  6071. - check symbol
  6072. - check parameter kind and set read-only flags if appropriate
  6073. **)
  6074. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6075. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6076. BEGIN
  6077. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6078. IF SymbolNeedsResolution(parameter) THEN
  6079. modifiers := parameter.modifiers;
  6080. parameter.SetType(ResolveType(parameter.type));
  6081. ASSERT(parameter.type.resolved # NIL);
  6082. CheckSymbolVisibility(parameter);
  6083. IF parameter.defaultValue # NIL THEN
  6084. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6085. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6086. ELSE
  6087. expression := ConstantExpression(parameter.defaultValue);
  6088. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6089. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6090. parameter.SetDefaultValue(expression);
  6091. END;
  6092. END;
  6093. END;
  6094. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6095. parameter.SetUntraced(TRUE);
  6096. IF ~ContainsPointer(parameter.type) THEN
  6097. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6098. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6099. END;
  6100. END;
  6101. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6102. parameter.SetMoveable(TRUE);
  6103. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6104. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6105. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6106. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6107. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6108. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6109. END;
  6110. END;
  6111. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6112. parameter.SetState(SyntaxTree.Resolved);
  6113. END;
  6114. END VisitParameter;
  6115. (** check and resolve a procedure (with declaration and implementation scope)
  6116. - check the procedure type
  6117. - check if method (i.e. in record scope), if so then
  6118. - check if (unique) constructor
  6119. - check if (unique) finalizer
  6120. - check if super method available, if so then check signature
  6121. - of not in record scope then negative check on constructor flag
  6122. - of not in record scope then negative check on finalizer flag
  6123. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6124. - check procedure symbol
  6125. **)
  6126. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6127. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6128. procedureType: SyntaxTree.ProcedureType;
  6129. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6130. qualifiedType: SyntaxTree.QualifiedType;
  6131. value: LONGINT;
  6132. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6133. position: LONGINT;
  6134. BEGIN
  6135. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6136. IF IsOberonInline(procedure) THEN
  6137. IF SyntaxTree.Public * procedure.access # {} THEN
  6138. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6139. END;
  6140. procedure.SetInline(FALSE);
  6141. procedure.SetOberonInline(TRUE);
  6142. END;
  6143. IF SymbolNeedsResolution(procedure) THEN
  6144. recentIsRealtime := currentIsRealtime;
  6145. recentIsBodyProcedure := currentIsBodyProcedure;
  6146. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6147. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6148. modifiers := procedureType.modifiers;
  6149. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6150. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6151. IF useDarwinCCalls THEN (*fld*)
  6152. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6153. ELSE
  6154. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6155. END
  6156. END;
  6157. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6158. procedureType.SetInterrupt(TRUE);
  6159. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6160. END;
  6161. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6162. procedureType.SetNoReturn(TRUE);
  6163. END;
  6164. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6165. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6166. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6167. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6168. END;
  6169. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6170. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6171. END;
  6172. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6173. IF ~PowerOf2(value) THEN
  6174. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6175. ELSE
  6176. procedureType.SetStackAlignment(value)
  6177. END;
  6178. END;
  6179. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6180. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6181. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6182. END;
  6183. CheckModifiers(modifiers, TRUE);
  6184. modifiers := procedureType.returnTypeModifiers;
  6185. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6186. CheckModifiers(modifiers, TRUE);
  6187. FixProcedureType(procedureType);
  6188. currentIsRealtime := procedureType.isRealtime;
  6189. currentIsBodyProcedure := procedure.isBodyProcedure;
  6190. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6191. THEN
  6192. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6193. END;
  6194. CheckSymbolVisibility(procedure);
  6195. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6196. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6197. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6198. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6199. END;
  6200. END;
  6201. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6202. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6203. procedureType.SetDelegate(TRUE);
  6204. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6205. IF record.pointerType.typeDeclaration = NIL THEN
  6206. selfParameter.SetType(record.pointerType);
  6207. ELSE
  6208. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6209. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6210. qualifiedType.SetResolved(record.pointerType);
  6211. selfParameter.SetType(qualifiedType);
  6212. END;
  6213. selfParameter.SetAccess(SyntaxTree.Hidden);
  6214. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6215. IF procedure.isConstructor THEN
  6216. (*! constructor is always visible, compatibility to paco
  6217. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6218. *)
  6219. procedure.MarkUsed;
  6220. IF procedureType.returnType # NIL THEN
  6221. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6222. END;
  6223. proc := procedure.scope.firstProcedure;
  6224. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6225. proc := proc.nextProcedure;
  6226. END;
  6227. IF proc # NIL THEN
  6228. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6229. ELSE
  6230. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6231. END;
  6232. END;
  6233. IF procedure.isFinalizer THEN
  6234. procedure.MarkUsed;
  6235. IF procedureType.returnType # NIL THEN
  6236. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6237. END;
  6238. IF procedureType.numberParameters # 0 THEN
  6239. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6240. END;
  6241. proc := procedure.scope.firstProcedure;
  6242. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6243. proc := proc.nextProcedure;
  6244. END;
  6245. IF proc # NIL THEN
  6246. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6247. ELSE
  6248. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6249. END;
  6250. END;
  6251. super := FindSuperProcedure(record.recordScope, procedure);
  6252. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6253. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6254. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6255. END;
  6256. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6257. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6258. END;
  6259. IF super.isFinal THEN
  6260. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6261. END;
  6262. procedure.SetSuper(super);
  6263. super.SetOverwritten(TRUE);
  6264. procedure.SetAccess(procedure.access+super.access);
  6265. procedure.MarkUsed;
  6266. END;
  6267. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6268. THEN
  6269. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6270. END;
  6271. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6272. IF cellsAreObjects THEN
  6273. procedureType.SetDelegate(TRUE);
  6274. END;
  6275. IF procedure.isConstructor THEN
  6276. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6277. END;
  6278. ELSIF procedure.isConstructor THEN
  6279. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6280. END;
  6281. Declarations(procedure.procedureScope);
  6282. (* body resolution part done as late fix of the procedure type *)
  6283. procedure.SetState(SyntaxTree.Resolved);
  6284. currentIsRealtime := recentIsRealtime;
  6285. currentIsBodyProcedure := recentIsBodyProcedure;
  6286. END;
  6287. END VisitProcedure;
  6288. (**
  6289. a builtin procedure is a global item that may not be modified locally
  6290. instead the resolving of builtin procedure calls are done in the NewBuiltinCallDesignator
  6291. **)
  6292. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6293. VAR type: SyntaxTree.Type;
  6294. BEGIN
  6295. type := ResolveType(builtinProcedure.type);
  6296. END VisitBuiltin;
  6297. (* nopov *)
  6298. (** check and resolve operator
  6299. - operators are first checked as procedures
  6300. - then additional operator-specific checks are done
  6301. - note that only module-scope operators are checked here
  6302. (operators in a record scope are only allowed in the context of
  6303. array-structured object types and checked in 'ResolveArrayStructure')
  6304. - also note that inter-operator conformity is not checked here
  6305. **)
  6306. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6307. VAR
  6308. procedureType: SyntaxTree.ProcedureType;
  6309. leftType, rightType: SyntaxTree.Type;
  6310. identifierNumber, position: LONGINT;
  6311. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6312. modifiers: SyntaxTree.Modifier;
  6313. (** whether a type is locally defined in the current module scope
  6314. for arrays, the base type must be locally defined **)
  6315. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6316. BEGIN
  6317. IF type = NIL THEN
  6318. RETURN FALSE
  6319. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6320. RETURN TRUE
  6321. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6322. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6323. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6324. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6325. ELSE
  6326. RETURN FALSE
  6327. END
  6328. END IsLocallyDefined;
  6329. BEGIN
  6330. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6331. procedureType := operator.type(SyntaxTree.ProcedureType);
  6332. modifiers := procedureType.modifiers;
  6333. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6334. CheckModifiers(modifiers, TRUE);
  6335. VisitProcedure(operator);
  6336. IF operator.scope IS SyntaxTree.RecordScope THEN
  6337. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6338. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6339. IF identifierNumber = -1 THEN
  6340. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6341. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6342. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6343. ELSE
  6344. IF procedureType.numberParameters < 1 THEN
  6345. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6346. ELSIF procedureType.numberParameters > 2 THEN
  6347. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6348. ELSE
  6349. (* determine operand types *)
  6350. leftType := procedureType.firstParameter.type;
  6351. IF procedureType.numberParameters > 1 THEN
  6352. rightType := procedureType.firstParameter.nextParameter.type
  6353. ELSE
  6354. rightType := NIL
  6355. END;
  6356. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6357. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6358. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6359. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6360. END
  6361. END;
  6362. (* TODO: refine the checks, think about how restrictive the checks should be
  6363. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6364. They might be used for intersection, union, complement of custom object types *)
  6365. (* defaults *)
  6366. hasReturnType := TRUE;
  6367. mustBeUnary := FALSE;
  6368. mustBeBinary := FALSE;
  6369. mustReturnBoolean := FALSE;
  6370. mustReturnInteger := FALSE;
  6371. mustHaveEquitypedOperands := FALSE;
  6372. (* operator-specific exceptions *)
  6373. CASE identifierNumber OF
  6374. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6375. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6376. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6377. mustBeBinary := TRUE
  6378. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6379. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6380. | Scanner.Times: mustBeBinary := TRUE
  6381. | Scanner.TimesTimes: mustBeBinary := TRUE
  6382. | Scanner.DotTimes: mustBeBinary := TRUE
  6383. | Scanner.PlusTimes: mustBeBinary := TRUE
  6384. | Scanner.Slash: mustBeBinary := TRUE
  6385. | Scanner.Backslash: mustBeBinary := TRUE
  6386. | Scanner.DotSlash: mustBeBinary := TRUE
  6387. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6388. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6389. | Scanner.Not: mustBeUnary := TRUE
  6390. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6391. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6392. | Scanner.Transpose: mustBeUnary := TRUE;
  6393. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6394. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6395. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6396. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6397. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6398. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6399. | Global.Abs: mustBeUnary := TRUE;
  6400. | Global.Ash: (* TODO: arity? *)
  6401. | Global.Cap: (* TODO: arity? *)
  6402. | Global.Chr: mustBeUnary := TRUE;
  6403. | Global.Entier: (* TODO: arity? *)
  6404. | Global.EntierH: (* TODO: arity? *)
  6405. | Global.Len: (* unary and binary *)
  6406. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6407. | Global.Max, Global.Min: (* unary and binary *)
  6408. | Global.Odd: (* TODO: arity? *)
  6409. | Global.Sum: (* TODO: arity? *)
  6410. | Global.All: (* TODO: arity? *)
  6411. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6412. | Scanner.Alias:
  6413. | Scanner.GreaterGreater, Scanner.LessLess:
  6414. mustBeBinary := TRUE;
  6415. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6416. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6417. END;
  6418. (* check parameter count *)
  6419. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6420. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6421. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6422. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6423. END;
  6424. (* check parameter types *)
  6425. (* TODO: is this used at all? *)
  6426. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6427. leftType := procedureType.firstParameter.type;
  6428. rightType := procedureType.firstParameter.nextParameter.type;
  6429. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6430. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6431. END
  6432. END;
  6433. (* check return type *)
  6434. IF hasReturnType THEN
  6435. IF procedureType.returnType = NIL THEN
  6436. Error(operator.position, Diagnostics.Invalid, "return type required")
  6437. ELSIF mustReturnBoolean THEN
  6438. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6439. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6440. END
  6441. ELSIF mustReturnInteger THEN
  6442. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6443. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6444. END
  6445. END
  6446. ELSIF procedureType.returnType # NIL THEN
  6447. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6448. END
  6449. END
  6450. END
  6451. END
  6452. END VisitOperator;
  6453. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6454. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6455. BEGIN
  6456. IF error THEN RETURN FALSE END;
  6457. prevScope := currentScope;
  6458. prevDiagnostics := diagnostics;
  6459. diagnostics := NIL; (* suppress error output *)
  6460. currentScope := module.moduleScope;
  6461. VisitImport(x);
  6462. IF ~error THEN
  6463. module.moduleScope.AddImport(x);
  6464. x.SetScope(module.moduleScope);
  6465. END;
  6466. currentScope := prevScope;
  6467. diagnostics := prevDiagnostics;
  6468. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6469. END AddImport;
  6470. (** check and resolve import
  6471. - check for name = SYSTEM
  6472. - check for forbidden self import
  6473. - search through global import cache: already imported?
  6474. - check if already imported indirectly
  6475. - import if necessary -> set module and enter into import cache
  6476. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6477. - after this import this direct import and all indirect imports are stored in the current module's import list
  6478. **)
  6479. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6480. VAR
  6481. module: SyntaxTree.Module;
  6482. moduleScope: SyntaxTree.ModuleScope;
  6483. import,reimport: SyntaxTree.Import;
  6484. filename: FileName;
  6485. BEGIN
  6486. IF SymbolNeedsResolution(x) THEN
  6487. x.SetType(SyntaxTree.importType);
  6488. moduleScope := currentScope.ownerModule.moduleScope;
  6489. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6490. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6491. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6492. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6493. ELSE
  6494. (* search through global import list: already imported ? *)
  6495. IF (x.module = NIL) & (importCache # NIL) THEN
  6496. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6497. ELSE import := NIL
  6498. END;
  6499. IF x.module # NIL THEN (* already imported indirectly *)
  6500. module := x.module;
  6501. ELSIF import # NIL THEN (* already in module list *)
  6502. module := import.module;
  6503. ASSERT(module # NIL);
  6504. x.SetModule(module);
  6505. ELSE (* must be imported *)
  6506. Global.ModuleFileName(x.moduleName,x.context,filename);
  6507. IF symbolFileFormat # NIL THEN
  6508. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6509. IF module = NIL THEN
  6510. ErrorSS(x.position,"could not import",filename);
  6511. IF VerboseErrorMessage THEN
  6512. Printout.Info("import",x)
  6513. END
  6514. ELSE
  6515. (*
  6516. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6517. (*! should rather be done by importer *)
  6518. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6519. checker.importCache := importCache;
  6520. checker.arrayBaseImported := arrayBaseImported;
  6521. checker.global := global;
  6522. checker.Module(module); (* semantic check *)
  6523. error := error OR checker.error;
  6524. END;
  6525. *)
  6526. (*
  6527. ASSERT(SyntaxTree.Resolved IN module.state);
  6528. *)
  6529. x.SetModule(module);
  6530. IF importCache # NIL THEN
  6531. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6532. import.SetContext(x.context);
  6533. import.SetModule(module);
  6534. importCache.AddImport(import);
  6535. END;
  6536. END;
  6537. ELSE
  6538. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6539. END;
  6540. END;
  6541. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6542. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6543. END;
  6544. import := module.moduleScope.firstImport;
  6545. WHILE(import # NIL) DO
  6546. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6547. ASSERT(currentScope # NIL);
  6548. ASSERT(currentScope.ownerModule # NIL);
  6549. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6550. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6551. Error(x.position,Diagnostics.Invalid,"recursive import");
  6552. ELSE
  6553. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6554. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6555. IF reimport = NIL THEN (* indirect import *)
  6556. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6557. reimport.SetContext(import.context);
  6558. reimport.SetModule(import.module);
  6559. moduleScope.AddImport(reimport);
  6560. reimport.SetScope(moduleScope);
  6561. ELSE
  6562. ASSERT(import.module # NIL);
  6563. reimport.SetModule(import.module); (* direct or indirect import *)
  6564. END;
  6565. END;
  6566. import := import.nextImport;
  6567. END;
  6568. END;
  6569. END;
  6570. (* ELSE nothing to be done *)
  6571. x.SetState(SyntaxTree.Resolved);
  6572. END;
  6573. END VisitImport;
  6574. (*** statements ***)
  6575. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6576. VAR prev,resolved: SyntaxTree.Statement;
  6577. BEGIN
  6578. prev := resolvedStatement;
  6579. resolvedStatement := x;
  6580. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6581. activeCellsStatement := FALSE;
  6582. x.Accept(SELF);
  6583. (* removed this, implementation restriction should be resolved by backend
  6584. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6585. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6586. END;
  6587. *)
  6588. resolved := resolvedStatement;
  6589. resolvedStatement := prev;
  6590. RETURN resolved
  6591. END ResolveStatement;
  6592. (** check and resolve statement sequence
  6593. - check all statements, replace if necessary
  6594. **)
  6595. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6596. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6597. BEGIN
  6598. IF statementSequence # NIL THEN (* else empty *)
  6599. FOR i := 0 TO statementSequence.Length()-1 DO
  6600. statement := statementSequence.GetStatement(i);
  6601. resolved := ResolveStatement(statement);
  6602. IF (resolved # statement) THEN
  6603. statementSequence.SetStatement(i,resolved);
  6604. END;
  6605. END;
  6606. END;
  6607. END StatementSequence;
  6608. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6609. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6610. - check if procedure is callable
  6611. - check return type = NIL (otherwise must be assignment statement)
  6612. **)
  6613. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6614. VAR call: SyntaxTree.Designator;
  6615. BEGIN
  6616. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6617. call := procedureCall.call;
  6618. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6619. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6620. END;
  6621. call := ResolveDesignator(call);
  6622. IF call = SyntaxTree.invalidDesignator THEN
  6623. (* error already handled *)
  6624. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6625. (* inline call in a statement *)
  6626. ELSIF ~IsCallable(call) THEN
  6627. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6628. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6629. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6630. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6631. END;
  6632. procedureCall.SetCall(call);
  6633. (*
  6634. IF call = SyntaxTree.invalidDesignator THEN
  6635. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6636. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6637. IF IsOberonInline(procedure) THEN
  6638. Warning(procedure.position,"call to inline proc");
  6639. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6640. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6641. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6642. resolvedStatement := block;
  6643. RETURN;
  6644. END;
  6645. END;
  6646. *)
  6647. END VisitProcedureCallStatement;
  6648. (** check and resolve assignment LHS := RHS
  6649. - resolve LHS and RHS
  6650. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6651. - check if assignment is compatible
  6652. - check if LHS is variable (i.e. assignable)
  6653. - convert RHS if necessary
  6654. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6655. - assignment between different ASOTs
  6656. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6657. - assignment to ASOT elements:
  6658. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6659. **)
  6660. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6661. VAR
  6662. left: SyntaxTree.Designator;
  6663. right, expression: SyntaxTree.Expression;
  6664. designator: SyntaxTree.Designator;
  6665. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6666. mathArrayType: SyntaxTree.MathArrayType;
  6667. BEGIN
  6668. right := ResolveExpression(assignment.right);
  6669. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6670. left := ResolveDesignator(assignment.left);
  6671. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6672. (* error already handled *)
  6673. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6674. (* LHS is index write operator call on ASOT *)
  6675. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6676. (* necessary ?
  6677. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6678. type := procedureType.firstParameter.type;
  6679. expression := procedureCallDesignator.parameters.GetExpression(0);
  6680. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6681. *)
  6682. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6683. ELSIF CheckVariable(left) THEN
  6684. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6685. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6686. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6687. (* conversion done by procedure call
  6688. (* try to convert to left argument *)
  6689. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6690. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6691. procedureCallDesignator.parameters.SetExpression(1, right);
  6692. END;
  6693. *)
  6694. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6695. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6696. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6697. ELSIF AssignmentCompatible(left, right) THEN
  6698. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6699. mathArrayType := MathArrayStructureOfType(left.type);
  6700. right := NewConversion(right.position, right, mathArrayType, NIL);
  6701. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6702. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6703. ELSE
  6704. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6705. assignment.SetLeft(left);
  6706. assignment.SetRight(right);
  6707. resolvedStatement := assignment
  6708. END
  6709. END
  6710. END
  6711. END VisitAssignment;
  6712. (** check and resolve assignment LHS := RHS
  6713. - resolve LHS and RHS
  6714. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6715. - check if assignment is compatible
  6716. - check if LHS is variable (i.e. assignable)
  6717. - convert RHS if necessary
  6718. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6719. - assignment between different ASOTs
  6720. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6721. - assignment to ASOT elements:
  6722. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6723. **)
  6724. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6725. VAR
  6726. left: SyntaxTree.Designator;
  6727. right: SyntaxTree.Expression;
  6728. inPort, outPort: SyntaxTree.PortType;
  6729. BEGIN
  6730. right := ResolveExpression(communication.right);
  6731. left := ResolveDesignator(communication.left);
  6732. communication.SetLeft(left);
  6733. communication.SetRight(right);
  6734. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6735. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6736. (* error already handled *)
  6737. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6738. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6739. IF outPort.direction # SyntaxTree.OutPort THEN
  6740. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6741. ELSIF outPort.sizeInBits # system.SizeOf(right.type) THEN
  6742. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6743. ELSE
  6744. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6745. communication.SetRight(right)
  6746. END;
  6747. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6748. IF CheckVariable(left) THEN
  6749. IF inPort.direction # SyntaxTree.InPort THEN
  6750. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6751. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6752. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6753. END;
  6754. END;
  6755. ELSE
  6756. Error(communication.position, -1, "unsupported stream operation");
  6757. END;
  6758. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,outPort) THEN
  6759. IF outPort.direction # SyntaxTree.OutPort THEN
  6760. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6761. ELSIF outPort.sizeInBits # system.SizeOf(right.type) THEN
  6762. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6763. ELSE
  6764. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6765. communication.SetRight(right)
  6766. END;
  6767. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,inPort) THEN
  6768. IF CheckVariable(right) THEN
  6769. IF inPort.direction # SyntaxTree.InPort THEN
  6770. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6771. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6772. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6773. END;
  6774. END;
  6775. ELSE
  6776. Error(communication.position, -1, "unsupported operation");
  6777. END
  6778. END VisitCommunicationStatement;
  6779. (** check and resolve if/eslif part
  6780. - check condition
  6781. - check statement sequence
  6782. **)
  6783. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6784. VAR prevUnreachable, b: BOOLEAN;
  6785. BEGIN
  6786. prevUnreachable := currentIsUnreachable;
  6787. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6788. IF IsBooleanValue(ifPart.condition,b) THEN
  6789. IF b=FALSE THEN
  6790. currentIsUnreachable := TRUE
  6791. ELSIF b=TRUE THEN
  6792. true := TRUE
  6793. END;
  6794. END;
  6795. StatementSequence(ifPart.statements);
  6796. currentIsUnreachable := prevUnreachable;
  6797. END IfPart;
  6798. (** check and resolve if statement
  6799. - check if parts and else part statement sequence
  6800. **)
  6801. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6802. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6803. BEGIN
  6804. prevUnreachable := currentIsUnreachable;
  6805. ifPartTrue := FALSE;
  6806. IfPart(ifStatement.ifPart,ifPartTrue);
  6807. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6808. elsif := ifStatement.GetElsifPart(i);
  6809. IfPart(elsif,ifPartTrue);
  6810. END;
  6811. IF ifStatement.elsePart # NIL THEN
  6812. IF ifPartTrue THEN
  6813. currentIsUnreachable := TRUE
  6814. END;
  6815. StatementSequence(ifStatement.elsePart)
  6816. END;
  6817. currentIsUnreachable := prevUnreachable;
  6818. END VisitIfStatement;
  6819. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6820. VAR variable: SyntaxTree.Designator;
  6821. type,variableType: SyntaxTree.Type;
  6822. withEntry: WithEntry;
  6823. BEGIN
  6824. variable := ResolveDesignator(withPart.variable);
  6825. variableType := variable.type.resolved;
  6826. withPart.SetVariable(variable);
  6827. type := ResolveType(withPart.type);
  6828. withPart.SetType(type);
  6829. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6830. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6831. END;
  6832. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6833. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6834. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6835. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6836. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6837. IF VerboseErrorMessage THEN
  6838. Printout.Info("variable",variable)
  6839. END;
  6840. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6841. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  6842. IF VerboseErrorMessage THEN
  6843. Printout.Info("variable",variable);
  6844. Printout.Info("type",type);
  6845. END;
  6846. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6847. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6848. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  6849. IF VerboseErrorMessage THEN
  6850. Printout.Info("variable",variable);
  6851. Printout.Info("type",type);
  6852. END;
  6853. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6854. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  6855. ELSE
  6856. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6857. NEW(withEntry);
  6858. withEntry.previous := withEntries;
  6859. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6860. withEntry.type := type;
  6861. withEntries := withEntry;
  6862. StatementSequence(withPart.statements);
  6863. withEntries := withEntries.previous;
  6864. END;
  6865. END WithPart;
  6866. (** check and resolve with statement WITH variable: type DO ... END;
  6867. - check type and variable
  6868. - check that variable type is type extension of type
  6869. - check that variable is a variable
  6870. - enter new with scope and enter guardedVariable with same name and reference to variable
  6871. - create if statement:
  6872. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6873. **)
  6874. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  6875. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  6876. BEGIN
  6877. prevScope := currentScope; symbol := NIL;
  6878. FOR i := 0 TO withStatement.WithParts()-1 DO
  6879. WithPart(withStatement.GetWithPart(i),symbol);
  6880. END;
  6881. IF withStatement.elsePart # NIL THEN
  6882. StatementSequence(withStatement.elsePart)
  6883. END;
  6884. currentScope := prevScope;
  6885. END VisitWithStatement;
  6886. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6887. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6888. - check 'first' < 'last' and no overlaps between different case labels
  6889. - check statement sequence
  6890. **)
  6891. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  6892. VAR
  6893. i: LONGINT;
  6894. position: LONGINT;
  6895. expression, left, right: SyntaxTree.Expression;
  6896. expressionType: SyntaxTree.Type;
  6897. l, r: LONGINT;
  6898. cl, cr: CHAR;
  6899. thiscases: SyntaxTree.CaseConstant;
  6900. BEGIN
  6901. thiscases := NIL;
  6902. FOR i := 0 TO casePart.elements.Length() - 1 DO
  6903. expression := casePart.elements.GetExpression(i);
  6904. position := expression.position;
  6905. (* set context of range *)
  6906. IF expression IS SyntaxTree.RangeExpression THEN
  6907. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  6908. END;
  6909. expression := ResolveExpression(expression);
  6910. IF expression = SyntaxTree.invalidExpression THEN
  6911. (* error already reported *)
  6912. expressionType := SyntaxTree.invalidType;
  6913. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  6914. (* read out 'first' and 'last' *)
  6915. left := expression(SyntaxTree.RangeExpression).first;
  6916. right := expression(SyntaxTree.RangeExpression).last;
  6917. (* guaranteed by VisitRangeExpression: *)
  6918. ASSERT((left # NIL) & (right # NIL));
  6919. ASSERT(left.type.resolved = right.type.resolved);
  6920. left := CompatibleConversion(left.position, left, type);
  6921. right := CompatibleConversion(right.position, right, type);
  6922. expression(SyntaxTree.RangeExpression).SetFirst(left);
  6923. expression(SyntaxTree.RangeExpression).SetLast(right);
  6924. expressionType := RegularType(position,left.type);
  6925. ELSE
  6926. expression := ConstantExpression(expression);
  6927. expression := CompatibleConversion(expression.position, expression, type);
  6928. (*
  6929. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  6930. left := Global.NewCharacterValue(system,expression.position,cl);
  6931. expression := casePart.elements.GetExpression(i);
  6932. expression.SetResolved(left(SyntaxTree.CharacterValue));
  6933. expression := left
  6934. END;
  6935. *)
  6936. casePart.elements.SetExpression(i,expression);
  6937. left := expression; right := expression;
  6938. expressionType := RegularType(position,expression.type)
  6939. END;
  6940. IF (expressionType = SyntaxTree.invalidType) THEN
  6941. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  6942. Error(position,Diagnostics.Invalid,"inadmissible case label");
  6943. expression := SyntaxTree.invalidExpression;
  6944. ELSE
  6945. l := 0; r := 0;
  6946. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  6947. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  6948. l := ORD(cl); r := ORD(cr);
  6949. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  6950. ELSE
  6951. expression := SyntaxTree.invalidExpression
  6952. END;
  6953. IF expression # SyntaxTree.invalidExpression THEN
  6954. IF l>r THEN
  6955. Error(position,Diagnostics.Invalid,"empty case label")
  6956. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  6957. Error(position,Diagnostics.Invalid,"duplicate case label");
  6958. ELSE
  6959. IF l < min THEN min := l END;
  6960. IF r > max THEN max := r END;
  6961. END;
  6962. END;
  6963. END;
  6964. casePart.elements.SetExpression(i,expression);
  6965. END;
  6966. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  6967. casePart.SetConstants(thiscases);
  6968. StatementSequence(casePart.statements);
  6969. END CasePart;
  6970. (** check and resolve case statement CASE variable OF ... END;
  6971. - check variable
  6972. - check case parts
  6973. **)
  6974. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  6975. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  6976. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  6977. BEGIN
  6978. expression := ResolveExpression(caseStatement.variable);
  6979. type := RegularType(expression.position,expression.type);
  6980. IF type = SyntaxTree.invalidType THEN
  6981. expression := SyntaxTree.invalidExpression;
  6982. ELSIF IsIntegerType(type) THEN
  6983. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  6984. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  6985. (*
  6986. expression := Global.NewCharacterValue(system,expression.position,ch);
  6987. *)
  6988. type := expression.type;
  6989. ELSIF IsCharacterType(type) THEN
  6990. ELSIF IsEnumerationType(type) THEN
  6991. ELSE
  6992. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  6993. expression := SyntaxTree.invalidExpression;
  6994. END;
  6995. caseStatement.SetVariable(expression);
  6996. caseList := NIL;
  6997. min := MAX(LONGINT); max := MIN(LONGINT);
  6998. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  6999. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7000. END;
  7001. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7002. msg := "huge sparse case table ";
  7003. Strings.AppendInt(msg, max-min);
  7004. Strings.Append(msg,"/");
  7005. Strings.AppendInt(msg, caseStatement.CaseParts());
  7006. Warning(caseStatement.position,msg);
  7007. END;
  7008. caseStatement.SetMinMax(min,max);
  7009. StatementSequence(caseStatement.elsePart);
  7010. IF expression.resolved # NIL THEN
  7011. IF IsCharacterValue(expression,ch) THEN
  7012. l := ORD(ch)
  7013. ELSIF IsIntegerValue(expression,l) THEN
  7014. END;
  7015. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7016. END;
  7017. END VisitCaseStatement;
  7018. (** check and resolve while statement
  7019. - check condition
  7020. - check statement sequence
  7021. **)
  7022. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7023. VAR prevIsUnreachable,b: BOOLEAN;
  7024. BEGIN
  7025. prevIsUnreachable := currentIsUnreachable;
  7026. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7027. IF IsBooleanValue(whileStatement.condition,b) THEN
  7028. IF b=FALSE THEN
  7029. currentIsUnreachable := TRUE
  7030. END;
  7031. END;
  7032. StatementSequence(whileStatement.statements);
  7033. currentIsUnreachable := prevIsUnreachable
  7034. END VisitWhileStatement;
  7035. (** check and resolve repeat statement
  7036. - check condition
  7037. - check statement sequence
  7038. **)
  7039. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7040. BEGIN
  7041. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7042. StatementSequence(repeatStatement.statements);
  7043. END VisitRepeatStatement;
  7044. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7045. VAR withEntry: WithEntry;
  7046. BEGIN
  7047. withEntry := withEntries;
  7048. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7049. withEntry := withEntry.previous
  7050. END;
  7051. IF withEntry = NIL THEN RETURN FALSE
  7052. ELSE
  7053. type := withEntry.type;
  7054. RETURN TRUE
  7055. END;
  7056. END GetGuard;
  7057. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7058. - check that variable is an integer variable
  7059. - check that from is integer typed with compatible type
  7060. - check that to has compatible type
  7061. - check that by is constant integer with compatible type
  7062. **)
  7063. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7064. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7065. BEGIN
  7066. designator := ResolveDesignator(forStatement.variable);
  7067. type := SyntaxTree.invalidType;
  7068. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7069. designator := SyntaxTree.invalidDesignator;
  7070. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7071. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7072. designator := SyntaxTree.invalidDesignator;
  7073. ELSIF CheckVariable(designator) THEN
  7074. type := designator.type;
  7075. END;
  7076. forStatement.SetVariable(designator);
  7077. expression := ResolveExpression(forStatement.from);
  7078. IF expression = SyntaxTree.invalidExpression THEN
  7079. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7080. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7081. expression := SyntaxTree.invalidExpression;
  7082. ELSIF type # SyntaxTree.invalidType THEN
  7083. expression := NewConversion(expression.position,expression,type,NIL)
  7084. END;
  7085. forStatement.SetFrom(expression);
  7086. expression := ResolveExpression(forStatement.to);
  7087. IF expression = SyntaxTree.invalidExpression THEN
  7088. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7089. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7090. expression := SyntaxTree.invalidExpression;
  7091. ELSIF type # SyntaxTree.invalidType THEN
  7092. expression := NewConversion(expression.position,expression,type,NIL)
  7093. END;
  7094. forStatement.SetTo(expression);
  7095. IF forStatement.by # NIL THEN
  7096. expression := ConstantInteger(forStatement.by);
  7097. ELSE
  7098. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7099. END;
  7100. IF expression = SyntaxTree.invalidExpression THEN
  7101. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7102. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7103. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7104. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7105. ELSIF type # SyntaxTree.invalidType THEN
  7106. expression := NewConversion(expression.position,expression,type,NIL)
  7107. END;
  7108. forStatement.SetBy(expression);
  7109. StatementSequence(forStatement.statements);
  7110. END VisitForStatement;
  7111. (** check and resolve loop statement LOOP StatementSequence END
  7112. - check statement sequence
  7113. **)
  7114. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7115. BEGIN
  7116. StatementSequence(loopStatement.statements)
  7117. END VisitLoopStatement;
  7118. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7119. BEGIN
  7120. StatementSequence(exitableBlock.statements);
  7121. END VisitExitableBlock;
  7122. (** check and resolve exit statement EXIT
  7123. - check that exit is within LOOP statement block
  7124. **)
  7125. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7126. VAR outer: SyntaxTree.Statement;
  7127. BEGIN
  7128. outer := exitStatement.outer;
  7129. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7130. outer := outer.outer;
  7131. END;
  7132. IF outer = NIL THEN
  7133. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7134. END;
  7135. END VisitExitStatement;
  7136. (** check and resolve return statement RETURN [expression]
  7137. - check expression (if any)
  7138. - check if in procedure scope
  7139. - if in procedure scope then check expression compatibility
  7140. - if not in procecdure scope then check on return without expression
  7141. **)
  7142. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7143. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7144. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7145. BEGIN
  7146. position := returnStatement.position;
  7147. expression := returnStatement.returnValue;
  7148. IF expression # NIL THEN
  7149. expression := ResolveExpression(expression);
  7150. returnStatement.SetReturnValue(expression);
  7151. END;
  7152. outer := returnStatement.outer;
  7153. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7154. outer := outer.outer
  7155. END;
  7156. IF (outer # NIL) THEN
  7157. scope := outer(SyntaxTree.Body).inScope;
  7158. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7159. IF (expression # NIL) THEN
  7160. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7161. END;
  7162. ELSE
  7163. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7164. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7165. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7166. END;
  7167. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7168. IF returnType # NIL THEN
  7169. returnType := returnType.resolved;
  7170. IF expression = NIL THEN
  7171. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7172. ELSIF expression.type = NIL THEN
  7173. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7174. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7175. Error(position,Diagnostics.Invalid,"return type not compatible");
  7176. IF VerboseErrorMessage THEN
  7177. Printout.Info("returnType",returnType);
  7178. Printout.Info("expression",expression);
  7179. END;
  7180. ELSE
  7181. expression := NewConversion(expression.position,expression,returnType,NIL);
  7182. returnStatement.SetReturnValue(expression);
  7183. END;
  7184. ELSIF expression # NIL THEN
  7185. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7186. END;
  7187. END;
  7188. END;
  7189. END VisitReturnStatement;
  7190. (** check and resolve await statement AWAIT(condition: Expression)
  7191. - check await condition
  7192. **)
  7193. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7194. VAR condition: SyntaxTree.Expression;
  7195. BEGIN
  7196. condition := ResolveCondition(awaitStatement.condition);
  7197. IF currentIsRealtime THEN
  7198. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7199. END;
  7200. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7201. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7202. END;
  7203. awaitStatement.SetCondition(condition);
  7204. END VisitAwaitStatement;
  7205. PROCEDURE CheckSystemImport(position: LONGINT);
  7206. VAR import: SyntaxTree.Import;
  7207. BEGIN
  7208. import := currentScope.ownerModule.moduleScope.firstImport;
  7209. WHILE(import # NIL) DO
  7210. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7211. RETURN;
  7212. END;
  7213. import := import.nextImport;
  7214. END;
  7215. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7216. END CheckSystemImport;
  7217. (** check and resolve code statement: do nothing, must be done by assembler
  7218. **)
  7219. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7220. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7221. BEGIN
  7222. CheckSystemImport(code.position);
  7223. FOR i := 0 TO code.inRules.Length()-1 DO
  7224. statement := code.inRules.GetStatement(i);
  7225. IF statement IS SyntaxTree.Assignment THEN
  7226. WITH statement: SyntaxTree.Assignment DO
  7227. statement.SetRight(ResolveExpression(statement.right));
  7228. END;
  7229. ELSE
  7230. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7231. END;
  7232. END;
  7233. FOR i := 0 TO code.outRules.Length()-1 DO
  7234. statement := code.outRules.GetStatement(i);
  7235. IF statement IS SyntaxTree.Assignment THEN
  7236. WITH statement: SyntaxTree.Assignment DO
  7237. statement.SetLeft(ResolveDesignator(statement.left));
  7238. END;
  7239. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7240. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7241. ELSE
  7242. Printout.Info("out statement ", statement);
  7243. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7244. END;
  7245. END;
  7246. END VisitCode;
  7247. (** check and set flags of a statement block
  7248. - check for multiply occurence of a flag
  7249. - check and set priority only in bodies
  7250. - check for valid names
  7251. **)
  7252. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7253. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7254. flag: LONGINT; recordBody: SyntaxTree.Body;
  7255. PROCEDURE SetProtectedRecord;
  7256. VAR scope: SyntaxTree.Scope;
  7257. BEGIN
  7258. scope := currentScope;
  7259. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7260. scope := scope.outerScope
  7261. END;
  7262. IF scope # NIL THEN
  7263. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7264. END;
  7265. END SetProtectedRecord;
  7266. BEGIN
  7267. flags := {};
  7268. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7269. recordBody := block(SyntaxTree.Body)
  7270. ELSE
  7271. recordBody := NIL
  7272. END;
  7273. blockModifier := block.blockModifiers;
  7274. WHILE(blockModifier # NIL) DO
  7275. name := blockModifier.identifier;
  7276. expression := blockModifier.expression;
  7277. position := blockModifier.position;
  7278. flag := -1;
  7279. IF name=Global.NamePriority THEN
  7280. IF expression = NIL THEN
  7281. Error(position,Diagnostics.Invalid,"missing priority expression");
  7282. ELSIF recordBody = NIL THEN
  7283. Error(position,Diagnostics.Invalid,"priority not on record body");
  7284. ELSIF recordBody.priority # NIL THEN
  7285. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7286. ELSE
  7287. recordBody.SetPriority(expression);
  7288. END;
  7289. ELSIF expression # NIL THEN
  7290. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7291. ELSIF name=Global.NameExclusive THEN
  7292. IF block.isExclusive THEN
  7293. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7294. END;
  7295. block.SetExclusive(TRUE); SetProtectedRecord;
  7296. ELSIF name=Global.NameActive THEN
  7297. IF recordBody = NIL THEN
  7298. Error(position,Diagnostics.Invalid,"active not in record body");
  7299. ELSIF recordBody.isActive THEN
  7300. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7301. ELSE
  7302. recordBody.SetActive(TRUE); SetProtectedRecord;
  7303. END;
  7304. ELSIF name=Global.NameSafe THEN
  7305. IF recordBody = NIL THEN
  7306. Error(position,Diagnostics.Invalid,"safe not in record body");
  7307. ELSIF recordBody.isSafe THEN
  7308. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7309. ELSE
  7310. recordBody.SetSafe(TRUE);
  7311. SetProtectedRecord;
  7312. END;
  7313. ELSIF name=Global.NameRealtime THEN
  7314. IF recordBody = NIL THEN
  7315. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7316. ELSIF recordBody.isRealtime THEN
  7317. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7318. ELSE
  7319. recordBody.SetRealtime(TRUE);
  7320. block.SetRealtime(TRUE);
  7321. END;
  7322. ELSIF name=Global.NameUnchecked THEN
  7323. IF block.isUnchecked THEN
  7324. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7325. ELSE
  7326. block.SetUnchecked(TRUE);
  7327. END;
  7328. ELSIF (name=Global.NameUncooperative) THEN
  7329. IF block.isUncooperative THEN
  7330. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7331. ELSE
  7332. block.SetUncooperative(TRUE);
  7333. END;
  7334. ELSE
  7335. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7336. END;
  7337. blockModifier := blockModifier.nextModifier;
  7338. END;
  7339. END BlockFlags;
  7340. (** check and resolve statement block
  7341. - check flags (exclusive)
  7342. - check statement sequence
  7343. **)
  7344. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7345. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7346. BEGIN
  7347. BlockFlags(statementBlock);
  7348. IF statementBlock.isExclusive THEN
  7349. (* check that not in exclusive block *)
  7350. IF currentIsExclusive THEN
  7351. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7352. ELSIF currentIsRealtime THEN
  7353. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7354. END;
  7355. END;
  7356. recentExclusive := currentIsExclusive;
  7357. recentUnreachable := currentIsUnreachable;
  7358. recentRealtime := currentIsRealtime;
  7359. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7360. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7361. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7362. StatementSequence(statementBlock.statements);
  7363. currentIsRealtime := recentRealtime;
  7364. currentIsExclusive := recentExclusive;
  7365. currentIsUnreachable := recentUnreachable;
  7366. END VisitStatementBlock;
  7367. (** check and resolve body
  7368. - check flags (active, priority, safe)
  7369. - check body and finally part
  7370. **)
  7371. PROCEDURE Body(body: SyntaxTree.Body);
  7372. BEGIN
  7373. VisitStatementBlock(body);
  7374. IF body.isActive THEN
  7375. IF ~currentIsBodyProcedure THEN
  7376. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7377. ELSIF body.priority # NIL THEN
  7378. body.SetPriority(ConstantInteger(body.priority));
  7379. END;
  7380. ELSIF body.isSafe THEN
  7381. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7382. ELSIF body.priority # NIL THEN
  7383. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7384. END;
  7385. IF body.code # NIL THEN
  7386. CheckSystemImport(body.position);
  7387. END;
  7388. StatementSequence(body.finally)
  7389. END Body;
  7390. (*** scopes ***)
  7391. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7392. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7393. VAR duplicateSymbol: BOOLEAN;
  7394. BEGIN
  7395. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7396. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7397. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7398. END;
  7399. scope.EnterSymbol(symbol,duplicateSymbol);
  7400. IF ~allowDuplicate & duplicateSymbol THEN
  7401. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7402. IF VerboseErrorMessage THEN
  7403. Printout.Info("multiply defined identifier",symbol);
  7404. Printout.Info("in scope",scope);
  7405. END;
  7406. END;
  7407. END Register;
  7408. (**
  7409. implementation: check and resolve an implementation part
  7410. **)
  7411. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7412. move implementation checker to a separate object ? *)
  7413. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7414. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7415. BEGIN
  7416. prevIsRealtime := currentIsRealtime;
  7417. prevIsBodyProcedure := currentIsBodyProcedure;
  7418. prevIsCellNet := currentIsCellNet;
  7419. prevScope := currentScope;
  7420. currentScope := scope;
  7421. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7422. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7423. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7424. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7425. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7426. (*
  7427. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7428. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7429. END;
  7430. *)
  7431. END;
  7432. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7433. (* module body, record bodies are wrapped into an artifical procedure *)
  7434. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7435. Body(scope(SyntaxTree.ProcedureScope).body)
  7436. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7437. Body(scope(SyntaxTree.ProcedureScope).body)
  7438. END;
  7439. END;
  7440. currentScope := prevScope;
  7441. currentIsRealtime := prevIsRealtime;
  7442. currentIsBodyProcedure := prevIsBodyProcedure;
  7443. currentIsCellNet := prevIsCellNet;
  7444. END Implementation;
  7445. (** implementation phase:
  7446. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7447. **)
  7448. PROCEDURE Implementations(x: SyntaxTree.Module);
  7449. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7450. BEGIN
  7451. prevPhase := phase;
  7452. phase := InlinePhase;
  7453. scope := x.firstScope;
  7454. WHILE(scope # NIL) DO
  7455. Implementation(scope);
  7456. scope := scope.nextScope;
  7457. END;
  7458. phase := ImplementationPhase;
  7459. scope := x.firstScope;
  7460. WHILE(scope # NIL) DO
  7461. Implementation(scope);
  7462. scope := scope.nextScope;
  7463. END;
  7464. phase := prevPhase;
  7465. END Implementations;
  7466. (** declaration phase:
  7467. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7468. - import lists (for module scopes)
  7469. - parameter list (for procedure scopes)
  7470. - constant declarations
  7471. - type declarations
  7472. - variable declarations
  7473. - procedure declarations
  7474. preformed in two stages:
  7475. - first all symbols are entered into the symbol table (with uniqueness check),
  7476. - then all symbols are resolved
  7477. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7478. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7479. **)
  7480. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7481. VAR
  7482. constant: SyntaxTree.Constant;
  7483. typeDeclaration: SyntaxTree.TypeDeclaration;
  7484. variable: SyntaxTree.Variable;
  7485. procedure: SyntaxTree.Procedure;
  7486. prevScope: SyntaxTree.Scope;
  7487. parameter: SyntaxTree.Parameter;
  7488. import: SyntaxTree.Import;
  7489. symbol: SyntaxTree.Symbol;
  7490. prevPhase: LONGINT;
  7491. prevError : BOOLEAN;
  7492. property: SyntaxTree.Property;
  7493. type: SyntaxTree.Type;
  7494. atype : SyntaxTree.ArrayType;
  7495. BEGIN
  7496. prevError := error;
  7497. prevPhase := phase;
  7498. phase := DeclarationPhase;
  7499. prevScope := currentScope;
  7500. currentScope := scope;
  7501. error := FALSE;
  7502. (* first enter all symbols in scope *)
  7503. IF scope IS SyntaxTree.ModuleScope THEN
  7504. (* treat imports first for a module scope, , set default context if necessary *)
  7505. import := scope(SyntaxTree.ModuleScope).firstImport;
  7506. WHILE(import # NIL) DO
  7507. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7508. Register(import, currentScope, FALSE);
  7509. import := import.nextImport;
  7510. END;
  7511. import := scope(SyntaxTree.ModuleScope).firstImport;
  7512. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7513. ResolveSymbol(import);
  7514. import := import.nextImport;
  7515. END;
  7516. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7517. (* enter parameters for a procedure scope *)
  7518. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7519. WHILE(parameter # NIL) DO
  7520. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7521. END;
  7522. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7523. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7524. ELSIF scope IS SyntaxTree.CellScope THEN
  7525. parameter := scope(SyntaxTree.CellScope).ownerCell.firstParameter;
  7526. WHILE(parameter # NIL) DO
  7527. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7528. variable.SetType(parameter.type);
  7529. variable.SetAccess(SyntaxTree.Hidden);
  7530. variable.SetModifiers(parameter.modifiers);
  7531. currentScope.PushVariable(variable);
  7532. (*
  7533. Register(parameter,scope, FALSE);
  7534. *)
  7535. parameter := parameter.nextParameter;
  7536. END;
  7537. property := scope(SyntaxTree.CellScope).ownerCell.firstProperty;
  7538. WHILE (property # NIL) DO
  7539. property.SetAccess(SyntaxTree.Hidden);
  7540. currentScope.PushVariable(property);
  7541. property := property.nextProperty;
  7542. END;
  7543. END;
  7544. IF error THEN RETURN END;
  7545. (* constants *)
  7546. constant := scope.firstConstant;
  7547. WHILE (constant # NIL) DO
  7548. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7549. END;
  7550. (* type declarations *)
  7551. typeDeclaration := scope.firstTypeDeclaration;
  7552. WHILE (typeDeclaration # NIL) DO
  7553. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7554. END;
  7555. (* variables *)
  7556. variable := scope.firstVariable;
  7557. WHILE (variable # NIL) DO
  7558. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7559. END;
  7560. (* procedures *)
  7561. procedure := scope.firstProcedure;
  7562. WHILE (procedure # NIL) DO
  7563. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7564. END;
  7565. (* now process all symbols without any presumption on the order *)
  7566. symbol := scope.firstSymbol;
  7567. WHILE(symbol # NIL) DO
  7568. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7569. ResolveSymbol(symbol);
  7570. END;
  7571. symbol := symbol.nextSymbol;
  7572. END;
  7573. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7574. symbol := scope.firstSymbol;
  7575. WHILE symbol # NIL DO
  7576. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7577. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7578. pointerFixes.Add(symbol, currentScope);
  7579. END;
  7580. IF ~symbol.type.resolved.isRealtime THEN
  7581. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7582. END;
  7583. END;
  7584. symbol := symbol.nextSymbol
  7585. END;
  7586. END;
  7587. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7588. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7589. END;
  7590. IF scope.ownerModule # NIL THEN
  7591. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7592. scope.ownerModule.AddScope(scope);
  7593. END;
  7594. phase := prevPhase;
  7595. currentScope := prevScope;
  7596. error := error OR prevError;
  7597. END Declarations;
  7598. (* nopov *)
  7599. (** check if all operators from one module are compatible to the ones in the other module
  7600. - check if there are not multiple operators with the same signature
  7601. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7602. - check for all operators whose signatures are compatible, whether the return types are compatible
  7603. note that:
  7604. - the return type is not considered to be part of the signature
  7605. - two signatures are considered compatible, if all of the operands are compatible
  7606. **)
  7607. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7608. VAR
  7609. thisOperator, thatOperator: SyntaxTree.Operator;
  7610. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7611. thisParameter, thatParameter: SyntaxTree.Parameter;
  7612. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7613. i: LONGINT;
  7614. BEGIN
  7615. currentScope := thisModuleScope;
  7616. hasError := FALSE;
  7617. (* go through all operators in the other module *)
  7618. thatOperator := thatModuleScope.firstOperator;
  7619. WHILE (thatOperator # NIL) & ~hasError DO
  7620. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7621. (* the other operator is accessible *)
  7622. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7623. (* the other operator is not the conversion operator *)
  7624. (* go through all operators in this module *)
  7625. thisOperator := thisModuleScope.firstOperator;
  7626. WHILE (thisOperator # NIL) & ~hasError DO
  7627. IF thisOperator # thatOperator THEN
  7628. (* the operators are not the same *)
  7629. IF thisOperator.name = thatOperator.name THEN
  7630. (* the operators share the same identifier *)
  7631. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7632. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7633. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7634. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7635. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7636. (* both operators have the same paramter count *)
  7637. thisParameter := thisProcedureType.firstParameter;
  7638. thatParameter := thatProcedureType.firstParameter;
  7639. operandsAreEqual := TRUE;
  7640. operandsAreCompatible := TRUE;
  7641. (* go through all parameters *)
  7642. FOR i := 1 TO thisProcedureType.numberParameters DO
  7643. ASSERT(thatParameter # NIL);
  7644. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7645. operandsAreEqual := FALSE;
  7646. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7647. operandsAreCompatible := FALSE
  7648. END
  7649. END;
  7650. thisParameter := thisParameter.nextParameter;
  7651. thatParameter := thatParameter.nextParameter
  7652. END;
  7653. IF operandsAreEqual THEN
  7654. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7655. hasError := TRUE
  7656. ELSIF operandsAreCompatible THEN
  7657. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7658. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7659. hasError := TRUE
  7660. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7661. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7662. hasError := TRUE
  7663. END
  7664. END
  7665. END
  7666. END
  7667. END;
  7668. thisOperator := thisOperator.nextOperator
  7669. END
  7670. END
  7671. END;
  7672. thatOperator := thatOperator.nextOperator
  7673. END
  7674. END CheckInterOperatorConformity;
  7675. (** check module:
  7676. - check module declaration
  7677. - add context, if necessary
  7678. - remove module from import cache, if necessary
  7679. - check declarations
  7680. - resolve all type fixes
  7681. - check implementation (bodies)
  7682. **)
  7683. PROCEDURE Module*(x: SyntaxTree.Module);
  7684. VAR (* nopov *)
  7685. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7686. BEGIN
  7687. prevScope := currentScope;
  7688. prevIsCellNet := currentIsCellNet;
  7689. module := x;
  7690. ASSERT(x # NIL);
  7691. global := system.globalScope[x.case];
  7692. x.moduleScope.SetGlobalScope(global);
  7693. currentScope := global;
  7694. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7695. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7696. RemoveModuleFromCache(importCache,x);
  7697. Declarations(x.moduleScope);
  7698. FixTypes();
  7699. IF module.isCellNet THEN
  7700. currentIsCellNet := TRUE;
  7701. modifier := x.modifiers;
  7702. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7703. CheckModifiers(modifier, FALSE);
  7704. END;
  7705. (* nopov *)
  7706. IF ~error THEN
  7707. (* check if operators conform to each other within this module *)
  7708. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7709. (* go through all imports *)
  7710. import := x.moduleScope.firstImport;
  7711. WHILE import # NIL DO
  7712. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7713. (* check if all operators in this module conform to the ones of the imported module *)
  7714. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7715. END;
  7716. import := import.nextImport
  7717. END;
  7718. END;
  7719. Implementations(x);
  7720. module := NIL;
  7721. currentIsCellNet := prevIsCellNet;
  7722. currentScope := prevScope;
  7723. END Module;
  7724. END Checker;
  7725. Warnings*=OBJECT (SyntaxTree.Visitor)
  7726. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7727. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7728. BEGIN
  7729. SELF.diagnostics := diagnostics
  7730. END InitWarnings;
  7731. (** types *)
  7732. PROCEDURE Type(x: SyntaxTree.Type);
  7733. BEGIN x.Accept(SELF)
  7734. END Type;
  7735. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7736. BEGIN END VisitType;
  7737. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7738. BEGIN END VisitBasicType;
  7739. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7740. BEGIN END VisitCharacterType;
  7741. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7742. BEGIN END VisitIntegerType;
  7743. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7744. BEGIN END VisitFloatType;
  7745. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7746. BEGIN END VisitQualifiedType;
  7747. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7748. BEGIN END VisitStringType;
  7749. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7750. BEGIN END VisitEnumerationType;
  7751. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7752. BEGIN END VisitRangeType;
  7753. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7754. BEGIN
  7755. IF ~(SyntaxTree.Warned IN x.state) THEN
  7756. x.SetState(SyntaxTree.Warned);
  7757. Type(x.arrayBase);
  7758. END;
  7759. END VisitArrayType;
  7760. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7761. BEGIN
  7762. IF ~(SyntaxTree.Warned IN x.state) THEN
  7763. x.SetState(SyntaxTree.Warned);
  7764. Type(x.arrayBase);
  7765. END;
  7766. END VisitMathArrayType;
  7767. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7768. BEGIN
  7769. IF ~(SyntaxTree.Warned IN x.state) THEN
  7770. x.SetState(SyntaxTree.Warned);
  7771. Type(x.pointerBase);
  7772. END;
  7773. END VisitPointerType;
  7774. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7775. BEGIN Scope(x.recordScope) END VisitRecordType;
  7776. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7777. BEGIN Scope(x.cellScope) END VisitCellType;
  7778. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7779. BEGIN END VisitProcedureType;
  7780. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7781. VAR msg: ARRAY 256 OF CHAR;
  7782. BEGIN
  7783. Global.GetSymbolName(x,msg);
  7784. Strings.Append(msg," ");
  7785. Strings.Append(msg,text);
  7786. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7787. END Warning;
  7788. (** symbols *)
  7789. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7790. BEGIN
  7791. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7792. IF ~(x IS SyntaxTree.Parameter) THEN
  7793. Warning(x,"never used");
  7794. END;
  7795. END;
  7796. x.Accept(SELF);
  7797. END Symbol;
  7798. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7799. BEGIN END VisitSymbol;
  7800. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7801. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7802. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7803. BEGIN END VisitConstant;
  7804. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7805. BEGIN END VisitVariable;
  7806. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  7807. BEGIN END VisitProperty;
  7808. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  7809. BEGIN END VisitParameter;
  7810. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  7811. BEGIN Scope(x.procedureScope) END VisitProcedure;
  7812. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  7813. BEGIN END VisitOperator;
  7814. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  7815. BEGIN END VisitImport;
  7816. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7817. VAR
  7818. symbol: SyntaxTree.Symbol;
  7819. BEGIN
  7820. symbol := scope.firstSymbol;
  7821. WHILE(symbol # NIL) DO
  7822. Symbol(symbol);
  7823. symbol := symbol.nextSymbol;
  7824. END;
  7825. END Scope;
  7826. PROCEDURE Module*(x: SyntaxTree.Module);
  7827. BEGIN
  7828. SELF.module := x;
  7829. Scope(x.moduleScope);
  7830. END Module;
  7831. END Warnings;
  7832. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7833. BEGIN
  7834. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7835. END IsOberonInline;
  7836. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7837. BEGIN
  7838. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7839. END Resolved;
  7840. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  7841. VAR i: LONGINT;
  7842. BEGIN
  7843. i := 1;
  7844. WHILE i < x DO
  7845. i := i *2
  7846. END;
  7847. RETURN i=x
  7848. END PowerOf2;
  7849. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7850. BEGIN
  7851. RETURN
  7852. (scope # NIL) &
  7853. (scope IS SyntaxTree.ModuleScope)
  7854. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7855. OR
  7856. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7857. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7858. END IsCellNetScope;
  7859. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7860. BEGIN
  7861. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7862. END IsCellScope;
  7863. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7864. BEGIN
  7865. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7866. RETURN (scope # NIL) & IsCellNetScope(scope)
  7867. END InCellNetScope;
  7868. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7869. BEGIN
  7870. ASSERT(size MOD system.dataUnit = 0);
  7871. RETURN size DIV system.dataUnit
  7872. END ToMemoryUnits;
  7873. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7874. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7875. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7876. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  7877. BEGIN
  7878. IF t = NIL THEN
  7879. RETURN TRUE
  7880. ELSE
  7881. t := t.resolved;
  7882. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  7883. END;
  7884. END TypeAllowed;
  7885. BEGIN
  7886. type := type.resolved;
  7887. IF ~(type IS SyntaxTree.ProcedureType) THEN
  7888. RETURN FALSE
  7889. ELSE
  7890. procedureType := type(SyntaxTree.ProcedureType);
  7891. numberParameters := procedureType.numberParameters;
  7892. RETURN
  7893. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  7894. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  7895. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  7896. END;
  7897. END GetProcedureAllowed;
  7898. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  7899. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  7900. VAR import: SyntaxTree.Import;
  7901. BEGIN
  7902. import := importCache.ImportByModuleName(x.name,x.context);
  7903. IF import # NIL THEN
  7904. importCache.RemoveImporters(x.name,x.context);
  7905. END;
  7906. END RemoveModuleFromCache;
  7907. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  7908. (* to <- this assignment compatibility *)
  7909. VAR result: BOOLEAN;
  7910. BEGIN
  7911. IF this= NIL THEN result := (to=NIL)
  7912. ELSIF to=NIL THEN result := FALSE
  7913. ELSE
  7914. (*! will be replaced by this:
  7915. ELSE result := this.CompatibleTo(to.resolved);
  7916. *)
  7917. this := this.resolved; to := to.resolved;
  7918. IF to=SyntaxTree.invalidType THEN result := FALSE
  7919. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  7920. ELSIF to = this THEN
  7921. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  7922. ELSIF to IS SyntaxTree.BasicType THEN
  7923. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  7924. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  7925. result := this.CompatibleTo(to.resolved)
  7926. ELSE
  7927. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  7928. END
  7929. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  7930. result := to.sizeInBits = this.sizeInBits;
  7931. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  7932. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  7933. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  7934. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  7935. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  7936. result := TRUE;
  7937. ELSIF to IS SyntaxTree.AnyType THEN
  7938. result := (this IS SyntaxTree.RecordType) & this(SyntaxTree.RecordType).isObject OR (this IS SyntaxTree.PointerType) OR (this IS SyntaxTree.ProcedureType) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.AnyType) OR (this IS SyntaxTree.ObjectType);
  7939. ELSIF to IS SyntaxTree.ObjectType THEN
  7940. result := IsPointerToRecord(this) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ObjectType) OR (this IS SyntaxTree.AnyType) (*! remove when symbol file can distinguish OBJECT from ANY *) ;
  7941. ELSIF to IS SyntaxTree.ByteType THEN
  7942. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  7943. ELSIF to IS SyntaxTree.CharacterType THEN
  7944. result := IsCharacterType(this)
  7945. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  7946. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  7947. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this)) THEN
  7948. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  7949. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  7950. result := TRUE;
  7951. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  7952. result := TRUE;
  7953. ELSE
  7954. result := FALSE
  7955. END;
  7956. ELSIF to IS SyntaxTree.PointerType THEN
  7957. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  7958. IsPointerType(this) & (IsTypeExtension(to,this) OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  7959. & (~to.isRealtime OR this.isRealtime);
  7960. ELSIF to IS SyntaxTree.ProcedureType THEN
  7961. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  7962. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  7963. & (~to.isRealtime OR this.isRealtime)
  7964. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  7965. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  7966. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  7967. ELSIF to IS SyntaxTree.RecordType THEN
  7968. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  7969. ELSIF to IS SyntaxTree.ArrayType THEN
  7970. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  7971. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  7972. ELSIF StaticArrayCompatible(to, this) THEN
  7973. result := TRUE
  7974. ELSE
  7975. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  7976. END;
  7977. ELSIF to IS SyntaxTree.MathArrayType THEN
  7978. IF this IS SyntaxTree.MathArrayType THEN
  7979. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  7980. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7981. result := TRUE;
  7982. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  7983. result := TRUE;
  7984. ELSE
  7985. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  7986. END;
  7987. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  7988. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  7989. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  7990. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  7991. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  7992. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  7993. (* ARRAY [x] OF <- ARRAY [x] OF *)
  7994. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  7995. ELSE
  7996. result := FALSE
  7997. END;
  7998. (* an array-structured object type is compatible to the type of its array structure *)
  7999. ELSIF IsArrayStructuredObjectType(this) THEN
  8000. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8001. ELSE
  8002. result := FALSE;
  8003. END;
  8004. ELSIF to IS SyntaxTree.StringType THEN
  8005. result := FALSE;
  8006. ELSIF to IS SyntaxTree.EnumerationType THEN
  8007. result := IsEnumerationExtension(this,to);
  8008. ELSIF to IS SyntaxTree.PortType THEN
  8009. result := SameType(to, this)
  8010. ELSE
  8011. Printout.Info("CompatibleTo",to);
  8012. HALT(100); (* implement missing type check *)
  8013. END;
  8014. END;
  8015. RETURN result
  8016. END CompatibleTo;
  8017. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8018. VAR actualBase, formalBase: SyntaxTree.Type;
  8019. BEGIN
  8020. IF SameType(formal,actual) THEN
  8021. RETURN TRUE
  8022. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8023. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8024. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8025. RETURN
  8026. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8027. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8028. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8029. & StaticArrayCompatible(formalBase,actualBase)
  8030. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8031. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8032. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8033. RETURN
  8034. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8035. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8036. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8037. & StaticArrayCompatible(formalBase,actualBase)
  8038. ELSE RETURN FALSE
  8039. END;
  8040. END StaticArrayCompatible;
  8041. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8042. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8043. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8044. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8045. BEGIN
  8046. result := SameType(formal,actual);
  8047. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8048. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8049. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8050. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8051. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8052. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8053. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8054. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8055. & TC(formalBase, actualBase);
  8056. END;
  8057. RETURN result
  8058. END TC;
  8059. BEGIN
  8060. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8061. ELSE
  8062. arrayBase := formalType.arrayBase.resolved;
  8063. IF (actualType IS SyntaxTree.StringType) THEN
  8064. result := arrayBase IS SyntaxTree.CharacterType
  8065. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8066. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8067. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8068. result := TC(formalType, actualType);
  8069. ELSE
  8070. result := (arrayBase IS SyntaxTree.ByteType)
  8071. END;
  8072. END;
  8073. RETURN result
  8074. END OpenArrayCompatible;
  8075. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8076. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8077. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8078. BEGIN
  8079. IF actualType IS SyntaxTree.MathArrayType THEN
  8080. actualArray := actualType(SyntaxTree.MathArrayType);
  8081. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8082. (*
  8083. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8084. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8085. *)
  8086. actualBase := ArrayBase(actualType,Infinity);
  8087. formalBase := ArrayBase(formalType,Infinity);
  8088. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8089. ELSE
  8090. (*
  8091. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8092. *)
  8093. formalBase := Resolved(formalType.arrayBase);
  8094. actualBase := Resolved(actualArray.arrayBase);
  8095. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8096. (*
  8097. ARRAY [k] -> ARRAY [n]
  8098. *)
  8099. result := (formalType.staticLength = actualArray.staticLength)
  8100. ELSE
  8101. result := TRUE
  8102. END;
  8103. IF ~result THEN
  8104. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8105. ELSIF actualBase = NIL THEN result := FALSE
  8106. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8107. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8108. ELSE
  8109. result := SameType(formalBase,actualBase)
  8110. END;
  8111. END;
  8112. ELSE
  8113. result := FALSE
  8114. END;
  8115. RETURN result
  8116. END MathArrayCompatible;
  8117. (**
  8118. Math Array Type distance for assignments / parameter passings of the form
  8119. from -> to
  8120. variants:
  8121. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8122. allowed:
  8123. static -> static (& size match)
  8124. static -> open
  8125. static -> tensor
  8126. open -> open
  8127. open -> tensor
  8128. open -> static
  8129. tensor -> tensor
  8130. tensor -> open
  8131. tensor -> static
  8132. **)
  8133. (*! think about the metric here: is form matching more important than element type matching? *)
  8134. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8135. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8136. BEGIN
  8137. fromBase := Resolved(from.arrayBase);
  8138. toBase := Resolved(to.arrayBase);
  8139. i := Infinity;
  8140. IF from = to THEN
  8141. i := 0;
  8142. ELSIF (from.form = to.form) THEN
  8143. (* static -> static, open -> open, tensor -> tensor *)
  8144. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8145. IF fromBase = toBase THEN i := 0
  8146. ELSIF toBase = NIL THEN i := 1
  8147. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8148. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8149. ELSE
  8150. i := TypeDistance(system,fromBase, toBase, varpar);
  8151. END;
  8152. END;
  8153. ELSIF (to.form = SyntaxTree.Static) THEN
  8154. (* forbidden *)
  8155. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8156. (* static -> tensor, open -> tensor, tensor -> open *)
  8157. IF toBase=fromBase THEN i := 0;
  8158. ELSIF toBase = NIL THEN i := 1;
  8159. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8160. toBase := ArrayBase(toBase,Infinity);
  8161. IF (fromBase=toBase) THEN i := 0
  8162. ELSIF (toBase = NIL) THEN i:= 1
  8163. ELSIF (fromBase = NIL) THEN i := Infinity;
  8164. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8165. END;
  8166. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8167. fromBase := ArrayBase(fromBase,Infinity);
  8168. IF (fromBase=toBase) THEN i := 0
  8169. ELSIF (toBase = NIL) THEN i := 1
  8170. ELSIF (fromBase = NIL) THEN i := Infinity;
  8171. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8172. END;
  8173. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8174. END;
  8175. IF i # Infinity THEN INC(i,2) END;
  8176. ELSIF (from.form = SyntaxTree.Static) THEN
  8177. (* static -> open *)
  8178. IF toBase=fromBase THEN i := 0
  8179. ELSIF toBase = NIL THEN i := 1
  8180. ELSIF fromBase = NIL THEN i := Infinity
  8181. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8182. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8183. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8184. END;
  8185. IF i # Infinity THEN INC(i,1) END;
  8186. ELSE HALT(100); (* unknown case *)
  8187. END;
  8188. RETURN i;
  8189. END MathArrayTypeDistance;
  8190. (** compute and return the distance of two array types
  8191. - return the distance of the base types
  8192. **)
  8193. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8194. VAR i: LONGINT;
  8195. BEGIN
  8196. i := Infinity;
  8197. IF from = to THEN
  8198. i := 0
  8199. ELSE
  8200. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8201. (*
  8202. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8203. i := TypeDistance(from.base, to.base);
  8204. IF i >= 0 THEN INC(i) END
  8205. ELSIF (from.mode = open) & (to.mode = open) THEN
  8206. i := TypeDistance(from.base, to.base);
  8207. *)
  8208. END;
  8209. RETURN i
  8210. END ArrayTypeDistance;
  8211. (** compute the signature distance of a procedure and an actual parameter list
  8212. - if any of the parameters are not compatible, the result is infinite
  8213. - add up and return the distance over all parameters
  8214. **)
  8215. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8216. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8217. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8218. BEGIN
  8219. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8220. result := Infinity
  8221. ELSE
  8222. formalParameter := procedureType.firstParameter;
  8223. i := 0;
  8224. result := 0;
  8225. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8226. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8227. actualParameter := actualParameters.GetExpression(i);
  8228. ASSERT(formalParameter.type # NIL);
  8229. IF (actualParameter.type = NIL) THEN distance := Infinity
  8230. ELSE
  8231. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8232. END;
  8233. IF distance = Infinity THEN
  8234. result := Infinity;
  8235. ELSE
  8236. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8237. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8238. (* already handled varpar *)
  8239. (*
  8240. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8241. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8242. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8243. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8244. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8245. END;
  8246. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8247. result := Infinity
  8248. END;
  8249. *)
  8250. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8251. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8252. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8253. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8254. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8255. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8256. END;
  8257. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8258. result := Infinity
  8259. END;
  8260. ELSE
  8261. result := Infinity
  8262. END;
  8263. ELSE
  8264. INC(result,distance);
  8265. END;
  8266. END;
  8267. formalParameter := formalParameter.nextParameter; INC(i);
  8268. END;
  8269. END;
  8270. ASSERT(result >= 0);
  8271. RETURN result
  8272. END Distance;
  8273. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8274. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8275. BEGIN
  8276. IF right.numberParameters # (procedureType.numberParameters) THEN
  8277. result := Infinity
  8278. ELSE
  8279. formalParameter := procedureType.firstParameter;
  8280. rightParameter := right.firstParameter;
  8281. i := 0;
  8282. result := 0;
  8283. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8284. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8285. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8286. IF distance = Infinity THEN
  8287. result := Infinity;
  8288. ELSE
  8289. INC(result,distance);
  8290. END;
  8291. formalParameter := formalParameter.nextParameter;
  8292. rightParameter := rightParameter.nextParameter;
  8293. END;
  8294. END;
  8295. ASSERT(result >= 0);
  8296. RETURN result
  8297. END ProcedureTypeDistance;
  8298. (** compute and return the distance between two types, used for computation of signature distance
  8299. from -> to
  8300. **)
  8301. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8302. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8303. BEGIN
  8304. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8305. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8306. END;
  8307. i := Infinity;
  8308. IF from = to THEN
  8309. i := 0
  8310. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8311. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8312. i := Infinity;
  8313. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8314. i := 1
  8315. ELSIF (from IS SyntaxTree.StringType) THEN
  8316. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8317. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8318. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8319. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8320. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8321. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8322. i := 1
  8323. ELSIF (from IS SyntaxTree.NilType) THEN
  8324. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8325. (*
  8326. ELSIF (from = NoType) THEN
  8327. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8328. *)
  8329. ELSIF (from IS SyntaxTree.BasicType) THEN
  8330. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8331. IF varpar & (i # 0) THEN i := Infinity END;
  8332. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8333. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8334. ELSIF (from IS SyntaxTree.RecordType) THEN
  8335. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8336. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8337. IF to IS SyntaxTree.MathArrayType THEN
  8338. (*
  8339. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8340. i := Infinity;
  8341. ELSE
  8342. *)
  8343. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8344. (*
  8345. END;
  8346. *)
  8347. END
  8348. ELSIF (from IS SyntaxTree.PointerType) THEN
  8349. ptr := from(SyntaxTree.PointerType);
  8350. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8351. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8352. (* ELSE i := TypeDistance(ptr.base, to); *)
  8353. END
  8354. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8355. IF (to IS SyntaxTree.ProcedureType) THEN
  8356. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8357. END;
  8358. (*no procedure test, procedure must be the same*)
  8359. END;
  8360. RETURN i
  8361. END TypeDistance;
  8362. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8363. BEGIN
  8364. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8365. END IsIntegerType;
  8366. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8367. BEGIN
  8368. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8369. END IsAddressType;
  8370. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8371. BEGIN
  8372. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8373. END IsSizeType;
  8374. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8375. BEGIN
  8376. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8377. END IsSignedIntegerType;
  8378. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8379. BEGIN
  8380. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8381. END IsUnsignedIntegerType;
  8382. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8383. VAR result: BOOLEAN;
  8384. BEGIN
  8385. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8386. value := x.resolved(SyntaxTree.IntegerValue).value;
  8387. result := TRUE
  8388. ELSE
  8389. result := FALSE
  8390. END;
  8391. RETURN result
  8392. END IsIntegerValue;
  8393. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8394. VAR result: BOOLEAN;
  8395. BEGIN
  8396. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8397. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8398. result := TRUE
  8399. ELSE
  8400. result := FALSE
  8401. END;
  8402. RETURN result
  8403. END IsEnumerationValue;
  8404. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8405. VAR result: BOOLEAN;
  8406. BEGIN
  8407. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8408. value := x.resolved(SyntaxTree.RealValue).value;
  8409. result := TRUE
  8410. ELSE
  8411. result := FALSE
  8412. END;
  8413. RETURN result
  8414. END IsRealValue;
  8415. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8416. VAR result: BOOLEAN;
  8417. BEGIN
  8418. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8419. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8420. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8421. result := TRUE
  8422. ELSE
  8423. result := FALSE
  8424. END;
  8425. RETURN result
  8426. END IsComplexValue;
  8427. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8428. VAR result: BOOLEAN;
  8429. BEGIN
  8430. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8431. value := x.resolved(SyntaxTree.CharacterValue).value;
  8432. result := TRUE
  8433. ELSE
  8434. result := FALSE
  8435. END;
  8436. RETURN result
  8437. END IsCharacterValue;
  8438. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8439. VAR result: BOOLEAN;
  8440. BEGIN
  8441. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8442. value := x.resolved(SyntaxTree.BooleanValue).value;
  8443. result := TRUE
  8444. ELSE
  8445. result := FALSE
  8446. END;
  8447. RETURN result
  8448. END IsBooleanValue;
  8449. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8450. VAR result: BOOLEAN;
  8451. BEGIN
  8452. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8453. value := x.resolved(SyntaxTree.SetValue).value;
  8454. result := TRUE
  8455. ELSE
  8456. result := FALSE
  8457. END;
  8458. RETURN result
  8459. END IsSetValue;
  8460. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8461. VAR result: BOOLEAN;
  8462. BEGIN
  8463. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8464. value := x.resolved(SyntaxTree.StringValue).value;
  8465. result := TRUE
  8466. ELSE
  8467. result := FALSE
  8468. END;
  8469. RETURN result
  8470. END IsStringValue;
  8471. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8472. BEGIN
  8473. x := x.resolved;
  8474. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8475. END Indexable;
  8476. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8477. BEGIN
  8478. RETURN t1.SameType(t2.resolved);
  8479. END SameType;
  8480. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8481. BEGIN
  8482. IF t IS SyntaxTree.MathArrayType THEN
  8483. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8484. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8485. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8486. DEC(max);
  8487. END;
  8488. ELSIF t IS SyntaxTree.ArrayType THEN
  8489. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8490. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8491. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8492. END;
  8493. END;
  8494. RETURN t;
  8495. END ArrayBase;
  8496. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8497. BEGIN
  8498. type := type.resolved;
  8499. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8500. base := type(SyntaxTree.ArrayType).arrayBase;
  8501. RETURN TRUE;
  8502. END;
  8503. RETURN FALSE;
  8504. END IsOpenArray;
  8505. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8506. BEGIN
  8507. type := type.resolved;
  8508. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8509. base := type(SyntaxTree.ArrayType).arrayBase;
  8510. dim := type(SyntaxTree.ArrayType).staticLength;
  8511. RETURN TRUE
  8512. ELSE
  8513. RETURN FALSE
  8514. END;
  8515. END IsStaticArray;
  8516. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8517. BEGIN
  8518. type := type.resolved;
  8519. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8520. base := type(SyntaxTree.ArrayType).arrayBase;
  8521. RETURN TRUE
  8522. ELSE
  8523. RETURN FALSE
  8524. END;
  8525. END IsDynamicArray;
  8526. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8527. VAR i: LONGINT;
  8528. BEGIN
  8529. i := 0;
  8530. t := t.resolved;
  8531. IF t IS SyntaxTree.MathArrayType THEN
  8532. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8533. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8534. END;
  8535. ELSIF t IS SyntaxTree.ArrayType THEN
  8536. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8537. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8538. END;
  8539. END;
  8540. RETURN i
  8541. END Dimension;
  8542. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8543. BEGIN
  8544. RETURN expression.assignable;
  8545. END IsVariable;
  8546. PROCEDURE IsVariableParameter (symbol: SyntaxTree.Symbol): BOOLEAN;
  8547. BEGIN
  8548. IF (symbol IS SyntaxTree.Parameter) THEN
  8549. WITH symbol: SyntaxTree.Parameter DO
  8550. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8551. END;
  8552. ELSE
  8553. RETURN FALSE
  8554. END;
  8555. END IsVariableParameter;
  8556. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8557. VAR result: BOOLEAN;
  8558. BEGIN
  8559. IF type = NIL THEN result := FALSE
  8560. ELSE
  8561. type := type.resolved;
  8562. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8563. END;
  8564. RETURN result
  8565. END IsPointerType;
  8566. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8567. VAR result: BOOLEAN;
  8568. BEGIN
  8569. IF type = NIL THEN result := FALSE
  8570. ELSE
  8571. type := type.resolved;
  8572. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8573. END;
  8574. RETURN result
  8575. END IsUnsafePointer;
  8576. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8577. BEGIN
  8578. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8579. END IsDisposable;
  8580. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8581. VAR result: BOOLEAN;
  8582. BEGIN
  8583. IF type = NIL THEN result := FALSE
  8584. ELSE
  8585. type := type.resolved;
  8586. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8587. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8588. result := result OR (type IS SyntaxTree.ObjectType);
  8589. END;
  8590. RETURN result
  8591. END IsPointerToRecord;
  8592. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8593. VAR result: BOOLEAN;
  8594. BEGIN
  8595. IF type = NIL THEN result := FALSE
  8596. ELSE
  8597. type := type.resolved;
  8598. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8599. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8600. ;
  8601. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8602. result := result OR (type IS SyntaxTree.ObjectType);
  8603. END;
  8604. RETURN result
  8605. END IsPointerToObject;
  8606. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8607. BEGIN
  8608. IF type # NIL THEN
  8609. RETURN type.resolved.hasPointers
  8610. ELSE
  8611. RETURN FALSE
  8612. END;
  8613. END ContainsPointer;
  8614. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8615. BEGIN
  8616. IF type = NIL THEN RETURN FALSE END;
  8617. type := type.resolved;
  8618. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8619. END IsStringType;
  8620. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8621. BEGIN
  8622. IF type = NIL THEN RETURN FALSE END;
  8623. type := type.resolved;
  8624. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8625. END IsCharacterType;
  8626. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8627. BEGIN
  8628. IF type = NIL THEN RETURN FALSE END;
  8629. type := type.resolved;
  8630. RETURN (type IS SyntaxTree.EnumerationType)
  8631. END IsEnumerationType;
  8632. (** cf. section "Type extension (base type)" in the language report **)
  8633. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8634. VAR result: BOOLEAN;
  8635. BEGIN
  8636. ASSERT(base # NIL); ASSERT(extension # NIL);
  8637. base := base.resolved; extension := extension.resolved;
  8638. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8639. result := TRUE;
  8640. ELSE
  8641. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8642. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8643. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8644. END;
  8645. WHILE (extension # NIL) & (extension # base) DO
  8646. IF extension IS SyntaxTree.RecordType THEN
  8647. extension := extension(SyntaxTree.RecordType).baseType;
  8648. IF (extension # NIL) THEN extension := extension.resolved END;
  8649. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8650. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8651. END;
  8652. ELSE extension := NIL;
  8653. END;
  8654. END;
  8655. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8656. END;
  8657. RETURN result
  8658. END IsTypeExtension;
  8659. (** check if base is the base enumeration type of extension **)
  8660. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8661. BEGIN
  8662. base := base.resolved; extension := extension.resolved;
  8663. WHILE (extension # NIL) & (extension # base) DO
  8664. IF extension IS SyntaxTree.EnumerationType THEN
  8665. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8666. IF extension # NIL THEN extension := extension.resolved END;
  8667. ELSE
  8668. extension := NIL
  8669. END;
  8670. END;
  8671. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8672. END IsEnumerationExtension;
  8673. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8674. BEGIN
  8675. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8676. RETURN TRUE
  8677. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8678. RETURN TRUE
  8679. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8680. RETURN TRUE
  8681. ELSE
  8682. RETURN FALSE
  8683. END
  8684. END IsCallable;
  8685. (** compute and return the distance of two record types
  8686. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8687. **)
  8688. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8689. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8690. BEGIN
  8691. i := 0;
  8692. WHILE (from # NIL) & (from # to) DO
  8693. baseType := from.baseType;
  8694. IF (baseType # NIL) THEN
  8695. baseType := baseType.resolved;
  8696. IF baseType IS SyntaxTree.PointerType THEN
  8697. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8698. END;
  8699. IF baseType IS SyntaxTree.RecordType THEN
  8700. from := baseType(SyntaxTree.RecordType);
  8701. ELSE
  8702. from := NIL;
  8703. END;
  8704. ELSE
  8705. from := NIL
  8706. END;
  8707. INC(i)
  8708. END;
  8709. IF from = NIL THEN i := Infinity END;
  8710. RETURN i
  8711. END RecordTypeDistance;
  8712. (** compute and return the distance of two pointer types **)
  8713. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8714. BEGIN
  8715. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8716. RETURN Infinity;
  8717. ELSE
  8718. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8719. END;
  8720. END PointerTypeDistance;
  8721. (** check if expression contains a symbol designator pointing to a type declaration.
  8722. - if so then enter type declaration into typeDeclaration and return true else return false
  8723. **)
  8724. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8725. VAR result: BOOLEAN;
  8726. BEGIN
  8727. result := FALSE;
  8728. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8729. result := TRUE;
  8730. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8731. END;
  8732. RETURN result
  8733. END IsTypeDesignator;
  8734. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8735. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8736. VAR result: BOOLEAN;
  8737. BEGIN
  8738. type := type.resolved;
  8739. IF type IS SyntaxTree.PointerType THEN
  8740. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8741. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8742. result := TRUE
  8743. ELSE
  8744. result := type IS SyntaxTree.RecordType
  8745. END;
  8746. RETURN result
  8747. END IsExtensibleType;
  8748. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8749. BEGIN
  8750. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8751. (d IS SyntaxTree.SymbolDesignator) &
  8752. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8753. OR
  8754. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8755. END IsUnextensibleRecord;
  8756. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8757. BEGIN
  8758. IF IsUnextensibleRecord(d) THEN
  8759. RETURN FALSE
  8760. ELSE RETURN IsExtensibleType(d.type.resolved)
  8761. END;
  8762. END IsExtensibleDesignator;
  8763. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8764. BEGIN
  8765. type := type.resolved;
  8766. IF (type IS SyntaxTree.PointerType) THEN
  8767. RETURN TRUE
  8768. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8769. RETURN TRUE
  8770. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8771. RETURN TRUE
  8772. ELSIF (type IS SyntaxTree.BasicType) THEN
  8773. RETURN TRUE
  8774. END;
  8775. RETURN FALSE
  8776. END IsBasicType;
  8777. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8778. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8779. BEGIN
  8780. baseType := record.baseType;
  8781. IF (baseType # NIL) THEN
  8782. baseType := baseType.resolved;
  8783. IF (baseType IS SyntaxTree.PointerType) THEN
  8784. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8785. END;
  8786. END;
  8787. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8788. recordType := baseType(SyntaxTree.RecordType);
  8789. ELSE
  8790. recordType := NIL;
  8791. END;
  8792. RETURN recordType
  8793. END RecordBase;
  8794. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8795. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8796. BEGIN
  8797. baseRecord := RecordBase(scope.ownerRecord);
  8798. IF baseRecord = NIL THEN RETURN NIL END;
  8799. scope := baseRecord.recordScope;
  8800. procedureType := procedure.type.resolved;
  8801. IF procedure IS SyntaxTree.Operator THEN
  8802. operator := scope.firstOperator;
  8803. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8804. (*
  8805. Printout.Info("not same ",procedureType);
  8806. Printout.Info("with ",operator.type);
  8807. *)
  8808. operator := operator.nextOperator;
  8809. END;
  8810. super := operator;
  8811. ELSE
  8812. super := scope.firstProcedure;
  8813. WHILE (super # NIL) & (super.name # procedure.name) DO
  8814. super := super.nextProcedure;
  8815. END;
  8816. END;
  8817. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8818. RETURN super
  8819. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8820. RETURN super
  8821. ELSE
  8822. RETURN FindSuperProcedure(scope,procedure);
  8823. END;
  8824. END FindSuperProcedure;
  8825. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8826. VAR procedure: SyntaxTree.Procedure;
  8827. BEGIN
  8828. procedure := record.recordScope.constructor;
  8829. IF procedure = NIL THEN
  8830. record := RecordBase(record);
  8831. IF record # NIL THEN
  8832. procedure := GetConstructor(record)
  8833. END;
  8834. END;
  8835. RETURN procedure;
  8836. END GetConstructor;
  8837. (* enter a case into a list of cases in a sorted way and check for collision *)
  8838. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  8839. VAR prev,this,new: SyntaxTree.CaseConstant;
  8840. BEGIN
  8841. this := root;
  8842. prev := NIL;
  8843. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8844. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8845. RETURN FALSE
  8846. ELSE
  8847. IF (this # NIL) & (this.min = max+1) THEN
  8848. this.min := min
  8849. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8850. prev.max := min
  8851. ELSE
  8852. NEW(new); new.min := min; new.max := max;
  8853. new.next := this;
  8854. IF prev = NIL THEN
  8855. root := new;
  8856. ELSE
  8857. prev.next := new
  8858. END
  8859. END;
  8860. RETURN TRUE
  8861. END;
  8862. END EnterCase;
  8863. (** generate and return a new checker object, errors are entered into diagnostics **)
  8864. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; activeCellsSpecification: ActiveCells.Specification; VAR importCache: SyntaxTree.ModuleScope): Checker;
  8865. VAR checker: Checker;
  8866. BEGIN
  8867. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,activeCellsSpecification, importCache);
  8868. RETURN checker
  8869. END NewChecker;
  8870. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  8871. VAR warnings: Warnings;
  8872. BEGIN
  8873. NEW(warnings, diagnostics); RETURN warnings;
  8874. END NewWarnings;
  8875. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  8876. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  8877. END IsRangeType;
  8878. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  8879. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  8880. END IsMathArrayType;
  8881. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  8882. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  8883. END IsArrayType;
  8884. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  8885. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  8886. END IsComplexType;
  8887. (** if a type is an array-structured object type *)
  8888. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  8889. VAR recordType: SyntaxTree.RecordType;
  8890. BEGIN
  8891. IF type = NIL THEN
  8892. RETURN FALSE
  8893. ELSE
  8894. type := type.resolved;
  8895. IF type IS SyntaxTree.PointerType THEN
  8896. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8897. IF type IS SyntaxTree.RecordType THEN
  8898. recordType := type(SyntaxTree.RecordType);
  8899. RETURN recordType.isObject & recordType.HasArrayStructure()
  8900. ELSE
  8901. RETURN FALSE
  8902. END
  8903. ELSE
  8904. RETURN FALSE
  8905. END
  8906. END
  8907. END IsArrayStructuredObjectType;
  8908. (** the math array structure of a type
  8909. - for math arrays: the array itself
  8910. - for pointers: the math array structure of the pointer base
  8911. - for array-structured object types: the underlying structure
  8912. - for non-math arrays and all other types: NIL
  8913. **)
  8914. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  8915. VAR
  8916. result: SyntaxTree.MathArrayType;
  8917. BEGIN
  8918. IF type = NIL THEN
  8919. result := NIL
  8920. ELSE
  8921. type := type.resolved;
  8922. IF type IS SyntaxTree.PointerType THEN
  8923. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8924. END;
  8925. IF type IS SyntaxTree.MathArrayType THEN
  8926. result := type(SyntaxTree.MathArrayType)
  8927. ELSIF type IS SyntaxTree.RecordType THEN
  8928. result := type(SyntaxTree.RecordType).arrayStructure
  8929. ELSE
  8930. result := NIL
  8931. END
  8932. END;
  8933. RETURN result
  8934. END MathArrayStructureOfType;
  8935. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  8936. VAR
  8937. result: BOOLEAN;
  8938. rangeExpression: SyntaxTree.RangeExpression;
  8939. BEGIN
  8940. IF x IS SyntaxTree.RangeExpression THEN
  8941. rangeExpression := x(SyntaxTree.RangeExpression);
  8942. result := TRUE;
  8943. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  8944. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  8945. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  8946. ELSE
  8947. result := FALSE
  8948. END;
  8949. RETURN result
  8950. END IsStaticRange;
  8951. (** whether a type is a math array of tensor form **)
  8952. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  8953. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8954. END IsTensor;
  8955. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  8956. BEGIN
  8957. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  8958. length := type(SyntaxTree.MathArrayType).staticLength;
  8959. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  8960. RETURN TRUE
  8961. ELSE
  8962. RETURN FALSE
  8963. END;
  8964. END IsStaticMathArray;
  8965. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  8966. BEGIN
  8967. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  8968. END SymbolHasAddress;
  8969. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  8970. BEGIN
  8971. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  8972. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  8973. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  8974. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  8975. END HasAddress;
  8976. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  8977. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  8978. BEGIN
  8979. IF (e IS SyntaxTree.Designator) THEN
  8980. d := e(SyntaxTree.Designator);
  8981. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  8982. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  8983. e := d.left;
  8984. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  8985. END;
  8986. IF d # NIL THEN
  8987. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  8988. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  8989. END;
  8990. END;
  8991. RETURN FALSE;
  8992. END IsLocalVariable;
  8993. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  8994. BEGIN
  8995. IF procedure.scope IS SyntaxTree.RecordScope THEN
  8996. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  8997. ELSE
  8998. RETURN TRUE
  8999. END;
  9000. END IsStaticProcedure;
  9001. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9002. CONST OptimizeMethodTable = FALSE;
  9003. BEGIN
  9004. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9005. END InMethodTable;
  9006. END FoxSemanticChecker.