FoxSemanticChecker.Mod 388 KB

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