FoxSemanticChecker.Mod 399 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144
  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,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  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. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  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. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  127. VAR errModule: SyntaxTree.Module;
  128. BEGIN
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  132. error := TRUE;
  133. END Error;
  134. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  135. VAR errModule: SyntaxTree.Module;
  136. BEGIN
  137. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  138. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  139. END Warning;
  140. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  141. VAR errorMessage: ARRAY 256 OF CHAR;
  142. BEGIN
  143. Basic.Concat(errorMessage,msg," ", msg2);
  144. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  145. error := TRUE;
  146. END ErrorSS;
  147. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  148. VAR msg, msg2: ARRAY 256 OF CHAR;
  149. BEGIN
  150. COPY(msg1, msg);
  151. Strings.Append(msg, " = ");
  152. Basic.GetString(s, msg2);
  153. Strings.Append(msg, msg2);
  154. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  155. END InfoSS;
  156. (*** symbol lookup ***)
  157. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  158. **)
  159. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  160. VAR
  161. scope,baseScope: SyntaxTree.Scope;
  162. symbol, s: SyntaxTree.Symbol;
  163. ownerRecord,base: SyntaxTree.RecordType;
  164. BEGIN
  165. scope := inScope;
  166. symbol := NIL;
  167. WHILE (scope # NIL) & (symbol = NIL) DO
  168. symbol := scope.FindSymbol(name);
  169. s := NIL;
  170. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  171. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  173. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. END;
  175. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  176. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  177. ELSE
  178. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  179. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  180. symbol.MarkUsed;
  181. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  182. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  183. base := RecordBase(ownerRecord);
  184. IF (base # NIL) THEN
  185. baseScope := base.recordScope;
  186. symbol := Find(baseScope,name,FALSE);
  187. ELSE
  188. symbol := NIL;
  189. END;
  190. ELSE
  191. symbol := NIL;
  192. END;
  193. END;
  194. END;
  195. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  196. END;
  197. IF (symbol # NIL) THEN
  198. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  199. ASSERT(phase = DeclarationPhase);
  200. ResolveSymbol(symbol)
  201. END;
  202. symbol.MarkUsed;
  203. END;
  204. RETURN symbol
  205. END Find;
  206. (*** types ***)
  207. (** find type declaration with name qualifiedIdentifier and return resolved type
  208. - check qualified identifier prefix, set scope to module scope if appropriate
  209. - check suffix in scope
  210. **)
  211. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  212. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  213. BEGIN
  214. result := NIL;
  215. prevScope := currentScope;
  216. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  217. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  218. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  219. IF symbol(SyntaxTree.Import).module = NIL THEN
  220. Error(qualifiedIdentifier.position,"module not loaded");
  221. result := SyntaxTree.invalidType;
  222. symbol := NIL;
  223. ELSE
  224. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  225. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  226. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  227. IF VerboseErrorMessage THEN
  228. Printout.Info("scope", currentScope);
  229. Printout.Info("symbol", symbol);
  230. END;
  231. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  232. END;
  233. END;
  234. ELSE
  235. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  236. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  237. symbol := NIL;
  238. END;
  239. ELSE
  240. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  241. IF symbol = NIL THEN
  242. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  243. IF VerboseErrorMessage THEN
  244. Printout.Info("Qualident",qualifiedIdentifier);
  245. Printout.Info("in scope",currentScope) ;
  246. END;
  247. END;
  248. END;
  249. IF symbol = NIL THEN (* error already handled *)
  250. typeDeclaration := NIL;
  251. result := SyntaxTree.invalidType;
  252. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  253. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSE
  257. currentScope := symbol.scope;
  258. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  259. result := ResolveType(typeDeclaration.declaredType);
  260. symbol.MarkUsed;
  261. ASSERT(result # NIL);
  262. END;
  263. currentScope := prevScope;
  264. RETURN result
  265. END ResolveNamedType;
  266. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  267. If node is currently being resolved then emit a cyclic definition error.
  268. Return TRUE only if node is fully resolved.
  269. **)
  270. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  271. VAR result: BOOLEAN;
  272. BEGIN
  273. IF SyntaxTree.Resolved IN x.state THEN
  274. result := FALSE
  275. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  276. Error(x.position,"cyclic definition");
  277. result := FALSE;
  278. ELSE
  279. result := TRUE;
  280. x.SetState(SyntaxTree.BeingResolved)
  281. END;
  282. RETURN result
  283. END TypeNeedsResolution;
  284. (** Return invalid type if x is currently being resolved, return x otherwise**)
  285. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  286. BEGIN
  287. IF SyntaxTree.Resolved IN x.state THEN
  288. RETURN x
  289. ELSE
  290. RETURN SyntaxTree.invalidType
  291. END;
  292. END ResolvedType;
  293. PROCEDURE VisitType(x: SyntaxTree.Type);
  294. BEGIN
  295. ASSERT(x = SyntaxTree.invalidType);
  296. END VisitType;
  297. (** resolve basic type **)
  298. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  299. BEGIN
  300. IF TypeNeedsResolution(x) THEN
  301. x.SetState(SyntaxTree.Resolved);
  302. END;
  303. resolvedType := ResolvedType(x)
  304. END VisitBasicType;
  305. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  306. BEGIN
  307. VisitBasicType(x);
  308. END VisitByteType;
  309. (** resolve character type **)
  310. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitCharacterType;
  314. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitBooleanType;
  318. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitSetType;
  322. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitAddressType;
  326. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitSizeType;
  330. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitAnyType;
  334. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitObjectType;
  338. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitNilType;
  342. (** resolve integer type **)
  343. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitIntegerType;
  347. (** resolve real type **)
  348. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitFloatType;
  352. (** resolve complex type **)
  353. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitComplexType;
  357. (**
  358. resolve string type: nothing to be done
  359. **)
  360. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  361. BEGIN
  362. IF TypeNeedsResolution(x) THEN
  363. x.SetState(SyntaxTree.Resolved);
  364. END;
  365. resolvedType := ResolvedType(x)
  366. END VisitStringType;
  367. (**
  368. check enumeration scope: enter symbols and check for duplicate names
  369. **)
  370. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  371. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  372. BEGIN
  373. prevScope := currentScope;
  374. currentScope := x;
  375. e := x.firstConstant;
  376. WHILE (e # NIL) DO
  377. Register(e,x,FALSE);
  378. IF SymbolNeedsResolution(e) THEN
  379. IF e.value # NIL THEN
  380. value := ConstantExpression(e.value);
  381. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  382. ELSE
  383. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  384. value.SetType(x.ownerEnumeration);
  385. END;
  386. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  387. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  388. IF nextHighest > highest THEN highest := nextHighest END;
  389. END;
  390. e.SetValue(value);
  391. CheckSymbolVisibility(e);
  392. e.SetType(x.ownerEnumeration);
  393. e.SetState(SyntaxTree.Resolved);
  394. END;
  395. e := e.nextConstant;
  396. END;
  397. currentScope := prevScope;
  398. END CheckEnumerationScope;
  399. (**
  400. resolve enumeration type: check enumeration scope
  401. **)
  402. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  403. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  404. lowest, highest: LONGINT;
  405. BEGIN
  406. IF TypeNeedsResolution(x) THEN
  407. IF x.enumerationBase # NIL THEN
  408. position := x.enumerationBase.position;
  409. baseType := ResolveType(x.enumerationBase);
  410. resolved := baseType.resolved;
  411. baseScope := NIL;
  412. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  413. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  414. Error(position, "base type is no enumeration type");
  415. ELSE
  416. enumerationBase := resolved(SyntaxTree.EnumerationType);
  417. lowest := enumerationBase.rangeHighest+1;
  418. END;
  419. x.SetEnumerationBase(baseType);
  420. ELSE lowest := 0;
  421. END;
  422. highest := lowest-1;
  423. CheckEnumerationScope(x.enumerationScope, highest);
  424. x.SetRange(lowest, highest);
  425. x.SetState(SyntaxTree.Resolved);
  426. END;
  427. resolvedType := ResolvedType(x);
  428. END VisitEnumerationType;
  429. (**
  430. resolve range type: nothing to be done
  431. **)
  432. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  433. BEGIN
  434. IF TypeNeedsResolution(x) THEN
  435. x.SetState(SyntaxTree.Resolved);
  436. END;
  437. resolvedType := ResolvedType(x)
  438. END VisitRangeType;
  439. (**
  440. resolve qualified type
  441. - find and resolve named type and set resolved type
  442. **)
  443. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  444. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  445. BEGIN
  446. IF TypeNeedsResolution(x) THEN
  447. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  448. x.SetResolved(type.resolved);
  449. x.SetState(SyntaxTree.Resolved);
  450. x.SetTypeDeclaration (typeDeclaration);
  451. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  452. x.SetResolved(SyntaxTree.invalidType);
  453. END;
  454. resolvedType := x;
  455. END VisitQualifiedType;
  456. (**
  457. resolve array type
  458. - check base type
  459. - array of math array forbidden
  460. - static array of open array forbidden
  461. **)
  462. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  463. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  464. BEGIN
  465. IF TypeNeedsResolution(x) THEN
  466. x.SetArrayBase(ResolveType(x.arrayBase));
  467. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  468. arrayBase := x.arrayBase.resolved;
  469. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  470. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  471. pointerType.SetPointerBase(arrayBase);
  472. pointerType.SetHidden(TRUE);
  473. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  474. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  475. ELSE
  476. x.SetArrayBase(pointerType);
  477. END;
  478. END;
  479. IF x.length # NIL THEN
  480. variableAccessed := FALSE;
  481. e := ResolveExpression(x.length);
  482. IF (e.resolved = NIL) THEN
  483. IF variableAccessed THEN
  484. Error(e.position, "forbidden variable access");
  485. END;
  486. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  487. ELSE
  488. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  489. END;
  490. END;
  491. IF arrayBase IS SyntaxTree.ArrayType THEN
  492. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  493. Error(x.position,"forbidden static array of dynamic array");
  494. END;
  495. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  496. Error(x.position,"forbidden array mixed form");
  497. END;
  498. x.SetHasPointers(arrayBase.hasPointers);
  499. x.SetState(SyntaxTree.Resolved);
  500. END;
  501. resolvedType := ResolvedType(x);
  502. END VisitArrayType;
  503. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  504. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  505. BEGIN
  506. module := currentScope.ownerModule;
  507. IF module.name=name THEN
  508. (* do nothing *)
  509. ELSE
  510. moduleScope := module.moduleScope;
  511. import := moduleScope.FindImport(name);
  512. IF import = NIL THEN
  513. import := SyntaxTree.NewImport(position,name,name,TRUE);
  514. moduleScope.AddImport(import);
  515. Register(import,moduleScope,FALSE);
  516. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  517. VisitImport(import);
  518. ELSIF import.direct=FALSE THEN
  519. import.SetScope(module.moduleScope);
  520. import.SetDirect(TRUE);
  521. IF moduleScope.FindSymbol(import.name) = NIL THEN
  522. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  523. duplicate.SetContext(import.context);
  524. duplicate.SetModule(import.module);
  525. Register(duplicate,moduleScope,TRUE);
  526. VisitImport(duplicate);
  527. END;
  528. END;
  529. import.MarkUsed
  530. END;
  531. END ImportModule;
  532. (**
  533. resolve math array type
  534. - check base type
  535. - open math array of array forbidden
  536. - math array of tensor forbidden
  537. - static array of open array forbidden
  538. **)
  539. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  540. VAR arrayBase: SyntaxTree.Type;
  541. BEGIN
  542. IF TypeNeedsResolution(x) THEN
  543. x.SetArrayBase(ResolveType(x.arrayBase));
  544. IF x.length # NIL THEN
  545. x.SetLength(ConstantIntegerGeq0(x.length));
  546. END;
  547. arrayBase := x.arrayBase;
  548. IF arrayBase # NIL THEN
  549. arrayBase := arrayBase.resolved;
  550. IF arrayBase = SyntaxTree.invalidType THEN
  551. (* error already handled *)
  552. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  553. Error(x.position,"forbidden array mixed form");
  554. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  555. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  556. Error(x.position,"forbidden Tensor Array mix")
  557. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  558. Error(x.position,"forbidden static array of dynamic array")
  559. END;
  560. END;
  561. IF x.form = SyntaxTree.Static THEN
  562. x.SetIncrement(system.SizeOf(arrayBase));
  563. END;
  564. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  565. END;
  566. x.SetState(SyntaxTree.Resolved);
  567. END;
  568. resolvedType := ResolvedType(x);
  569. END VisitMathArrayType;
  570. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  571. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  572. (1) Rec = RECORD ... END; Ptr <---> Rec
  573. Ptr = POINTER TO Rec; ^ |
  574. | |
  575. TypeDesc TypeDesc
  576. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  577. ^ /
  578. | /
  579. TypeDesc <-- /
  580. *)
  581. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  582. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  583. BEGIN
  584. Strings.IntToStr(x.position.start,number);
  585. COPY(prefix,name);
  586. Strings.Append(name,"@");
  587. Strings.Append(name,number);
  588. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  589. typeDeclaration.SetDeclaredType(x);
  590. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  591. x.SetTypeDeclaration(typeDeclaration);
  592. currentScope.AddTypeDeclaration(typeDeclaration);
  593. typeDeclaration.SetScope(currentScope);
  594. END AnonymousTypeDeclaration;
  595. (**
  596. deferred pointer type resolving
  597. - resolve base type
  598. - check that base type is a record or array type
  599. - if error then set base type to invalid type
  600. **)
  601. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  602. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  603. BEGIN
  604. ASSERT(type.pointerBase # NIL);
  605. position := type.pointerBase.position;
  606. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  607. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  608. (* not for pointers, a type is needed for the records only
  609. IF type.typeDeclaration = NIL THEN
  610. AnonymousTypeDeclaration(type);
  611. END;
  612. *)
  613. END;
  614. resolved := ResolveType(type.pointerBase);
  615. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  616. type.SetPointerBase(resolved);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  618. recordType := resolved.resolved(SyntaxTree.RecordType);
  619. IF recordType.isObject & (recordType.baseType # NIL) THEN
  620. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  621. Error(position, "base type of object must be a realtime object");
  622. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  623. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  624. END;
  625. END;
  626. END;
  627. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  628. Error(position, "realtime object contains references to non-realtime objects");
  629. END;
  630. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  631. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  632. Error(position, "forbidden unsafe at static array");
  633. ELS
  634. *)
  635. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  636. Error(position, "forbidden unsafe at multidimensional array");
  637. END;
  638. END;
  639. ELSE
  640. Error(position, "forbidden pointer base type");
  641. type.SetPointerBase(SyntaxTree.invalidType)
  642. END
  643. END FixPointerType;
  644. (**
  645. resolve pointer type
  646. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  647. **)
  648. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  649. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  650. modifiers: SyntaxTree.Modifier; position: Position;
  651. BEGIN
  652. IF TypeNeedsResolution(x) THEN
  653. modifiers := x.modifiers;
  654. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  655. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  656. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  657. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  658. (* inheritance cycle check
  659. example:
  660. A=POINTER TO RECORD(B) END;
  661. B=POINTER TO RECORD(A) END;
  662. *)
  663. IF x.pointerBase IS SyntaxTree.RecordType THEN
  664. recordType := x.pointerBase(SyntaxTree.RecordType);
  665. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetCellsAreObjects(cellsAreObjects);
  685. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  686. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  687. x.SetSize(value)
  688. ELSE
  689. x.SetSize(system.SizeOf(system.longintType));
  690. END;
  691. x.SetState(SyntaxTree.Resolved);
  692. END;
  693. resolvedType := ResolvedType(x)
  694. END VisitPortType;
  695. (**
  696. deferred procedure type resolving
  697. - resolve return type
  698. - traverse and resolve parameters
  699. **)
  700. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  701. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  702. BEGIN
  703. resolved := ResolveType(procedureType.returnType);
  704. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  705. Error(procedureType.position,"forbidden open array return type");
  706. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  707. Error(procedureType.position,"procedure with return type does not return");
  708. END;
  709. procedureType.SetReturnType(resolved);
  710. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  711. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  712. parameter.SetType(procedureType.returnType);
  713. parameter.SetAccess(SyntaxTree.Hidden);
  714. parameter.SetUntraced(procedureType.hasUntracedReturn);
  715. VisitParameter(parameter);
  716. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  717. END;
  718. (* process parameters *)
  719. parameter :=procedureType.firstParameter;
  720. WHILE (parameter # NIL) DO
  721. VisitParameter(parameter);
  722. parameter := parameter.nextParameter;
  723. END;
  724. parameter := procedureType.selfParameter;
  725. IF parameter # NIL THEN
  726. VisitParameter(parameter)
  727. END;
  728. END FixProcedureType;
  729. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  730. VAR prev,this: SyntaxTree.Modifier;
  731. BEGIN
  732. this := modifiers;prev := NIL;
  733. WHILE (this # NIL) & (this.identifier # name) DO
  734. prev := this; this := this.nextModifier;
  735. END;
  736. IF this # NIL THEN
  737. IF this.expression # NIL THEN
  738. Error(this.position,"unexpected expression");
  739. END;
  740. this.Resolved;
  741. position := this.position;
  742. RETURN TRUE
  743. ELSE
  744. RETURN FALSE
  745. END;
  746. END HasFlag;
  747. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  748. VAR prev,this: SyntaxTree.Modifier;
  749. BEGIN
  750. this := modifiers;prev := NIL;
  751. WHILE (this # NIL) & (this.identifier # name) DO
  752. prev := this; this := this.nextModifier;
  753. END;
  754. IF this # NIL THEN
  755. IF this.expression = NIL THEN
  756. Error(this.position,"expected expression value");
  757. ELSE
  758. this.SetExpression(ConstantExpression(this.expression));
  759. IF CheckIntegerValue(this.expression,value) THEN END;
  760. END;
  761. this.Resolved;
  762. position := this.position;
  763. RETURN TRUE
  764. ELSE RETURN FALSE
  765. END;
  766. END HasValue;
  767. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  768. VAR prev,this: SyntaxTree.Modifier;
  769. BEGIN
  770. this := modifiers;prev := NIL;
  771. WHILE (this # NIL) & (this.identifier # name) DO
  772. prev := this; this := this.nextModifier;
  773. END;
  774. IF this # NIL THEN
  775. IF this.expression = NIL THEN
  776. Error(this.position,"expected expression value");
  777. ELSE
  778. this.SetExpression(ConstantExpression(this.expression));
  779. IF CheckStringValue(this.expression,value) THEN END;
  780. END;
  781. this.Resolved;
  782. position := this.position;
  783. RETURN TRUE
  784. ELSE RETURN FALSE
  785. END;
  786. END HasStringValue;
  787. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  788. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  789. BEGIN
  790. IF cellsAreObjects THEN RETURN FALSE END;
  791. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  792. IF svalue = "A2" THEN
  793. RETURN TRUE
  794. END;
  795. END;
  796. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  797. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  798. END;
  799. RETURN FALSE;
  800. (*
  801. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  802. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  803. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  804. IF svalue[0] = "~" THEN
  805. Strings.TrimLeft(svalue, "~");
  806. IF svalue = backendName THEN
  807. RETURN TRUE;
  808. END;
  809. ELSIF svalue # backendName THEN
  810. RETURN TRUE;
  811. END;
  812. END;
  813. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  814. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  815. END;
  816. RETURN FALSE;
  817. *)
  818. END SkipImplementation;
  819. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  820. VAR this: SyntaxTree.Modifier;
  821. BEGIN
  822. this := modifiers;
  823. WHILE this # NIL DO
  824. IF ~this.resolved THEN
  825. IF checkUse THEN
  826. Error(this.position,"unexpected modifier");
  827. ELSE
  828. this.SetExpression(ResolveExpression(this.expression));
  829. this.Resolved;
  830. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  831. END;
  832. END;
  833. this := this.nextModifier
  834. END;
  835. END CheckModifiers;
  836. (**
  837. resolve procedure type
  838. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  839. **)
  840. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  841. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  842. BEGIN
  843. IF TypeNeedsResolution(procedureType) THEN
  844. modifiers := procedureType.modifiers;
  845. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  846. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  847. procedureType.SetInterrupt(TRUE);
  848. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  849. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  850. IF useDarwinCCalls THEN (*fld*)
  851. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  852. ELSE
  853. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  854. END
  855. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  856. procedureType.SetNoReturn(TRUE);
  857. END;
  858. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  859. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  860. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  861. CheckModifiers(modifiers, TRUE);
  862. modifiers := procedureType.returnTypeModifiers;
  863. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  864. CheckModifiers(modifiers, TRUE);
  865. typeFixes.Add(procedureType,currentScope);
  866. procedureType.SetHasPointers(procedureType.isDelegate);
  867. procedureType.SetState(SyntaxTree.Resolved);
  868. END;
  869. resolvedType := ResolvedType(procedureType)
  870. END VisitProcedureType;
  871. (** check and resolve record type
  872. - check base type: must be record, math array or array-structured object type
  873. - check declarations
  874. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  875. **)
  876. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  877. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  878. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  879. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  880. hasPointers: BOOLEAN;
  881. modifiers: SyntaxTree.Modifier;
  882. value: LONGINT;
  883. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  884. BEGIN
  885. type := type.resolved;
  886. IF (type IS SyntaxTree.PointerType) &
  887. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  888. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  889. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  890. RETURN TRUE
  891. ELSE
  892. RETURN FALSE
  893. END;
  894. END IsPointerToRecord;
  895. BEGIN
  896. IF TypeNeedsResolution(x) THEN
  897. hasPointers := FALSE;
  898. modifiers := x.modifiers;
  899. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  900. CheckModifiers(modifiers, TRUE);
  901. IF x.baseType # NIL THEN
  902. position := x.baseType.position;
  903. baseType := ResolveType(x.baseType);
  904. resolved := baseType.resolved;
  905. hasPointers := hasPointers OR resolved.hasPointers;
  906. IF x.isObject THEN (* object *)
  907. ASSERT(x.pointerType # NIL);
  908. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  909. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  910. baseType := NIL
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  913. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  914. ELSE
  915. Error(position,"object does not extend pointer to record, object or math array ")
  916. END;
  917. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  918. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  919. ELSIF IsPointerToRecord(resolved,recordType) THEN
  920. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  921. ELSIF resolved IS SyntaxTree.RecordType THEN
  922. ELSE
  923. Error(position,"pointer to record does not extend pointer to record or record")
  924. END;
  925. ELSE
  926. IF resolved IS SyntaxTree.RecordType THEN
  927. ELSE
  928. Error(position,"record does not extend record")
  929. END;
  930. END;
  931. x.SetBaseType(baseType);
  932. IF x.Level() > 15 THEN
  933. Error(position, "record/object inheritance level too high");
  934. (* note:
  935. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  936. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  937. inheritance history of a type.
  938. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  939. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  940. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  941. *)
  942. END;
  943. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  944. Error(position, "invalid inheritance of disposable types");
  945. END;
  946. END;
  947. Declarations(x.recordScope, FALSE, {0});
  948. x.SetState(SyntaxTree.Resolved);
  949. Declarations(x.recordScope, FALSE, {1});
  950. ResolveArrayStructure(x);
  951. (* computation of sizes and offsets skipped -> done in backend / system *)
  952. recordBase := x.GetBaseRecord();
  953. IF recordBase = NIL THEN numberMethods := 0
  954. ELSE numberMethods := recordBase.recordScope.numberMethods
  955. END;
  956. isRealtime := TRUE;
  957. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  958. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  959. END;
  960. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  961. WHILE symbol # NIL DO
  962. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  963. IF symbol IS SyntaxTree.Variable THEN
  964. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  965. END;
  966. IF symbol IS SyntaxTree.Procedure THEN
  967. procedure := symbol(SyntaxTree.Procedure);
  968. IF procedure.super # NIL THEN
  969. procedure.SetMethodNumber(procedure.super.methodNumber)
  970. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  971. procedure.SetMethodNumber(numberMethods);
  972. INC(numberMethods);
  973. END;
  974. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  975. Error(procedure.position,"realtime procedure in non-realtime object")
  976. END;
  977. END;
  978. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  979. Error(symbol.position,"non-realtime symbol in realtime object")
  980. END;
  981. symbol := symbol.nextSymbol;
  982. END;
  983. IF isRealtime THEN x.SetRealtime(TRUE) END;
  984. x.recordScope.SetNumberMethods(numberMethods);
  985. (* TODO: is this needed anymore? *)
  986. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  987. Error(x.position,"object extends a record")
  988. END;
  989. IF (x.typeDeclaration = NIL) THEN
  990. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  991. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  992. (*
  993. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  994. AnonymousTypeDeclaration(x,name);
  995. *)
  996. ELSE
  997. AnonymousTypeDeclaration(x,"Anonymous");
  998. END;
  999. END;
  1000. x.SetHasPointers(hasPointers);
  1001. x.SetState(SyntaxTree.Resolved);
  1002. END;
  1003. resolvedType := ResolvedType(x);
  1004. END VisitRecordType;
  1005. (** check and resolve cell type
  1006. - check base type: must be cell
  1007. - check declarations
  1008. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1009. **)
  1010. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1011. VAR
  1012. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1013. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1014. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1015. recordBase: SyntaxTree.RecordType;
  1016. numberMethods, int: LONGINT;
  1017. real: LONGREAL;
  1018. bool: BOOLEAN;
  1019. set: SET;
  1020. v: SyntaxTree.Expression;
  1021. str: Scanner.StringType;
  1022. atype: SyntaxTree.ArrayType;
  1023. prev: SyntaxTree.Scope;
  1024. skip: BOOLEAN;
  1025. svalue: ARRAY 32 OF CHAR;
  1026. BEGIN
  1027. IF TypeNeedsResolution(x) THEN
  1028. recordBase := NIL;
  1029. IF cellsAreObjects THEN
  1030. IF x.baseType = NIL THEN
  1031. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1032. ImportModule(qualifiedIdentifier.prefix, x.position);
  1033. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1034. x.SetBaseType(ResolveType(x.baseType));
  1035. recordBase := x.GetBaseRecord();
  1036. IF recordBase = NIL THEN
  1037. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1038. END;
  1039. ELSE
  1040. x.SetBaseType(ResolveType(x.baseType));
  1041. END;
  1042. ELSE
  1043. x.SetBaseType(ResolveType(x.baseType));
  1044. END;
  1045. IF recordBase = NIL THEN numberMethods := 0
  1046. ELSE numberMethods := recordBase.recordScope.numberMethods
  1047. END;
  1048. modifier := x.modifiers;
  1049. (*IF ~x.isCellNet THEN*)
  1050. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1051. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1052. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1053. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1054. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1055. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1056. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1057. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1058. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1059. symbol := system.activeCellsCapabilities;
  1060. WHILE symbol # NIL DO
  1061. IF HasFlag(modifier, symbol.name, position) THEN END;
  1062. symbol := symbol.nextSymbol;
  1063. END;
  1064. modifier := x.modifiers;
  1065. WHILE (modifier # NIL) DO
  1066. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1067. IF modifier.expression # NIL THEN
  1068. v := ConstantExpression(modifier.expression);
  1069. property.SetValue(v);
  1070. IF IsIntegerValue(modifier.expression, int) THEN
  1071. (*property.SetValue(modifier.expression);*)
  1072. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1073. property.SetType(system.longintType);
  1074. ELSIF IsRealValue(modifier.expression, real) THEN
  1075. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1076. property.SetType(system.longrealType);
  1077. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1078. property.SetType(system.booleanType);
  1079. ELSIF IsSetValue(modifier.expression, set) THEN
  1080. property.SetType(system.setType);
  1081. ELSIF IsStringValue(modifier.expression, str) THEN
  1082. (*property.SetValue(modifier.expression);*)
  1083. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1084. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1085. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1086. property.SetType(atype);
  1087. ELSE
  1088. Error(modifier.position, "unsupported property type");
  1089. END;
  1090. ELSE (* flag property *)
  1091. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1092. property.SetType(system.booleanType);
  1093. END;
  1094. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1095. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1096. x.AddProperty(property);
  1097. modifier := modifier.nextModifier;
  1098. END;
  1099. CheckModifiers(modifier, FALSE);
  1100. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1101. (* process parameters *)
  1102. prev := currentScope;
  1103. currentScope := x.cellScope;
  1104. parameter :=x.firstParameter;
  1105. WHILE (parameter # NIL) DO
  1106. VisitParameter(parameter);
  1107. type := parameter.type.resolved;
  1108. IF ~(type IS SyntaxTree.PortType) THEN
  1109. WHILE IsStaticArray(type, type, len) DO
  1110. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1111. END;
  1112. WHILE IsDynamicArray(type, type) DO
  1113. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1114. END;
  1115. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1116. Error(parameter.position, "invalid type, must be port or static array of port ");
  1117. END;
  1118. END;
  1119. parameter := parameter.nextParameter;
  1120. END;
  1121. currentScope := prev;
  1122. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1123. WHILE symbol # NIL DO
  1124. IF symbol IS SyntaxTree.Variable THEN
  1125. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1126. END;
  1127. symbol := symbol.nextSymbol;
  1128. END;
  1129. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1130. IF (x.typeDeclaration = NIL) THEN
  1131. AnonymousTypeDeclaration(x,"Anonymous");
  1132. END;
  1133. x.SetState(SyntaxTree.Resolved);
  1134. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1135. Warning(x.position, "Forbidden empty Body.");
  1136. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1137. Warning(x.position, "Non-empty body for an engine?");
  1138. END;
  1139. END;
  1140. resolvedType := ResolvedType(x);
  1141. END VisitCellType;
  1142. (* check if an object is an array-structured object type
  1143. - determine the array structure
  1144. - collect operators from top to bottom in the inheritance hierarchy
  1145. - check if LEN operator is declared
  1146. - determine number of possible index operators
  1147. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1148. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1149. *)
  1150. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1151. VAR
  1152. indexOperatorCount, i: LONGINT;
  1153. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1154. isTensor: BOOLEAN;
  1155. BEGIN
  1156. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1157. (* determine array structure *)
  1158. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1159. END;
  1160. IF recordType.HasArrayStructure() THEN
  1161. (* the object is an ASOT *)
  1162. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1163. (* reset array access operators *)
  1164. arrayAccessOperators.len := NIL;
  1165. arrayAccessOperators.generalRead := NIL;
  1166. arrayAccessOperators.generalWrite := NIL;
  1167. IF isTensor THEN
  1168. (* all operators of dimensionalities 1 to max *)
  1169. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1170. ELSE
  1171. (* all operators of certain dimensionality *)
  1172. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1173. END;
  1174. NEW(arrayAccessOperators.read, indexOperatorCount);
  1175. NEW(arrayAccessOperators.write, indexOperatorCount);
  1176. FOR i := 0 TO indexOperatorCount - 1 DO
  1177. arrayAccessOperators.read[i] := NIL;
  1178. arrayAccessOperators.write[i] := NIL
  1179. END;
  1180. (* collect access operators in the record scope *)
  1181. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1182. IF arrayAccessOperators.len = NIL THEN
  1183. (* TODO: think about making this operator optional for static array structures *)
  1184. Error(recordType.position, "LEN operator missing")
  1185. END;
  1186. (* show error messages *)
  1187. IF isTensor THEN
  1188. (* require ARRAY [*] OF RANGE *)
  1189. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1190. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1191. ELSE
  1192. (* forbid ARRAY [*] OF RANGE *)
  1193. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1194. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1195. (* require RANGE, RANGE, ... RANGE *)
  1196. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1197. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1198. END;
  1199. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1200. ELSE
  1201. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1202. IF recordType.recordScope.firstOperator # NIL THEN
  1203. RETURN;
  1204. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1205. END
  1206. END
  1207. END ResolveArrayStructure;
  1208. (** collect array access operators in a record scope **)
  1209. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1210. VAR
  1211. baseType: SyntaxTree.Type;
  1212. operator: SyntaxTree.Operator;
  1213. isReadOperator, isGeneralOperator: BOOLEAN;
  1214. indexListSize, indexListKind, hashValue: LONGINT;
  1215. BEGIN
  1216. (* if a parent record scope exists, collect the operators there first *)
  1217. baseType := recordScope.ownerRecord.baseType;
  1218. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1219. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1220. END;
  1221. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1222. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1223. END;
  1224. (* go through all operators in the current record scope *)
  1225. operator := recordScope.firstOperator;
  1226. WHILE operator # NIL DO
  1227. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1228. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1229. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1230. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1231. IF isGeneralOperator THEN
  1232. IF isReadOperator THEN
  1233. arrayAccessOperators.generalRead := operator
  1234. ELSE
  1235. arrayAccessOperators.generalWrite := operator
  1236. END
  1237. ELSE
  1238. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1239. IF isReadOperator THEN
  1240. arrayAccessOperators.read[hashValue] := operator
  1241. ELSE
  1242. arrayAccessOperators.write[hashValue] := operator
  1243. END
  1244. END
  1245. END
  1246. ELSE
  1247. Error(operator.position, 'invalid operator')
  1248. END;
  1249. operator := operator.nextOperator
  1250. END
  1251. END CollectArrayAccessOperators;
  1252. (** the hash value of an index operator **)
  1253. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1254. VAR result: LONGINT;
  1255. BEGIN
  1256. IF isTensor THEN
  1257. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1258. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1259. ELSE
  1260. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1261. END
  1262. ELSE
  1263. result := indexListKind
  1264. END;
  1265. RETURN result
  1266. END IndexOperatorHash;
  1267. (** 2 to the power of exponent **)
  1268. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1269. VAR result, i: LONGINT;
  1270. BEGIN
  1271. result := 1;
  1272. FOR i := 1 TO exponent DO
  1273. result := result * 2;
  1274. END;
  1275. RETURN result
  1276. END TwoToThePowerOf;
  1277. (** check if a LEN operator has a correct signature. i.e.
  1278. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1279. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1280. **)
  1281. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1282. VAR
  1283. procedureType: SyntaxTree.ProcedureType;
  1284. returnedArrayType: SyntaxTree.MathArrayType;
  1285. result: BOOLEAN;
  1286. BEGIN
  1287. result := FALSE;
  1288. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1289. IF (procedureType.numberParameters = 0) THEN
  1290. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1291. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1292. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1293. IF returnedArrayType.form = SyntaxTree.Open THEN
  1294. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1295. result := TRUE
  1296. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1297. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1298. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1299. result := TRUE
  1300. END
  1301. END
  1302. END
  1303. END
  1304. END;
  1305. IF result THEN
  1306. (* export symbol automatically *)
  1307. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1308. ELSE
  1309. Error(operator.position, "LEN operator with invalid signature");
  1310. END;
  1311. RETURN result
  1312. END CheckLenOperator;
  1313. (** check if an index operator has a correct signature. i.e.
  1314. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1315. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1316. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1317. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1318. - determine if it is a read or write operator (existance of return type)
  1319. - check index parameters
  1320. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1321. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1322. [LONGINT] -> binary 0 -> 0
  1323. [RANGE] -> binary 1 -> 1
  1324. [LONGINT, LONGINT] -> binary 00 -> 0
  1325. [LONGINT, RANGE] -> binary 01 -> 1
  1326. [RANGE, LONGINT] -> binary 10 -> 2
  1327. [RANGE, RANGE] -> binary 11 -> 3
  1328. etc.
  1329. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1330. - for read operators, check if return type matches the type of data that is read
  1331. - for write operators, check if last parameter type matches the type of data that is written
  1332. **)
  1333. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1334. VAR
  1335. elementType, otherElementType, dataType: SyntaxTree.Type;
  1336. procedureType: SyntaxTree.ProcedureType;
  1337. mathArrayType: SyntaxTree.MathArrayType;
  1338. parameter: SyntaxTree.Parameter;
  1339. parameterCount, rangeCount, i: LONGINT;
  1340. hasTypeError: BOOLEAN;
  1341. BEGIN
  1342. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1343. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1344. (* determine if it is a read or write operator *)
  1345. isReadOperator := (procedureType.returnType # NIL);
  1346. IF isReadOperator THEN
  1347. indexListSize := parameterCount;
  1348. ELSE
  1349. indexListSize := parameterCount - 1;
  1350. END;
  1351. IF indexListSize < 1 THEN
  1352. Error(operator.position, "index operator with too few parameters");
  1353. RETURN FALSE
  1354. END;
  1355. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1356. (* general operator *)
  1357. isGeneralOperator := TRUE;
  1358. IF indexListSize > 1 THEN
  1359. Error(operator.position, "index operator with too many parameters");
  1360. RETURN FALSE
  1361. END;
  1362. (* ARRAY [*] OF RANGE*)
  1363. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1364. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1365. Error(operator.position, "index parameter not dynamic math array of range");
  1366. RETURN FALSE
  1367. END;
  1368. parameter := procedureType.firstParameter.nextParameter
  1369. ELSE
  1370. (* fixed-dim. operator *)
  1371. isGeneralOperator := FALSE;
  1372. (* check number of index parameters *)
  1373. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1374. (* for tensors, limited to a certain size *)
  1375. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1376. Error(operator.position, "too many index parameters for tensor");
  1377. RETURN FALSE
  1378. END
  1379. ELSE
  1380. (* for non-tensors, depends on dimensionality *)
  1381. IF indexListSize # arrayStructure.Dimensionality() THEN
  1382. Error(operator.position, "index parameter count does not match dimensionality");
  1383. RETURN FALSE
  1384. END
  1385. END;
  1386. (* go through all index parameters
  1387. - count the number of ranges
  1388. - determine the index list kind number
  1389. *)
  1390. indexListKind := 0;
  1391. rangeCount := 0;
  1392. parameter := procedureType.firstParameter;
  1393. FOR i := 1 TO indexListSize DO
  1394. indexListKind := indexListKind * 2;
  1395. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1396. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1397. INC(indexListKind);
  1398. INC(rangeCount)
  1399. ELSE
  1400. Error(parameter.position, "integer or range expected");
  1401. RETURN FALSE
  1402. END;
  1403. parameter := parameter.nextParameter
  1404. END;
  1405. END;
  1406. (*
  1407. - for read operators: check type of last parameter
  1408. - for write operators: check return type
  1409. *)
  1410. IF isReadOperator THEN
  1411. dataType := procedureType.returnType (* the return type *)
  1412. ELSE
  1413. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1414. END;
  1415. elementType := arrayStructure.ElementType();
  1416. hasTypeError := FALSE;
  1417. IF isGeneralOperator THEN
  1418. (* ARRAY [?] OF <Element> *)
  1419. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1420. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1421. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1422. hasTypeError := TRUE
  1423. END
  1424. ELSE
  1425. hasTypeError := TRUE
  1426. END
  1427. ELSE
  1428. IF rangeCount = 0 THEN
  1429. (* <Element> *)
  1430. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1431. ELSE
  1432. (* ARRAY [*, *, ..., *] OF <Element> *)
  1433. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1434. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1435. IF mathArrayType.IsFullyDynamic() THEN
  1436. IF mathArrayType.Dimensionality() = rangeCount THEN
  1437. otherElementType := mathArrayType.ElementType();
  1438. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1439. ELSE
  1440. hasTypeError := TRUE
  1441. END
  1442. ELSE
  1443. hasTypeError := TRUE
  1444. END
  1445. ELSE
  1446. hasTypeError := TRUE
  1447. END
  1448. END
  1449. END;
  1450. IF hasTypeError THEN
  1451. IF isReadOperator THEN
  1452. Error(operator.position, "return type does not match")
  1453. ELSE
  1454. Error(parameter.position, "type of last parameter does not match")
  1455. END;
  1456. RETURN FALSE
  1457. END;
  1458. (* export symbol automatically *)
  1459. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1460. RETURN TRUE
  1461. END CheckIndexOperator;
  1462. (** resolve all pending types (late resolving).
  1463. - type fixes are resolved at the end of the declaration phase
  1464. - type fixes may imply new type fixes that are also entered at the end of the list
  1465. **)
  1466. PROCEDURE FixTypes;
  1467. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1468. BEGIN
  1469. prevScope := currentScope;
  1470. p := typeFixes.Get(currentScope);
  1471. WHILE p # NIL DO
  1472. ASSERT(currentScope # NIL);
  1473. ASSERT(p IS SyntaxTree.Type);
  1474. IF p IS SyntaxTree.PointerType THEN
  1475. FixPointerType(p(SyntaxTree.PointerType))
  1476. ELSIF p IS SyntaxTree.ProcedureType THEN
  1477. FixProcedureType(p(SyntaxTree.ProcedureType))
  1478. ELSE
  1479. HALT(100);
  1480. END;
  1481. p := typeFixes.Get(currentScope);
  1482. END;
  1483. currentScope :=prevScope;
  1484. END FixTypes;
  1485. (**
  1486. resolve type x
  1487. - if x is nil then return nil
  1488. - if x cannot be resolved then the result is invalidType else the result is x
  1489. - the resolved type is entered into x.resolved
  1490. **)
  1491. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1492. VAR prev,resolved: SyntaxTree.Type;
  1493. BEGIN
  1494. prev := resolvedType;
  1495. resolvedType := SyntaxTree.invalidType;
  1496. IF x = NIL THEN resolvedType := NIL
  1497. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1498. END;
  1499. resolved := resolvedType;
  1500. resolvedType := prev;
  1501. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1502. RETURN resolved
  1503. END ResolveType;
  1504. (*** compatibility rules ***)
  1505. (**
  1506. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1507. **)
  1508. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1509. VAR result: SyntaxTree.Type;
  1510. BEGIN
  1511. result := SyntaxTree.invalidType;
  1512. IF type = NIL THEN Error(position, "expression of type NIL");
  1513. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1514. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1515. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1516. ELSE result := type.resolved
  1517. END;
  1518. RETURN result
  1519. END RegularType;
  1520. (** returns signature compatibility of procedure types this and to
  1521. - if not compatible then error is reported
  1522. - compatibility means type equality
  1523. **)
  1524. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1525. VAR result: BOOLEAN;
  1526. BEGIN
  1527. result := SameType(to,this);
  1528. IF ~result THEN
  1529. Error(position, "signature incompatible");
  1530. IF VerboseErrorMessage THEN
  1531. Printout.Info("this",this);
  1532. Printout.Info("to",to);
  1533. END;
  1534. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1535. Error(position, "signature incompatible: realtime flag must be inherited");
  1536. END;
  1537. RETURN result
  1538. END SignatureCompatible;
  1539. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1540. - for var parameters compatibility means same type except for
  1541. - formal is of open array of system byte
  1542. - formal is of record type
  1543. - formal is of open array type
  1544. - formal is of open math array type
  1545. - for value parameters compatibllity means assignment compatibility except for
  1546. - formal is of open array type
  1547. if compatible the return true else report error and return false
  1548. **)
  1549. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1550. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1551. BEGIN
  1552. formalType := RegularType(formal.position,formal.type);
  1553. actualType := RegularType(actual.position,actual.type);
  1554. error := FALSE;
  1555. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1556. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1557. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1558. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1559. END;
  1560. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1561. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1562. ELSIF ~IsVariable(actual) THEN
  1563. result := FALSE; error := TRUE;
  1564. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1565. Error(actual.position,"not a variable: no operator for writing");
  1566. ELSE
  1567. Error(actual.position,"is not a variable");
  1568. END;
  1569. IF VerboseErrorMessage THEN
  1570. Printout.Info("actual",actual);
  1571. Printout.Info("formal",formal);
  1572. END;
  1573. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1574. result := CompatibleTo(system,actualType,formalType);
  1575. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1576. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1577. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1578. IF IsArrayStructuredObjectType(actualType) THEN
  1579. actualType := MathArrayStructureOfType(actualType)
  1580. END;
  1581. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1582. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1583. Error(actual.position,"incompatible non-static actual type");
  1584. END;
  1585. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1586. Error(actual.position,"incompatible tensor (use a range expression)");
  1587. END;
  1588. ELSE
  1589. result := SameType(actualType,formalType)
  1590. END
  1591. ELSE
  1592. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1593. actualType := system.characterType;
  1594. END;
  1595. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1596. result := TRUE; (* special rule for WINAPI parameters *)
  1597. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1598. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1599. ELSE
  1600. result := CompatibleTo(system,actualType,formalType);
  1601. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1602. Error(actual.position,"incompatible non-static actual type");
  1603. END;
  1604. END;
  1605. END;
  1606. IF ~result & ~error THEN
  1607. Error(actual.position,"incompatible parameter");
  1608. IF VerboseErrorMessage THEN
  1609. Printout.Info("actual",actual);
  1610. Printout.Info("formal",formal);
  1611. END;
  1612. END;
  1613. RETURN result
  1614. END ParameterCompatible;
  1615. (** check compatibility for expressions of the form left := right
  1616. - if compatible then return true else error report and return false
  1617. - check if left is variable
  1618. - check compatiblity
  1619. **)
  1620. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1621. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1622. BEGIN
  1623. result := FALSE;
  1624. leftType := RegularType(left.position,left.type);
  1625. rightType := RegularType(right.position,right.type);
  1626. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1627. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1628. END;
  1629. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1630. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1631. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1632. (* error already handled *)
  1633. result := TRUE;
  1634. ELSIF ~IsVariable(left) THEN
  1635. Error(left.position,"is not a variable");
  1636. IF VerboseErrorMessage THEN
  1637. Printout.Info("left",left);
  1638. Printout.Info("right",right);
  1639. END;
  1640. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1641. result := TRUE;
  1642. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1643. result := TRUE
  1644. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1645. Error(left.position,"incompatible assignment");
  1646. IF VerboseErrorMessage THEN
  1647. Printout.Info("left",left);
  1648. Printout.Info("right",right);
  1649. END;
  1650. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1651. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1652. Error(right.position,"forbidden assignment of a nested procedure");
  1653. ELSE
  1654. result := TRUE
  1655. END;
  1656. RETURN result
  1657. END AssignmentCompatible;
  1658. (*** values ***)
  1659. (** check and resolve integer value **)
  1660. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1661. VAR hugeint: HUGEINT;
  1662. BEGIN
  1663. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1664. value.SetType(Global.GetIntegerType(system,hugeint));
  1665. resolvedExpression := value
  1666. END VisitIntegerValue;
  1667. (** check and resolve real value **)
  1668. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1669. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1670. BEGIN
  1671. subtype := value(SyntaxTree.RealValue).subtype;
  1672. IF subtype = Scanner.Real THEN
  1673. type := system.realType
  1674. ELSIF subtype = Scanner.Longreal THEN
  1675. type := system.longrealType
  1676. ELSE
  1677. HALT(100)
  1678. END;
  1679. value.SetType(type);
  1680. resolvedExpression := value
  1681. END VisitRealValue;
  1682. (** check and resolve complex value **)
  1683. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1684. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1685. BEGIN
  1686. subtype := value(SyntaxTree.ComplexValue).subtype;
  1687. IF subtype = Scanner.Real THEN
  1688. type := system.complexType
  1689. ELSIF subtype = Scanner.Longreal THEN
  1690. type := system.longcomplexType
  1691. ELSE
  1692. HALT(100)
  1693. END;
  1694. value.SetType(type);
  1695. resolvedExpression := value
  1696. END VisitComplexValue;
  1697. (** check and resolve set value **)
  1698. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1699. BEGIN
  1700. value.SetType(system.setType);
  1701. resolvedExpression := value
  1702. END VisitSetValue;
  1703. (** check and resolve set value **)
  1704. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1705. BEGIN
  1706. value.SetType(SyntaxTree.invalidType);
  1707. resolvedExpression := value
  1708. END VisitMathArrayValue;
  1709. (** check and resolve boolean value **)
  1710. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1711. BEGIN
  1712. value.SetType(system.booleanType);
  1713. resolvedExpression := value
  1714. END VisitBooleanValue;
  1715. (** check and resolve string value **)
  1716. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1717. BEGIN
  1718. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1719. resolvedExpression := value
  1720. END VisitStringValue;
  1721. (** check and resolve character value **)
  1722. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1723. BEGIN
  1724. value.SetType(system.characterType);
  1725. resolvedExpression := value
  1726. END VisitCharacterValue;
  1727. (** check and resolve nil value **)
  1728. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1729. BEGIN
  1730. value.SetType(system.nilType);
  1731. resolvedExpression := value
  1732. END VisitNilValue;
  1733. (** check and resolve enumerator value **)
  1734. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1735. BEGIN
  1736. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1737. ASSERT(value.type # NIL);
  1738. resolvedExpression := value
  1739. END VisitEnumerationValue;
  1740. (*** expressions ***)
  1741. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1742. - check all elements on integer type
  1743. - if element range is constant, then check lower and upper bound
  1744. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1745. if an error occurs then report error and return invalidExpression
  1746. **)
  1747. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1748. VAR
  1749. i: LONGINT;
  1750. element: SyntaxTree.Expression;
  1751. constant: BOOLEAN;
  1752. elements: SyntaxTree.ExpressionList;
  1753. s: SET;
  1754. result: SyntaxTree.Expression;
  1755. value: SyntaxTree.Value;
  1756. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1757. VAR
  1758. left, right: SyntaxTree.Expression;
  1759. elementResult: SyntaxTree.Expression;
  1760. leftInteger, rightInteger, temp: LONGINT;
  1761. BEGIN
  1762. (* set context of range *)
  1763. IF element IS SyntaxTree.RangeExpression THEN
  1764. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1765. END;
  1766. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1767. IF elementResult = SyntaxTree.invalidExpression THEN
  1768. (* error already reported *)
  1769. constant := FALSE
  1770. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1771. (* the element is a range expression *)
  1772. (* extract left and right hand side of range *)
  1773. left := elementResult(SyntaxTree.RangeExpression).first;
  1774. right := elementResult(SyntaxTree.RangeExpression).last;
  1775. (* guaranteed by VisitRangeExpression: *)
  1776. ASSERT((left # NIL) & (right # NIL));
  1777. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1778. ELSE
  1779. (* the element is not a range expression *)
  1780. (* check type and add conversion if needed *)
  1781. IF IsIntegerType(elementResult.type.resolved) THEN
  1782. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1783. ELSE
  1784. Error(elementResult.position, "non integer element in set");
  1785. elementResult := SyntaxTree.invalidExpression;
  1786. constant := FALSE
  1787. END;
  1788. left := elementResult;
  1789. right := elementResult
  1790. END;
  1791. IF elementResult # SyntaxTree.invalidExpression THEN
  1792. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1793. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1794. Error(left.position,"not allowed set integer value");
  1795. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1796. Error(right.position,"not allowed set integer value");
  1797. END
  1798. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1799. Error(right.position,"not allowed set integer value");
  1800. ELSE
  1801. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1802. s := {};
  1803. ELSE
  1804. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1805. IF leftInteger < 0 THEN leftInteger := 0 END;
  1806. (*!!!!!!!!! this is a hack !!!!!!! *)
  1807. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1808. extends the range x..31 to x..63 !!!!!! *)
  1809. s := s + {leftInteger..rightInteger};
  1810. END;
  1811. END;
  1812. ELSE
  1813. constant := FALSE;
  1814. END
  1815. END;
  1816. RETURN elementResult
  1817. END CheckElement;
  1818. BEGIN
  1819. result := set; constant := TRUE; s := {}; elements := set.elements;
  1820. IF elements # NIL THEN
  1821. FOR i := 0 TO elements.Length()-1 DO
  1822. element := elements.GetExpression(i);
  1823. element := CheckElement(element);
  1824. IF element = SyntaxTree.invalidExpression THEN
  1825. result := SyntaxTree.invalidExpression
  1826. END;
  1827. elements.SetExpression(i,element);
  1828. END;
  1829. END;
  1830. IF constant THEN
  1831. value := SyntaxTree.NewSetValue(set.position,s);
  1832. value.SetType(system.setType);
  1833. result.SetResolved(value);
  1834. END;
  1835. (* optimization possible
  1836. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1837. left this to the programmer...
  1838. *)
  1839. result.SetType(system.setType);
  1840. resolvedExpression := result;
  1841. END VisitSet;
  1842. (*
  1843. old variant: quite generic but needs better conversion handling, do this?
  1844. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1845. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1846. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1847. BEGIN
  1848. type := NIL;
  1849. numberElements := x.elements.Length();
  1850. FOR i := 0 TO numberElements-1 DO
  1851. expression := x.elements.GetExpression(i);
  1852. position := expression.position;
  1853. expression := ResolveExpression(x.elements.GetExpression(i));
  1854. x.elements.SetExpression(i,de);
  1855. IF type = NIL THEN
  1856. type := expression.type;
  1857. ELSIF CompatibleTo(system,expression.type,type) THEN
  1858. (* ok *)
  1859. ELSIF CompatibleTo(system,type,expression.type) THEN
  1860. type := expression.type
  1861. ELSE
  1862. Error(expression.position, "incompatible element types");
  1863. type := SyntaxTree.invalidType;
  1864. END;
  1865. END;
  1866. isValue := TRUE;
  1867. FOR i := 0 TO numberElements-1 DO
  1868. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1869. x.elements.SetExpression(i,expression);
  1870. isValue := isValue & (expression.resolved # NIL);
  1871. END;
  1872. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1873. arrayType.SetArrayBase(type);
  1874. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1875. IF isValue THEN
  1876. value := SyntaxTree.NewMathArrayValue(position);
  1877. value.SetElements(x.elements);
  1878. x.SetResolved(value);
  1879. END;
  1880. x.SetType(arrayType);
  1881. resolvedExpression := x;
  1882. END VisitMathArrayExpression;
  1883. *)
  1884. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1885. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1886. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1887. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1888. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1889. BEGIN
  1890. numberElements := x.elements.Length();
  1891. FOR i := 0 TO numberElements-1 DO
  1892. expression := x.elements.GetExpression(i);
  1893. IF expression IS SyntaxTree.MathArrayExpression THEN
  1894. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1895. ELSE
  1896. position := expression.position;
  1897. expression := ResolveExpression(x.elements.GetExpression(i));
  1898. x.elements.SetExpression(i,expression);
  1899. IF type = NIL THEN
  1900. type := expression.type;
  1901. ELSIF CompatibleTo(system,expression.type,type) THEN
  1902. (* ok *)
  1903. ELSIF CompatibleTo(system,type,expression.type) THEN
  1904. type := expression.type
  1905. ELSE
  1906. Error(expression.position, "incompatible element types");
  1907. type := SyntaxTree.invalidType;
  1908. END;
  1909. END;
  1910. END;
  1911. END RecursivelyFindType;
  1912. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1913. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1914. BEGIN
  1915. numberElements := x.elements.Length();
  1916. FOR i := 0 TO numberElements-1 DO
  1917. expression := x.elements.GetExpression(i);
  1918. IF expression IS SyntaxTree.MathArrayExpression THEN
  1919. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1920. ELSE
  1921. position := expression.position;
  1922. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1923. x.elements.SetExpression(i,expression);
  1924. isValue := isValue & (expression.resolved # NIL);
  1925. END;
  1926. END;
  1927. END RecursivelySetExpression;
  1928. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1929. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1930. arrayType: SyntaxTree.MathArrayType;
  1931. BEGIN
  1932. numberElements := x.elements.Length();
  1933. baseType := NIL;
  1934. gsize := 0;
  1935. FOR i := 0 TO numberElements-1 DO
  1936. expression := x.elements.GetExpression(i);
  1937. IF expression IS SyntaxTree.MathArrayExpression THEN
  1938. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1939. IF i=0 THEN
  1940. gsize := size;
  1941. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1942. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1943. ELSE expression.SetType(baseType)
  1944. END;
  1945. ELSIF baseType = NIL THEN baseType := type;
  1946. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1947. END;
  1948. END;
  1949. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1950. arrayType.SetArrayBase(baseType);
  1951. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1952. RETURN ResolveType(arrayType);
  1953. END RecursivelySetType;
  1954. BEGIN
  1955. type := NIL;
  1956. RecursivelyFindType(x);
  1957. isValue := TRUE;
  1958. RecursivelySetExpression(x);
  1959. arrayType := RecursivelySetType(x);
  1960. x.SetType(arrayType);
  1961. IF isValue THEN
  1962. value := SyntaxTree.NewMathArrayValue(x.position);
  1963. value.SetArray(x);
  1964. x.SetResolved(value);
  1965. value.SetType(arrayType);
  1966. END;
  1967. x.SetType(arrayType);
  1968. resolvedExpression := x;
  1969. END VisitMathArrayExpression;
  1970. (** check and resolve unary expression **)
  1971. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1972. VAR
  1973. left: SyntaxTree.Expression;
  1974. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1975. bool: BOOLEAN;
  1976. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1977. value: SyntaxTree.Value;
  1978. BEGIN
  1979. type := SyntaxTree.invalidType;
  1980. left := ResolveExpression(unaryExpression.left);
  1981. unaryExpression.SetLeft(left);
  1982. operator := unaryExpression.operator;
  1983. result := unaryExpression;
  1984. IF ~system.operatorDefined[operator] THEN
  1985. Error(left.position,"Operator Not Defined");
  1986. RETURN
  1987. ELSIF left.type = NIL THEN
  1988. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1989. resolvedExpression := SyntaxTree.invalidExpression;
  1990. RETURN
  1991. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1992. RETURN
  1993. END;
  1994. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1995. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1996. END;
  1997. IF operatorCall # NIL THEN
  1998. result := operatorCall;
  1999. type := operatorCall.type;
  2000. (* admissible operators
  2001. Minus number, set
  2002. Not boolean
  2003. *)
  2004. ELSE
  2005. CASE unaryExpression.operator OF
  2006. |Scanner.Minus:
  2007. IF IsIntegerType(left.type.resolved) THEN
  2008. IF left.resolved # NIL THEN
  2009. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2010. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2011. result.SetResolved(value);
  2012. type := Global.GetIntegerType(system,int);
  2013. value.SetType(type);
  2014. ELSE
  2015. type := left.type
  2016. END
  2017. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2018. IF IsRealValue(left,real) THEN
  2019. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2020. result.SetResolved(value);
  2021. type := left.type;
  2022. value.SetType(type);
  2023. ELSE
  2024. type := left.type;
  2025. END;
  2026. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2027. IF IsSetValue(left,set) THEN
  2028. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2029. result.SetResolved(value);
  2030. type := left.type;
  2031. value.SetType(type);
  2032. ELSE
  2033. type := left.type;
  2034. END;
  2035. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2036. IF IsComplexValue(left, real, imaginary) THEN
  2037. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2038. result.SetResolved(value);
  2039. type := left.type;
  2040. value.SetType(type);
  2041. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2042. ELSE
  2043. type := left.type;
  2044. END
  2045. ELSE
  2046. Error(left.position,"unary operator not applicable");
  2047. END;
  2048. |Scanner.Not:
  2049. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2050. IF IsBooleanValue(left,bool) THEN
  2051. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2052. result.SetResolved(value);
  2053. type := system.booleanType;
  2054. value.SetType(type);
  2055. ELSE
  2056. type := system.booleanType;
  2057. END;
  2058. ELSE
  2059. Error(left.position,"unary operator not applicable");
  2060. END;
  2061. |Scanner.Plus:
  2062. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2063. result := left; type := left.type;
  2064. ELSE
  2065. Error(left.position,"unary operator not applicable");
  2066. END;
  2067. (* ADDRESS OF *)
  2068. |Scanner.Address:
  2069. IF HasAddress(left) THEN
  2070. type := system.addressType;
  2071. ELSE
  2072. type := SyntaxTree.invalidType;
  2073. Error(left.position,"has no address");
  2074. Printout.Info("par", left);
  2075. END;
  2076. (* SIZE OF *)
  2077. |Scanner.Size:
  2078. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2079. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2080. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2081. value := SyntaxTree.NewIntegerValue(left.position, int);
  2082. result.SetResolved(value);
  2083. type := Global.GetIntegerType(system,int);
  2084. value.SetType(type)
  2085. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2086. ELSE
  2087. (* for variables, system sizeof could represent the physically occupied size
  2088. determined via the type descriptor, implement that ? *)
  2089. Error(left.position,"is not a type symbol");
  2090. END
  2091. (* ALIAS OF *)
  2092. |Scanner.Alias:
  2093. type := left.type.resolved;
  2094. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2095. type := SyntaxTree.invalidType;
  2096. Error(left.position,"alias on non math array type");
  2097. END;
  2098. ELSE
  2099. Error(left.position,"unary operator not defined");
  2100. END;
  2101. END;
  2102. result.SetType(type);
  2103. resolvedExpression := result
  2104. END VisitUnaryExpression;
  2105. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2106. VAR
  2107. result: SyntaxTree.Expression;
  2108. array: SyntaxTree.MathArrayExpression;
  2109. value: SyntaxTree.MathArrayValue;
  2110. isValue: BOOLEAN;
  2111. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2112. BEGIN
  2113. type := type.resolved;
  2114. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2115. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2116. END;
  2117. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2118. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2119. END;
  2120. RETURN type
  2121. END BaseType;
  2122. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2123. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2124. BEGIN
  2125. numberElements := x.elements.Length();
  2126. FOR i := 0 TO numberElements-1 DO
  2127. expression := x.elements.GetExpression(i);
  2128. IF expression IS SyntaxTree.MathArrayExpression THEN
  2129. array := SyntaxTree.NewMathArrayExpression(position);
  2130. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2131. expression := array;
  2132. ELSE
  2133. position := expression.position;
  2134. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2135. isValue := isValue & (expression.resolved # NIL);
  2136. END;
  2137. to.elements.AddExpression(expression);
  2138. END;
  2139. END RecursivelyConvert;
  2140. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2141. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2142. arrayType: SyntaxTree.MathArrayType;
  2143. BEGIN
  2144. numberElements := x.elements.Length();
  2145. baseType := NIL;
  2146. gsize := 0;
  2147. FOR i := 0 TO numberElements-1 DO
  2148. expression := x.elements.GetExpression(i);
  2149. IF expression IS SyntaxTree.MathArrayExpression THEN
  2150. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2151. IF i=0 THEN
  2152. gsize := size;
  2153. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2154. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2155. ELSE expression.SetType(baseType)
  2156. END;
  2157. ELSIF baseType = NIL THEN baseType := type;
  2158. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2159. END;
  2160. END;
  2161. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2162. arrayType.SetArrayBase(baseType);
  2163. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2164. RETURN ResolveType(arrayType);
  2165. END RecursivelySetType;
  2166. BEGIN
  2167. result := SyntaxTree.invalidExpression;
  2168. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2169. result := expression (* do not convert *)
  2170. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2171. isValue := TRUE;
  2172. type := BaseType(type);
  2173. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2174. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2175. value := SyntaxTree.NewMathArrayValue(array.position);
  2176. value.SetArray(array);
  2177. value.SetType(RecursivelySetType(array));
  2178. result := value;
  2179. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2180. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2181. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2182. IF result = NIL THEN
  2183. result := SyntaxTree.invalidExpression;
  2184. Error(position, "incompatible conversion");
  2185. IF VerboseErrorMessage THEN
  2186. Printout.Info("expression",expression);
  2187. Printout.Info("type",type);
  2188. END;
  2189. END;
  2190. END;
  2191. RETURN result
  2192. END MathArrayConversion;
  2193. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2194. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2195. BEGIN
  2196. result := expression; type := type.resolved;
  2197. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2198. (* skip, no conversion *)
  2199. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2200. int := expression(SyntaxTree.IntegerValue).hvalue;
  2201. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2202. int := Global.ConvertSigned(int,system.SizeOf(type));
  2203. result := SyntaxTree.NewIntegerValue(position,int);
  2204. result.SetType(type);
  2205. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2206. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2207. result := SyntaxTree.NewIntegerValue(position,int);
  2208. result.SetType(type);
  2209. ELSIF (type IS SyntaxTree.FloatType) THEN
  2210. result := SyntaxTree.NewRealValue(expression.position,int);
  2211. result.SetType(type);
  2212. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2213. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2214. result.SetType(type);
  2215. ELSIF (type IS SyntaxTree.SetType) THEN
  2216. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2217. result.SetType(type);
  2218. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2219. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2222. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2223. Error(position, "huge integer value incompatible to enumeration");
  2224. END;
  2225. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2226. result.SetType(type);
  2227. ELSIF (type IS SyntaxTree.PortType) THEN
  2228. result := ConvertValue(position, expression, system.integerType);
  2229. ELSE
  2230. Error(position, "integer value cannot be converted");
  2231. result := SyntaxTree.invalidExpression;
  2232. IF VerboseErrorMessage THEN
  2233. Printout.Info("expression",expression);
  2234. Printout.Info("type",type);
  2235. END;
  2236. END;
  2237. ELSIF IsRealValue(expression,real) THEN
  2238. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2239. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2240. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2241. result.SetType(type);
  2242. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2243. int := ENTIERH(real);
  2244. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2245. result.SetType(type);
  2246. ELSIF (type IS SyntaxTree.FloatType) THEN
  2247. result := SyntaxTree.NewRealValue(position,real);
  2248. result.SetType(type);
  2249. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2250. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2251. result.SetType(type);
  2252. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2253. ELSIF (type IS SyntaxTree.PortType) THEN
  2254. result := ConvertValue(position, expression, system.integerType);
  2255. ELSE
  2256. Error(position, "real value cannot be converted");
  2257. result := SyntaxTree.invalidExpression;
  2258. END
  2259. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2260. IF (type IS SyntaxTree.ComplexType) THEN
  2261. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2262. result.SetType(type);
  2263. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2264. ELSE
  2265. Error(position, "complex value cannot be converted");
  2266. result := SyntaxTree.invalidExpression;
  2267. END
  2268. ELSIF IsSetValue(expression,set) THEN
  2269. IF (type IS SyntaxTree.IntegerType) THEN
  2270. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2271. result.SetType(type);
  2272. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2273. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2274. result.SetType(type);
  2275. ELSIF (type IS SyntaxTree.PortType) THEN
  2276. result := ConvertValue(position, expression, system.integerType);
  2277. ELSE
  2278. Error(position, "set value cannot be converted");
  2279. result := SyntaxTree.invalidExpression;
  2280. END;
  2281. ELSIF IsStringValue(expression,string) THEN
  2282. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2283. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2284. result.SetType(type);
  2285. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2286. ELSE
  2287. Error(position, "string value cannot be converted");
  2288. result := SyntaxTree.invalidExpression;
  2289. END;
  2290. ELSIF IsCharacterValue(expression,char) THEN
  2291. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2292. string[0] := char; string[1] := 0X;
  2293. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2294. result := SyntaxTree.NewStringValue(expression.position,string);
  2295. result.SetType(type);
  2296. ELSIF (type IS SyntaxTree.ByteType) THEN
  2297. (* do not simply set the new type as this could invalidate types of constants *)
  2298. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2299. result.SetType(type)
  2300. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2301. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2302. result.SetType(type);
  2303. ELSIF (type IS SyntaxTree.SetType) THEN
  2304. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2305. result.SetType(type);
  2306. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2307. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2308. result.SetType(type);
  2309. ELSIF (type IS SyntaxTree.PortType) THEN
  2310. result := ConvertValue(position, expression, system.integerType);
  2311. ELSE
  2312. Error(position, "character value cannot be converted");
  2313. result := SyntaxTree.invalidExpression;
  2314. END;
  2315. ELSIF expression IS SyntaxTree.NilValue THEN
  2316. IF type IS SyntaxTree.AddressType THEN
  2317. result := SyntaxTree.NewIntegerValue(position,0);
  2318. result.SetType(type);
  2319. ELSE
  2320. result := expression;
  2321. END;
  2322. (* nothing to be done *)
  2323. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2324. result := MathArrayConversion(position, expression,type);
  2325. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2326. int := expression(SyntaxTree.EnumerationValue).value;
  2327. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2328. int := Global.ConvertSigned(int,system.SizeOf(type));
  2329. result := SyntaxTree.NewIntegerValue(position,int);
  2330. result.SetType(type);
  2331. ELSE
  2332. result := expression;
  2333. END;
  2334. (* nothing to be done *)
  2335. ELSE
  2336. Error(position, "expression cannot be converted");
  2337. IF VerboseErrorMessage THEN
  2338. Printout.Info("expression",expression);
  2339. Printout.Info("type",type);
  2340. END;
  2341. result := SyntaxTree.invalidExpression;
  2342. END;
  2343. RETURN result
  2344. END ConvertValue;
  2345. (**
  2346. return a conversion of an expression to a given type
  2347. - if expression is already of same type then return expression
  2348. - if incompatible conversion then report error and return invalidExpression
  2349. **)
  2350. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2351. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2352. BEGIN
  2353. type := type.resolved;
  2354. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2355. result := expression;
  2356. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2357. ELSIF expression = NIL THEN (* NIL expression *)
  2358. ELSIF expression.type = NIL THEN
  2359. Error(position, "expression of type NIL cannot be converted");
  2360. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2361. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2362. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2363. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2364. ELSIF expression.resolved # NIL THEN (* value *)
  2365. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2366. IF value IS SyntaxTree.Value THEN
  2367. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2368. result.SetResolved(value(SyntaxTree.Value));
  2369. result.SetType(value.type);
  2370. ELSE
  2371. result := value
  2372. END;
  2373. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2374. expressionList := SyntaxTree.NewExpressionList();
  2375. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2376. typeDeclaration.SetDeclaredType(type);
  2377. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2378. typeSymbol.SetType(typeDeclaration.type);
  2379. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2380. expressionList.AddExpression(expression);
  2381. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2382. result.SetType(type);
  2383. ELSIF IsArrayStructuredObjectType(type) THEN
  2384. (* no type can be converted to an array-structured object type *)
  2385. HALT(100)
  2386. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2387. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2388. result := MathArrayConversion(position, expression,type);
  2389. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2390. expression := ConvertToMathArray(expression);
  2391. type := MathArrayStructureOfType(type);
  2392. result := MathArrayConversion(position, expression, type)
  2393. ELSE
  2394. Error(expression.position,"cannot convert non array type to array type")
  2395. END;
  2396. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2397. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2398. OR ~(type IS SyntaxTree.ArrayType) THEN
  2399. Error(expression.position,"cannot convert array type to non-array type")
  2400. END;
  2401. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2402. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2403. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2404. (*skip, no conversion*)
  2405. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2406. (* skip, no conversion *)
  2407. ELSE
  2408. ASSERT(~(type IS SyntaxTree.RangeType));
  2409. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2410. ASSERT(type # NIL);
  2411. END;
  2412. RETURN result
  2413. END NewConversion;
  2414. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2415. BEGIN
  2416. IF CompatibleTo(system,expression.type, type) THEN
  2417. RETURN NewConversion(position, expression, type, NIL);
  2418. ELSE
  2419. Error(expression.position, "incompatible expression");
  2420. RETURN SyntaxTree.invalidExpression
  2421. END;
  2422. END CompatibleConversion;
  2423. (**
  2424. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2425. **)
  2426. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2427. VAR leftType,rightType: SyntaxTree.Type;
  2428. BEGIN
  2429. IF left.type = NIL THEN Error(left.position,"no type")
  2430. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2431. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2432. (* error already handled *)
  2433. ELSE
  2434. leftType := left.type.resolved; rightType := right.type.resolved;
  2435. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2436. right := NewConversion(right.position, right, leftType, NIL);
  2437. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2438. left := NewConversion(left.position,left,rightType,NIL);
  2439. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2440. right := NewConversion(right.position, right, leftType, NIL);
  2441. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2442. left := NewConversion(left.position,left,rightType,NIL);
  2443. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2444. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2445. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2446. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2447. ELSIF
  2448. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2449. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2450. (* must be the case LONGREAL / COMPLEX ) *)
  2451. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2452. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2453. ELSE
  2454. Error(left.position,"incompatible operands");
  2455. END;
  2456. END;
  2457. END ConvertOperands;
  2458. (** find and return best operator matching to parameter list (nil, if none)
  2459. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2460. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2461. **)
  2462. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2463. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2464. identifier: SyntaxTree.Identifier;
  2465. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2466. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2467. BEGIN
  2468. operator := scope.firstOperator;
  2469. WHILE(operator # NIL) DO
  2470. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2471. procedureType := operator.type(SyntaxTree.ProcedureType);
  2472. distance := Distance(system, procedureType,actualParameters);
  2473. IF (distance < Infinity) THEN
  2474. IF returnType # NIL THEN
  2475. IF procedureType.returnType = NIL THEN
  2476. distance := Infinity
  2477. ELSE
  2478. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2479. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2480. END;
  2481. END;
  2482. END;
  2483. (*
  2484. IF distance < Infinity THEN
  2485. TRACE(distance, operator);
  2486. Printout.Info("potential operator",operator);
  2487. ELSE
  2488. Printout.Info("operator not possible",operator);
  2489. END;
  2490. *)
  2491. IF distance < bestDistance THEN
  2492. bestDistance := distance;
  2493. bestOperator := operator;
  2494. END;
  2495. END;
  2496. operator := operator.nextOperator;
  2497. END;
  2498. (*
  2499. Printout.Info("taken operator",bestOperator);
  2500. *)
  2501. END FindInScope;
  2502. BEGIN
  2503. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2504. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2505. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2506. import := currentScope.ownerModule.moduleScope.firstImport;
  2507. WHILE (bestDistance > 0) & (import # NIL) DO
  2508. IF import.module # NIL THEN
  2509. identifier := Global.GetIdentifier(operator,import.module.case);
  2510. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2511. END;
  2512. import := import.nextImport;
  2513. END;
  2514. RETURN bestOperator
  2515. END FindOperator;
  2516. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2517. BEGIN
  2518. currentScope := scope;
  2519. END SetCurrentScope;
  2520. (**
  2521. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2522. - handle LEN and DIM operator for array-structured object types
  2523. - find operator, if found then
  2524. - if in other module then add import designator
  2525. - create symbol designator for operator
  2526. - if error then return invalidExpression, if no operator then return NIL
  2527. **)
  2528. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2529. VAR
  2530. operator: SyntaxTree.Operator;
  2531. import: SyntaxTree.Import;
  2532. expression, result: SyntaxTree.Expression;
  2533. designator: SyntaxTree.Designator;
  2534. actualParameters, tempList: SyntaxTree.ExpressionList;
  2535. recordType: SyntaxTree.RecordType;
  2536. castReturnType : SyntaxTree.MathArrayType;
  2537. BEGIN
  2538. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2539. result := SyntaxTree.invalidExpression
  2540. ELSIF leftExpression = NIL THEN
  2541. result := NIL
  2542. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2543. (* LEN or DIM operator on array-structured object type *)
  2544. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2545. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2546. IF recordType.arrayAccessOperators.len = NIL THEN
  2547. Error(position, "call of undeclared LEN operator");
  2548. result := SyntaxTree.invalidExpression
  2549. ELSE
  2550. ASSERT(leftExpression IS SyntaxTree.Designator);
  2551. designator := leftExpression(SyntaxTree.Designator);
  2552. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2553. ASSERT(expression IS SyntaxTree.Designator);
  2554. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2555. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2556. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2557. result := designator
  2558. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2559. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2560. tempList := SyntaxTree.NewExpressionList();
  2561. tempList.AddExpression(rightExpression);
  2562. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2563. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2564. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2565. tempList := SyntaxTree.NewExpressionList();
  2566. tempList.AddExpression(designator);
  2567. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2568. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2569. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2570. END
  2571. END;
  2572. ELSE
  2573. (* import OCArrayBase if needed *)
  2574. IF ~arrayBaseImported THEN
  2575. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2576. (* operators on complex numbers *)
  2577. ImportModule(Global.ArrayBaseName,position);
  2578. arrayBaseImported := TRUE;
  2579. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2580. IF op = Global.Dim THEN
  2581. (* not existing in OCArrayBase *)
  2582. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2583. (* not existing in OCArrayBase *)
  2584. ELSE
  2585. ImportModule(Global.ArrayBaseName,position);
  2586. arrayBaseImported := TRUE;
  2587. END
  2588. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2589. ImportModule(Global.ArrayBaseName,position);
  2590. arrayBaseImported := TRUE
  2591. END;
  2592. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2593. (* LEN(RANGE) *)
  2594. ImportModule(Global.ArrayBaseName,position);
  2595. arrayBaseImported := TRUE;
  2596. END;
  2597. END;
  2598. actualParameters := SyntaxTree.NewExpressionList();
  2599. actualParameters.AddExpression(leftExpression);
  2600. IF rightExpression # NIL THEN
  2601. actualParameters.AddExpression(rightExpression)
  2602. END;
  2603. operator := FindOperator(system,op,actualParameters,resultType);
  2604. IF operator # NIL THEN
  2605. designator := NIL;
  2606. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2607. import := currentScope.ownerModule.moduleScope.firstImport;
  2608. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2609. import := import.nextImport;
  2610. END;
  2611. expression := NewSymbolDesignator(position,NIL,import);
  2612. designator := expression(SyntaxTree.Designator);
  2613. END;
  2614. expression := NewSymbolDesignator(position,designator,operator);
  2615. designator := expression(SyntaxTree.Designator);
  2616. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2617. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2618. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2619. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2620. result.SetType(castReturnType);
  2621. END;
  2622. ELSE
  2623. result := NIL;
  2624. END;
  2625. END;
  2626. RETURN result
  2627. END NewOperatorCall;
  2628. (** check and resolve binary expression **)
  2629. (*! clean up *)
  2630. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2631. VAR left,right,result: SyntaxTree.Expression;
  2632. leftType, rightType: SyntaxTree.Type;
  2633. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2634. cl,cr: CHAR;
  2635. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2636. type: SyntaxTree.Type;
  2637. value: SyntaxTree.Value;
  2638. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2639. integerConstantFolding: BOOLEAN;
  2640. list: SyntaxTree.ExpressionList;
  2641. PROCEDURE NewBool(v: BOOLEAN);
  2642. BEGIN
  2643. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2644. value.SetType(system.booleanType);
  2645. result.SetResolved(value);
  2646. type := system.booleanType
  2647. END NewBool;
  2648. PROCEDURE NewSet(v: SET);
  2649. BEGIN
  2650. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2651. value.SetType(system.setType);
  2652. result.SetResolved(value);
  2653. type := system.setType;
  2654. END NewSet;
  2655. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2656. BEGIN
  2657. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2658. (* type cast to "larger" type only if the value is still in the range *)
  2659. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2660. value.SetType(t);
  2661. END;
  2662. result.SetResolved(value);
  2663. type := value.type;
  2664. END NewInteger;
  2665. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2666. BEGIN
  2667. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2668. value.SetType(t);
  2669. result.SetResolved(value);
  2670. type := t;
  2671. END NewReal;
  2672. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2673. BEGIN
  2674. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2675. value.SetType(t);
  2676. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2677. result.SetResolved(value);
  2678. type := t;
  2679. END NewComplex;
  2680. BEGIN
  2681. type := SyntaxTree.invalidType;
  2682. left := ResolveExpression(binaryExpression.left);
  2683. right := ResolveExpression(binaryExpression.right);
  2684. binaryExpression.SetLeft(left);
  2685. binaryExpression.SetRight(right);
  2686. result := binaryExpression;
  2687. operator := binaryExpression.operator;
  2688. IF ~system.operatorDefined[operator] THEN
  2689. Error(left.position,"Operator Not Defined");
  2690. result := SyntaxTree.invalidExpression;
  2691. RETURN
  2692. END;
  2693. IF left.type = NIL THEN
  2694. Error(left.position,"Expression has no result type");
  2695. result := SyntaxTree.invalidExpression;
  2696. RETURN;
  2697. END;
  2698. IF right.type = NIL THEN
  2699. Error(right.position,"Expression has no result type");
  2700. result := SyntaxTree.invalidExpression;
  2701. RETURN;
  2702. END;
  2703. leftType := left.type.resolved; rightType := right.type.resolved;
  2704. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2705. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2706. END;
  2707. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2708. list := SyntaxTree.NewExpressionList();
  2709. list.AddExpression(right);
  2710. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2711. END;
  2712. IF operatorCall # NIL THEN
  2713. result := operatorCall;
  2714. type := operatorCall.type;
  2715. (* admissible operators:
  2716. Times, Plus, Minus numeric numeric numeric
  2717. set set set
  2718. Slash numeric numeric real /complex
  2719. set set set
  2720. Div , Mod integer integer integer
  2721. And, Or bool bool bool
  2722. Equal, Unequal basic basic bool
  2723. pointer pointer bool
  2724. object object bool
  2725. record record bool
  2726. string string bool
  2727. enumerator enumerator bool
  2728. Less, LessEqual,
  2729. Greater, GreaterEqual integer/real integer/real bool
  2730. enumerator enumerator bool
  2731. In integer set bool
  2732. Is pointer type bool
  2733. object type bool
  2734. record type bool
  2735. Upto: special abbreviation for a..b
  2736. *)
  2737. ELSIF (left.type = NIL) THEN
  2738. Error(left.position,"type (left operand) = NIL in binary expression");
  2739. D.Str("nil type in "); D.Type(left); D.Ln;
  2740. result := SyntaxTree.invalidExpression;
  2741. ELSIF (right.type = NIL) THEN
  2742. Error(right.position,"type (right operand) = NIL in binary expression");
  2743. result := SyntaxTree.invalidExpression;
  2744. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2745. result := SyntaxTree.invalidExpression;
  2746. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2747. HALT(100);
  2748. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2749. type := system.booleanType;
  2750. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2751. Error(right.position,"is not a type ");
  2752. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2753. Error(binaryExpression.position,"is not a type extension of ");
  2754. IF VerboseErrorMessage THEN
  2755. Printout.Info("left",left);
  2756. Printout.Info("right",right);
  2757. END;
  2758. ELSIF IsUnsafePointer(left.type) THEN
  2759. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2760. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2761. NewBool(TRUE)
  2762. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2763. NewBool(TRUE);
  2764. ELSIF IsUnextensibleRecord(left) THEN
  2765. NewBool(FALSE)
  2766. END
  2767. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2768. Error(right.position,"must not be a type");
  2769. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2770. Error(left.position,"must not be a type");
  2771. ELSIF operator = Scanner.In THEN (* left IN right *)
  2772. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2773. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2774. NewBool(il IN sr);
  2775. ELSE
  2776. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2777. left := NewConversion(left.position, left, system.longintType,NIL);
  2778. binaryExpression.SetLeft(left)
  2779. END;
  2780. type := system.booleanType;
  2781. END
  2782. ELSE
  2783. Error(binaryExpression.position, "incompatible operands");
  2784. END
  2785. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2786. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2787. Error(binaryExpression.position,"incompatible operands");
  2788. END;
  2789. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2790. ELSE Error(binaryExpression.position,"operator not defined 1")
  2791. END
  2792. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2793. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2794. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2795. Error(binaryExpression.position,"incompatible operands");
  2796. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2797. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2798. ConvertOperands(left, right);
  2799. binaryExpression.SetLeft(left);
  2800. binaryExpression.SetRight(right);
  2801. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2802. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2803. END;
  2804. type := system.booleanType;
  2805. ELSE
  2806. Error(binaryExpression.position,"operator not defined 3");
  2807. END
  2808. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2809. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2810. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2811. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2812. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2813. CASE operator OF
  2814. |Scanner.Equal: NewBool(strl^=strr^);
  2815. |Scanner.Unequal:NewBool(strl^#strr^);
  2816. |Scanner.Less: NewBool(strl^<strr^);
  2817. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2818. |Scanner.Greater: NewBool(strl^>strr^);
  2819. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2820. ELSE
  2821. Error(binaryExpression.position,"operator not defined 4");
  2822. END;
  2823. END;
  2824. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2825. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2826. type := system.booleanType
  2827. ELSE
  2828. Error(binaryExpression.position,"operator not defined 5");
  2829. END;
  2830. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2831. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2832. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2833. type := system.booleanType;
  2834. ELSE
  2835. Error(binaryExpression.position,"operator not defined 6");
  2836. END
  2837. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2838. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2839. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2840. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2841. type := system.booleanType
  2842. ELSE
  2843. Error(binaryExpression.position,"operator not defined for enumerators");
  2844. END;
  2845. ELSE
  2846. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2847. END;
  2848. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2849. type := system.booleanType;
  2850. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2851. type := system.booleanType;
  2852. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2853. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2854. THEN
  2855. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2856. IF (leftType # rightType) THEN
  2857. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2858. ConvertOperands(left,right); (* operands must be of the same type here *)
  2859. END;
  2860. binaryExpression.SetLeft(left);
  2861. binaryExpression.SetRight(right);
  2862. leftType := left.type.resolved;
  2863. rightType := right.type.resolved;
  2864. END;
  2865. type := leftType;
  2866. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2867. Error(binaryExpression.position,"conversion failed ?");
  2868. IF VerboseErrorMessage THEN
  2869. Printout.Info("left",left);
  2870. Printout.Info("right",right);
  2871. END;
  2872. ELSIF IsIntegerType(leftType) THEN
  2873. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2874. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2875. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2876. Error(binaryExpression.position,"division by zero");
  2877. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2878. Error(binaryExpression.position,"integer division by negative number");
  2879. END;
  2880. END;
  2881. (* constant folding *)
  2882. (* bootstrap64
  2883. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2884. CASE operator OF
  2885. |Scanner.Plus: NewInteger(il+ir,left.type);
  2886. |Scanner.Minus: NewInteger(il-ir,left.type);
  2887. |Scanner.Times: NewInteger(il*ir,left.type);
  2888. |Scanner.Slash:
  2889. IF ir # 0 THEN
  2890. NewReal(il/ir, system.realType);
  2891. END;
  2892. |Scanner.Mod:
  2893. IF ir > 0 THEN
  2894. NewInteger(il MOD ir,left.type);
  2895. END;
  2896. |Scanner.Div:
  2897. IF ir > 0 THEN
  2898. NewInteger(il DIV ir,left.type);
  2899. END;
  2900. |Scanner.Equal: NewBool(il=ir);
  2901. |Scanner.Unequal:NewBool(il#ir);
  2902. |Scanner.Less: NewBool(il<ir);
  2903. |Scanner.LessEqual: NewBool(il<=ir);
  2904. |Scanner.Greater: NewBool(il>ir);
  2905. |Scanner.GreaterEqual: NewBool(il>=ir);
  2906. ELSE Error(binaryExpression.position,"operator not defined 7");
  2907. END;
  2908. ELS*)
  2909. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2910. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2911. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2912. CASE operator OF
  2913. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2914. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2915. |Scanner.Times: NewInteger(hl*hr,left.type);
  2916. |Scanner.Slash:
  2917. IF hr = 0 THEN
  2918. Error(binaryExpression.position,"division by zero");
  2919. ELSE
  2920. IF type.sizeInBits = 64 THEN
  2921. NewReal(hl/hr,system.longrealType);
  2922. ELSE
  2923. NewReal(hl/hr,system.realType)
  2924. END
  2925. END;
  2926. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2927. |Scanner.Mod:
  2928. IF hr = 0 THEN
  2929. Error(binaryExpression.position,"division by zero");
  2930. ELSE
  2931. NewInteger(hl MOD hr, left.type);
  2932. (* bootstrap64
  2933. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2934. *)
  2935. END;
  2936. |Scanner.Div:
  2937. IF hr = 0 THEN
  2938. Error(binaryExpression.position,"division by zero");
  2939. ELSE
  2940. NewInteger(hl DIV hr, left.type);
  2941. (* bootstrap64
  2942. NewInteger(Machine.DivH(hl,hr),left.type);
  2943. *)
  2944. END;
  2945. (* *)
  2946. |Scanner.Equal: NewBool(hl=hr);
  2947. |Scanner.Unequal: NewBool(hl#hr);
  2948. |Scanner.Less: NewBool(hl<hr);
  2949. |Scanner.LessEqual: NewBool(hl<=hr);
  2950. |Scanner.Greater: NewBool(hl>hr);
  2951. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2952. ELSE Error(binaryExpression.position,"operator not defined 8");
  2953. END;
  2954. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2955. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2956. type := left.type
  2957. ELSIF (operator = Scanner.Slash) THEN
  2958. left := NewConversion(left.position,left,system.realType,NIL);
  2959. right := NewConversion(right.position,right,system.realType,NIL);
  2960. binaryExpression.SetLeft(left);
  2961. binaryExpression.SetRight(right);
  2962. type := system.realType
  2963. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2964. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2965. type := system.booleanType
  2966. ELSE
  2967. Error(binaryExpression.position,"operator not defined 9");
  2968. END;
  2969. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2970. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2971. CASE operator OF
  2972. |Scanner.Plus: NewReal(rl+rr,leftType);
  2973. |Scanner.Minus: NewReal(rl-rr,leftType);
  2974. |Scanner.Times:NewReal(rl*rr,leftType);
  2975. |Scanner.Slash:
  2976. IF rr = 0 THEN
  2977. Error(binaryExpression.position,"division by zero");
  2978. ELSE
  2979. NewReal(rl/rr,leftType);
  2980. END
  2981. |Scanner.Equal: NewBool(rl=rr);
  2982. |Scanner.Unequal: NewBool(rl#rr);
  2983. |Scanner.Less: NewBool(rl<rr);
  2984. |Scanner.LessEqual: NewBool(rl<=rr);
  2985. |Scanner.Greater: NewBool(rl>rr);
  2986. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2987. ELSE Error(binaryExpression.position,"operator not defined 10");
  2988. END;
  2989. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2990. type := left.type
  2991. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2992. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2993. type := system.booleanType
  2994. ELSE
  2995. Error(binaryExpression.position,"operator not defined 11");
  2996. IF VerboseErrorMessage THEN
  2997. Printout.Info("left",left);
  2998. Printout.Info("right",right);
  2999. END;
  3000. END;
  3001. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3002. CASE operator OF
  3003. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3004. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3005. ELSE
  3006. Error(binaryExpression.position,"operator not defined");
  3007. IF VerboseErrorMessage THEN
  3008. Printout.Info("left", left);
  3009. Printout.Info("right", right)
  3010. END;
  3011. END;
  3012. IF ~error THEN
  3013. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3014. Error(binaryExpression.position,"division by zero")
  3015. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3016. (* do constant folding *)
  3017. CASE operator OF
  3018. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3019. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3020. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3021. |Scanner.Slash:
  3022. divisor := c * c + d * d;
  3023. ASSERT(divisor # 0);
  3024. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3025. |Scanner.Equal: NewBool((a = c) & (b = d))
  3026. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3027. END
  3028. END
  3029. END
  3030. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3031. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3032. CASE operator OF
  3033. |Scanner.And: NewBool(bl & br);
  3034. |Scanner.Or: NewBool(bl OR br);
  3035. |Scanner.Equal: NewBool(bl = br);
  3036. |Scanner.Unequal: NewBool(bl # br);
  3037. ELSE Error(binaryExpression.position,"operator not defined 12");
  3038. END;
  3039. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3040. type := system.booleanType
  3041. ELSE
  3042. Error(binaryExpression.position,"operator not defined 13");
  3043. END;
  3044. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3045. (* constant folding *)
  3046. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3047. IF operator = Scanner.Equal THEN
  3048. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3049. ELSIF operator = Scanner.Unequal THEN
  3050. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3051. END;
  3052. END;
  3053. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3054. type := system.booleanType;
  3055. ELSE
  3056. Error(binaryExpression.position, "operator not defined");
  3057. END;
  3058. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3059. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3060. CASE operator OF
  3061. |Scanner.Plus: NewSet(sl + sr);
  3062. |Scanner.Minus: NewSet(sl - sr);
  3063. |Scanner.Times: NewSet(sl * sr);
  3064. |Scanner.Slash: NewSet(sl / sr);
  3065. |Scanner.Equal: NewBool(sl=sr);
  3066. |Scanner.Unequal: NewBool(sl#sr);
  3067. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3068. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3069. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3070. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3071. ELSE Error(binaryExpression.position,"operator not defined 14");
  3072. END;
  3073. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3074. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3075. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3076. THEN
  3077. type := system.booleanType
  3078. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3079. type := left.type
  3080. ELSE
  3081. Error(binaryExpression.position,"operator not defined 15");
  3082. END;
  3083. ELSIF IsCharacterType(left.type) THEN
  3084. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3085. CASE operator OF
  3086. |Scanner.Equal: NewBool(cl=cr);
  3087. |Scanner.Unequal: NewBool(cl#cr);
  3088. |Scanner.Less: NewBool(cl<cr);
  3089. |Scanner.LessEqual: NewBool(cl<=cr);
  3090. |Scanner.Greater: NewBool(cl>cr);
  3091. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3092. ELSE Error(binaryExpression.position,"operator not defined 16");
  3093. END;
  3094. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3095. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3096. type := system.booleanType
  3097. ELSE
  3098. Error(binaryExpression.position,"operator not defined 17");
  3099. END;
  3100. ELSE
  3101. Error(binaryExpression.position,"operator not defined 18");
  3102. END;
  3103. ELSE
  3104. Error(binaryExpression.position,"operator not defined 19");
  3105. END;
  3106. IF type = SyntaxTree.invalidType THEN
  3107. result := SyntaxTree.invalidExpression
  3108. ELSE
  3109. result.SetType(type)
  3110. END;
  3111. resolvedExpression := result
  3112. END VisitBinaryExpression;
  3113. (** resolve a range expression of the from <<first .. last BY step>>
  3114. - depending on the context different things are checked:
  3115. ArrayIndex:
  3116. - components must be integers
  3117. - replace missing lower bound with 0
  3118. - replace missing upper bound with MAX(LONGINT)
  3119. - replace missing step size with 1
  3120. SetElement:
  3121. - components must be integers
  3122. - replace missing lower bound with 0
  3123. - replace missing upper bound with MAX(SET)
  3124. - must not have step size
  3125. CaseGuard:
  3126. - components must be constant
  3127. - components must be integers or characters
  3128. - must have lower and upper bound present
  3129. - components are made compatible
  3130. - must not have step size
  3131. - if error: return invalidExpression
  3132. **)
  3133. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3134. VAR
  3135. hasError: BOOLEAN;
  3136. first, last, step: SyntaxTree.Expression;
  3137. BEGIN
  3138. hasError := FALSE;
  3139. first := x.first;
  3140. last := x.last;
  3141. step := x.step;
  3142. (* check lower bound *)
  3143. IF x.context = SyntaxTree.CaseGuard THEN
  3144. IF first = NIL THEN
  3145. Error(x.position, "missing lower bound");
  3146. hasError := TRUE
  3147. ELSE
  3148. first := ResolveExpression(first);
  3149. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3150. Error(first.position, "lower bound not integer or character");
  3151. hasError := TRUE
  3152. ELSE
  3153. IF first IS SyntaxTree.StringValue THEN
  3154. (* add conversion from string to character *)
  3155. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3156. END
  3157. END;
  3158. (* check if expression is constant *)
  3159. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3160. (* error already reported *)
  3161. hasError := TRUE
  3162. END
  3163. END
  3164. ELSE (* ArrayIndex, SetElement *)
  3165. IF first = NIL THEN
  3166. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3167. END;
  3168. first := ResolveExpression(first);
  3169. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3170. first := NewConversion(first.position, first, system.longintType, NIL)
  3171. ELSE
  3172. Error(first.position, "lower bound not integer");
  3173. hasError := TRUE
  3174. END
  3175. END;
  3176. (* check upper bound *)
  3177. IF x.context = SyntaxTree.CaseGuard THEN
  3178. IF last = NIL THEN
  3179. Error(x.position, "missing upper bound");
  3180. hasError := TRUE
  3181. ELSE
  3182. last := ResolveExpression(last);
  3183. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3184. Error(last.position, "lower bound not integer or character");
  3185. hasError := TRUE
  3186. ELSE
  3187. IF last IS SyntaxTree.StringValue THEN
  3188. (* add conversion from string to character *)
  3189. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3190. END
  3191. END;
  3192. (* check if expression is constant *)
  3193. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3194. (* error already reported *)
  3195. hasError := TRUE
  3196. ELSE
  3197. (* try to make lower and upper bound compatible *)
  3198. ConvertOperands(first, last);
  3199. IF first.type.resolved # last.type.resolved THEN
  3200. Error(x.position, "lower and upper bounds incompatible");
  3201. hasError := TRUE
  3202. END
  3203. END
  3204. END
  3205. ELSE (* ArrayIndex, SetElement *)
  3206. IF last = NIL THEN
  3207. IF x.context = SyntaxTree.ArrayIndex THEN
  3208. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3209. ELSE
  3210. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3211. END
  3212. END;
  3213. last := ResolveExpression(last);
  3214. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3215. last := NewConversion(last.position, last, system.longintType, NIL)
  3216. ELSE
  3217. Error(last.position, "upper bound not integer");
  3218. hasError := TRUE
  3219. END
  3220. END;
  3221. (* check step size *)
  3222. IF x.context = SyntaxTree.ArrayIndex THEN
  3223. IF step = NIL THEN
  3224. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3225. END;
  3226. step := ResolveExpression(step);
  3227. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3228. step := NewConversion(step.position, step, system.longintType, NIL)
  3229. ELSE
  3230. Error(step.position, "step size not integer");
  3231. hasError := TRUE
  3232. END
  3233. ELSE (* SetElement, CaseGuard *)
  3234. IF step # NIL THEN
  3235. Error(last.position, "step size not allowed in this context");
  3236. hasError := TRUE
  3237. END
  3238. END;
  3239. IF hasError THEN
  3240. resolvedExpression := SyntaxTree.invalidExpression
  3241. ELSE
  3242. x.SetFirst(first);
  3243. x.SetLast(last);
  3244. x.SetStep(step);
  3245. x.SetType(system.rangeType);
  3246. resolvedExpression := x;
  3247. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3248. END
  3249. END VisitRangeExpression;
  3250. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3251. BEGIN
  3252. x.SetType(NIL);
  3253. resolvedExpression := x;
  3254. END VisitTensorRangeExpression;
  3255. (** resolve the expression d and return result as designator
  3256. - resolve expression
  3257. - if expression is a designator then return designator else error message and return invalidDesignator
  3258. **)
  3259. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3260. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3261. BEGIN
  3262. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3263. resolved := ResolveExpression(d);
  3264. IF resolved = SyntaxTree.invalidExpression THEN
  3265. (* error should already have been reported *)
  3266. result := SyntaxTree.invalidDesignator;
  3267. ELSIF resolved IS SyntaxTree.Designator THEN
  3268. result := resolved(SyntaxTree.Designator);
  3269. ELSE
  3270. Error(d.position,"is no designator ! ");
  3271. result := SyntaxTree.invalidDesignator;
  3272. END;
  3273. (* result.type might be nil. *)
  3274. RETURN result
  3275. END ResolveDesignator;
  3276. (**
  3277. symbol designator generated in this module
  3278. nothing to be resolved
  3279. **)
  3280. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3281. BEGIN
  3282. resolvedExpression := x;
  3283. END VisitSymbolDesignator;
  3284. (**
  3285. self designator generated in this module
  3286. nothing to be resolved
  3287. **)
  3288. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3289. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3290. BEGIN
  3291. (* check if in record scope *)
  3292. scope := currentScope;
  3293. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3294. scope := scope.outerScope;
  3295. END;
  3296. IF scope = NIL THEN (* in module scope *)
  3297. x.SetType(system.anyType);
  3298. ELSIF scope IS SyntaxTree.CellScope THEN
  3299. cell := scope(SyntaxTree.CellScope).ownerCell;
  3300. x.SetType(cell);
  3301. ELSE (* in record scope *)
  3302. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3303. IF (record # NIL) & (record.pointerType # NIL) THEN
  3304. type := ResolveType(record.pointerType);
  3305. x.SetType(type);
  3306. ELSE
  3307. x.SetType(record);
  3308. END;
  3309. END;
  3310. resolvedExpression := x;
  3311. END VisitSelfDesignator;
  3312. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3313. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3314. BEGIN
  3315. scope := currentScope;
  3316. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3317. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3318. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3319. returnType := procedureType.returnType;
  3320. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3321. THEN
  3322. x.SetType(returnType);
  3323. ELSE
  3324. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3325. x.SetType(SyntaxTree.invalidType);
  3326. END;
  3327. ELSE
  3328. Error(x.position,"forbidden access to result designator");
  3329. x.SetType(SyntaxTree.invalidType);
  3330. END;
  3331. x.SetAssignable(TRUE);
  3332. resolvedExpression := x;
  3333. END VisitResultDesignator;
  3334. (**
  3335. return symbol designator as an expression
  3336. - if symbol is a constant then return the constant value expression
  3337. - else
  3338. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3339. - if symbol is a guarded variable then return a TypeGuardDesignator
  3340. - else return a symbol designator
  3341. **)
  3342. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3343. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3344. guardType: SyntaxTree.Type;
  3345. BEGIN
  3346. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3347. result := SyntaxTree.invalidExpression;
  3348. ASSERT(symbol # NIL);
  3349. (*
  3350. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3351. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3352. Error(position, "type not allowed here");
  3353. ELS *)
  3354. (* not needed any more as values are stored in the expression
  3355. IF symbol IS SyntaxTree.Constant THEN
  3356. result := symbol(SyntaxTree.Constant).value
  3357. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3358. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3359. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3360. ELSE
  3361. result := symbol(SyntaxTree.Constant).value
  3362. END;
  3363. ELSE
  3364. *)
  3365. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3366. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3367. THEN
  3368. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3369. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3370. left := NewDereferenceDesignator(position,left);
  3371. left.SetHidden(TRUE);
  3372. END;
  3373. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3374. scope := currentScope;
  3375. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3376. scope := scope.outerScope;
  3377. END;
  3378. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3379. Error(position, "forbidden access to symbol in parent procedure scope");
  3380. END;
  3381. END;
  3382. 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);
  3383. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3384. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3385. ELSE
  3386. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3387. END;
  3388. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3389. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3390. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3391. result.SetType(symbol.type);
  3392. result.SetAssignable(assignable);
  3393. symbol.MarkUsed;
  3394. IF symbol IS SyntaxTree.Constant THEN
  3395. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3396. END;
  3397. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3398. variableAccessed := TRUE
  3399. END;
  3400. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3401. IF GetGuard(symbol,guardType) THEN
  3402. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3403. END;
  3404. END;
  3405. ASSERT(result.type # NIL);
  3406. RETURN result
  3407. END NewSymbolDesignator;
  3408. (** check and resolve an identifier designator "identifier"
  3409. - if identifier = self then return SelfDesignator
  3410. - else find symbol in current scope
  3411. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3412. **)
  3413. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3414. VAR symbol: SyntaxTree.Symbol;
  3415. BEGIN
  3416. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3417. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3418. IF symbol # NIL THEN
  3419. ResolveSymbol(symbol);
  3420. ASSERT(symbol.type # NIL);
  3421. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3422. ELSE
  3423. Error(identifierDesignator.position,"Undeclared Identifier");
  3424. IF VerboseErrorMessage THEN
  3425. Printout.Info("undeclared identifier designator",identifierDesignator);
  3426. END;
  3427. resolvedExpression := SyntaxTree.invalidDesignator;
  3428. END;
  3429. END VisitIdentifierDesignator;
  3430. (** check and resolve a selector designator of the form left.designator
  3431. - if left is a pointer type then do auto dereferenciation
  3432. - left denotes a search scope:
  3433. - if left type is import type then set search scope to respective module
  3434. - if left type is enumeration type then set search scope to respective enumeration scope
  3435. - elsif left type is record type then set search scope to record scope
  3436. - search symbol in computed scope
  3437. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3438. **)
  3439. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3440. VAR
  3441. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3442. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3443. BEGIN
  3444. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3445. left := ResolveDesignator(selectorDesignator.left);
  3446. result := SyntaxTree.invalidDesignator;
  3447. IF left # NIL THEN
  3448. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3449. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3450. END;
  3451. scope := NIL;
  3452. IF left.type = NIL THEN
  3453. Error(selectorDesignator.position,"field on nil typed designator");
  3454. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3455. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3456. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3457. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3458. module := symbol(SyntaxTree.Import).module;
  3459. IF module # NIL THEN
  3460. scope := module.moduleScope
  3461. ELSE
  3462. Error(left.position,"module not loaded");
  3463. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3464. END;
  3465. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3466. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3467. ASSERT(scope # NIL)
  3468. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3469. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3470. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3471. IF type IS SyntaxTree.EnumerationType THEN
  3472. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3473. ELSE
  3474. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3475. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3476. END;
  3477. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3478. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3479. ELSE
  3480. Error(selectorDesignator.position,"field on non-record type designator");
  3481. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3482. END;
  3483. symbol := NIL;
  3484. IF scope # NIL THEN
  3485. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3486. IF symbol # NIL THEN
  3487. ResolveSymbol(symbol);
  3488. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3489. symbol.MarkUsed
  3490. ELSE
  3491. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3492. IF VerboseErrorMessage THEN
  3493. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3494. Printout.Info("scope", scope);
  3495. Printout.Info("left", left);
  3496. Printout.Info("undeclared identifier",selectorDesignator);
  3497. Printout.Info("left resolved designator",left);
  3498. END
  3499. END;
  3500. END;
  3501. END;
  3502. resolvedExpression := result;
  3503. END VisitSelectorDesignator;
  3504. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3505. VAR len,idx: LONGINT;
  3506. BEGIN
  3507. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3508. IF idx < 0 THEN
  3509. Error(index.position,"index out of bounds (too small)")
  3510. ELSE
  3511. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3512. Error(index.position,"index out of bounds (too large)");
  3513. END;
  3514. END;
  3515. END;
  3516. END IndexCheck;
  3517. (*
  3518. - if index designator has not type, use newBaseType as its type
  3519. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3520. - special rule: if static array of dynamic array occurs, make it all dynamic
  3521. index designator type: new base type: new index designator type:
  3522. NIL z z
  3523. ARRAY [x, y] z ARRAY [x, y] OF z
  3524. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3525. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3526. *)
  3527. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3528. VAR
  3529. mathArrayType: SyntaxTree.MathArrayType;
  3530. makeDynamic: BOOLEAN;
  3531. BEGIN
  3532. IF indexDesignator.type = NIL THEN
  3533. indexDesignator.SetType(newBaseType)
  3534. ELSE
  3535. (* index designator must be a of math array type *)
  3536. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3537. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3538. (* determine if all arrays have to be made dynamic *)
  3539. makeDynamic :=
  3540. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3541. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3542. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3543. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3544. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3545. END;
  3546. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3547. mathArrayType.SetArrayBase(newBaseType)
  3548. END
  3549. END SetIndexBaseType;
  3550. (** check and append index list element to index designator of math array
  3551. - check validity of single index or array range
  3552. - compute new type
  3553. - if range then create new array type (calculate length of resulting array)
  3554. - otherwise take sourceArray.arrayBase as new type
  3555. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3556. **)
  3557. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3558. VAR
  3559. targetArray: SyntaxTree.MathArrayType;
  3560. first, last, step: SyntaxTree.Expression;
  3561. firstValue, lastValue, stepValue, length: LONGINT;
  3562. rangeExpression: SyntaxTree.RangeExpression;
  3563. BEGIN
  3564. IF indexListItem.type = SyntaxTree.invalidType THEN
  3565. (* error already handled *)
  3566. indexDesignator.parameters.AddExpression(indexListItem)
  3567. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3568. indexDesignator.HasRange;
  3569. indexDesignator.HasTensorRange;
  3570. indexDesignator.parameters.AddExpression(indexListItem);
  3571. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3572. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3573. IndexCheck(indexListItem, sourceArray.length);
  3574. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3575. indexDesignator.parameters.AddExpression(indexListItem)
  3576. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3577. indexDesignator.HasRange;
  3578. (* if the range is given as an array range expression, check the validity of its components *)
  3579. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3580. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3581. first := rangeExpression.first;
  3582. last := rangeExpression.last;
  3583. step := rangeExpression.step;
  3584. (* perform static checks on range components *)
  3585. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3586. Error(indexListItem.position,"lower bound of array range too small")
  3587. END;
  3588. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3589. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3590. Error(indexListItem.position,"upper bound of array range too large")
  3591. END
  3592. END;
  3593. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3594. Error(indexListItem.position,"invalid step size")
  3595. END;
  3596. (* add conversions to size type *)
  3597. (* TODO: needed? *)
  3598. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3599. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3600. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3601. END;
  3602. IF indexDesignator.hasTensorRange THEN
  3603. (* the index designator's base type is a tensor: leave it as is *)
  3604. ELSE
  3605. (* append a new math array to the index designator's base type *)
  3606. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3607. IF ~error THEN
  3608. (*
  3609. (* optimization: calculate length of target array for static ranges *)
  3610. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3611. IF IsStaticallyOpenRange(rangeExpression) THEN
  3612. (* range is open ('*'): reuse source array length as target array length *)
  3613. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3614. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3615. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3616. IF lastValue = MAX(LONGINT) THEN
  3617. IF IsIntegerValue(sourceArray.length, length) THEN
  3618. lastValue := length - 1;
  3619. isStaticTargetArrayLength := TRUE
  3620. ELSE
  3621. isStaticTargetArrayLength := FALSE
  3622. END
  3623. ELSE
  3624. isStaticTargetArrayLength := TRUE
  3625. END;
  3626. IF isStaticTargetArrayLength THEN
  3627. (* calculate static target array length *)
  3628. IF firstValue > lastValue THEN
  3629. length := 0
  3630. ELSE
  3631. length := 1 + lastValue - firstValue;
  3632. IF length MOD stepValue = 0 THEN
  3633. length := length DIV stepValue
  3634. ELSE
  3635. length := length DIV stepValue + 1
  3636. END
  3637. END;
  3638. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3639. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3640. ASSERT(targetArray.form = SyntaxTree.Static)
  3641. END
  3642. END
  3643. END
  3644. *)
  3645. END;
  3646. SetIndexBaseType(indexDesignator, targetArray)
  3647. END;
  3648. indexDesignator.parameters.AddExpression(indexListItem)
  3649. ELSE
  3650. Error(position,"invalid index list item");
  3651. END;
  3652. END AppendMathIndex;
  3653. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3654. VAR parameters: SyntaxTree.ExpressionList;
  3655. BEGIN
  3656. parameters := index.parameters;
  3657. IF (expression.type = NIL) THEN
  3658. Error(position, "invalid index");
  3659. ELSIF IsIntegerType(expression.type.resolved) THEN
  3660. IF over IS SyntaxTree.ArrayType THEN
  3661. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3662. ELSIF over IS SyntaxTree.StringType THEN
  3663. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3664. END;
  3665. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3666. parameters.AddExpression(expression);
  3667. ELSE
  3668. Error(position, "invalid index");
  3669. END;
  3670. END AppendIndex;
  3671. (** convert an expression to math array type
  3672. if expression is of math array type: return expression itself
  3673. if expression is of array-structured object type: return an index operator call on it
  3674. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3675. otherwise: return invalid expression
  3676. **)
  3677. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3678. VAR
  3679. result: SyntaxTree.Expression;
  3680. mathArrayType: SyntaxTree.MathArrayType;
  3681. BEGIN
  3682. IF expression.type = NIL THEN
  3683. result := SyntaxTree.invalidExpression
  3684. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3685. (* expression of math array type *)
  3686. result := expression
  3687. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3688. (* expression of array-structured object type *)
  3689. mathArrayType := MathArrayStructureOfType(expression.type);
  3690. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3691. ELSE
  3692. result := SyntaxTree.invalidExpression
  3693. END;
  3694. RETURN result
  3695. END ConvertToMathArray;
  3696. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3697. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3698. VAR
  3699. result: SyntaxTree.ExpressionList;
  3700. i: LONGINT;
  3701. BEGIN
  3702. result := SyntaxTree.NewExpressionList();
  3703. FOR i := 1 TO itemCount DO
  3704. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3705. END;
  3706. RETURN result
  3707. END ListOfOpenRanges;
  3708. (** create a procedure call designator for an index operator call on an array-structured object type
  3709. - use given index list as actual parameters
  3710. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3711. **)
  3712. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3713. VAR
  3714. operator: SyntaxTree.Operator;
  3715. expression: SyntaxTree.Expression;
  3716. actualParameters, tempList: SyntaxTree.ExpressionList;
  3717. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3718. result, tempDesignator: SyntaxTree.Designator;
  3719. recordType: SyntaxTree.RecordType;
  3720. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3721. i, hashValue, indexListSize, indexListKind: LONGINT;
  3722. castReturnType: SyntaxTree.MathArrayType;
  3723. BEGIN
  3724. ASSERT(IsArrayStructuredObjectType(left.type));
  3725. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3726. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3727. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3728. indexListSize := indexList.Length();
  3729. indexListKind := 0;
  3730. containsNonRange := FALSE;
  3731. FOR i := 0 TO indexList.Length() - 1 DO
  3732. indexListKind := indexListKind * 2;
  3733. expression := indexList.GetExpression(i);
  3734. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3735. INC(indexListKind)
  3736. ELSE
  3737. containsNonRange := TRUE
  3738. END
  3739. END;
  3740. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3741. (* select applicable index operator
  3742. - try to look up optimal index operator
  3743. - if not present, use operator on ranges
  3744. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3745. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3746. *)
  3747. usesGeneralOperator := FALSE;
  3748. IF rhs # NIL THEN
  3749. (* write operator *)
  3750. IF hashValue = -1 THEN
  3751. operator := NIL
  3752. ELSE
  3753. operator := recordType.arrayAccessOperators.write[hashValue];
  3754. END;
  3755. IF operator = NIL THEN
  3756. usesPureRangeOperator := TRUE;
  3757. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3758. operator := recordType.arrayAccessOperators.generalWrite;
  3759. usesGeneralOperator := TRUE
  3760. ELSE
  3761. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3762. operator := recordType.arrayAccessOperators.write[hashValue];
  3763. END
  3764. END
  3765. ELSE
  3766. (* read operator *)
  3767. IF hashValue = -1 THEN
  3768. operator := NIL
  3769. ELSE
  3770. operator := recordType.arrayAccessOperators.read[hashValue];
  3771. END;
  3772. IF operator = NIL THEN
  3773. usesPureRangeOperator := TRUE;
  3774. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3775. operator := recordType.arrayAccessOperators.generalRead;
  3776. usesGeneralOperator := TRUE
  3777. ELSE
  3778. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3779. operator := recordType.arrayAccessOperators.read[hashValue];
  3780. END
  3781. END
  3782. END;
  3783. IF operator = NIL THEN
  3784. Error(position, "call of undeclared [] operator");
  3785. result := SyntaxTree.invalidDesignator;
  3786. ELSE
  3787. (* determine if reshaping is needed *)
  3788. needsReshaping := containsNonRange & usesPureRangeOperator;
  3789. (* import OCArrayBase if reshaping is needed *)
  3790. IF needsReshaping & ~arrayBaseImported THEN
  3791. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3792. arrayBaseImported := TRUE
  3793. END;
  3794. (* add the index list item to the list of actual parameters
  3795. - for general operators: add a single inline array containing the index list items as parameter
  3796. - otherwise: add all index list items as individual parameters
  3797. *)
  3798. actualParameters := SyntaxTree.NewExpressionList();
  3799. IF usesGeneralOperator THEN
  3800. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3801. END;
  3802. FOR i := 0 TO indexListSize - 1 DO
  3803. expression := indexList.GetExpression(i);
  3804. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3805. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3806. tempList := SyntaxTree.NewExpressionList();
  3807. tempList.AddExpression(expression);
  3808. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3809. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3810. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3811. END;
  3812. IF usesGeneralOperator THEN
  3813. tempMathArrayExpression.elements.AddExpression(expression);
  3814. ELSE
  3815. actualParameters.AddExpression(expression)
  3816. END
  3817. END;
  3818. IF usesGeneralOperator THEN
  3819. actualParameters.AddExpression(tempMathArrayExpression)
  3820. END;
  3821. IF rhs # NIL THEN
  3822. (* add actual parameter for RHS *)
  3823. IF needsReshaping THEN
  3824. (* reshape using OCArrayBase.ExpandDimensions *)
  3825. tempList := SyntaxTree.NewExpressionList();
  3826. (* source array *)
  3827. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3828. tempList.AddExpression(rhs);
  3829. ELSE
  3830. (* convert scalar to one-dimensional array *)
  3831. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3832. tempMathArrayExpression.elements.AddExpression(rhs);
  3833. tempList.AddExpression(tempMathArrayExpression)
  3834. END;
  3835. (* list of kept dimensions *)
  3836. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3837. FOR i := 0 TO indexListSize - 1 DO
  3838. expression := indexList.GetExpression(i);
  3839. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3840. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3841. ELSE
  3842. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3843. END
  3844. END;
  3845. tempList.AddExpression(tempMathArrayExpression);
  3846. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3847. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3848. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3849. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3850. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3851. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3852. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3853. expression.SetType(castReturnType);
  3854. ELSE
  3855. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3856. END;
  3857. actualParameters.AddExpression(expression)
  3858. ELSE
  3859. actualParameters.AddExpression(rhs)
  3860. END
  3861. END;
  3862. (* add dereference operator and create procedure call designator *)
  3863. ASSERT(left IS SyntaxTree.Designator);
  3864. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3865. ASSERT(expression IS SyntaxTree.Designator);
  3866. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3867. IF (rhs = NIL) & needsReshaping THEN
  3868. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3869. tempList := SyntaxTree.NewExpressionList();
  3870. FOR i := 0 TO indexList.Length() - 1 DO
  3871. expression := indexList.GetExpression(i);
  3872. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3873. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3874. ELSE
  3875. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3876. END
  3877. END;
  3878. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3879. END;
  3880. IF rhs = NIL THEN
  3881. (* special rule: index read operator calls are considered to be assignable *)
  3882. result.SetAssignable(TRUE)
  3883. END;
  3884. (* put information about this index operator call into the resulting designator *)
  3885. result.SetRelatedAsot(left);
  3886. result.SetRelatedIndexList(indexList)
  3887. END;
  3888. RETURN result
  3889. END NewIndexOperatorCall;
  3890. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3891. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3892. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3893. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3894. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3895. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3896. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3897. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3898. CONST trace = FALSE;
  3899. BEGIN
  3900. IF trace THEN
  3901. FOR i := 0 TO actualParameters.Length()-1 DO
  3902. Printout.Info("par", actualParameters.GetExpression(i));
  3903. END;
  3904. END;
  3905. operator := scope.firstOperator;
  3906. WHILE(operator # NIL) DO
  3907. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3908. procedureType := operator.type(SyntaxTree.ProcedureType);
  3909. distance := Distance(system, procedureType,actualParameters);
  3910. IF trace THEN Printout.Info("check op ",operator) END;
  3911. IF distance < bestDistance THEN
  3912. IF trace THEN Printout.Info("taken op",operator) END;
  3913. bestDistance := distance;
  3914. bestOperator := operator;
  3915. END;
  3916. END;
  3917. operator := operator.nextOperator;
  3918. END;
  3919. END FindInScope;
  3920. BEGIN
  3921. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3922. IF oper = 0 THEN (* index *)
  3923. identifier := SyntaxTree.NewIdentifier("[]");
  3924. ELSE
  3925. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3926. END;
  3927. WHILE (recordType # NIL) DO
  3928. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3929. recordType := recordType.GetBaseRecord();
  3930. END;
  3931. RETURN bestOperator
  3932. END FindOperator;
  3933. BEGIN
  3934. type := left.type.resolved;
  3935. IF type IS SyntaxTree.RecordType THEN
  3936. pointer := FALSE;
  3937. recordType := type(SyntaxTree.RecordType);
  3938. ELSE
  3939. pointer := TRUE;
  3940. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3941. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3942. END;
  3943. actualParameters := SyntaxTree.NewExpressionList();
  3944. IF parameters # NIL THEN
  3945. FOR i := 0 TO parameters.Length()-1 DO
  3946. expression := ResolveExpression(parameters.GetExpression(i));
  3947. actualParameters.AddExpression(expression);
  3948. END;
  3949. END;
  3950. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3951. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3952. IF op # NIL THEN
  3953. designator := left(SyntaxTree.Designator);
  3954. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  3955. expression := NewSymbolDesignator(position, designator , op);
  3956. ASSERT(expression IS SyntaxTree.Designator);
  3957. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3958. result.SetRelatedAsot(left);
  3959. result.SetRelatedIndexList(parameters);
  3960. (* check if write operator exists, for var parameters *)
  3961. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3962. actualParameters := SyntaxTree.NewExpressionList();
  3963. FOR i := 0 TO parameters.Length()-1 DO
  3964. expression := ResolveExpression(parameters.GetExpression(i));
  3965. actualParameters.AddExpression(expression);
  3966. END;
  3967. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3968. actualParameters.AddExpression(rhs);
  3969. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3970. IF op = NIL THEN rhs := NIL END;
  3971. END;
  3972. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3973. ELSE
  3974. result := NIL;
  3975. END;
  3976. RETURN result;
  3977. END NewObjectOperatorCall;
  3978. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3979. 1. convert bracket designator chains into a single one that contains separators
  3980. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3981. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3982. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3983. - if an array or math array is indexed over, create index designator
  3984. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3985. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3986. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3987. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3988. - if an array-structured object type is indexed over, create procedure call designator
  3989. e.g.: a[x, y] -> a^."[]"(x, y)
  3990. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3991. - a[i, *] = a[i][*]
  3992. - a[*, i] # a[*][i]
  3993. Because:
  3994. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3995. - 'i-th column' = a[*, i]
  3996. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3997. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3998. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3999. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4000. **)
  4001. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  4002. VAR
  4003. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4004. indexDesignator: SyntaxTree.IndexDesignator;
  4005. designator: SyntaxTree.Designator;
  4006. type: SyntaxTree.Type;
  4007. recordType: SyntaxTree.RecordType;
  4008. expression, rhs: SyntaxTree.Expression;
  4009. indexList: SyntaxTree.ExpressionList;
  4010. i: LONGINT;
  4011. hasError, done: BOOLEAN;
  4012. PROCEDURE FinalizeIndexDesignator;
  4013. BEGIN
  4014. IF indexDesignator # NIL THEN
  4015. (* the end of a tensor has been reached: *)
  4016. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4017. SetIndexBaseType(indexDesignator, type);
  4018. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4019. designator := indexDesignator;
  4020. type := designator.type.resolved;
  4021. indexDesignator := NIL;
  4022. ASSERT(SyntaxTree.Resolved IN type.state)
  4023. END
  4024. END FinalizeIndexDesignator;
  4025. BEGIN
  4026. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4027. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4028. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4029. (* copy all index list entries including a separator to the left bracket designator *)
  4030. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4031. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4032. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4033. END;
  4034. (* propagate the related RHS *)
  4035. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4036. (* only resolve left bracket designator and use as final result *)
  4037. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4038. ELSE
  4039. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4040. designator := ResolveDesignator(bracketDesignator.left);
  4041. type := designator.type.resolved;
  4042. indexDesignator := NIL;
  4043. (*!!! clean up *)
  4044. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4045. OR (type IS SyntaxTree.RecordType)
  4046. THEN
  4047. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4048. IF resolvedExpression = NIL THEN
  4049. Error(bracketDesignator.position,"undefined operator");
  4050. resolvedExpression := SyntaxTree.invalidDesignator
  4051. END;
  4052. RETURN;
  4053. END;
  4054. i := 0;
  4055. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4056. expression := bracketDesignator.parameters.GetExpression(i);
  4057. expression := ResolveExpression(expression);
  4058. bracketDesignator.parameters.SetExpression(i, expression);
  4059. IF expression = SyntaxTree.indexListSeparator THEN
  4060. (* finalize an existing index designator if needed *)
  4061. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4062. INC(i)
  4063. ELSE
  4064. (* do auto-dereferencing if needed *)
  4065. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4066. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4067. & (i=0)*)
  4068. THEN
  4069. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4070. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4071. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4072. designator := SyntaxTree.invalidDesignator;
  4073. type := SyntaxTree.invalidType
  4074. ELSE
  4075. FinalizeIndexDesignator;
  4076. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4077. type := designator.type.resolved
  4078. END
  4079. END;
  4080. (* create a new index designator, if needed *)
  4081. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4082. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4083. indexDesignator.SetAssignable(designator.assignable);
  4084. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4085. (* designator := indexDesignator *)
  4086. END;
  4087. IF type = SyntaxTree.invalidType THEN
  4088. (* error already handled *)
  4089. INC(i)
  4090. ELSIF type IS SyntaxTree.ArrayType THEN
  4091. (* indexing over an array *)
  4092. ASSERT(indexDesignator # NIL);
  4093. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4094. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4095. INC(i)
  4096. ELSIF type IS SyntaxTree.StringType THEN
  4097. (* indexing over an array *)
  4098. ASSERT(indexDesignator # NIL);
  4099. AppendIndex(expression.position, indexDesignator, expression, type);
  4100. type := type(SyntaxTree.StringType).baseType.resolved;
  4101. INC(i)
  4102. ELSIF type IS SyntaxTree.MathArrayType THEN
  4103. (* indexing over a math array *)
  4104. ASSERT(indexDesignator # NIL);
  4105. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4106. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4107. INC(i)
  4108. ELSIF IsArrayStructuredObjectType(type) THEN
  4109. (* indexing over ASOTs *)
  4110. FinalizeIndexDesignator;
  4111. ASSERT(type IS SyntaxTree.PointerType);
  4112. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4113. (*
  4114. - collect index list items from bracket designator that belong to ASOT
  4115. - check for errors
  4116. *)
  4117. indexList := SyntaxTree.NewExpressionList();
  4118. hasError := FALSE;
  4119. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4120. (* indexing over tensor ASOT:
  4121. - stop at index list end or separator
  4122. - dimensionality is given by number of index list items
  4123. *)
  4124. done := FALSE;
  4125. WHILE ~done DO
  4126. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4127. done := TRUE;
  4128. ELSE
  4129. expression := bracketDesignator.parameters.GetExpression(i);
  4130. IF expression = SyntaxTree.indexListSeparator THEN
  4131. done := TRUE;
  4132. ELSE
  4133. expression := ResolveExpression(expression);
  4134. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4135. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4136. hasError := TRUE
  4137. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4138. Error(expression.position, "integer or range expected");
  4139. expression := SyntaxTree.invalidExpression;
  4140. hasError := TRUE
  4141. END;
  4142. indexList.AddExpression(expression)
  4143. END;
  4144. INC(i)
  4145. END
  4146. END
  4147. ELSE
  4148. (* indexing over non-tensor ASOT:
  4149. - ignore separators
  4150. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4151. *)
  4152. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4153. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4154. expression := bracketDesignator.parameters.GetExpression(i);
  4155. ELSE
  4156. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4157. END;
  4158. IF expression # SyntaxTree.indexListSeparator THEN
  4159. expression := ResolveExpression(expression);
  4160. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4161. Error(expression.position, "integer or range expected");
  4162. expression := SyntaxTree.invalidExpression;
  4163. hasError := TRUE
  4164. END;
  4165. indexList.AddExpression(expression)
  4166. END;
  4167. INC(i)
  4168. END;
  4169. END;
  4170. IF hasError THEN
  4171. designator := SyntaxTree.invalidDesignator;
  4172. type := SyntaxTree.invalidType;
  4173. ELSE
  4174. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4175. and the last entry in the index list belongs to the array-structured object type in question.
  4176. E.g.: for a 2-dimensional array-structured object type:
  4177. - 'lhs := asot[1, 2]' -> read mode
  4178. - 'asot[1, 2] := rhs' -> write mode
  4179. - 'asot[1, 2, 3] := rhs' -> read mode
  4180. *)
  4181. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4182. rhs := bracketDesignator.relatedRhs
  4183. ELSE
  4184. rhs := NIL
  4185. END;
  4186. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4187. type := designator.type
  4188. END
  4189. ELSE
  4190. Error(expression.position,"indexing over non-array type");
  4191. designator := SyntaxTree.invalidDesignator;
  4192. type := SyntaxTree.invalidType;
  4193. INC(i)
  4194. END
  4195. END
  4196. END;
  4197. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4198. resolvedExpression := designator
  4199. END
  4200. END VisitBracketDesignator;
  4201. (** check and resolve expression list
  4202. - resolve each expression in an expression list
  4203. - returns true if and only if all statements could have successfully been resolved
  4204. **)
  4205. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4206. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4207. BEGIN
  4208. result := TRUE;
  4209. FOR i := 0 TO expressionList.Length()-1 DO
  4210. expression := ResolveExpression(expressionList.GetExpression(i));
  4211. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4212. expressionList.SetExpression(i,expression);
  4213. END;
  4214. RETURN result
  4215. END ExpressionList;
  4216. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4217. BEGIN
  4218. type := type.resolved;
  4219. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4220. RETURN TRUE
  4221. ELSIF system.CanPassInRegister # NIL THEN
  4222. RETURN system.CanPassInRegister(type);
  4223. ELSE
  4224. RETURN FALSE
  4225. END;
  4226. END CanPassInRegister;
  4227. (** return procedure call designator left(actualParameters)
  4228. - check realtime procedure call in realtime procedure
  4229. - check number of parameters
  4230. - check parameter compatibility
  4231. return invalidDesignator if error
  4232. **)
  4233. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4234. VAR result: SyntaxTree.Designator;
  4235. numberFormalParameters, numberActualParameters: LONGINT;
  4236. formalType: SyntaxTree.ProcedureType;
  4237. formalParameter: SyntaxTree.Parameter;
  4238. actualParameter: SyntaxTree.Expression;
  4239. i: LONGINT;
  4240. self: SyntaxTree.Expression;
  4241. BEGIN
  4242. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4243. result := SyntaxTree.invalidDesignator;
  4244. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4245. numberFormalParameters := formalType.numberParameters;
  4246. numberActualParameters := actualParameters.Length();
  4247. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4248. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4249. END;
  4250. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4251. self := left.left;
  4252. IF (self # NIL) & ~IsVariable(self) THEN
  4253. Error(self.position, "Non-variable expression on variable receiver");
  4254. END;
  4255. END;
  4256. IF ~ExpressionList(actualParameters) THEN
  4257. result := SyntaxTree.invalidDesignator
  4258. ELSE
  4259. IF numberActualParameters <= numberFormalParameters THEN
  4260. formalParameter := formalType.firstParameter;
  4261. FOR i := 0 TO numberActualParameters-1 DO
  4262. actualParameter := actualParameters.GetExpression(i);
  4263. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4264. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4265. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4266. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4267. ELSE
  4268. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4269. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4270. END;
  4271. actualParameters.SetExpression(i,actualParameter);
  4272. END;
  4273. formalParameter := formalParameter.nextParameter;
  4274. END;
  4275. WHILE (formalParameter # NIL) DO
  4276. IF formalParameter.defaultValue # NIL THEN
  4277. actualParameters.AddExpression(formalParameter.defaultValue);
  4278. formalParameter := formalParameter.nextParameter
  4279. ELSE
  4280. Error(position, "less actual than formal parameters");
  4281. formalParameter := NIL;
  4282. END;
  4283. END;
  4284. ELSE
  4285. Error(position, "more actual than formal parameters")
  4286. END;
  4287. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4288. result.SetAssignable(FALSE);
  4289. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4290. END;
  4291. RETURN result
  4292. END NewProcedureCallDesignator;
  4293. (**
  4294. builtin call designator generated in VisitParameterDesignator
  4295. -> nothing to be resolved
  4296. **)
  4297. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4298. BEGIN
  4299. resolvedExpression := x;
  4300. END VisitTypeGuardDesignator;
  4301. (**
  4302. builtin call designator generated in VisitParameterDesignator
  4303. -> nothing to be resolved
  4304. **)
  4305. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4306. BEGIN
  4307. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4308. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4309. ASSERT(resolvedExpression.type # NIL);
  4310. ELSIF ExpressionList(x.parameters) THEN
  4311. resolvedExpression := x;
  4312. END;
  4313. END VisitBuiltinCallDesignator;
  4314. (**
  4315. procedure call designator generated in VisitParameterDesignator
  4316. -> nothing to be resolved
  4317. **)
  4318. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4319. BEGIN
  4320. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4321. resolvedExpression := x;
  4322. END VisitProcedureCallDesignator;
  4323. (** return true if x is a variable else return false and report error **)
  4324. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4325. VAR result: BOOLEAN;
  4326. BEGIN
  4327. result := TRUE;
  4328. IF x = SyntaxTree.invalidExpression THEN
  4329. result := FALSE;
  4330. ELSIF ~IsVariable(x) THEN
  4331. Error(x.position,"non variable expression");
  4332. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4333. result := FALSE;
  4334. END;
  4335. RETURN result
  4336. END CheckVariable;
  4337. (**
  4338. if expression x is of basic type then return true else report error and return false
  4339. **)
  4340. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4341. VAR result: BOOLEAN;
  4342. BEGIN
  4343. result := FALSE;
  4344. IF x = SyntaxTree.invalidExpression THEN
  4345. ELSIF ~IsBasicType(x.type) THEN
  4346. Error(x.position,"is no basic type");
  4347. result := FALSE
  4348. ELSE result := TRUE
  4349. END;
  4350. RETURN result
  4351. END CheckBasicType;
  4352. (**
  4353. if expression x is of number type then return true else report error and return false
  4354. **)
  4355. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4356. VAR result: BOOLEAN;
  4357. BEGIN
  4358. result := FALSE;
  4359. IF x = SyntaxTree.invalidExpression THEN
  4360. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4361. Error(x.position,"is non number type");
  4362. ELSE result := TRUE
  4363. END;
  4364. RETURN result
  4365. END CheckNumberType;
  4366. (**
  4367. if expression x is of number or size type but not complex then return true else report error and return false
  4368. **)
  4369. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4370. VAR result: BOOLEAN;
  4371. BEGIN
  4372. result := FALSE;
  4373. IF x = SyntaxTree.invalidExpression THEN
  4374. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4375. Error(x.position,"is complex type");
  4376. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4377. Error(x.position,"is non number type");
  4378. ELSE result := TRUE
  4379. END;
  4380. RETURN result
  4381. END CheckNonComplexNumberSizeType;
  4382. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4383. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4384. BEGIN
  4385. result := FALSE; type := x.type.resolved;
  4386. IF x = SyntaxTree.invalidExpression THEN
  4387. 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
  4388. TRACE(type.sizeInBits);
  4389. TRACE(system.addressType.sizeInBits);
  4390. Error(x.position,"is no address type");
  4391. ELSE result := TRUE
  4392. END;
  4393. RETURN result
  4394. END CheckAddressType;
  4395. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4396. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4397. BEGIN
  4398. result := FALSE; type := x.type.resolved;
  4399. IF x = SyntaxTree.invalidExpression THEN
  4400. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4401. Error(x.position,"is no size type");
  4402. ELSE result := TRUE
  4403. END;
  4404. RETURN result
  4405. END CheckSizeType;
  4406. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4407. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4408. BEGIN
  4409. result := FALSE; type := x.type.resolved;
  4410. IF x = SyntaxTree.invalidExpression THEN
  4411. 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
  4412. Error(x.position,"is no object type");
  4413. ELSE result := TRUE
  4414. END;
  4415. RETURN result
  4416. END CheckObjectType;
  4417. (**
  4418. if expression x is of integer type then return true else report error and return false
  4419. **)
  4420. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4421. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4422. BEGIN
  4423. result := FALSE; type := x.type.resolved;
  4424. IF x = SyntaxTree.invalidExpression THEN
  4425. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4426. Error(x.position,"is no integer type");
  4427. ELSE result := TRUE
  4428. END;
  4429. RETURN result
  4430. END CheckIntegerType;
  4431. (**
  4432. if expression x is of character type then return true else report error and return false
  4433. **)
  4434. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4435. VAR result: BOOLEAN;
  4436. BEGIN
  4437. result := FALSE;
  4438. IF x = SyntaxTree.invalidExpression THEN
  4439. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4440. Error(x.position,"is no character type");
  4441. ELSE result := TRUE
  4442. END;
  4443. RETURN result
  4444. END CheckCharacterType;
  4445. (**
  4446. if expression x is of real type then return true else report error and return false
  4447. **)
  4448. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4449. VAR result: BOOLEAN;
  4450. BEGIN
  4451. result := FALSE;
  4452. IF x = SyntaxTree.invalidExpression THEN
  4453. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4454. Error(x.position,"is no float type");
  4455. ELSE result := TRUE
  4456. END;
  4457. RETURN result
  4458. END CheckRealType;
  4459. (**
  4460. if expression x is of range type then return true else report error and return false
  4461. **)
  4462. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4463. VAR result: BOOLEAN;
  4464. BEGIN
  4465. result := FALSE;
  4466. IF x = SyntaxTree.invalidExpression THEN
  4467. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4468. Error(x.position,"is no range type");
  4469. ELSE result := TRUE
  4470. END;
  4471. RETURN result
  4472. END CheckRangeType;
  4473. (**
  4474. if expression x is of boolean type then return true else report error and return false
  4475. **)
  4476. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4477. VAR result: BOOLEAN;
  4478. BEGIN
  4479. result := FALSE;
  4480. IF x = SyntaxTree.invalidExpression THEN
  4481. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4482. Error(x.position,"is no boolean type");
  4483. ELSE result := TRUE
  4484. END;
  4485. RETURN result
  4486. END CheckBooleanType;
  4487. (**
  4488. if expression x is of set type then return true else report error and return false
  4489. **)
  4490. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4491. VAR result: BOOLEAN;
  4492. BEGIN
  4493. result := FALSE;
  4494. IF x = SyntaxTree.invalidExpression THEN
  4495. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4496. Error(x.position,"is no set type");
  4497. ELSE result := TRUE
  4498. END;
  4499. RETURN result
  4500. END CheckSetType;
  4501. (**
  4502. if expression x is of string or array of character type then return true else report error and return false
  4503. **)
  4504. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4505. VAR result: BOOLEAN;
  4506. BEGIN
  4507. result := FALSE;
  4508. IF x = SyntaxTree.invalidExpression THEN
  4509. ELSIF ~IsStringType(x.type.resolved) THEN
  4510. Error(x.position,"is no string type");
  4511. ELSE result := TRUE
  4512. END;
  4513. RETURN result
  4514. END CheckStringType;
  4515. (**
  4516. if expression x is a type declaration type return true else report error and return false
  4517. **)
  4518. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4519. VAR result: BOOLEAN;
  4520. BEGIN
  4521. result := FALSE;
  4522. IF x = SyntaxTree.invalidExpression THEN
  4523. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4524. Error(x.position,"is not a type declaration");
  4525. ELSE result := TRUE
  4526. END;
  4527. RETURN result
  4528. END CheckTypeDeclarationType;
  4529. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4530. VAR result: BOOLEAN;
  4531. BEGIN
  4532. result := FALSE;
  4533. IF x = SyntaxTree.invalidExpression THEN
  4534. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4535. result := TRUE;
  4536. value := x.resolved(SyntaxTree.IntegerValue).value;
  4537. ELSE
  4538. Error(x.position,"expression is not an integer constant");
  4539. END;
  4540. RETURN result;
  4541. END CheckIntegerValue;
  4542. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4543. VAR result: BOOLEAN;
  4544. BEGIN
  4545. result := FALSE;
  4546. IF x = SyntaxTree.invalidExpression THEN
  4547. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4548. result := TRUE;
  4549. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4550. ELSE
  4551. Error(x.position,"expression is not an integer constant");
  4552. END;
  4553. RETURN result;
  4554. END CheckStringValue;
  4555. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4556. BEGIN
  4557. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4558. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4559. ELSE
  4560. RETURN FALSE
  4561. END;
  4562. END IsUnsignedValue;
  4563. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4564. BEGIN
  4565. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4566. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4567. ELSE
  4568. RETURN FALSE
  4569. END
  4570. END IsAddressValue;
  4571. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4572. BEGIN
  4573. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4574. END IsAddressExpression;
  4575. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4576. BEGIN
  4577. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4578. END IsSizeExpression;
  4579. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4580. VAR result: BOOLEAN;
  4581. BEGIN
  4582. result := FALSE;
  4583. IF x = SyntaxTree.invalidExpression THEN
  4584. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4585. result := TRUE;
  4586. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4587. ELSE
  4588. Error(x.position,"expression is not an integer constant");
  4589. END;
  4590. RETURN result;
  4591. END CheckEnumerationValue;
  4592. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4593. VAR result: BOOLEAN;
  4594. BEGIN
  4595. result := FALSE;
  4596. IF x = SyntaxTree.invalidExpression THEN
  4597. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4598. result := TRUE;
  4599. value := x.resolved(SyntaxTree.CharacterValue).value;
  4600. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4601. result := TRUE;
  4602. value := x.resolved(SyntaxTree.StringValue).value[0];
  4603. ELSE
  4604. Error(x.position,"expression is not a character constant");
  4605. END;
  4606. RETURN result;
  4607. END CheckCharacterValue;
  4608. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4609. VAR result: BOOLEAN;
  4610. BEGIN
  4611. result := FALSE;
  4612. IF x = SyntaxTree.invalidExpression THEN
  4613. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4614. value := x.resolved(SyntaxTree.IntegerValue).value;
  4615. IF (value > 0) OR includeZero & (value = 0) THEN
  4616. result := TRUE;
  4617. ELSE
  4618. Error(x.position,"integer is not positive");
  4619. END
  4620. ELSE
  4621. Error(x.position,"expression is not an integer constant");
  4622. END;
  4623. RETURN result;
  4624. END CheckPositiveIntegerValue;
  4625. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4626. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4627. BEGIN
  4628. result := FALSE;
  4629. IF x = SyntaxTree.invalidExpression THEN
  4630. ELSE
  4631. type := x.type.resolved;
  4632. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4633. portType := type(SyntaxTree.PortType);
  4634. result := TRUE
  4635. ELSE
  4636. Error(x.position,"no port type");
  4637. END;
  4638. END;
  4639. RETURN result
  4640. END CheckPortType;
  4641. (* move to builtin procedure call statement ?
  4642. remove builtin procedure call designator ?
  4643. *)
  4644. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4645. VAR
  4646. numberActualParameters,numberFormalParameters: LONGINT;
  4647. formalParameter: SyntaxTree.Parameter;
  4648. actualParameter: SyntaxTree.Expression;
  4649. procedureType: SyntaxTree.ProcedureType;
  4650. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4651. inPort, outPort: SyntaxTree.PortType;
  4652. constructor: SyntaxTree.Procedure;
  4653. type0,type1,type2: SyntaxTree.Type;
  4654. type,base,parameterType: SyntaxTree.Type;
  4655. arrayType: SyntaxTree.ArrayType;
  4656. i,i0,i1: LONGINT;
  4657. r,r0,r1,im: LONGREAL;
  4658. c: CHAR;
  4659. id: LONGINT;
  4660. b: BOOLEAN;
  4661. first: LONGINT;
  4662. mathArrayType: SyntaxTree.MathArrayType;
  4663. customBuiltin: SyntaxTree.CustomBuiltin;
  4664. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4665. VAR resultB: BOOLEAN;
  4666. BEGIN
  4667. IF numberActualParameters < from THEN
  4668. Error(position, "less actual than formal parameters");
  4669. result := SyntaxTree.invalidExpression;
  4670. resultB := FALSE;
  4671. ELSIF numberActualParameters > to THEN
  4672. Error(position, "more actual than formal parameters");
  4673. result := SyntaxTree.invalidExpression;
  4674. resultB := FALSE;
  4675. ELSE
  4676. resultB := TRUE;
  4677. END;
  4678. RETURN resultB
  4679. END CheckArity;
  4680. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4681. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4682. BEGIN
  4683. WHILE modifier # NIL DO
  4684. symbol := cellType.FindProperty(modifier.identifier);
  4685. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4686. propertyType := symbol.type.resolved;
  4687. modifierType := modifier.expression.type.resolved;
  4688. IF ~CompatibleTo(system, modifierType, propertyType) &
  4689. ~(
  4690. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4691. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4692. Error(modifier.position,"incompatible to cell property");
  4693. END;
  4694. ELSE
  4695. Error(modifier.position, "undefined property");
  4696. END;
  4697. modifier := modifier.nextModifier;
  4698. END;
  4699. END CheckModifiers;
  4700. BEGIN
  4701. type := NIL; result := NIL;
  4702. type0 := NIL; type1 := NIL; type2 := NIL;
  4703. numberActualParameters := actualParameters.Length();
  4704. IF numberActualParameters>0 THEN
  4705. parameter0 := actualParameters.GetExpression(0);
  4706. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4707. Error(parameter0.position,"forbidden type-less argument");
  4708. result := SyntaxTree.invalidExpression
  4709. END
  4710. END;
  4711. IF numberActualParameters >1 THEN
  4712. parameter1 := actualParameters.GetExpression(1);
  4713. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4714. ELSE
  4715. Error(parameter1.position,"forbidden type-less argument");
  4716. result := SyntaxTree.invalidExpression
  4717. END
  4718. END;
  4719. IF numberActualParameters >2 THEN
  4720. parameter2 := actualParameters.GetExpression(2);
  4721. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4722. ELSE
  4723. Error(parameter2.position,"forbidden type-less argument");
  4724. result := SyntaxTree.invalidExpression
  4725. END
  4726. END;
  4727. IF returnType # NIL THEN
  4728. id := Global.New;
  4729. result := NIL;
  4730. ELSE
  4731. id := builtin.id;
  4732. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4733. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4734. END;
  4735. END;
  4736. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4737. ELSIF result # NIL THEN type := result.type (* operator *)
  4738. ELSE
  4739. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4740. result(SyntaxTree.Designator).SetLeft(left);
  4741. IF returnType # NIL THEN
  4742. type := returnType;
  4743. END;
  4744. (* ---- ASSERT ----- *)
  4745. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4746. IF CheckBooleanType(parameter0) THEN
  4747. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4748. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4749. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4750. *)
  4751. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4752. Error(position, "assert failed");
  4753. END;
  4754. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4755. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4756. rules imposed by the architecture / current runtime
  4757. *)
  4758. END;
  4759. END;
  4760. (* ---- COPY ----- *)
  4761. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4762. IF~IsStringType(type0) THEN
  4763. Error(parameter0.position,"no string type");
  4764. END;
  4765. IF ~IsStringType(type1) THEN
  4766. Error(parameter1.position,"no string type");
  4767. ELSIF CheckVariable(parameter1) THEN
  4768. IF (type0 IS SyntaxTree.StringType) THEN
  4769. arrayType := type1(SyntaxTree.ArrayType);
  4770. IF arrayType.form = SyntaxTree.Static THEN
  4771. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4772. Error(position, "destination length smaller than source length")
  4773. END;
  4774. END;
  4775. END;
  4776. END;
  4777. (* ---- INC, DEC----- *)
  4778. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4779. IF numberActualParameters = 1 THEN
  4780. parameter1 :=Global.NewIntegerValue(system,position,1);
  4781. actualParameters.AddExpression(parameter1);
  4782. END;
  4783. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4784. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4785. Error(position, "incompatible increment");
  4786. ELSE
  4787. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4788. actualParameters.SetExpression(1,parameter1);
  4789. END;
  4790. END;
  4791. (* ---- EXCL, INCL----- *)
  4792. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4793. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4794. IF IsIntegerValue(parameter1,i0) THEN
  4795. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4796. Error(position, "parameter out of SET range")
  4797. END;
  4798. END;
  4799. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4800. actualParameters.SetExpression(1,parameter1);
  4801. END;
  4802. (* ---- HALT, SYSTEM.HALT ----- *)
  4803. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4804. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4805. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4806. rules imposed by the architecture / current runtime
  4807. *)
  4808. END;
  4809. (* ---- WAIT ----- *)
  4810. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4811. IF CheckObjectType(parameter0) THEN
  4812. END;
  4813. (* ---- NEW ----- *)
  4814. ELSIF (id = Global.New) THEN
  4815. IF returnType # NIL THEN
  4816. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4817. ELSE
  4818. first := 1;
  4819. END;
  4820. IF CheckArity(first,Infinity) THEN
  4821. IF currentIsRealtime THEN
  4822. Error(position, "forbidden new in realtime block");
  4823. END;
  4824. (* check constructor *)
  4825. IF (first =0) OR CheckVariable(parameter0) THEN
  4826. IF type0 IS SyntaxTree.PointerType THEN
  4827. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4828. ELSIF type0 IS SyntaxTree.CellType THEN
  4829. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4830. ELSE
  4831. Error(position, "forbidden new on value type");
  4832. END;
  4833. IF type0 IS SyntaxTree.ArrayType THEN
  4834. arrayType := type0(SyntaxTree.ArrayType);
  4835. IF arrayType.form = SyntaxTree.Static THEN
  4836. i := first
  4837. ELSIF arrayType.form = SyntaxTree.Open THEN
  4838. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4839. ELSE HALT(100)
  4840. END;
  4841. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4842. i := first;
  4843. REPEAT
  4844. actualParameter := actualParameters.GetExpression(i);
  4845. IF CheckSizeType(actualParameter) THEN
  4846. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4847. actualParameters.SetExpression(i,actualParameter);
  4848. END;
  4849. INC(i);
  4850. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4851. END;
  4852. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4853. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4854. IF constructor = NIL THEN
  4855. IF CheckArity(first,first) THEN END;
  4856. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4857. Error(position, "new on object with hidden constructor");
  4858. ELSE
  4859. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4860. numberFormalParameters := procedureType.numberParameters;
  4861. IF numberActualParameters-first <= numberFormalParameters THEN
  4862. formalParameter := procedureType.firstParameter;
  4863. FOR i := first TO numberActualParameters-1 DO
  4864. actualParameter := actualParameters.GetExpression(i);
  4865. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4866. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4867. ELSE
  4868. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4869. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4870. END;
  4871. actualParameters.SetExpression(i,actualParameter);
  4872. END;
  4873. formalParameter := formalParameter.nextParameter;
  4874. END;
  4875. WHILE (formalParameter # NIL) DO
  4876. IF formalParameter.defaultValue # NIL THEN
  4877. actualParameters.AddExpression(formalParameter.defaultValue);
  4878. formalParameter := formalParameter.nextParameter
  4879. ELSE
  4880. Error(position, "less actual than formal parameters");
  4881. formalParameter := NIL;
  4882. END;
  4883. END;
  4884. ELSE
  4885. Error(position, "more actual than formal parameters")
  4886. END;
  4887. END;
  4888. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4889. mathArrayType := type0(SyntaxTree.MathArrayType);
  4890. IF mathArrayType.form = SyntaxTree.Static THEN
  4891. Error(position, "new on static array");
  4892. ELSE
  4893. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4894. i0 := first+1; i1 := Infinity;
  4895. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4896. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4897. i1 := i0;
  4898. ELSE HALT(100);
  4899. END;
  4900. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4901. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4902. base := ArrayBase(type0,MAX(LONGINT));
  4903. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4904. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4905. IF ~CompatibleTo(system,type0,parameterType) THEN
  4906. Error(parameter0.position,"incompatible parameter in new");
  4907. result := SyntaxTree.invalidExpression;
  4908. ELSE
  4909. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4910. END;
  4911. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4912. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4913. IF ~CompatibleTo(system,type1,parameterType) THEN
  4914. Error(parameter1.position,"parameter incompatible to math array of longint");
  4915. result := SyntaxTree.invalidExpression;
  4916. ELSE
  4917. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4918. END;
  4919. ELSE
  4920. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4921. i := first;
  4922. REPEAT
  4923. actualParameter := actualParameters.GetExpression(i);
  4924. IF CheckSizeType(actualParameter) THEN
  4925. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4926. actualParameters.SetExpression(i,actualParameter);
  4927. END;
  4928. INC(i);
  4929. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4930. END;
  4931. END;
  4932. END;
  4933. ELSIF type0 IS SyntaxTree.CellType THEN
  4934. IF ~(currentIsCellNet) THEN
  4935. Error(position, "cell allocation outside activeCells ");
  4936. ELSE
  4937. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4938. IF (constructor = NIL) & CheckArity(1,1) THEN
  4939. (* ok *)
  4940. ELSE
  4941. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4942. numberFormalParameters := procedureType.numberParameters;
  4943. DEC(numberActualParameters);
  4944. IF numberActualParameters <= numberFormalParameters THEN
  4945. formalParameter := procedureType.firstParameter;
  4946. FOR i := first TO numberActualParameters DO
  4947. actualParameter := actualParameters.GetExpression(i);
  4948. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4949. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4950. ELSE
  4951. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4952. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4953. END;
  4954. actualParameters.SetExpression(i,actualParameter);
  4955. END;
  4956. formalParameter := formalParameter.nextParameter;
  4957. END;
  4958. WHILE (formalParameter # NIL) DO
  4959. IF formalParameter.defaultValue # NIL THEN
  4960. actualParameters.AddExpression(formalParameter.defaultValue);
  4961. formalParameter := formalParameter.nextParameter
  4962. ELSE
  4963. Error(position, "less actual than formal parameters");
  4964. formalParameter := NIL;
  4965. END;
  4966. END;
  4967. ELSE
  4968. Error(position, "more actual than formal parameters")
  4969. END;
  4970. END;
  4971. END;
  4972. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4973. activeCellsStatement := TRUE;
  4974. ELSE
  4975. Error(position, "cannot be allocated");
  4976. END;
  4977. END;
  4978. END;
  4979. (* ---- DISPOSE ----- *)
  4980. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4981. IF ~IsPointerType(parameter0.type) THEN
  4982. Error(parameter0.position,"is not a pointer")
  4983. ELSIF ~IsDisposable(parameter0.type) THEN
  4984. Error(parameter0.position,"is not disposable")
  4985. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4986. END
  4987. (* ---- GETPROCEDURE ----- *)
  4988. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4989. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4990. IF CheckVariable(parameter2) THEN
  4991. IF ~GetProcedureAllowed(parameter2.type) THEN
  4992. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  4993. END;
  4994. END;
  4995. END;
  4996. (* ---- ABS ----- *)
  4997. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4998. (* note: ABS on complex numbers is done using overloading *)
  4999. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5000. type := type0;
  5001. IF IsIntegerValue(parameter0,i0) THEN
  5002. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5003. type := Global.GetIntegerType(system,ABS(i0));
  5004. ELSIF IsRealValue(parameter0,r) THEN
  5005. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5006. END;
  5007. ELSE
  5008. type := SyntaxTree.invalidType;
  5009. END;
  5010. (* ---- ASH, ASR ----- *)
  5011. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5012. type := type0;
  5013. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5014. (*
  5015. ConvertOperands(parameter0,parameter1); (* same type *)
  5016. *)
  5017. type := parameter0.type;
  5018. IF IsIntegerValue(parameter0,i0) THEN
  5019. IF IsIntegerValue(parameter1,i1) THEN
  5020. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5021. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5022. result := ResolveExpression(result);
  5023. type := Global.GetIntegerType(system,i0);
  5024. END;
  5025. END;
  5026. IF type.resolved.sizeInBits < 32 THEN
  5027. type := system.longintType;
  5028. END;
  5029. (*!compatibility with release, remove when resolved
  5030. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5031. *)
  5032. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5033. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5034. actualParameters.SetExpression(0,parameter0);
  5035. actualParameters.SetExpression(1,parameter1);
  5036. END;
  5037. (* ---- CAP ----- *)
  5038. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5039. type := system.characterType;
  5040. IF CheckCharacterType (parameter0) THEN
  5041. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5042. actualParameters.SetExpression(0,parameter0);
  5043. IF IsCharacterValue(parameter0,c) THEN
  5044. IF (c <= "z") & (c >= "a") THEN
  5045. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5046. ELSE
  5047. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5048. END;
  5049. END;
  5050. END;
  5051. (* ---- CHR ----- *)
  5052. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5053. IF id = Global.Chr THEN
  5054. type := system.characterType
  5055. ELSE
  5056. type := system.characterType32
  5057. END;
  5058. IF CheckIntegerType(parameter0) THEN
  5059. IF IsIntegerValue(parameter0,i0) THEN
  5060. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5061. result := ResolveExpression(result);
  5062. ELSE
  5063. (*
  5064. result := NewConversion(parameter0.position,parameter0,type);
  5065. *)
  5066. END;
  5067. END
  5068. (* ---- ENTIER ----- *)
  5069. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5070. type := system.longintType;
  5071. IF CheckRealType(parameter0) THEN
  5072. IF IsRealValue(parameter0,r) THEN
  5073. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5074. type := Global.GetIntegerType(system,ENTIER(r));
  5075. END
  5076. END;
  5077. (* ---- ENTIERH ----- *)
  5078. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5079. type := system.hugeintType;
  5080. IF CheckRealType(parameter0) THEN
  5081. IF IsRealValue(parameter0,r) THEN
  5082. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5083. END
  5084. END;
  5085. (* ---- LEN ----- *)
  5086. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5087. type := system.longintType;
  5088. base := type0;
  5089. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5090. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5091. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5092. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5093. Error(position, "forbidden len on unsafe pointer");
  5094. END;
  5095. type0 := base;
  5096. ELSE
  5097. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5098. type0 := parameter0.type.resolved;
  5099. actualParameters.SetExpression(0,parameter0);
  5100. base := type0;
  5101. END;
  5102. END;
  5103. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5104. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5105. IF i1 < 0 THEN
  5106. Error(position, "invalid dimension");
  5107. base := SyntaxTree.invalidType;
  5108. ELSE
  5109. base := ArrayBase(base,i1);
  5110. IF (base # NIL) & Indexable(base) THEN
  5111. ELSE
  5112. Error(position, "len on no array");
  5113. IF VerboseErrorMessage THEN
  5114. Printout.Info("base",base);
  5115. END;
  5116. base := SyntaxTree.invalidType;
  5117. END;
  5118. END;
  5119. IF numberActualParameters=2 THEN
  5120. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5121. actualParameters.SetExpression(1,parameter1);
  5122. ELSIF base IS SyntaxTree.MathArrayType THEN
  5123. Error(position, "missing dimension specification");
  5124. END;
  5125. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5126. IF base IS SyntaxTree.ArrayType THEN
  5127. arrayType := base(SyntaxTree.ArrayType);
  5128. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5129. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5130. result := Global.NewIntegerValue(system,position,i);
  5131. type := result.type;(* arrayType.length.type;*)
  5132. ASSERT(type # NIL);
  5133. END;
  5134. ELSIF base IS SyntaxTree.MathArrayType THEN
  5135. mathArrayType := base(SyntaxTree.MathArrayType);
  5136. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5137. result := Global.NewIntegerValue(system,position,i);
  5138. type := result.type;
  5139. (*
  5140. type := mathArrayType.length.type;
  5141. *)
  5142. ASSERT(type # NIL);
  5143. END;
  5144. END;
  5145. END;
  5146. ELSE
  5147. type := system.longintType;
  5148. END;
  5149. (* ---- FIRST ---- *)
  5150. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5151. type := system.longintType;
  5152. IF CheckRangeType(parameter0) THEN END;
  5153. result.SetAssignable(parameter0.assignable)
  5154. (* ---- LAST ---- *)
  5155. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5156. type := system.longintType;
  5157. IF CheckRangeType(parameter0) THEN END;
  5158. result.SetAssignable(parameter0.assignable)
  5159. (* ---- STEP ---- *)
  5160. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5161. type := system.longintType;
  5162. IF CheckRangeType(parameter0) THEN END;
  5163. result.SetAssignable(parameter0.assignable)
  5164. (* ---- RE ---- *)
  5165. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5166. IF CheckNumberType(parameter0) THEN
  5167. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5168. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5169. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5170. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5171. type := parameter0.type
  5172. ELSE
  5173. type := system.realType
  5174. END
  5175. END;
  5176. result.SetAssignable(parameter0.assignable)
  5177. (* ---- IM ---- *)
  5178. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5179. IF CheckNumberType(parameter0) THEN
  5180. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5181. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5182. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5183. ELSE
  5184. type := system.realType;
  5185. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5186. END
  5187. END;
  5188. result.SetAssignable(parameter0.assignable)
  5189. (* ---- MAX ----- *)
  5190. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5191. IF numberActualParameters = 1 THEN
  5192. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5193. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5194. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5195. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5196. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5197. *)
  5198. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5199. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5200. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5201. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5202. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5203. END;
  5204. ELSE
  5205. Error(parameter0.position,"is not a type symbol");
  5206. END
  5207. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5208. ConvertOperands(parameter0,parameter1);
  5209. actualParameters.SetExpression(0,parameter0);
  5210. actualParameters.SetExpression(1,parameter1);
  5211. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5212. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5213. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5214. END;
  5215. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5216. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5217. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5218. END;
  5219. END;
  5220. type := parameter0.type;
  5221. ELSE type := SyntaxTree.invalidType;
  5222. END;
  5223. (* ---- MIN ----- *)
  5224. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5225. IF numberActualParameters = 1 THEN
  5226. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5227. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5228. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5229. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5230. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5231. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5232. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5233. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5234. END;
  5235. ELSE
  5236. Error(parameter0.position,"is not a type symbol");
  5237. END
  5238. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5239. ConvertOperands(parameter0,parameter1);
  5240. actualParameters.SetExpression(0,parameter0);
  5241. actualParameters.SetExpression(1,parameter1);
  5242. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5243. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5244. ELSE result.SetResolved(parameter1.resolved)
  5245. END;
  5246. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5247. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5248. ELSE result.SetResolved(parameter1.resolved)
  5249. END;
  5250. END;
  5251. type := parameter0.type;
  5252. ELSE type := SyntaxTree.invalidType;
  5253. END;
  5254. (* ---- ODD ----- *)
  5255. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5256. type := system.booleanType;
  5257. IF CheckIntegerType(parameter0) THEN
  5258. IF IsIntegerValue(parameter0,i0) THEN
  5259. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5260. type := system.booleanType;
  5261. END;
  5262. END;
  5263. (* ---- ORD ----- *)
  5264. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5265. IF id = Global.Ord THEN
  5266. type := system.integerType;
  5267. ELSE
  5268. type := system.longintType;
  5269. END;
  5270. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5271. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5272. actualParameters.SetExpression(0,parameter0);
  5273. (* IF CheckCharacterType(parameter0) THEN*)
  5274. IF IsCharacterValue(parameter0,c)THEN
  5275. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5276. type := Global.GetSignedIntegerType(system,ORD(c));
  5277. END;
  5278. ELSE Error(parameter0.position, "incompatible parameter");
  5279. END;
  5280. (* ---- SHORT ----- *)
  5281. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5282. type := type0;
  5283. IF IsSignedIntegerType(type) THEN
  5284. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5285. ELSIF type = system.integerType THEN type := system.shortintType
  5286. ELSIF type = system.longintType THEN type := system.integerType
  5287. ELSIF type = system.hugeintType THEN type:= system.longintType
  5288. ELSE
  5289. CASE type.sizeInBits OF
  5290. 16: type := Global.Integer8
  5291. |32: type := Global.Integer16
  5292. |64: type := Global.Integer32
  5293. END;
  5294. END;
  5295. ELSIF type IS SyntaxTree.FloatType THEN
  5296. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5297. ELSIF type = system.longrealType THEN type := system.realType
  5298. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5299. END;
  5300. ELSIF type IS SyntaxTree.ComplexType THEN
  5301. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5302. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5303. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5304. END;
  5305. ELSE
  5306. Error(parameter0.position,"short not applicable")
  5307. END;
  5308. IF (parameter0.resolved # NIL) THEN
  5309. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5310. IF parameter0 IS SyntaxTree.Value THEN
  5311. result.SetResolved(parameter0(SyntaxTree.Value));
  5312. END;
  5313. END;
  5314. (* ---- LONG ----- *)
  5315. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5316. type := type0;
  5317. IF IsSignedIntegerType(type) THEN
  5318. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5319. ELSIF type = system.longintType THEN type := system.hugeintType
  5320. ELSIF type = system.integerType THEN type := system.longintType
  5321. ELSIF type = system.shortintType THEN type := system.integerType
  5322. ELSE
  5323. CASE type.sizeInBits OF
  5324. 8: type := Global.Integer16
  5325. |16: type := Global.Integer32
  5326. |32: type := Global.Integer64
  5327. END;
  5328. END;
  5329. ELSIF type IS SyntaxTree.FloatType THEN
  5330. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5331. ELSIF type= system.realType THEN type := system.longrealType
  5332. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5333. END;
  5334. ELSIF type IS SyntaxTree.ComplexType THEN
  5335. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5336. ELSIF type = system.complexType THEN type := system.longcomplexType
  5337. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5338. END;
  5339. ELSE
  5340. Error(parameter0.position,"long not applicable")
  5341. END;
  5342. IF (parameter0.resolved # NIL) THEN
  5343. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5344. IF parameter0 IS SyntaxTree.Value THEN
  5345. result.SetResolved(parameter0(SyntaxTree.Value));
  5346. END;
  5347. END;
  5348. (* ---- SIZE OF ----- *)
  5349. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5350. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5351. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5352. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5353. type := system.integerType;
  5354. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5355. ELSE
  5356. (* for variables, system sizeof could represent the physically occupied size
  5357. determined via the type descriptor, implement that ? *)
  5358. Error(parameter0.position,"is not a type symbol");
  5359. END
  5360. (* ---- SYSTEM.TRACE -----*)
  5361. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5362. FOR i := 0 TO numberActualParameters-1 DO
  5363. parameter0 := actualParameters.GetExpression(i);
  5364. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5365. Error(parameter0.position,"incompatible parameter");
  5366. END;
  5367. END;
  5368. (* remaining issues can only be tested in backend *)
  5369. (* ---- ADDRESSOF----- *)
  5370. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5371. IF HasAddress(parameter0) THEN
  5372. type := system.addressType;
  5373. ELSE
  5374. type := SyntaxTree.invalidType;
  5375. Error(parameter0.position,"has no address");
  5376. END;
  5377. (* ---- BIT ----- *)
  5378. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5379. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5380. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5381. actualParameters.SetExpression(0,parameter0);
  5382. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5383. actualParameters.SetExpression(1,parameter1);
  5384. END;
  5385. type := system.booleanType;
  5386. (* ----- MSK ---- *)
  5387. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5388. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5389. ConvertOperands(parameter0,parameter1);
  5390. actualParameters.SetExpression(0,parameter0);
  5391. actualParameters.SetExpression(1,parameter1);
  5392. END;
  5393. type := parameter0.type;
  5394. (* ---- SYSTEM.GET64 ----- *)
  5395. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5396. IF CheckAddressType(parameter0) THEN
  5397. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5398. actualParameters.SetExpression(0,parameter0);
  5399. END;
  5400. type := system.hugeintType;
  5401. (* ---- SYSTEM.GET32 ----- *)
  5402. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5403. IF CheckAddressType(parameter0) THEN
  5404. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5405. actualParameters.SetExpression(0,parameter0);
  5406. END;
  5407. type := system.longintType;
  5408. (* ---- SYSTEM.GET16 ----- *)
  5409. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5410. IF CheckAddressType(parameter0) THEN
  5411. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5412. actualParameters.SetExpression(0,parameter0);
  5413. END;
  5414. type := system.integerType;
  5415. (* ---- SYSTEM.GET8 ----- *)
  5416. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5417. IF CheckAddressType(parameter0) THEN
  5418. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5419. actualParameters.SetExpression(0,parameter0);
  5420. END;
  5421. type := system.shortintType;
  5422. (* ---- SYSTEM.GetStackPointer ----- *)
  5423. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5424. type := system.addressType;
  5425. (* ---- SYSTEM.GetFramePointer ----- *)
  5426. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5427. type := system.addressType;
  5428. (* ---- SYSTEM.GetActivity ----- *)
  5429. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5430. type := system.objectType;
  5431. (* ---- SYSTEM.SetStackPointer ----- *)
  5432. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5433. IF CheckAddressType(parameter0) THEN
  5434. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5435. actualParameters.SetExpression(0,parameter0);
  5436. END;
  5437. (* ---- SYSTEM.SetFramePointer ----- *)
  5438. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5439. IF CheckAddressType(parameter0) THEN
  5440. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5441. actualParameters.SetExpression(0,parameter0);
  5442. END;
  5443. (* ---- SYSTEM.SetActivity ----- *)
  5444. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5445. IF CheckObjectType(parameter0) THEN
  5446. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5447. actualParameters.SetExpression(0,parameter0);
  5448. END;
  5449. (* ---- LSH, LSL, ROT, ROR ----- *)
  5450. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5451. type := type0;
  5452. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5453. actualParameters.SetExpression(1, parameter1);
  5454. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5455. IF id = Global.Lsh THEN
  5456. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5457. ELSIF id = Global.Rot THEN
  5458. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5459. ELSIF id = Global.Ror THEN
  5460. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5461. END;
  5462. END;
  5463. (* ---- SYSTEM.VAL ----- *)
  5464. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5465. IF CheckTypeDeclarationType(parameter0) THEN
  5466. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5467. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5468. result := SyntaxTree.invalidExpression;
  5469. Error(parameter0.position,"is no basic type");
  5470. ELSE
  5471. IF (parameter1.resolved # NIL) THEN
  5472. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5473. IF parameter0 IS SyntaxTree.Value THEN
  5474. result.SetResolved(parameter0(SyntaxTree.Value));
  5475. END;
  5476. END;
  5477. result.SetAssignable(parameter1.assignable);
  5478. END;
  5479. END;
  5480. (* ---- SYSTEM.GET ----- *)
  5481. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5482. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5483. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5484. actualParameters.SetExpression(0,parameter0);
  5485. END;
  5486. (* ---- SYSTEM.PUT ----- *)
  5487. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5488. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5489. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5490. actualParameters.SetExpression(0,parameter0);
  5491. END;
  5492. (* ---- SYSTEM.PUT64 ----- *)
  5493. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5494. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5495. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5496. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5497. actualParameters.SetExpression(0,parameter0);
  5498. actualParameters.SetExpression(1,parameter1);
  5499. END;
  5500. (* ---- SYSTEM.PUT32 ----- *)
  5501. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5502. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5503. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5504. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5505. actualParameters.SetExpression(0,parameter0);
  5506. actualParameters.SetExpression(1,parameter1);
  5507. END;
  5508. (* ---- SYSTEM.PUT16 ----- *)
  5509. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5510. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5511. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5512. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5513. actualParameters.SetExpression(0,parameter0);
  5514. actualParameters.SetExpression(1,parameter1);
  5515. END;
  5516. (* ---- SYSTEM.PUT8 ----- *)
  5517. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5518. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5519. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5520. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5521. actualParameters.SetExpression(0,parameter0);
  5522. actualParameters.SetExpression(1,parameter1);
  5523. END;
  5524. (* ---- SYSTEM.MOVE ----- *)
  5525. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5526. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5527. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5528. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5529. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5530. actualParameters.SetExpression(0,parameter0);
  5531. actualParameters.SetExpression(1,parameter1);
  5532. actualParameters.SetExpression(2,parameter2);
  5533. END;
  5534. (* ---- SYSTEM.NEW ----- *)
  5535. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5536. IF ~IsPointerType(parameter0.type) THEN
  5537. Error(parameter0.position,"is not a pointer")
  5538. ELSIF CheckSizeType(parameter1) THEN
  5539. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5540. actualParameters.SetExpression(1,parameter1);
  5541. END;
  5542. (* ----SYSTEM.REF ---- *)
  5543. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5544. type := system.addressType
  5545. (* ---- INCR ----- *)
  5546. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5547. type := system.sizeType;
  5548. base := type0;
  5549. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5550. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5551. IF i1 < 0 THEN
  5552. Error(position, "invalid dimension");
  5553. base := SyntaxTree.invalidType;
  5554. ELSE
  5555. base := ArrayBase(base,i1);
  5556. IF (base # NIL) & Indexable(base) THEN
  5557. ELSE
  5558. Error(position, "len on no array");
  5559. IF VerboseErrorMessage THEN
  5560. Printout.Info("base",base);
  5561. END;
  5562. base := SyntaxTree.invalidType;
  5563. END;
  5564. END;
  5565. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5566. actualParameters.SetExpression(1,parameter1);
  5567. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5568. mathArrayType := base(SyntaxTree.MathArrayType);
  5569. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5570. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5571. type := system.longintType;
  5572. END;
  5573. END;
  5574. ELSE
  5575. type := system.longintType;
  5576. END;
  5577. (* ---- SUM ----- *)
  5578. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5579. Error(position, "sum operator not applicable");
  5580. (* ---- ALL ----- *)
  5581. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5582. Error(position, "all operator not applicable");
  5583. (* ---- DIM ----- *)
  5584. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5585. type := system.sizeType;
  5586. IF type0 IS SyntaxTree.MathArrayType THEN
  5587. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5588. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5589. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5590. END;
  5591. ELSE
  5592. Error(position, "dimension on non math array type");
  5593. END;
  5594. (* ---- CAS ----- *)
  5595. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5596. IF type0.IsComposite () THEN
  5597. Error(position, "first parameter of composite type");
  5598. result := SyntaxTree.invalidExpression;
  5599. ELSIF ~IsVariable (parameter0) THEN
  5600. Error(position, "first parameter not assignable");
  5601. result := SyntaxTree.invalidExpression;
  5602. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5603. Error(position, "second parameter incompatible");
  5604. result := SyntaxTree.invalidExpression;
  5605. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5606. Error(position, "third parameter incompatible");
  5607. result := SyntaxTree.invalidExpression;
  5608. ELSE
  5609. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5610. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5611. type := type0;
  5612. END;
  5613. (* ---- RESHAPE ----- *)
  5614. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5615. IF type0 IS SyntaxTree.MathArrayType THEN
  5616. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5617. base := ArrayBase(type0,MAX(LONGINT));
  5618. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5619. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5620. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5621. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5622. IF ~CompatibleTo(system,type0,parameterType) THEN
  5623. Error(parameter0.position,"incompatible parameter in reshape");
  5624. result := SyntaxTree.invalidExpression;
  5625. ELSE
  5626. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5627. END;
  5628. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5629. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5630. IF ~CompatibleTo(system,type1,parameterType) THEN
  5631. Error(parameter1.position,"parameter incompatible to math array of longint");
  5632. result := SyntaxTree.invalidExpression;
  5633. ELSE
  5634. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5635. END;
  5636. ELSE
  5637. Error(position,"reshape on non math array type");
  5638. result := SyntaxTree.invalidExpression;
  5639. END;
  5640. (* ---- SYSTEM.TYPECODE ----- *)
  5641. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5642. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5643. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5644. type := type.resolved;
  5645. IF type IS SyntaxTree.PointerType THEN
  5646. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5647. END;
  5648. IF ~(type IS SyntaxTree.RecordType) THEN
  5649. Error(parameter0.position,"must be type with type descriptor");
  5650. END;
  5651. ELSE
  5652. Error(parameter0.position,"is not a type symbol");
  5653. END;
  5654. type := system.addressType;
  5655. (* -------- FLT --------- *)
  5656. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5657. type := system.realType;
  5658. IF IsRealValue(parameter0, r) THEN
  5659. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5660. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5661. i0 := i; i := ABS(i);
  5662. IF i # 0 THEN
  5663. i1 := 23;
  5664. IF i >= 2*800000H THEN
  5665. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5666. ELSIF i < 800000H THEN
  5667. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5668. END;
  5669. i := (i1 + 127)*800000H - 800000H + i;
  5670. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5671. END;
  5672. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5673. END;
  5674. (* ------- CONNECT -------*)
  5675. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5676. (*IF ~(currentIsCellNet) THEN
  5677. Error(position, "connection outside activeCells body block");
  5678. END;*)
  5679. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5680. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5681. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5682. END;
  5683. IF numberActualParameters = 3 THEN
  5684. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5685. Error(position, "incompatible channel size parameter");
  5686. END;
  5687. *)
  5688. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5689. actualParameters.SetExpression(2,parameter2);
  5690. END;
  5691. activeCellsStatement := TRUE;
  5692. (* ---------- DELEGATE --------*)
  5693. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5694. (*
  5695. IF ~(currentIsCellNet) THEN
  5696. Error(position, "connection delegation outside activeCells body block");
  5697. END;
  5698. *)
  5699. IF ~CheckPortType(parameter1, inPort) THEN
  5700. Error(parameter0.position,"not a port")
  5701. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5702. Error(parameter1.position,"not a port")
  5703. ELSIF (outPort.direction # inPort.direction) THEN
  5704. Error(parameter0.position,"invalid port direction");
  5705. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5706. Error(position, "incompatible port sizes");
  5707. END;
  5708. activeCellsStatement := TRUE;
  5709. (* --------- RECEIVE ---------*)
  5710. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5711. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5712. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5713. IF inPort.direction # SyntaxTree.InPort THEN
  5714. Error(parameter0.position,"not an in-port")
  5715. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5716. Error(parameter1.position,"incompatible to port type");
  5717. END;
  5718. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5719. IF ~SameType(parameter2.type, system.integerType) THEN
  5720. Error(parameter2.position,"incompatible to integer type");
  5721. END;
  5722. END;
  5723. END;
  5724. (* --------- SEND ---------*)
  5725. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5726. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5727. IF CheckPortType(parameter0,outPort) THEN
  5728. IF outPort.direction # SyntaxTree.OutPort THEN
  5729. Error(parameter1.position,"not an out-port")
  5730. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5731. Error(parameter1.position,"incompatible to port type");
  5732. ELSE
  5733. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5734. actualParameters.SetExpression(1,parameter1);
  5735. END;
  5736. END;
  5737. (* ------- custom builtins ----- *)
  5738. ELSIF id = Global.systemSpecial THEN
  5739. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5740. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5741. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5742. type := procedureType.returnType;
  5743. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5744. (* go through all formal parameters *)
  5745. formalParameter := procedureType.firstParameter;
  5746. FOR i := 0 TO actualParameters.Length() - 1 DO
  5747. actualParameter := actualParameters.GetExpression(i);
  5748. IF actualParameter = SyntaxTree.invalidExpression THEN
  5749. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5750. Error(position, "incompatible parameter")
  5751. ELSE
  5752. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5753. END;
  5754. actualParameters.SetExpression(i, actualParameter);
  5755. formalParameter := formalParameter.nextParameter
  5756. END
  5757. END
  5758. ELSE
  5759. Error(position, "builtin not implemented");
  5760. result := SyntaxTree.invalidExpression;
  5761. END;
  5762. END;
  5763. IF result # SyntaxTree.invalidExpression THEN
  5764. type := ResolveType(type);
  5765. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5766. result.SetType(type);
  5767. END;
  5768. RETURN result
  5769. END NewBuiltinCallDesignator;
  5770. (** return type guard designator left(type)
  5771. - check if type can be extended (i.e. is no static record)
  5772. - check if type is a type extension of left.type
  5773. - returns new type guard designator
  5774. returns invalidDesignator = invalidExpression if error
  5775. **)
  5776. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5777. VAR result: SyntaxTree.Designator;
  5778. BEGIN
  5779. result := SyntaxTree.invalidDesignator;
  5780. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5781. Error(position, "no type extension of type");
  5782. IF VerboseErrorMessage THEN
  5783. Printout.Info("left",left);
  5784. Printout.Info("type",type);
  5785. END;
  5786. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5787. Error(position, "variable cannot be extended");
  5788. ELSIF IsUnsafePointer(left.type) THEN
  5789. Error(position, "forbidden type guard on unsafe pointer");
  5790. ELSE
  5791. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5792. result.SetType(type);
  5793. result.SetAssignable(left.assignable);
  5794. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5795. END;
  5796. RETURN result
  5797. END NewTypeGuardDesignator;
  5798. (** check and resolve parameter designator left(expression list)
  5799. - check expression list
  5800. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5801. - elsif left is a procedure type then
  5802. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5803. - else return is a procedure call then return ProcedureCallDesignator
  5804. returns invalidDesignator = invalidExpression if error
  5805. **)
  5806. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5807. VAR
  5808. parameters: SyntaxTree.ExpressionList;
  5809. left: SyntaxTree.Designator;
  5810. result,expression: SyntaxTree.Expression;
  5811. typeDeclaration: SyntaxTree.TypeDeclaration;
  5812. type, expressionType: SyntaxTree.Type;
  5813. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5814. BEGIN
  5815. type := type.resolved;
  5816. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5817. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5818. END;
  5819. RETURN type
  5820. END BaseType;
  5821. BEGIN
  5822. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5823. result := SyntaxTree.invalidDesignator;
  5824. left := ResolveDesignator(designator.left);
  5825. IF left # SyntaxTree.invalidDesignator THEN
  5826. parameters := designator.parameters;
  5827. IF ExpressionList(parameters) THEN
  5828. IF (left.type = NIL) THEN
  5829. Error(left.position,"object is not a procedure or cannot be extended");
  5830. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5831. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5832. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5833. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5834. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5835. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5836. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5837. ELSE
  5838. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5839. END
  5840. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5841. expression := parameters.GetExpression(0);
  5842. type := typeDeclaration.declaredType.resolved;
  5843. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5844. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5845. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5846. OR (expressionType IS SyntaxTree.EnumerationType)
  5847. ) THEN
  5848. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5849. ELSE
  5850. Error(left.position,"invalid type in explicit conversion");
  5851. END;
  5852. ELSE
  5853. Error(left.position,"called object is not a procedure or cannot be extended");
  5854. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5855. result := SyntaxTree.invalidDesignator;
  5856. END;
  5857. ELSE
  5858. result := SyntaxTree.invalidDesignator
  5859. END;
  5860. END;
  5861. resolvedExpression := result;
  5862. END VisitParameterDesignator;
  5863. (** check dereference designator left^
  5864. - check if left is pointer type or left is object type
  5865. - return new dereference designator with type = left.baseType.type (if appropriate)
  5866. with error handling
  5867. returns invalidDesignator = invalidExpression if error
  5868. **)
  5869. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5870. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5871. BEGIN
  5872. result := SyntaxTree.invalidDesignator;
  5873. type := left.type;
  5874. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5875. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5876. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5877. result.SetAssignable(TRUE);
  5878. result.SetType(type);
  5879. result.SetHidden(left.isHidden);
  5880. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5881. type := type.resolved;
  5882. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5883. result.SetAssignable(TRUE);
  5884. result.SetType(type);
  5885. result.SetHidden(left.isHidden);
  5886. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5887. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5888. result.SetAssignable(TRUE);
  5889. result.SetType(type);
  5890. result.SetHidden(left.isHidden);
  5891. ELSE
  5892. Error(position, "dereference on no pointer");
  5893. IF VerboseErrorMessage THEN
  5894. Printout.Info("pointer", type);
  5895. Printout.Info("scope", currentScope);
  5896. END;
  5897. END;
  5898. RETURN result
  5899. END NewDereferenceDesignator;
  5900. (** check supercall designator left^
  5901. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5902. - return new supercall designator with type = left.type
  5903. with error handling
  5904. **)
  5905. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5906. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5907. objectScope: SyntaxTree.Scope;
  5908. BEGIN
  5909. result := SyntaxTree.invalidDesignator;
  5910. IF left = SyntaxTree.invalidDesignator THEN
  5911. (* error already handled *)
  5912. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5913. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5914. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5915. IF symbol IS SyntaxTree.Procedure THEN
  5916. procedure := symbol(SyntaxTree.Procedure);
  5917. objectScope := currentScope;
  5918. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5919. objectScope := objectScope.outerScope;
  5920. END;
  5921. IF (left.left = NIL) OR ~
  5922. (
  5923. (left.left IS SyntaxTree.SelfDesignator) OR
  5924. (left.left IS SyntaxTree.DereferenceDesignator)
  5925. & (left.left(SyntaxTree.Designator).left # NIL)
  5926. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5927. Error(position, "procedure not in immediate object scope");
  5928. IF VerboseErrorMessage THEN
  5929. Printout.Info("left.left",left.left);
  5930. END;
  5931. ELSIF procedure.super # NIL THEN
  5932. result := SyntaxTree.NewSupercallDesignator(position,left);
  5933. result.SetType(left.type.resolved)
  5934. ELSE
  5935. Error(position, "no supermethod for this procedure");
  5936. END;
  5937. ELSE
  5938. Error(position, "symbol is not a procedure");
  5939. END;
  5940. ELSE
  5941. Error(position, "is no symbol designator");
  5942. END;
  5943. RETURN result
  5944. END NewSupercallDesignator;
  5945. (** check and semantically resolve arrow designator left^
  5946. - if left is procedure type -> result := SupercallDesignator
  5947. - else result := DereferenceDesignator
  5948. returns result via global variable resolvedExpression
  5949. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5950. **)
  5951. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5952. VAR left: SyntaxTree.Designator;
  5953. BEGIN
  5954. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5955. left := ResolveDesignator(arrowDesignator.left);
  5956. IF left # NIL THEN
  5957. IF (left.type = NIL) THEN
  5958. Error(arrowDesignator.position,"Invalid arrow designator");
  5959. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5960. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5961. ELSE
  5962. IF IsPointerToObject(left.type) THEN
  5963. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  5964. END;
  5965. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5966. END
  5967. END
  5968. END VisitArrowDesignator;
  5969. (** check and return expression
  5970. - if expression has no type then resolve expression
  5971. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5972. - return result
  5973. **)
  5974. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5975. VAR result,prev: SyntaxTree.Expression;
  5976. BEGIN
  5977. IF expression = NIL THEN result := NIL
  5978. ELSIF (expression.type = NIL) THEN
  5979. prev := resolvedExpression;
  5980. resolvedExpression := SyntaxTree.invalidExpression;
  5981. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5982. expression.SetType(SyntaxTree.invalidType);
  5983. END;
  5984. expression.Accept(SELF);
  5985. result := resolvedExpression;
  5986. IF currentIsRealtime THEN
  5987. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5988. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  5989. END;
  5990. END;
  5991. (* designator modifiers for backends if they support it ...*)
  5992. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5993. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5994. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5995. END;
  5996. resolvedExpression := prev
  5997. ELSE
  5998. result := expression
  5999. END;
  6000. RETURN result
  6001. END ResolveExpression;
  6002. (**
  6003. check expression to be constant expression
  6004. - resolve expression
  6005. - if valid then check that of value type
  6006. report error and return invalidExpression if anything fails
  6007. **)
  6008. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6009. VAR position: Position;
  6010. BEGIN
  6011. position := expression.position;
  6012. expression := ResolveExpression(expression);
  6013. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6014. ELSIF (expression.resolved = NIL) THEN
  6015. Error(position, "expression is not constant");
  6016. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6017. expression := SyntaxTree.invalidExpression;
  6018. END;
  6019. RETURN expression
  6020. END ConstantExpression;
  6021. (** check expression to be constant integer
  6022. - resolve expresssion
  6023. - if valid then check that of integer value type
  6024. report error and return invalidExpression if anything fails
  6025. **)
  6026. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6027. VAR position: Position;
  6028. BEGIN
  6029. position := expression.position;
  6030. expression := ResolveExpression(expression);
  6031. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6032. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6033. expression := SyntaxTree.invalidExpression;
  6034. Error(position, "expression is not a constant integer");
  6035. END;
  6036. RETURN expression
  6037. END ConstantInteger;
  6038. (** check expression as positive (>=0) constant integer
  6039. - resolve expression
  6040. - if valid then check that integer value
  6041. - if integer value then check that value >= 0
  6042. report error and return invalidExpression if anything fails
  6043. **)
  6044. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6045. VAR position: Position;
  6046. BEGIN
  6047. position := expression.position;
  6048. expression := ConstantExpression(expression);
  6049. IF expression = SyntaxTree.invalidExpression THEN
  6050. (* error already reported *)
  6051. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6052. Error(position, "expression is not integer valued");
  6053. expression := SyntaxTree.invalidExpression
  6054. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6055. Error(position, "integer is not greater or equal zero");
  6056. END;
  6057. RETURN expression
  6058. END ConstantIntegerGeq0;
  6059. (** check expression as condition
  6060. - resolve expression
  6061. - if valid expression then check that result type is boolean
  6062. report error and return invalidExpression if anything fails
  6063. **)
  6064. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6065. VAR position: Position;
  6066. BEGIN
  6067. position := expression.position;
  6068. expression := ResolveExpression(expression);
  6069. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6070. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6071. expression := SyntaxTree.invalidExpression;
  6072. Error(position, "expression is not boolean");
  6073. END;
  6074. RETURN expression
  6075. END ResolveCondition;
  6076. (*** symbols ***)
  6077. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6078. BEGIN
  6079. x.Accept(SELF);
  6080. END ResolveSymbol;
  6081. (** check a symbol
  6082. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6083. **)
  6084. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6085. VAR scope: SyntaxTree.Scope;
  6086. BEGIN
  6087. (* visibility *)
  6088. scope := symbol.scope;
  6089. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6090. scope := scope.outerScope;
  6091. END;
  6092. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6093. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6094. Error(symbol.position,"cannot be exported");
  6095. IF VerboseErrorMessage THEN
  6096. Printout.Info("symbol",symbol);
  6097. END;
  6098. END;
  6099. END;
  6100. END CheckSymbolVisibility;
  6101. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6102. If node is currently being resolved then emit a cyclic definition error.
  6103. Return TRUE only if node is fully resolved.
  6104. **)
  6105. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6106. VAR result: BOOLEAN;
  6107. BEGIN
  6108. IF SyntaxTree.Resolved IN x.state THEN
  6109. result := FALSE
  6110. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6111. Error(x.position,"cyclic definition");
  6112. result := FALSE;
  6113. ELSE
  6114. result := TRUE;
  6115. x.SetState(SyntaxTree.BeingResolved)
  6116. END;
  6117. RETURN result
  6118. END SymbolNeedsResolution;
  6119. (** check and resolve a type declaration symbol = Type
  6120. - set type to declaration type
  6121. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6122. This is so because the type declaration itself does not have a type but it only stands for a type.
  6123. In the implementation of the compiler this made a lot much easier.
  6124. - resolve and set declared type
  6125. - check symbol
  6126. **)
  6127. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6128. VAR prevScope: SyntaxTree.Scope;
  6129. BEGIN
  6130. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6131. IF SymbolNeedsResolution(typeDeclaration) THEN
  6132. typeDeclaration.SetState(SyntaxTree.Resolved);
  6133. prevScope := currentScope;
  6134. currentScope := typeDeclaration.scope;
  6135. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6136. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6137. CheckSymbolVisibility(typeDeclaration);
  6138. typeDeclaration.SetState(SyntaxTree.Resolved);
  6139. currentScope := prevScope;
  6140. END;
  6141. END VisitTypeDeclaration;
  6142. (** check and resolve a constant declaration symbol = (constant) expression
  6143. - check expression
  6144. - set type and value
  6145. - check symbol
  6146. **)
  6147. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6148. VAR
  6149. expression: SyntaxTree.Expression;
  6150. type: SyntaxTree.Type;
  6151. name: Basic.SegmentedName;
  6152. replacement: Replacement;
  6153. BEGIN
  6154. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6155. IF SymbolNeedsResolution(constant) THEN
  6156. expression := constant.value;
  6157. IF replacements # NIL THEN
  6158. Global.GetSymbolSegmentedName(constant, name);
  6159. replacement := replacements;
  6160. WHILE (replacement # NIL) & (replacement.name # name) DO
  6161. replacement := replacement.next;
  6162. END;
  6163. IF replacement # NIL THEN
  6164. InfoSS(constant.position, "replacing constant", constant.name);
  6165. (*
  6166. NEW(stringReader, Strings.Length(replacement.string^));
  6167. stringReader.Set(replacement.string^);
  6168. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6169. NEW(parser, scanner, diagnostics);
  6170. expression := parser.Expression();
  6171. *)
  6172. expression := replacement.expression;
  6173. replacement.used := TRUE;
  6174. END;
  6175. END;
  6176. constant.SetType(SyntaxTree.invalidType);
  6177. expression := ConstantExpression(expression);
  6178. ASSERT(expression.type # NIL);
  6179. type := expression.type.resolved;
  6180. constant.SetType(type);
  6181. constant.SetValue(expression);
  6182. CheckSymbolVisibility(constant);
  6183. constant.SetState(SyntaxTree.Resolved);
  6184. END;
  6185. END VisitConstant;
  6186. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6187. VAR procedureAlignment: LONGINT;
  6188. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6189. (* least common multiple *)
  6190. VAR a,b: LONGINT;
  6191. BEGIN
  6192. a := a0; b := b0;
  6193. WHILE (a # b) DO
  6194. IF a < b THEN a := a+a0
  6195. ELSE b := b + b0
  6196. END;
  6197. END;
  6198. RETURN a
  6199. END LCM;
  6200. BEGIN
  6201. IF alignment > 1 THEN
  6202. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6203. IF (procedureAlignment > 1) THEN
  6204. alignment := LCM(alignment, procedureAlignment);
  6205. END;
  6206. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6207. END;
  6208. END AdaptStackAlignment;
  6209. (** check and resolve a variable / field
  6210. - check and set type
  6211. - negative check on open array type
  6212. - check symbol
  6213. **)
  6214. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6215. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6216. BEGIN
  6217. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6218. IF SymbolNeedsResolution(variable) THEN
  6219. modifiers := variable.modifiers;
  6220. (*
  6221. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6222. variable.AddFlags(flags);
  6223. *)
  6224. variable.SetType(ResolveType(variable.type));
  6225. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6226. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6227. Error(variable.position,"forbidden open array variable");
  6228. END;
  6229. END;
  6230. CheckSymbolVisibility(variable);
  6231. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6232. variable.SetUntraced(TRUE);
  6233. IF ~ContainsPointer(variable.type) THEN
  6234. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6235. Error(position, "untraced flag on non-pointer variable");
  6236. END;
  6237. END;
  6238. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6239. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6240. IF ~PowerOf2(value) THEN
  6241. Error(position, "forbidden alignment - must be power of two");
  6242. ELSE
  6243. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6244. END;
  6245. END;
  6246. variable.SetAlignment(FALSE,value);
  6247. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6248. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6249. Error(position, "fixed position not possible in procedure");
  6250. END;
  6251. variable.SetAlignment(TRUE, value);
  6252. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6253. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6254. Error(position,"fictive offset not possible in procedure");
  6255. END;
  6256. variable.SetFictive(value);
  6257. variable.SetOffset(value*system.dataUnit);
  6258. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6259. END;
  6260. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6261. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6262. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6263. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6264. END;
  6265. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6266. IF variable.initializer # NIL THEN
  6267. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6268. END;
  6269. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6270. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6271. pointerType.SetPointerBase(variable.type);
  6272. pointerType.SetHidden(TRUE);
  6273. variable.SetType(ResolveType(pointerType));
  6274. END;
  6275. variable.SetState(SyntaxTree.Resolved);
  6276. END;
  6277. END VisitVariable;
  6278. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6279. BEGIN
  6280. VisitVariable(property)
  6281. END VisitProperty;
  6282. (** check and resolve a (procedure) parameter
  6283. - check and set type
  6284. - check symbol
  6285. - check parameter kind and set read-only flags if appropriate
  6286. **)
  6287. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6288. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6289. BEGIN
  6290. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6291. IF SymbolNeedsResolution(parameter) THEN
  6292. modifiers := parameter.modifiers;
  6293. parameter.SetType(ResolveType(parameter.type));
  6294. ASSERT(parameter.type.resolved # NIL);
  6295. CheckSymbolVisibility(parameter);
  6296. IF parameter.defaultValue # NIL THEN
  6297. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6298. Error(parameter.position,"forbidden default value on non-value parameter");
  6299. ELSE
  6300. expression := ConstantExpression(parameter.defaultValue);
  6301. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6302. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6303. parameter.SetDefaultValue(expression);
  6304. END;
  6305. END;
  6306. END;
  6307. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6308. parameter.SetUntraced(TRUE);
  6309. IF ~ContainsPointer(parameter.type) THEN
  6310. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6311. Error(position, "untraced flag on non-pointer variable");
  6312. END;
  6313. END;
  6314. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6315. parameter.SetMoveable(TRUE);
  6316. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6317. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6318. Error(position, "illegal movable flag on non-address variable");
  6319. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6320. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6321. Error(position, "unnecessary movable flag on variable variable");
  6322. END;
  6323. END;
  6324. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6325. parameter.SetState(SyntaxTree.Resolved);
  6326. END;
  6327. END VisitParameter;
  6328. (** check and resolve a procedure (with declaration and implementation scope)
  6329. - check the procedure type
  6330. - check if method (i.e. in record scope), if so then
  6331. - check if (unique) constructor
  6332. - check if (unique) finalizer
  6333. - check if super method available, if so then check signature
  6334. - of not in record scope then negative check on constructor flag
  6335. - of not in record scope then negative check on finalizer flag
  6336. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6337. - check procedure symbol
  6338. **)
  6339. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6340. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6341. procedureType: SyntaxTree.ProcedureType;
  6342. type: SyntaxTree.Type;
  6343. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6344. qualifiedType: SyntaxTree.QualifiedType;
  6345. value: LONGINT;
  6346. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6347. position: Position;
  6348. fp: SyntaxTree.FingerPrint;
  6349. BEGIN
  6350. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6351. IF IsOberonInline(procedure) THEN
  6352. IF SyntaxTree.Public * procedure.access # {} THEN
  6353. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6354. END;
  6355. procedure.SetInline(FALSE);
  6356. procedure.SetOberonInline(TRUE);
  6357. END;
  6358. IF SymbolNeedsResolution(procedure) THEN
  6359. recentIsRealtime := currentIsRealtime;
  6360. recentIsBodyProcedure := currentIsBodyProcedure;
  6361. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6362. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6363. modifiers := procedureType.modifiers;
  6364. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6365. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6366. IF useDarwinCCalls THEN (*fld*)
  6367. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6368. ELSE
  6369. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6370. END
  6371. END;
  6372. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6373. procedureType.SetInterrupt(TRUE);
  6374. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6375. END;
  6376. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6377. procedureType.SetNoReturn(TRUE);
  6378. END;
  6379. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6380. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6381. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6382. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6383. END;
  6384. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6385. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6386. END;
  6387. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6388. IF ~PowerOf2(value) THEN
  6389. Error(position, "forbidden stack alignment - must be power of two");
  6390. ELSE
  6391. procedureType.SetStackAlignment(value)
  6392. END;
  6393. END;
  6394. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6395. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6396. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6397. END;
  6398. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6399. SyntaxTree.InitFingerPrint(fp);
  6400. fp.shallow := value;
  6401. fp.shallowAvailable := TRUE;
  6402. procedure.SetFingerPrint(fp);
  6403. END;
  6404. CheckModifiers(modifiers, TRUE);
  6405. modifiers := procedureType.returnTypeModifiers;
  6406. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6407. CheckModifiers(modifiers, TRUE);
  6408. procedure.SetState(SyntaxTree.Resolved);
  6409. FixProcedureType(procedureType);
  6410. currentIsRealtime := procedureType.isRealtime;
  6411. currentIsBodyProcedure := procedure.isBodyProcedure;
  6412. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6413. THEN
  6414. Error(procedure.position,"problems during parameter offset computation");
  6415. END;
  6416. CheckSymbolVisibility(procedure);
  6417. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6418. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6419. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6420. Error(procedure.position,"problem during parameter offset generation");
  6421. END;
  6422. END;
  6423. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6424. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6425. procedureType.SetDelegate(TRUE);
  6426. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6427. (* add auto-self *)
  6428. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6429. IF (record.pointerType.typeDeclaration = NIL) THEN
  6430. selfParameter.SetType(record.pointerType);
  6431. ELSE
  6432. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6433. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6434. qualifiedType.SetResolved(record.pointerType);
  6435. selfParameter.SetType(qualifiedType);
  6436. END;
  6437. selfParameter.SetAccess(SyntaxTree.Hidden);
  6438. END;
  6439. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6440. IF procedure.isConstructor THEN
  6441. (*! constructor is always visible, compatibility to paco
  6442. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6443. *)
  6444. procedure.MarkUsed;
  6445. IF procedureType.returnType # NIL THEN
  6446. Error(procedure.position,"constructor with forbidden return type");
  6447. END;
  6448. proc := procedure.scope.firstProcedure;
  6449. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6450. proc := proc.nextProcedure;
  6451. END;
  6452. IF proc # NIL THEN
  6453. Error(procedure.position,"duplicate constructor")
  6454. ELSE
  6455. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6456. END;
  6457. END;
  6458. IF procedure.isFinalizer THEN
  6459. procedure.MarkUsed;
  6460. IF procedureType.returnType # NIL THEN
  6461. Error(procedure.position,"finalizer with forbidden return type");
  6462. END;
  6463. IF procedureType.numberParameters # 0 THEN
  6464. Error(procedure.position,"finalizer with formal parameters");
  6465. END;
  6466. proc := procedure.scope.firstProcedure;
  6467. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6468. proc := proc.nextProcedure;
  6469. END;
  6470. IF proc # NIL THEN
  6471. Error(procedure.position,"duplicate finalizer")
  6472. ELSE
  6473. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6474. END;
  6475. END;
  6476. super := FindSuperProcedure(record.recordScope, procedure);
  6477. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6478. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6479. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6480. END;
  6481. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6482. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6483. END;
  6484. IF super.isFinal THEN
  6485. Error(procedure.position,"forbidden method extending final method");
  6486. END;
  6487. procedure.SetSuper(super);
  6488. super.SetOverwritten(TRUE);
  6489. procedure.SetAccess(procedure.access+super.access);
  6490. procedure.MarkUsed;
  6491. END;
  6492. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6493. THEN
  6494. Error(procedure.position,"problems during parameter offset computation");
  6495. END;
  6496. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6497. IF cellsAreObjects THEN
  6498. procedureType.SetDelegate(TRUE);
  6499. END;
  6500. IF procedure.isConstructor THEN
  6501. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6502. END;
  6503. ELSIF procedure.isConstructor THEN
  6504. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6505. END;
  6506. Declarations(procedure.procedureScope, FALSE, {0,1});
  6507. (* body resolution part done as late fix of the procedure type *)
  6508. procedure.SetState(SyntaxTree.Resolved);
  6509. currentIsRealtime := recentIsRealtime;
  6510. currentIsBodyProcedure := recentIsBodyProcedure;
  6511. END;
  6512. END VisitProcedure;
  6513. (**
  6514. a builtin procedure is a global item that may not be modified locally
  6515. instead the resolving of builtin procedure calls are done in the esignator
  6516. **)
  6517. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6518. VAR type: SyntaxTree.Type;
  6519. BEGIN
  6520. type := ResolveType(builtinProcedure.type);
  6521. END VisitBuiltin;
  6522. (* nopov *)
  6523. (** check and resolve operator
  6524. - operators are first checked as procedures
  6525. - then additional operator-specific checks are done
  6526. - note that only module-scope operators are checked here
  6527. (operators in a record scope are only allowed in the context of
  6528. array-structured object types and checked in 'ResolveArrayStructure')
  6529. - also note that inter-operator conformity is not checked here
  6530. **)
  6531. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6532. VAR
  6533. procedureType: SyntaxTree.ProcedureType;
  6534. leftType, rightType: SyntaxTree.Type;
  6535. identifierNumber: LONGINT; position: Position;
  6536. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6537. modifiers: SyntaxTree.Modifier;
  6538. (** whether a type is locally defined in the current module scope
  6539. for arrays, the base type must be locally defined **)
  6540. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6541. BEGIN
  6542. IF type = NIL THEN
  6543. RETURN FALSE
  6544. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6545. RETURN TRUE
  6546. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6547. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6548. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6549. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6550. ELSE
  6551. RETURN FALSE
  6552. END
  6553. END IsLocallyDefined;
  6554. BEGIN
  6555. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6556. procedureType := operator.type(SyntaxTree.ProcedureType);
  6557. modifiers := procedureType.modifiers;
  6558. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6559. CheckModifiers(modifiers, TRUE);
  6560. VisitProcedure(operator);
  6561. IF operator.scope IS SyntaxTree.RecordScope THEN
  6562. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6563. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6564. IF identifierNumber = -1 THEN
  6565. Error(operator.position, "operator with unknown identifier")
  6566. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6567. Error(operator.position, "identifier may not be used for operator")
  6568. ELSE
  6569. IF procedureType.numberParameters < 1 THEN
  6570. Error(operator.position, "operator without operand");
  6571. ELSIF procedureType.numberParameters > 2 THEN
  6572. Error(operator.position, "operator with more than two operands");
  6573. ELSE
  6574. (* determine operand types *)
  6575. leftType := procedureType.firstParameter.type;
  6576. IF procedureType.numberParameters > 1 THEN
  6577. rightType := procedureType.firstParameter.nextParameter.type
  6578. ELSE
  6579. rightType := NIL
  6580. END;
  6581. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6582. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6583. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6584. Error(operator.position, "none of the operands is declared in the same module")
  6585. END
  6586. END;
  6587. (* TODO: refine the checks, think about how restrictive the checks should be
  6588. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6589. They might be used for intersection, union, complement of custom object types *)
  6590. (* defaults *)
  6591. hasReturnType := TRUE;
  6592. mustBeUnary := FALSE;
  6593. mustBeBinary := FALSE;
  6594. mustReturnBoolean := FALSE;
  6595. mustReturnInteger := FALSE;
  6596. mustHaveEquitypedOperands := FALSE;
  6597. (* operator-specific exceptions *)
  6598. CASE identifierNumber OF
  6599. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6600. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6601. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6602. mustBeBinary := TRUE
  6603. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6604. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6605. | Scanner.Times: mustBeBinary := TRUE
  6606. | Scanner.TimesTimes: mustBeBinary := TRUE
  6607. | Scanner.DotTimes: mustBeBinary := TRUE
  6608. | Scanner.PlusTimes: mustBeBinary := TRUE
  6609. | Scanner.Slash: mustBeBinary := TRUE
  6610. | Scanner.Backslash: mustBeBinary := TRUE
  6611. | Scanner.DotSlash: mustBeBinary := TRUE
  6612. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6613. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6614. | Scanner.Not: mustBeUnary := TRUE
  6615. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6616. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6617. | Scanner.Transpose: mustBeUnary := TRUE;
  6618. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6619. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6620. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6621. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6622. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6623. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6624. | Global.Abs: mustBeUnary := TRUE;
  6625. | Global.Ash: (* TODO: arity? *)
  6626. | Global.Cap: (* TODO: arity? *)
  6627. | Global.Chr: mustBeUnary := TRUE;
  6628. | Global.Entier: (* TODO: arity? *)
  6629. | Global.EntierH: (* TODO: arity? *)
  6630. | Global.Len: (* unary and binary *)
  6631. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6632. | Global.Max, Global.Min: (* unary and binary *)
  6633. | Global.Odd: (* TODO: arity? *)
  6634. | Global.Sum: (* TODO: arity? *)
  6635. | Global.All: (* TODO: arity? *)
  6636. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6637. | Scanner.Alias:
  6638. | Scanner.GreaterGreater, Scanner.LessLess:
  6639. mustBeBinary := TRUE; hasReturnType := FALSE;
  6640. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6641. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6642. END;
  6643. (* check parameter count *)
  6644. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6645. Error(operator.position,"operator is not unary")
  6646. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6647. Error(operator.position,"operator is not binary")
  6648. END;
  6649. (* check parameter types *)
  6650. (* TODO: is this used at all? *)
  6651. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6652. leftType := procedureType.firstParameter.type;
  6653. rightType := procedureType.firstParameter.nextParameter.type;
  6654. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6655. Error(operator.position, "the two operands are not of the same type")
  6656. END
  6657. END;
  6658. (* check return type *)
  6659. IF hasReturnType THEN
  6660. IF procedureType.returnType = NIL THEN
  6661. Error(operator.position, "return type required")
  6662. ELSIF mustReturnBoolean THEN
  6663. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6664. Error(operator.position,"return type is not Boolean")
  6665. END
  6666. ELSIF mustReturnInteger THEN
  6667. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6668. Error(operator.position,"return type is not integer")
  6669. END
  6670. END
  6671. ELSIF procedureType.returnType # NIL THEN
  6672. Error(operator.position, "return type not allowed")
  6673. END
  6674. END
  6675. END
  6676. END
  6677. END VisitOperator;
  6678. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6679. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6680. BEGIN
  6681. IF error THEN RETURN FALSE END;
  6682. prevScope := currentScope;
  6683. prevDiagnostics := diagnostics;
  6684. diagnostics := NIL; (* suppress error output *)
  6685. currentScope := module.moduleScope;
  6686. VisitImport(x);
  6687. IF ~error THEN
  6688. module.moduleScope.AddImport(x);
  6689. x.SetScope(module.moduleScope);
  6690. END;
  6691. currentScope := prevScope;
  6692. diagnostics := prevDiagnostics;
  6693. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6694. END AddImport;
  6695. (** check and resolve import
  6696. - check for name = SYSTEM
  6697. - check for forbidden self import
  6698. - search through global import cache: already imported?
  6699. - check if already imported indirectly
  6700. - import if necessary -> set module and enter into import cache
  6701. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6702. - after this import this direct import and all indirect imports are stored in the current module's import list
  6703. **)
  6704. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6705. VAR
  6706. module: SyntaxTree.Module;
  6707. moduleScope: SyntaxTree.ModuleScope;
  6708. import,reimport: SyntaxTree.Import;
  6709. filename: FileName;
  6710. prevScope: SyntaxTree.Scope;
  6711. BEGIN
  6712. IF SymbolNeedsResolution(x) THEN
  6713. prevScope := currentScope;
  6714. x.SetType(SyntaxTree.importType);
  6715. moduleScope := currentScope.ownerModule.moduleScope;
  6716. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6717. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6718. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6719. Error(x.position,"forbidden self import");
  6720. ELSE
  6721. (* search through global import list: already imported ? *)
  6722. IF (x.module = NIL) & (importCache # NIL) THEN
  6723. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6724. ELSE import := NIL
  6725. END;
  6726. IF x.module # NIL THEN (* already imported indirectly *)
  6727. module := x.module;
  6728. ELSIF import # NIL THEN (* already in module list *)
  6729. module := import.module;
  6730. ASSERT(module # NIL);
  6731. x.SetModule(module);
  6732. ELSE (* must be imported *)
  6733. Global.ModuleFileName(x.moduleName,x.context,filename);
  6734. IF symbolFileFormat # NIL THEN
  6735. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6736. IF module = NIL THEN
  6737. ErrorSS(x.position,"could not import",filename);
  6738. IF VerboseErrorMessage THEN
  6739. Printout.Info("import",x)
  6740. END
  6741. ELSE
  6742. (*
  6743. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6744. (*! should rather be done by importer *)
  6745. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6746. checker.importCache := importCache;
  6747. checker.arrayBaseImported := arrayBaseImported;
  6748. checker.global := global;
  6749. checker.Module(module); (* semantic check *)
  6750. error := error OR checker.error;
  6751. END;
  6752. *)
  6753. (*
  6754. ASSERT(SyntaxTree.Resolved IN module.state);
  6755. *)
  6756. x.SetModule(module);
  6757. IF importCache # NIL THEN
  6758. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6759. import.SetContext(x.context);
  6760. import.SetModule(module);
  6761. importCache.AddImport(import);
  6762. END;
  6763. END;
  6764. ELSE
  6765. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6766. END;
  6767. END;
  6768. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6769. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6770. END;
  6771. import := module.moduleScope.firstImport;
  6772. WHILE(import # NIL) DO
  6773. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6774. ASSERT(currentScope # NIL);
  6775. ASSERT(currentScope.ownerModule # NIL);
  6776. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6777. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6778. Error(x.position,"recursive import");
  6779. ELSE
  6780. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6781. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6782. IF reimport = NIL THEN (* indirect import *)
  6783. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6784. reimport.SetContext(import.context);
  6785. reimport.SetModule(import.module);
  6786. moduleScope.AddImport(reimport);
  6787. reimport.SetScope(moduleScope);
  6788. ELSE
  6789. ASSERT(import.module # NIL);
  6790. reimport.SetModule(import.module); (* direct or indirect import *)
  6791. END;
  6792. END;
  6793. import := import.nextImport;
  6794. END;
  6795. END;
  6796. END;
  6797. currentScope := prevScope;
  6798. (* ELSE nothing to be done *)
  6799. x.SetState(SyntaxTree.Resolved);
  6800. END;
  6801. END VisitImport;
  6802. (*** statements ***)
  6803. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6804. VAR prev,resolved: SyntaxTree.Statement;
  6805. BEGIN
  6806. prev := resolvedStatement;
  6807. resolvedStatement := x;
  6808. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6809. activeCellsStatement := FALSE;
  6810. x.Accept(SELF);
  6811. (* removed this, implementation restriction should be resolved by backend
  6812. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6813. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6814. END;
  6815. *)
  6816. resolved := resolvedStatement;
  6817. resolvedStatement := prev;
  6818. RETURN resolved
  6819. END ResolveStatement;
  6820. (** check and resolve statement sequence
  6821. - check all statements, replace if necessary
  6822. **)
  6823. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6824. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6825. BEGIN
  6826. IF statementSequence # NIL THEN (* else empty *)
  6827. FOR i := 0 TO statementSequence.Length()-1 DO
  6828. statement := statementSequence.GetStatement(i);
  6829. resolved := ResolveStatement(statement);
  6830. IF (resolved # statement) THEN
  6831. statementSequence.SetStatement(i,resolved);
  6832. END;
  6833. END;
  6834. END;
  6835. END StatementSequence;
  6836. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6837. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6838. - check if procedure is callable
  6839. - check return type = NIL (otherwise must be assignment statement)
  6840. **)
  6841. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6842. VAR call: SyntaxTree.Designator;
  6843. BEGIN
  6844. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6845. call := procedureCall.call;
  6846. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6847. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6848. END;
  6849. call := ResolveDesignator(call);
  6850. IF call = SyntaxTree.invalidDesignator THEN
  6851. (* error already handled *)
  6852. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6853. (* inline call in a statement *)
  6854. ELSIF ~IsCallable(call) THEN
  6855. Error(procedureCall.position,"called object is not a procedure");
  6856. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6857. Error(procedureCall.position,"calling procedure with non-void return type");
  6858. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6859. END;
  6860. procedureCall.SetCall(call);
  6861. (*
  6862. IF call = SyntaxTree.invalidDesignator THEN
  6863. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6864. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6865. IF IsOberonInline(procedure) THEN
  6866. Warning(procedure.position,"call to inline proc");
  6867. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6868. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6869. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6870. resolvedStatement := block;
  6871. RETURN;
  6872. END;
  6873. END;
  6874. *)
  6875. END VisitProcedureCallStatement;
  6876. (** check and resolve assignment LHS := RHS
  6877. - resolve LHS and RHS
  6878. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6879. - check if assignment is compatible
  6880. - check if LHS is variable (i.e. assignable)
  6881. - convert RHS if necessary
  6882. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6883. - assignment between different ASOTs
  6884. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6885. - assignment to ASOT elements:
  6886. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6887. **)
  6888. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6889. VAR
  6890. left: SyntaxTree.Designator;
  6891. right, expression: SyntaxTree.Expression;
  6892. designator: SyntaxTree.Designator;
  6893. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6894. mathArrayType: SyntaxTree.MathArrayType;
  6895. BEGIN
  6896. right := ResolveExpression(assignment.right);
  6897. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6898. left := ResolveDesignator(assignment.left);
  6899. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6900. (* error already handled *)
  6901. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6902. (* LHS is index write operator call on ASOT *)
  6903. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6904. (* necessary ?
  6905. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6906. type := procedureType.firstParameter.type;
  6907. expression := procedureCallDesignator.parameters.GetExpression(0);
  6908. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6909. *)
  6910. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6911. ELSIF CheckVariable(left) THEN
  6912. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6913. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6914. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6915. (* conversion done by procedure call
  6916. (* try to convert to left argument *)
  6917. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6918. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6919. procedureCallDesignator.parameters.SetExpression(1, right);
  6920. END;
  6921. *)
  6922. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6923. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6924. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6925. ELSIF AssignmentCompatible(left, right) THEN
  6926. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6927. mathArrayType := MathArrayStructureOfType(left.type);
  6928. right := NewConversion(right.position, right, mathArrayType, NIL);
  6929. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6930. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6931. ELSE
  6932. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6933. assignment.SetLeft(left);
  6934. assignment.SetRight(right);
  6935. resolvedStatement := assignment
  6936. END
  6937. END
  6938. END
  6939. END VisitAssignment;
  6940. (** check and resolve assignment LHS := RHS
  6941. - resolve LHS and RHS
  6942. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6943. - check if assignment is compatible
  6944. - check if LHS is variable (i.e. assignable)
  6945. - convert RHS if necessary
  6946. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6947. - assignment between different ASOTs
  6948. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6949. - assignment to ASOT elements:
  6950. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6951. **)
  6952. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6953. VAR
  6954. left: SyntaxTree.Designator;
  6955. right: SyntaxTree.Expression;
  6956. inPort, outPort: SyntaxTree.PortType;
  6957. expression: SyntaxTree.Expression;
  6958. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6959. BEGIN
  6960. right := ResolveExpression(communication.right);
  6961. left := ResolveDesignator(communication.left);
  6962. communication.SetLeft(left);
  6963. communication.SetRight(right);
  6964. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6965. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6966. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6967. (* conversion done by procedure call
  6968. (* try to convert to left argument *)
  6969. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6970. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6971. procedureCallDesignator.parameters.SetExpression(1, right);
  6972. END;
  6973. *)
  6974. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6975. ELSE
  6976. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6977. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6978. (* error already handled *)
  6979. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6980. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6981. IF outPort.direction # SyntaxTree.OutPort THEN
  6982. Error(left.position,"not an out-port")
  6983. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6984. Error(left.position,"incompatible to port type");
  6985. ELSE
  6986. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6987. communication.SetRight(right)
  6988. END;
  6989. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6990. IF CheckVariable(left) THEN
  6991. IF inPort.direction # SyntaxTree.InPort THEN
  6992. Error(left.position,"not an in-port")
  6993. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6994. Error(right.position,"incompatible to port type");
  6995. END;
  6996. END;
  6997. ELSE
  6998. Error(communication.position,"unsupported stream operation");
  6999. END;
  7000. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7001. IF outPort.direction # SyntaxTree.OutPort THEN
  7002. Error(left.position,"not an out-port")
  7003. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7004. Error(left.position,"incompatible to port type");
  7005. ELSE
  7006. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7007. communication.SetRight(right)
  7008. END;
  7009. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7010. IF CheckVariable(right) THEN
  7011. IF inPort.direction # SyntaxTree.InPort THEN
  7012. Error(left.position,"not an in-port")
  7013. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7014. Error(right.position,"incompatible to port type");
  7015. END;
  7016. END;
  7017. ELSE
  7018. Error(communication.position, "unsupported operation");
  7019. END;
  7020. END;
  7021. END VisitCommunicationStatement;
  7022. (** check and resolve if/eslif part
  7023. - check condition
  7024. - check statement sequence
  7025. **)
  7026. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7027. VAR prevUnreachable, b: BOOLEAN;
  7028. BEGIN
  7029. prevUnreachable := currentIsUnreachable;
  7030. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7031. IF IsBooleanValue(ifPart.condition,b) THEN
  7032. IF b=FALSE THEN
  7033. currentIsUnreachable := TRUE
  7034. ELSIF b=TRUE THEN
  7035. true := TRUE
  7036. END;
  7037. END;
  7038. StatementSequence(ifPart.statements);
  7039. currentIsUnreachable := prevUnreachable;
  7040. END IfPart;
  7041. (** check and resolve if statement
  7042. - check if parts and else part statement sequence
  7043. **)
  7044. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7045. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7046. BEGIN
  7047. prevUnreachable := currentIsUnreachable;
  7048. ifPartTrue := FALSE;
  7049. IfPart(ifStatement.ifPart,ifPartTrue);
  7050. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7051. elsif := ifStatement.GetElsifPart(i);
  7052. IfPart(elsif,ifPartTrue);
  7053. END;
  7054. IF ifStatement.elsePart # NIL THEN
  7055. IF ifPartTrue THEN
  7056. currentIsUnreachable := TRUE
  7057. END;
  7058. StatementSequence(ifStatement.elsePart)
  7059. END;
  7060. currentIsUnreachable := prevUnreachable;
  7061. END VisitIfStatement;
  7062. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7063. VAR variable: SyntaxTree.Designator;
  7064. type,variableType: SyntaxTree.Type;
  7065. withEntry: WithEntry;
  7066. BEGIN
  7067. variable := ResolveDesignator(withPart.variable);
  7068. variableType := variable.type.resolved;
  7069. withPart.SetVariable(variable);
  7070. type := ResolveType(withPart.type);
  7071. withPart.SetType(type);
  7072. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7073. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7074. END;
  7075. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7076. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7077. Error(variable.position,"is not extensible designator");
  7078. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7079. Error(variable.position,"is no local variable ");
  7080. IF VerboseErrorMessage THEN
  7081. Printout.Info("variable",variable)
  7082. END;
  7083. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7084. Error(variable.position,"withguarded symbol is no type extension of ");
  7085. IF VerboseErrorMessage THEN
  7086. Printout.Info("variable",variable);
  7087. Printout.Info("type",type);
  7088. END;
  7089. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7090. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7091. Error(variable.position,"withguarded symbol is no variable ");
  7092. IF VerboseErrorMessage THEN
  7093. Printout.Info("variable",variable);
  7094. Printout.Info("type",type);
  7095. END;
  7096. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7097. Error(variable.position,"invalid change of withguarded symbol");
  7098. ELSE
  7099. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7100. NEW(withEntry);
  7101. withEntry.previous := withEntries;
  7102. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7103. withEntry.type := type;
  7104. withEntries := withEntry;
  7105. StatementSequence(withPart.statements);
  7106. withEntries := withEntries.previous;
  7107. END;
  7108. END WithPart;
  7109. (** check and resolve with statement WITH variable: type DO ... END;
  7110. - check type and variable
  7111. - check that variable type is type extension of type
  7112. - check that variable is a variable
  7113. - enter new with scope and enter guardedVariable with same name and reference to variable
  7114. - create if statement:
  7115. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7116. **)
  7117. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7118. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7119. BEGIN
  7120. prevScope := currentScope; symbol := NIL;
  7121. FOR i := 0 TO withStatement.WithParts()-1 DO
  7122. WithPart(withStatement.GetWithPart(i),symbol);
  7123. END;
  7124. IF withStatement.elsePart # NIL THEN
  7125. StatementSequence(withStatement.elsePart)
  7126. END;
  7127. currentScope := prevScope;
  7128. END VisitWithStatement;
  7129. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7130. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7131. - check 'first' < 'last' and no overlaps between different case labels
  7132. - check statement sequence
  7133. **)
  7134. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7135. VAR
  7136. i: LONGINT;
  7137. position: Position;
  7138. expression, left, right: SyntaxTree.Expression;
  7139. expressionType: SyntaxTree.Type;
  7140. l, r: LONGINT;
  7141. cl, cr: CHAR;
  7142. thiscases: SyntaxTree.CaseConstant;
  7143. BEGIN
  7144. thiscases := NIL;
  7145. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7146. expression := casePart.elements.GetExpression(i);
  7147. position := expression.position;
  7148. (* set context of range *)
  7149. IF expression IS SyntaxTree.RangeExpression THEN
  7150. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7151. END;
  7152. expression := ResolveExpression(expression);
  7153. IF expression = SyntaxTree.invalidExpression THEN
  7154. (* error already reported *)
  7155. expressionType := SyntaxTree.invalidType;
  7156. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7157. (* read out 'first' and 'last' *)
  7158. left := expression(SyntaxTree.RangeExpression).first;
  7159. right := expression(SyntaxTree.RangeExpression).last;
  7160. (* guaranteed by VisitRangeExpression: *)
  7161. ASSERT((left # NIL) & (right # NIL));
  7162. ASSERT(left.type.resolved = right.type.resolved);
  7163. left := CompatibleConversion(left.position, left, type);
  7164. right := CompatibleConversion(right.position, right, type);
  7165. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7166. expression(SyntaxTree.RangeExpression).SetLast(right);
  7167. expressionType := RegularType(position,left.type);
  7168. ELSE
  7169. expression := ConstantExpression(expression);
  7170. expression := CompatibleConversion(expression.position, expression, type);
  7171. (*
  7172. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7173. left := Global.NewCharacterValue(system,expression.position,cl);
  7174. expression := casePart.elements.GetExpression(i);
  7175. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7176. expression := left
  7177. END;
  7178. *)
  7179. casePart.elements.SetExpression(i,expression);
  7180. left := expression; right := expression;
  7181. expressionType := RegularType(position,expression.type)
  7182. END;
  7183. IF (expressionType = SyntaxTree.invalidType) THEN
  7184. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7185. Error(position, "inadmissible case label");
  7186. expression := SyntaxTree.invalidExpression;
  7187. ELSE
  7188. l := 0; r := 0;
  7189. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7190. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7191. l := ORD(cl); r := ORD(cr);
  7192. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7193. ELSE
  7194. expression := SyntaxTree.invalidExpression
  7195. END;
  7196. IF expression # SyntaxTree.invalidExpression THEN
  7197. IF l>r THEN
  7198. Error(position, "empty case label")
  7199. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7200. Error(position, "duplicate case label");
  7201. ELSE
  7202. IF l < min THEN min := l END;
  7203. IF r > max THEN max := r END;
  7204. END;
  7205. END;
  7206. END;
  7207. casePart.elements.SetExpression(i,expression);
  7208. END;
  7209. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7210. casePart.SetConstants(thiscases);
  7211. StatementSequence(casePart.statements);
  7212. END CasePart;
  7213. (** check and resolve case statement CASE variable OF ... END;
  7214. - check variable
  7215. - check case parts
  7216. **)
  7217. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7218. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7219. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7220. BEGIN
  7221. expression := ResolveExpression(caseStatement.variable);
  7222. type := RegularType(expression.position,expression.type);
  7223. IF type = SyntaxTree.invalidType THEN
  7224. expression := SyntaxTree.invalidExpression;
  7225. ELSIF IsIntegerType(type) THEN
  7226. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7227. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7228. (*
  7229. expression := Global.NewCharacterValue(system,expression.position,ch);
  7230. *)
  7231. type := expression.type;
  7232. ELSIF IsCharacterType(type) THEN
  7233. ELSIF IsEnumerationType(type) THEN
  7234. ELSE
  7235. Error(caseStatement.variable.position,"variable must be integer or character type");
  7236. expression := SyntaxTree.invalidExpression;
  7237. END;
  7238. caseStatement.SetVariable(expression);
  7239. caseList := NIL;
  7240. min := MAX(LONGINT); max := MIN(LONGINT);
  7241. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7242. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7243. END;
  7244. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7245. msg := "huge sparse case table ";
  7246. Strings.AppendInt(msg, max-min);
  7247. Strings.Append(msg,"/");
  7248. Strings.AppendInt(msg, caseStatement.CaseParts());
  7249. Warning(caseStatement.position,msg);
  7250. END;
  7251. caseStatement.SetMinMax(min,max);
  7252. StatementSequence(caseStatement.elsePart);
  7253. IF expression.resolved # NIL THEN
  7254. IF IsCharacterValue(expression,ch) THEN
  7255. l := ORD(ch)
  7256. ELSIF IsIntegerValue(expression,l) THEN
  7257. END;
  7258. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7259. END;
  7260. END VisitCaseStatement;
  7261. (** check and resolve while statement
  7262. - check condition
  7263. - check statement sequence
  7264. **)
  7265. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7266. VAR prevIsUnreachable,b: BOOLEAN;
  7267. BEGIN
  7268. prevIsUnreachable := currentIsUnreachable;
  7269. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7270. IF IsBooleanValue(whileStatement.condition,b) THEN
  7271. IF b=FALSE THEN
  7272. currentIsUnreachable := TRUE
  7273. END;
  7274. END;
  7275. StatementSequence(whileStatement.statements);
  7276. currentIsUnreachable := prevIsUnreachable
  7277. END VisitWhileStatement;
  7278. (** check and resolve repeat statement
  7279. - check condition
  7280. - check statement sequence
  7281. **)
  7282. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7283. BEGIN
  7284. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7285. StatementSequence(repeatStatement.statements);
  7286. END VisitRepeatStatement;
  7287. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7288. VAR withEntry: WithEntry;
  7289. BEGIN
  7290. withEntry := withEntries;
  7291. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7292. withEntry := withEntry.previous
  7293. END;
  7294. IF withEntry = NIL THEN RETURN FALSE
  7295. ELSE
  7296. type := withEntry.type;
  7297. RETURN TRUE
  7298. END;
  7299. END GetGuard;
  7300. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7301. - check that variable is an integer variable
  7302. - check that from is integer typed with compatible type
  7303. - check that to has compatible type
  7304. - check that by is constant integer with compatible type
  7305. **)
  7306. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7307. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7308. BEGIN
  7309. designator := ResolveDesignator(forStatement.variable);
  7310. type := SyntaxTree.invalidType;
  7311. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7312. designator := SyntaxTree.invalidDesignator;
  7313. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7314. Error(designator.position,"control variable of non-integer type");
  7315. designator := SyntaxTree.invalidDesignator;
  7316. ELSIF CheckVariable(designator) THEN
  7317. type := designator.type;
  7318. END;
  7319. forStatement.SetVariable(designator);
  7320. expression := ResolveExpression(forStatement.from);
  7321. IF expression = SyntaxTree.invalidExpression THEN
  7322. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7323. Error(expression.position,"start value of incompatible type");
  7324. expression := SyntaxTree.invalidExpression;
  7325. ELSIF type # SyntaxTree.invalidType THEN
  7326. expression := NewConversion(expression.position,expression,type,NIL)
  7327. END;
  7328. forStatement.SetFrom(expression);
  7329. expression := ResolveExpression(forStatement.to);
  7330. IF expression = SyntaxTree.invalidExpression THEN
  7331. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7332. Error(expression.position,"end value of incompatible type");
  7333. expression := SyntaxTree.invalidExpression;
  7334. ELSIF type # SyntaxTree.invalidType THEN
  7335. expression := NewConversion(expression.position,expression,type,NIL)
  7336. END;
  7337. forStatement.SetTo(expression);
  7338. IF forStatement.by # NIL THEN
  7339. expression := ConstantInteger(forStatement.by);
  7340. ELSE
  7341. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7342. END;
  7343. IF expression = SyntaxTree.invalidExpression THEN
  7344. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7345. Error(expression.position,"step value of incompatible type");
  7346. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7347. Error(expression.position,"invalid step value");
  7348. ELSIF type # SyntaxTree.invalidType THEN
  7349. expression := NewConversion(expression.position,expression,type,NIL)
  7350. END;
  7351. forStatement.SetBy(expression);
  7352. StatementSequence(forStatement.statements);
  7353. END VisitForStatement;
  7354. (** check and resolve loop statement LOOP StatementSequence END
  7355. - check statement sequence
  7356. **)
  7357. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7358. BEGIN
  7359. StatementSequence(loopStatement.statements)
  7360. END VisitLoopStatement;
  7361. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7362. BEGIN
  7363. StatementSequence(exitableBlock.statements);
  7364. END VisitExitableBlock;
  7365. (** check and resolve exit statement EXIT
  7366. - check that exit is within LOOP statement block
  7367. **)
  7368. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7369. VAR outer: SyntaxTree.Statement;
  7370. BEGIN
  7371. outer := exitStatement.outer;
  7372. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7373. outer := outer.outer;
  7374. END;
  7375. IF outer = NIL THEN
  7376. Error(exitStatement.position,"exit statement not within loop statement");
  7377. END;
  7378. END VisitExitStatement;
  7379. (** check and resolve return statement RETURN [expression]
  7380. - check expression (if any)
  7381. - check if in procedure scope
  7382. - if in procedure scope then check expression compatibility
  7383. - if not in procecdure scope then check on return without expression
  7384. **)
  7385. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7386. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7387. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7388. BEGIN
  7389. position := returnStatement.position;
  7390. expression := returnStatement.returnValue;
  7391. IF expression # NIL THEN
  7392. expression := ResolveExpression(expression);
  7393. returnStatement.SetReturnValue(expression);
  7394. END;
  7395. outer := returnStatement.outer;
  7396. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7397. outer := outer.outer
  7398. END;
  7399. IF (outer # NIL) THEN
  7400. scope := outer(SyntaxTree.Body).inScope;
  7401. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7402. IF (expression # NIL) THEN
  7403. Error(position, "return statement with parameter not in procedure scope");
  7404. END;
  7405. ELSE
  7406. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7407. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7408. Error(position, "return statement in procedure that does not return");
  7409. END;
  7410. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7411. IF returnType # NIL THEN
  7412. returnType := returnType.resolved;
  7413. IF expression = NIL THEN
  7414. Error(position, "empty return type in procedure providing a return type")
  7415. ELSIF expression.type = NIL THEN
  7416. Error(position,"returned type incompatible: expression has no type");
  7417. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7418. Error(position, "return type not compatible");
  7419. IF VerboseErrorMessage THEN
  7420. Printout.Info("returnType",returnType);
  7421. Printout.Info("expression",expression);
  7422. END;
  7423. ELSE
  7424. expression := NewConversion(expression.position,expression,returnType,NIL);
  7425. returnStatement.SetReturnValue(expression);
  7426. END;
  7427. ELSIF expression # NIL THEN
  7428. Error(position, "non-empty return type in procedure providing no return type");
  7429. END;
  7430. END;
  7431. END;
  7432. END VisitReturnStatement;
  7433. (** check and resolve await statement AWAIT(condition: Expression)
  7434. - check await condition
  7435. **)
  7436. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7437. VAR condition: SyntaxTree.Expression;
  7438. BEGIN
  7439. condition := ResolveCondition(awaitStatement.condition);
  7440. IF currentIsRealtime THEN
  7441. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7442. END;
  7443. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7444. Error(awaitStatement.position,"senseless await statement with constant condition");
  7445. END;
  7446. awaitStatement.SetCondition(condition);
  7447. END VisitAwaitStatement;
  7448. PROCEDURE CheckSystemImport(position: Position);
  7449. VAR import: SyntaxTree.Import;
  7450. BEGIN
  7451. import := currentScope.ownerModule.moduleScope.firstImport;
  7452. WHILE(import # NIL) DO
  7453. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7454. RETURN;
  7455. END;
  7456. import := import.nextImport;
  7457. END;
  7458. Error(position, "forbidden code without system import");
  7459. END CheckSystemImport;
  7460. (** check and resolve code statement: do nothing, must be done by assembler
  7461. **)
  7462. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7463. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7464. BEGIN
  7465. CheckSystemImport(code.position);
  7466. FOR i := 0 TO code.inRules.Length()-1 DO
  7467. statement := code.inRules.GetStatement(i);
  7468. IF statement IS SyntaxTree.Assignment THEN
  7469. WITH statement: SyntaxTree.Assignment DO
  7470. statement.SetRight(ResolveExpression(statement.right));
  7471. END;
  7472. ELSE
  7473. Error(statement.position, "can only be assignment")
  7474. END;
  7475. END;
  7476. FOR i := 0 TO code.outRules.Length()-1 DO
  7477. statement := code.outRules.GetStatement(i);
  7478. IF statement IS SyntaxTree.Assignment THEN
  7479. WITH statement: SyntaxTree.Assignment DO
  7480. statement.SetLeft(ResolveDesignator(statement.left));
  7481. END;
  7482. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7483. (* must be a reference to some register *)
  7484. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7485. ELSE
  7486. Printout.Info("out statement ", statement);
  7487. Error(statement.position, "(out) can only be assignment")
  7488. END;
  7489. END;
  7490. END VisitCode;
  7491. (** check and set flags of a statement block
  7492. - check for multiply occurence of a flag
  7493. - check and set priority only in bodies
  7494. - check for valid names
  7495. **)
  7496. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7497. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7498. flag: LONGINT; recordBody: SyntaxTree.Body;
  7499. PROCEDURE SetProtectedRecord;
  7500. VAR scope: SyntaxTree.Scope;
  7501. BEGIN
  7502. scope := currentScope;
  7503. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7504. scope := scope.outerScope
  7505. END;
  7506. IF scope # NIL THEN
  7507. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7508. END;
  7509. END SetProtectedRecord;
  7510. BEGIN
  7511. flags := {};
  7512. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7513. recordBody := block(SyntaxTree.Body)
  7514. ELSE
  7515. recordBody := NIL
  7516. END;
  7517. blockModifier := block.blockModifiers;
  7518. WHILE(blockModifier # NIL) DO
  7519. name := blockModifier.identifier;
  7520. expression := blockModifier.expression;
  7521. position := blockModifier.position;
  7522. flag := -1;
  7523. IF name=Global.NamePriority THEN
  7524. IF expression = NIL THEN
  7525. Error(position, "missing priority expression");
  7526. ELSIF recordBody = NIL THEN
  7527. Error(position, "priority not on record body");
  7528. ELSIF recordBody.priority # NIL THEN
  7529. Error(position, "duplicate priority expression");
  7530. ELSE
  7531. recordBody.SetPriority(expression);
  7532. END;
  7533. ELSIF expression # NIL THEN
  7534. Error(expression.position,"expression not in connection with priority")
  7535. ELSIF name=Global.NameExclusive THEN
  7536. IF block.isExclusive THEN
  7537. Error(position, "duplicate exclusive flag")
  7538. END;
  7539. block.SetExclusive(TRUE); SetProtectedRecord;
  7540. ELSIF name=Global.NameActive THEN
  7541. IF recordBody = NIL THEN
  7542. Error(position, "active not in record body");
  7543. ELSIF recordBody.isActive THEN
  7544. Error(position, "duplicate active flag")
  7545. ELSE
  7546. recordBody.SetActive(TRUE); SetProtectedRecord;
  7547. END;
  7548. ELSIF name=Global.NameSafe THEN
  7549. IF recordBody = NIL THEN
  7550. Error(position, "safe not in record body");
  7551. ELSIF recordBody.isSafe THEN
  7552. Error(position, "duplicate safe flag")
  7553. ELSE
  7554. recordBody.SetSafe(TRUE);
  7555. SetProtectedRecord;
  7556. END;
  7557. ELSIF name=Global.NameRealtime THEN
  7558. IF recordBody = NIL THEN
  7559. Error(position, "realtime not in record body");
  7560. ELSIF recordBody.isRealtime THEN
  7561. Error(position, "duplicate realtime flag")
  7562. ELSE
  7563. recordBody.SetRealtime(TRUE);
  7564. block.SetRealtime(TRUE);
  7565. END;
  7566. ELSIF name=Global.NameUnchecked THEN
  7567. IF block.isUnchecked THEN
  7568. Error(position, "duplicate unchecked flag")
  7569. ELSE
  7570. block.SetUnchecked(TRUE);
  7571. END;
  7572. ELSIF (name=Global.NameUncooperative) THEN
  7573. IF block.isUncooperative THEN
  7574. Error(position, "duplicate uncooperative flag")
  7575. ELSE
  7576. block.SetUncooperative(TRUE);
  7577. END;
  7578. ELSE
  7579. Error(position, "unknown block modifier");
  7580. END;
  7581. blockModifier := blockModifier.nextModifier;
  7582. END;
  7583. END BlockFlags;
  7584. (** check and resolve statement block
  7585. - check flags (exclusive)
  7586. - check statement sequence
  7587. **)
  7588. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7589. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7590. BEGIN
  7591. BlockFlags(statementBlock);
  7592. IF statementBlock.isExclusive THEN
  7593. (* check that not in exclusive block *)
  7594. IF currentIsExclusive THEN
  7595. Error (statementBlock.position,"forbidden recursive exclusive")
  7596. ELSIF currentIsRealtime THEN
  7597. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7598. END;
  7599. END;
  7600. recentExclusive := currentIsExclusive;
  7601. recentUnreachable := currentIsUnreachable;
  7602. recentRealtime := currentIsRealtime;
  7603. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7604. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7605. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7606. StatementSequence(statementBlock.statements);
  7607. currentIsRealtime := recentRealtime;
  7608. currentIsExclusive := recentExclusive;
  7609. currentIsUnreachable := recentUnreachable;
  7610. END VisitStatementBlock;
  7611. (** check and resolve body
  7612. - check flags (active, priority, safe)
  7613. - check body and finally part
  7614. **)
  7615. PROCEDURE Body(body: SyntaxTree.Body);
  7616. BEGIN
  7617. VisitStatementBlock(body);
  7618. IF body.isActive THEN
  7619. IF ~currentIsBodyProcedure THEN
  7620. Error(body.position,"active flag not in object body");
  7621. ELSIF body.priority # NIL THEN
  7622. body.SetPriority(ConstantInteger(body.priority));
  7623. END;
  7624. ELSIF body.isSafe THEN
  7625. Error(body.position,"safe flag not in active body");
  7626. ELSIF body.priority # NIL THEN
  7627. Error(body.position,"priority flag not in active body");
  7628. END;
  7629. IF body.code # NIL THEN
  7630. CheckSystemImport(body.position);
  7631. END;
  7632. StatementSequence(body.finally)
  7633. END Body;
  7634. (*** scopes ***)
  7635. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7636. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7637. VAR duplicateSymbol: BOOLEAN;
  7638. BEGIN
  7639. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7640. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7641. Error(symbol.position,"globally defined keyword")
  7642. END;
  7643. scope.EnterSymbol(symbol,duplicateSymbol);
  7644. IF ~allowDuplicate & duplicateSymbol THEN
  7645. Error(symbol.position,"Multiply defined identifier.");
  7646. IF VerboseErrorMessage THEN
  7647. Printout.Info("multiply defined identifier",symbol);
  7648. Printout.Info("in scope",scope);
  7649. END;
  7650. END;
  7651. END Register;
  7652. (**
  7653. implementation: check and resolve an implementation part
  7654. **)
  7655. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7656. move implementation checker to a separate object ? *)
  7657. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7658. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7659. BEGIN
  7660. prevIsRealtime := currentIsRealtime;
  7661. prevIsBodyProcedure := currentIsBodyProcedure;
  7662. prevIsCellNet := currentIsCellNet;
  7663. prevScope := currentScope;
  7664. currentScope := scope;
  7665. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7666. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7667. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7668. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7669. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7670. (*
  7671. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7672. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7673. END;
  7674. *)
  7675. END;
  7676. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7677. (* module body, record bodies are wrapped into an artifical procedure *)
  7678. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7679. Body(scope(SyntaxTree.ProcedureScope).body)
  7680. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7681. Body(scope(SyntaxTree.ProcedureScope).body)
  7682. END;
  7683. END;
  7684. currentScope := prevScope;
  7685. currentIsRealtime := prevIsRealtime;
  7686. currentIsBodyProcedure := prevIsBodyProcedure;
  7687. currentIsCellNet := prevIsCellNet;
  7688. END Implementation;
  7689. (** implementation phase:
  7690. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7691. **)
  7692. PROCEDURE Implementations(x: SyntaxTree.Module);
  7693. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7694. BEGIN
  7695. prevPhase := phase;
  7696. phase := InlinePhase;
  7697. scope := x.firstScope;
  7698. WHILE(scope # NIL) DO
  7699. Implementation(scope);
  7700. scope := scope.nextScope;
  7701. END;
  7702. phase := ImplementationPhase;
  7703. scope := x.firstScope;
  7704. WHILE(scope # NIL) DO
  7705. Implementation(scope);
  7706. scope := scope.nextScope;
  7707. END;
  7708. phase := prevPhase;
  7709. END Implementations;
  7710. (** declaration phase:
  7711. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7712. - import lists (for module scopes)
  7713. - parameter list (for procedure scopes)
  7714. - constant declarations
  7715. - type declarations
  7716. - variable declarations
  7717. - procedure declarations
  7718. preformed in two stages:
  7719. - first all symbols are entered into the symbol table (with uniqueness check),
  7720. - then all symbols are resolved
  7721. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7722. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7723. phases :
  7724. 0 = before procedures
  7725. 1 = procedures and later
  7726. **)
  7727. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7728. VAR
  7729. constant: SyntaxTree.Constant;
  7730. typeDeclaration: SyntaxTree.TypeDeclaration;
  7731. variable: SyntaxTree.Variable;
  7732. procedure: SyntaxTree.Procedure;
  7733. procedureType : SyntaxTree.ProcedureType;
  7734. prevScope: SyntaxTree.Scope;
  7735. parameter: SyntaxTree.Parameter;
  7736. import: SyntaxTree.Import;
  7737. symbol: SyntaxTree.Symbol;
  7738. prevPhase: LONGINT;
  7739. prevError : BOOLEAN;
  7740. i: LONGINT;
  7741. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7742. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7743. BEGIN
  7744. IF type.baseType # NIL THEN
  7745. baseType := type.baseType.resolved;
  7746. IF baseType IS SyntaxTree.PointerType THEN
  7747. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7748. END;
  7749. (*
  7750. IF baseType IS SyntaxTree.CellType THEN
  7751. DeclareCell(baseType(SyntaxTree.CellType));
  7752. END;
  7753. *)
  7754. END;
  7755. parameter := type.firstParameter;
  7756. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7757. (*
  7758. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7759. variable.SetType(parameter.type);
  7760. variable.SetAccess(SyntaxTree.Hidden);
  7761. variable.SetModifiers(parameter.modifiers);
  7762. currentScope.PushVariable(variable);
  7763. *)
  7764. Register(parameter,scope, FALSE);
  7765. parameter := parameter.nextParameter;
  7766. END;
  7767. property := type.firstProperty;
  7768. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7769. (*
  7770. variable := currentScope.FindVariable(property.name);
  7771. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7772. prop := variable(SyntaxTree.Property);
  7773. ELSE (* add, duplicate symbols detection later *)
  7774. prop := SyntaxTree.NewProperty(property.position, property.name);
  7775. currentScope.PushVariable(prop);
  7776. END;
  7777. prop.SetType(property.type);
  7778. prop.SetValue(property.value);
  7779. prop.SetAccess(SyntaxTree.Hidden);
  7780. *)
  7781. Register(property, scope, FALSE);
  7782. property := property.nextProperty;
  7783. END;
  7784. END DeclareCell;
  7785. BEGIN
  7786. prevError := error;
  7787. prevPhase := phase;
  7788. phase := DeclarationPhase;
  7789. prevScope := currentScope;
  7790. currentScope := scope;
  7791. error := FALSE;
  7792. IF 0 IN phases THEN
  7793. (* first enter all symbols in scope *)
  7794. IF scope IS SyntaxTree.ModuleScope THEN
  7795. (* treat imports first for a module scope, , set default context if necessary *)
  7796. import := scope(SyntaxTree.ModuleScope).firstImport;
  7797. WHILE(import # NIL) DO
  7798. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7799. Register(import, currentScope, FALSE);
  7800. import := import.nextImport;
  7801. END;
  7802. import := scope(SyntaxTree.ModuleScope).firstImport;
  7803. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7804. ResolveSymbol(import);
  7805. import := import.nextImport;
  7806. END;
  7807. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7808. (* enter parameters for a procedure scope *)
  7809. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7810. parameter := procedureType.firstParameter;
  7811. WHILE(parameter # NIL) DO
  7812. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7813. END;
  7814. parameter := procedureType.returnParameter;
  7815. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7816. parameter := procedureType.selfParameter;
  7817. IF parameter # NIL THEN
  7818. Register(parameter, currentScope, FALSE);
  7819. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7820. END;
  7821. ELSIF scope IS SyntaxTree.CellScope THEN
  7822. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7823. IF~skipImplementation THEN
  7824. import := scope(SyntaxTree.CellScope).firstImport;
  7825. WHILE(import # NIL) DO
  7826. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7827. Register(import, currentScope, FALSE);
  7828. import := import.nextImport;
  7829. END;
  7830. import := scope(SyntaxTree.CellScope).firstImport;
  7831. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7832. ResolveSymbol(import);
  7833. import := import.nextImport;
  7834. END;
  7835. END;
  7836. END;
  7837. IF error THEN RETURN END;
  7838. IF skipImplementation THEN
  7839. scope.Clear;
  7840. END;
  7841. (* constants *)
  7842. constant := scope.firstConstant;
  7843. WHILE (constant # NIL) DO
  7844. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7845. END;
  7846. (* type declarations *)
  7847. typeDeclaration := scope.firstTypeDeclaration;
  7848. WHILE (typeDeclaration # NIL) DO
  7849. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7850. END;
  7851. (* variables *)
  7852. variable := scope.firstVariable;
  7853. WHILE (variable # NIL) DO
  7854. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7855. END;
  7856. (* procedures *)
  7857. IF scope.procedures # NIL THEN
  7858. FOR i := 0 TO scope.procedures.Length()-1 DO
  7859. procedure := scope.procedures.GetProcedure(i);
  7860. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7861. IF procedureType.selfParameter = NIL THEN
  7862. scope.AddProcedure(procedure);
  7863. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7864. ELSE
  7865. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7866. IF typeDeclaration = NIL THEN
  7867. Error(procedureType.selfParameter.position, "No such type declaration");
  7868. ELSE
  7869. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7870. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7871. typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7872. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7873. END;
  7874. END;
  7875. END;
  7876. END;
  7877. END;
  7878. (* now process all symbols without any presumption on the order *)
  7879. symbol := scope.firstSymbol;
  7880. WHILE(symbol # NIL) DO
  7881. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7882. IF (symbol IS SyntaxTree.Procedure) THEN
  7883. IF 1 IN phases THEN
  7884. ResolveSymbol(symbol);
  7885. END;
  7886. ELSE
  7887. IF 0 IN phases THEN
  7888. ResolveSymbol(symbol);
  7889. END;
  7890. END;
  7891. END;
  7892. symbol := symbol.nextSymbol;
  7893. END;
  7894. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7895. symbol := scope.firstSymbol;
  7896. WHILE symbol # NIL DO
  7897. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7898. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7899. pointerFixes.Add(symbol, currentScope);
  7900. END;
  7901. IF ~symbol.type.resolved.isRealtime THEN
  7902. Error(symbol.position,"symbol has no realtime type");
  7903. END;
  7904. END;
  7905. symbol := symbol.nextSymbol
  7906. END;
  7907. END;
  7908. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7909. Error(Basic.invalidPosition,"problems during offset computation in module");
  7910. END;
  7911. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7912. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7913. scope.ownerModule.AddScope(scope);
  7914. END;
  7915. phase := prevPhase;
  7916. currentScope := prevScope;
  7917. error := error OR prevError;
  7918. END Declarations;
  7919. (* nopov *)
  7920. (** check if all operators from one module are compatible to the ones in the other module
  7921. - check if there are not multiple operators with the same signature
  7922. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7923. - check for all operators whose signatures are compatible, whether the return types are compatible
  7924. note that:
  7925. - the return type is not considered to be part of the signature
  7926. - two signatures are considered compatible, if all of the operands are compatible
  7927. **)
  7928. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7929. VAR
  7930. thisOperator, thatOperator: SyntaxTree.Operator;
  7931. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7932. thisParameter, thatParameter: SyntaxTree.Parameter;
  7933. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7934. i: LONGINT;
  7935. BEGIN
  7936. currentScope := thisModuleScope;
  7937. hasError := FALSE;
  7938. (* go through all operators in the other module *)
  7939. thatOperator := thatModuleScope.firstOperator;
  7940. WHILE (thatOperator # NIL) & ~hasError DO
  7941. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7942. (* the other operator is accessible *)
  7943. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7944. (* the other operator is not the conversion operator *)
  7945. (* go through all operators in this module *)
  7946. thisOperator := thisModuleScope.firstOperator;
  7947. WHILE (thisOperator # NIL) & ~hasError DO
  7948. IF thisOperator # thatOperator THEN
  7949. (* the operators are not the same *)
  7950. IF thisOperator.name = thatOperator.name THEN
  7951. (* the operators share the same identifier *)
  7952. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7953. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7954. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7955. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7956. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7957. (* both operators have the same paramter count *)
  7958. thisParameter := thisProcedureType.firstParameter;
  7959. thatParameter := thatProcedureType.firstParameter;
  7960. operandsAreEqual := TRUE;
  7961. operandsAreCompatible := TRUE;
  7962. (* go through all parameters *)
  7963. FOR i := 1 TO thisProcedureType.numberParameters DO
  7964. ASSERT(thatParameter # NIL);
  7965. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7966. operandsAreEqual := FALSE;
  7967. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7968. operandsAreCompatible := FALSE
  7969. END
  7970. END;
  7971. thisParameter := thisParameter.nextParameter;
  7972. thatParameter := thatParameter.nextParameter
  7973. END;
  7974. IF operandsAreEqual THEN
  7975. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7976. hasError := TRUE
  7977. ELSIF operandsAreCompatible THEN
  7978. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7979. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  7980. hasError := TRUE
  7981. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7982. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7983. hasError := TRUE
  7984. END
  7985. END
  7986. END
  7987. END
  7988. END;
  7989. thisOperator := thisOperator.nextOperator
  7990. END
  7991. END
  7992. END;
  7993. thatOperator := thatOperator.nextOperator
  7994. END
  7995. END CheckInterOperatorConformity;
  7996. (** check module:
  7997. - check module declaration
  7998. - add context, if necessary
  7999. - remove module from import cache, if necessary
  8000. - check declarations
  8001. - resolve all type fixes
  8002. - check implementation (bodies)
  8003. **)
  8004. PROCEDURE Module*(x: SyntaxTree.Module);
  8005. VAR (* nopov *)
  8006. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8007. BEGIN
  8008. prevScope := currentScope;
  8009. prevIsCellNet := currentIsCellNet;
  8010. module := x;
  8011. ASSERT(x # NIL);
  8012. global := system.globalScope[x.case];
  8013. x.moduleScope.SetGlobalScope(global);
  8014. currentScope := global;
  8015. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8016. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8017. RemoveModuleFromCache(importCache,x);
  8018. Declarations(x.moduleScope, FALSE, {0,1});
  8019. FixTypes();
  8020. IF module.isCellNet THEN
  8021. currentIsCellNet := TRUE;
  8022. modifier := x.modifiers;
  8023. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8024. CheckModifiers(modifier, FALSE);
  8025. END;
  8026. (* nopov *)
  8027. IF ~error THEN
  8028. (* check if operators conform to each other within this module *)
  8029. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8030. (* go through all imports *)
  8031. import := x.moduleScope.firstImport;
  8032. WHILE import # NIL DO
  8033. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8034. (* check if all operators in this module conform to the ones of the imported module *)
  8035. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8036. END;
  8037. import := import.nextImport
  8038. END;
  8039. END;
  8040. Implementations(x);
  8041. module := NIL;
  8042. currentIsCellNet := prevIsCellNet;
  8043. currentScope := prevScope;
  8044. END Module;
  8045. END Checker;
  8046. Warnings*=OBJECT (SyntaxTree.Visitor)
  8047. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8048. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8049. BEGIN
  8050. SELF.diagnostics := diagnostics
  8051. END InitWarnings;
  8052. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  8053. BEGIN END VisitPortType;
  8054. (** types *)
  8055. PROCEDURE Type(x: SyntaxTree.Type);
  8056. BEGIN x.Accept(SELF)
  8057. END Type;
  8058. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8059. BEGIN END VisitType;
  8060. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8061. BEGIN END VisitBasicType;
  8062. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8063. BEGIN END VisitCharacterType;
  8064. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8065. BEGIN END VisitIntegerType;
  8066. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8067. BEGIN END VisitFloatType;
  8068. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8069. BEGIN END VisitQualifiedType;
  8070. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8071. BEGIN END VisitStringType;
  8072. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8073. BEGIN END VisitEnumerationType;
  8074. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8075. BEGIN END VisitRangeType;
  8076. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8077. BEGIN
  8078. IF ~(SyntaxTree.Warned IN x.state) THEN
  8079. x.SetState(SyntaxTree.Warned);
  8080. Type(x.arrayBase);
  8081. END;
  8082. END VisitArrayType;
  8083. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8084. BEGIN
  8085. IF ~(SyntaxTree.Warned IN x.state) THEN
  8086. x.SetState(SyntaxTree.Warned);
  8087. Type(x.arrayBase);
  8088. END;
  8089. END VisitMathArrayType;
  8090. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8091. BEGIN
  8092. IF ~(SyntaxTree.Warned IN x.state) THEN
  8093. x.SetState(SyntaxTree.Warned);
  8094. Type(x.pointerBase);
  8095. END;
  8096. END VisitPointerType;
  8097. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8098. BEGIN Scope(x.recordScope) END VisitRecordType;
  8099. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8100. BEGIN Scope(x.cellScope) END VisitCellType;
  8101. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8102. BEGIN END VisitProcedureType;
  8103. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8104. VAR msg: ARRAY 256 OF CHAR;
  8105. BEGIN
  8106. Global.GetSymbolName(x,msg);
  8107. Strings.Append(msg," ");
  8108. Strings.Append(msg,text);
  8109. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8110. END Warning;
  8111. (** symbols *)
  8112. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8113. BEGIN
  8114. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8115. IF ~(x IS SyntaxTree.Parameter) THEN
  8116. Warning(x,"never used");
  8117. END;
  8118. END;
  8119. x.Accept(SELF);
  8120. END Symbol;
  8121. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8122. BEGIN END VisitSymbol;
  8123. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8124. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8125. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8126. BEGIN END VisitConstant;
  8127. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8128. BEGIN END VisitVariable;
  8129. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8130. BEGIN END VisitProperty;
  8131. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8132. BEGIN END VisitParameter;
  8133. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8134. BEGIN
  8135. Scope(x.procedureScope)
  8136. END VisitProcedure;
  8137. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8138. BEGIN END VisitOperator;
  8139. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8140. BEGIN END VisitImport;
  8141. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8142. VAR
  8143. symbol: SyntaxTree.Symbol;
  8144. BEGIN
  8145. symbol := scope.firstSymbol;
  8146. WHILE(symbol # NIL) DO
  8147. Symbol(symbol);
  8148. symbol := symbol.nextSymbol;
  8149. END;
  8150. END Scope;
  8151. PROCEDURE Module*(x: SyntaxTree.Module);
  8152. BEGIN
  8153. SELF.module := x;
  8154. Scope(x.moduleScope);
  8155. END Module;
  8156. END Warnings;
  8157. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8158. BEGIN
  8159. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8160. END IsOberonInline;
  8161. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8162. BEGIN
  8163. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8164. END Resolved;
  8165. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8166. VAR i: LONGINT;
  8167. BEGIN
  8168. i := 1;
  8169. WHILE i < x DO
  8170. i := i *2
  8171. END;
  8172. RETURN i=x
  8173. END PowerOf2;
  8174. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8175. BEGIN
  8176. RETURN
  8177. (scope # NIL) &
  8178. (scope IS SyntaxTree.ModuleScope)
  8179. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8180. OR
  8181. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8182. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8183. END IsCellNetScope;
  8184. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8185. BEGIN
  8186. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8187. END IsCellScope;
  8188. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8189. BEGIN
  8190. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8191. RETURN (scope # NIL) & IsCellNetScope(scope)
  8192. END InCellNetScope;
  8193. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8194. BEGIN
  8195. ASSERT(size MOD system.dataUnit = 0);
  8196. RETURN size DIV system.dataUnit
  8197. END ToMemoryUnits;
  8198. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8199. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8200. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8201. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8202. BEGIN
  8203. IF t = NIL THEN
  8204. RETURN TRUE
  8205. ELSE
  8206. t := t.resolved;
  8207. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8208. END;
  8209. END TypeAllowed;
  8210. BEGIN
  8211. type := type.resolved;
  8212. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8213. RETURN FALSE
  8214. ELSE
  8215. procedureType := type(SyntaxTree.ProcedureType);
  8216. numberParameters := procedureType.numberParameters;
  8217. RETURN
  8218. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8219. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8220. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8221. END;
  8222. END GetProcedureAllowed;
  8223. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8224. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8225. VAR import: SyntaxTree.Import;
  8226. BEGIN
  8227. import := importCache.ImportByModuleName(x.name,x.context);
  8228. IF import # NIL THEN
  8229. importCache.RemoveImporters(x.name,x.context);
  8230. END;
  8231. END RemoveModuleFromCache;
  8232. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8233. (* to <- this assignment compatibility *)
  8234. VAR result: BOOLEAN;
  8235. BEGIN
  8236. IF this= NIL THEN result := (to=NIL)
  8237. ELSIF to=NIL THEN result := FALSE
  8238. ELSE
  8239. (*! will be replaced by this:
  8240. ELSE result := this.CompatibleTo(to.resolved);
  8241. *)
  8242. this := this.resolved; to := to.resolved;
  8243. IF to=SyntaxTree.invalidType THEN result := FALSE
  8244. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8245. ELSIF to = this THEN
  8246. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8247. ELSIF to IS SyntaxTree.BasicType THEN
  8248. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8249. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8250. result := this.CompatibleTo(to.resolved)
  8251. ELSE
  8252. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8253. END
  8254. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8255. result := to.sizeInBits = this.sizeInBits;
  8256. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8257. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8258. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8259. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8260. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8261. result := TRUE;
  8262. ELSIF to IS SyntaxTree.AnyType THEN
  8263. 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);
  8264. ELSIF to IS SyntaxTree.ObjectType THEN
  8265. 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 *) ;
  8266. ELSIF to IS SyntaxTree.ByteType THEN
  8267. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8268. ELSIF to IS SyntaxTree.CharacterType THEN
  8269. result := IsCharacterType(this)
  8270. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8271. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8272. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8273. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8274. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8275. result := TRUE;
  8276. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8277. result := TRUE;
  8278. ELSE
  8279. result := FALSE
  8280. END;
  8281. ELSIF to IS SyntaxTree.PointerType THEN
  8282. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8283. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8284. & (~to.isRealtime OR this.isRealtime);
  8285. ELSIF to IS SyntaxTree.ProcedureType THEN
  8286. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8287. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8288. & (~to.isRealtime OR this.isRealtime)
  8289. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8290. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8291. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8292. ELSIF to IS SyntaxTree.RecordType THEN
  8293. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8294. ELSIF to IS SyntaxTree.ArrayType THEN
  8295. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8296. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8297. ELSIF StaticArrayCompatible(to, this) THEN
  8298. result := TRUE
  8299. ELSE
  8300. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8301. END;
  8302. ELSIF to IS SyntaxTree.MathArrayType THEN
  8303. IF this IS SyntaxTree.MathArrayType THEN
  8304. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8305. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8306. result := TRUE;
  8307. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8308. result := TRUE;
  8309. ELSE
  8310. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8311. END;
  8312. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8313. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8314. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8315. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8316. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8317. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8318. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8319. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8320. ELSE
  8321. result := FALSE
  8322. END;
  8323. (* an array-structured object type is compatible to the type of its array structure *)
  8324. ELSIF IsArrayStructuredObjectType(this) THEN
  8325. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8326. ELSE
  8327. result := FALSE;
  8328. END;
  8329. ELSIF to IS SyntaxTree.StringType THEN
  8330. result := FALSE;
  8331. ELSIF to IS SyntaxTree.EnumerationType THEN
  8332. result := IsEnumerationExtension(this,to);
  8333. ELSIF to IS SyntaxTree.PortType THEN
  8334. result := SameType(to, this)
  8335. ELSE
  8336. Printout.Info("CompatibleTo",to);
  8337. HALT(100); (* implement missing type check *)
  8338. END;
  8339. END;
  8340. RETURN result
  8341. END CompatibleTo;
  8342. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8343. VAR actualBase, formalBase: SyntaxTree.Type;
  8344. BEGIN
  8345. IF SameType(formal,actual) THEN
  8346. RETURN TRUE
  8347. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8348. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8349. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8350. RETURN
  8351. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8352. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8353. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8354. & StaticArrayCompatible(formalBase,actualBase)
  8355. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8356. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8357. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8358. RETURN
  8359. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8360. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8361. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8362. & StaticArrayCompatible(formalBase,actualBase)
  8363. ELSE RETURN FALSE
  8364. END;
  8365. END StaticArrayCompatible;
  8366. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8367. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8368. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8369. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8370. BEGIN
  8371. result := SameType(formal,actual);
  8372. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8373. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8374. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8375. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8376. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8377. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8378. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8379. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8380. & TC(formalBase, actualBase);
  8381. END;
  8382. RETURN result
  8383. END TC;
  8384. BEGIN
  8385. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8386. ELSE
  8387. arrayBase := formalType.arrayBase.resolved;
  8388. IF (actualType IS SyntaxTree.StringType) THEN
  8389. result := arrayBase IS SyntaxTree.CharacterType
  8390. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8391. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8392. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8393. result := TC(formalType, actualType);
  8394. ELSE
  8395. result := (arrayBase IS SyntaxTree.ByteType)
  8396. END;
  8397. END;
  8398. RETURN result
  8399. END OpenArrayCompatible;
  8400. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8401. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8402. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8403. BEGIN
  8404. IF actualType IS SyntaxTree.MathArrayType THEN
  8405. actualArray := actualType(SyntaxTree.MathArrayType);
  8406. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8407. (*
  8408. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8409. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8410. *)
  8411. actualBase := ArrayBase(actualType,Infinity);
  8412. formalBase := ArrayBase(formalType,Infinity);
  8413. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8414. ELSE
  8415. (*
  8416. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8417. *)
  8418. formalBase := Resolved(formalType.arrayBase);
  8419. actualBase := Resolved(actualArray.arrayBase);
  8420. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8421. (*
  8422. ARRAY [k] -> ARRAY [n]
  8423. *)
  8424. result := (formalType.staticLength = actualArray.staticLength)
  8425. ELSE
  8426. result := TRUE
  8427. END;
  8428. IF ~result THEN
  8429. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8430. ELSIF actualBase = NIL THEN result := FALSE
  8431. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8432. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8433. ELSE
  8434. result := SameType(formalBase,actualBase)
  8435. END;
  8436. END;
  8437. ELSE
  8438. result := FALSE
  8439. END;
  8440. RETURN result
  8441. END MathArrayCompatible;
  8442. (**
  8443. Math Array Type distance for assignments / parameter passings of the form
  8444. from -> to
  8445. variants:
  8446. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8447. allowed:
  8448. static -> static (& size match)
  8449. static -> open
  8450. static -> tensor
  8451. open -> open
  8452. open -> tensor
  8453. open -> static
  8454. tensor -> tensor
  8455. tensor -> open
  8456. tensor -> static
  8457. **)
  8458. (*! think about the metric here: is form matching more important than element type matching? *)
  8459. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8460. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8461. BEGIN
  8462. fromBase := Resolved(from.arrayBase);
  8463. toBase := Resolved(to.arrayBase);
  8464. i := Infinity;
  8465. IF from = to THEN
  8466. i := 0;
  8467. ELSIF (from.form = to.form) THEN
  8468. (* static -> static, open -> open, tensor -> tensor *)
  8469. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8470. IF fromBase = toBase THEN i := 0
  8471. ELSIF toBase = NIL THEN i := 1
  8472. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8473. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8474. ELSE
  8475. i := TypeDistance(system,fromBase, toBase, varpar);
  8476. END;
  8477. END;
  8478. ELSIF (to.form = SyntaxTree.Static) THEN
  8479. (* forbidden *)
  8480. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8481. (* static -> tensor, open -> tensor, tensor -> open *)
  8482. IF toBase=fromBase THEN i := 0;
  8483. ELSIF toBase = NIL THEN i := 1;
  8484. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8485. toBase := ArrayBase(toBase,Infinity);
  8486. IF (fromBase=toBase) THEN i := 0
  8487. ELSIF (toBase = NIL) THEN i:= 1
  8488. ELSIF (fromBase = NIL) THEN i := Infinity;
  8489. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8490. END;
  8491. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8492. fromBase := ArrayBase(fromBase,Infinity);
  8493. IF (fromBase=toBase) THEN i := 0
  8494. ELSIF (toBase = NIL) THEN i := 1
  8495. ELSIF (fromBase = NIL) THEN i := Infinity;
  8496. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8497. END;
  8498. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8499. END;
  8500. IF i # Infinity THEN INC(i,2) END;
  8501. ELSIF (from.form = SyntaxTree.Static) THEN
  8502. (* static -> open *)
  8503. IF toBase=fromBase THEN i := 0
  8504. ELSIF toBase = NIL THEN i := 1
  8505. ELSIF fromBase = NIL THEN i := Infinity
  8506. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8507. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8508. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8509. END;
  8510. IF i # Infinity THEN INC(i,1) END;
  8511. ELSE HALT(100); (* unknown case *)
  8512. END;
  8513. RETURN i;
  8514. END MathArrayTypeDistance;
  8515. (** compute and return the distance of two array types
  8516. - return the distance of the base types
  8517. **)
  8518. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8519. VAR i: LONGINT;
  8520. BEGIN
  8521. i := Infinity;
  8522. IF from = to THEN
  8523. i := 0
  8524. ELSE
  8525. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8526. (*
  8527. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8528. i := TypeDistance(from.base, to.base);
  8529. IF i >= 0 THEN INC(i) END
  8530. ELSIF (from.mode = open) & (to.mode = open) THEN
  8531. i := TypeDistance(from.base, to.base);
  8532. *)
  8533. END;
  8534. RETURN i
  8535. END ArrayTypeDistance;
  8536. (** compute the signature distance of a procedure and an actual parameter list
  8537. - if any of the parameters are not compatible, the result is infinite
  8538. - add up and return the distance over all parameters
  8539. **)
  8540. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8541. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8542. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8543. BEGIN
  8544. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8545. result := Infinity
  8546. ELSE
  8547. formalParameter := procedureType.firstParameter;
  8548. i := 0;
  8549. result := 0;
  8550. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8551. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8552. actualParameter := actualParameters.GetExpression(i);
  8553. ASSERT(formalParameter.type # NIL);
  8554. IF (actualParameter.type = NIL) THEN distance := Infinity
  8555. ELSE
  8556. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8557. END;
  8558. IF distance = Infinity THEN
  8559. result := Infinity;
  8560. ELSE
  8561. to := formalParameter.type.resolved;
  8562. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8563. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8564. (* already handled varpar *)
  8565. (*
  8566. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8567. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8568. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8569. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8570. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8571. END;
  8572. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8573. result := Infinity
  8574. END;
  8575. *)
  8576. INC(result, distance);
  8577. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8578. INC(result, distance);
  8579. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8580. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8581. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8582. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8583. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8584. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8585. END;
  8586. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8587. result := Infinity
  8588. END;
  8589. ELSE
  8590. result := Infinity
  8591. END;
  8592. ELSE
  8593. INC(result,distance);
  8594. END;
  8595. END;
  8596. (*
  8597. Printout.Info("actual=", actualParameter);
  8598. Printout.Info("formal=", formalParameter);
  8599. TRACE(result);
  8600. *)
  8601. formalParameter := formalParameter.nextParameter; INC(i);
  8602. END;
  8603. END;
  8604. ASSERT(result >= 0);
  8605. RETURN result
  8606. END Distance;
  8607. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8608. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8609. BEGIN
  8610. IF right.numberParameters # (procedureType.numberParameters) THEN
  8611. result := Infinity
  8612. ELSE
  8613. formalParameter := procedureType.firstParameter;
  8614. rightParameter := right.firstParameter;
  8615. i := 0;
  8616. result := 0;
  8617. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8618. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8619. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8620. IF distance = Infinity THEN
  8621. result := Infinity;
  8622. ELSE
  8623. INC(result,distance);
  8624. END;
  8625. formalParameter := formalParameter.nextParameter;
  8626. rightParameter := rightParameter.nextParameter;
  8627. END;
  8628. END;
  8629. ASSERT(result >= 0);
  8630. RETURN result
  8631. END ProcedureTypeDistance;
  8632. (** compute and return the distance between two types, used for computation of signature distance
  8633. from -> to
  8634. **)
  8635. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8636. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8637. BEGIN
  8638. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8639. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8640. END;
  8641. i := Infinity;
  8642. IF from = to THEN
  8643. i := 0
  8644. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8645. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8646. i := Infinity;
  8647. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8648. i := 10;
  8649. ELSIF (from IS SyntaxTree.StringType) THEN
  8650. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8651. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8652. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8653. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8654. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8655. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8656. i := 1
  8657. ELSIF (from IS SyntaxTree.NilType) THEN
  8658. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8659. (*
  8660. ELSIF (from = NoType) THEN
  8661. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8662. *)
  8663. ELSIF (from IS SyntaxTree.BasicType) THEN
  8664. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8665. IF varpar & (i # 0) THEN i := Infinity END;
  8666. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8667. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8668. ELSIF (from IS SyntaxTree.RecordType) THEN
  8669. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8670. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8671. IF to IS SyntaxTree.MathArrayType THEN
  8672. (*
  8673. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8674. i := Infinity;
  8675. ELSE
  8676. *)
  8677. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8678. (*
  8679. END;
  8680. *)
  8681. END
  8682. ELSIF (from IS SyntaxTree.PointerType) THEN
  8683. ptr := from(SyntaxTree.PointerType);
  8684. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8685. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8686. (* ELSE i := TypeDistance(ptr.base, to); *)
  8687. END
  8688. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8689. IF (to IS SyntaxTree.ProcedureType) THEN
  8690. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8691. END;
  8692. ELSIF (from IS SyntaxTree.PortType) THEN
  8693. IF (to IS SyntaxTree.PortType) THEN
  8694. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8695. i := 0;
  8696. END;
  8697. END;
  8698. (*no procedure test, procedure must be the same*)
  8699. END;
  8700. RETURN i
  8701. END TypeDistance;
  8702. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8703. BEGIN
  8704. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8705. END IsIntegerType;
  8706. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8707. BEGIN
  8708. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8709. END IsAddressType;
  8710. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8711. BEGIN
  8712. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8713. END IsSizeType;
  8714. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8715. BEGIN
  8716. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8717. END IsSignedIntegerType;
  8718. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8719. BEGIN
  8720. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8721. END IsUnsignedIntegerType;
  8722. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8723. VAR result: BOOLEAN;
  8724. BEGIN
  8725. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8726. value := x.resolved(SyntaxTree.IntegerValue).value;
  8727. result := TRUE
  8728. ELSE
  8729. result := FALSE
  8730. END;
  8731. RETURN result
  8732. END IsIntegerValue;
  8733. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8734. VAR result: BOOLEAN;
  8735. BEGIN
  8736. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8737. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8738. result := TRUE
  8739. ELSE
  8740. result := FALSE
  8741. END;
  8742. RETURN result
  8743. END IsEnumerationValue;
  8744. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8745. VAR result: BOOLEAN;
  8746. BEGIN
  8747. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8748. value := x.resolved(SyntaxTree.RealValue).value;
  8749. result := TRUE
  8750. ELSE
  8751. result := FALSE
  8752. END;
  8753. RETURN result
  8754. END IsRealValue;
  8755. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8756. VAR result: BOOLEAN;
  8757. BEGIN
  8758. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8759. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8760. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8761. result := TRUE
  8762. ELSE
  8763. result := FALSE
  8764. END;
  8765. RETURN result
  8766. END IsComplexValue;
  8767. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8768. VAR result: BOOLEAN;
  8769. BEGIN
  8770. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8771. value := x.resolved(SyntaxTree.CharacterValue).value;
  8772. result := TRUE
  8773. ELSE
  8774. result := FALSE
  8775. END;
  8776. RETURN result
  8777. END IsCharacterValue;
  8778. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8779. VAR result: BOOLEAN;
  8780. BEGIN
  8781. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8782. value := x.resolved(SyntaxTree.BooleanValue).value;
  8783. result := TRUE
  8784. ELSE
  8785. result := FALSE
  8786. END;
  8787. RETURN result
  8788. END IsBooleanValue;
  8789. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8790. VAR result: BOOLEAN;
  8791. BEGIN
  8792. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8793. value := x.resolved(SyntaxTree.SetValue).value;
  8794. result := TRUE
  8795. ELSE
  8796. result := FALSE
  8797. END;
  8798. RETURN result
  8799. END IsSetValue;
  8800. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8801. VAR result: BOOLEAN;
  8802. BEGIN
  8803. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8804. value := x.resolved(SyntaxTree.StringValue).value;
  8805. result := TRUE
  8806. ELSE
  8807. result := FALSE
  8808. END;
  8809. RETURN result
  8810. END IsStringValue;
  8811. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8812. BEGIN
  8813. x := x.resolved;
  8814. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8815. END Indexable;
  8816. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8817. BEGIN
  8818. RETURN t1.SameType(t2.resolved);
  8819. END SameType;
  8820. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8821. BEGIN
  8822. IF t IS SyntaxTree.MathArrayType THEN
  8823. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8824. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8825. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8826. DEC(max);
  8827. END;
  8828. ELSIF t IS SyntaxTree.ArrayType THEN
  8829. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8830. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8831. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8832. END;
  8833. END;
  8834. RETURN t;
  8835. END ArrayBase;
  8836. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8837. BEGIN
  8838. type := type.resolved;
  8839. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8840. base := type(SyntaxTree.ArrayType).arrayBase;
  8841. RETURN TRUE;
  8842. END;
  8843. RETURN FALSE;
  8844. END IsOpenArray;
  8845. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8846. BEGIN
  8847. type := type.resolved;
  8848. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8849. base := type(SyntaxTree.ArrayType).arrayBase;
  8850. dim := type(SyntaxTree.ArrayType).staticLength;
  8851. RETURN TRUE
  8852. ELSE
  8853. RETURN FALSE
  8854. END;
  8855. END IsStaticArray;
  8856. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8857. BEGIN
  8858. type := type.resolved;
  8859. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8860. base := type(SyntaxTree.ArrayType).arrayBase;
  8861. RETURN TRUE
  8862. ELSE
  8863. RETURN FALSE
  8864. END;
  8865. END IsDynamicArray;
  8866. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8867. VAR i: LONGINT;
  8868. BEGIN
  8869. i := 0;
  8870. t := t.resolved;
  8871. IF t IS SyntaxTree.MathArrayType THEN
  8872. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8873. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8874. END;
  8875. ELSIF t IS SyntaxTree.ArrayType THEN
  8876. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8877. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8878. END;
  8879. END;
  8880. RETURN i
  8881. END Dimension;
  8882. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8883. BEGIN
  8884. RETURN expression.assignable;
  8885. END IsVariable;
  8886. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8887. BEGIN
  8888. IF (symbol IS SyntaxTree.Parameter) THEN
  8889. WITH symbol: SyntaxTree.Parameter DO
  8890. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8891. END;
  8892. ELSE
  8893. RETURN FALSE
  8894. END;
  8895. END IsVariableParameter;
  8896. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8897. VAR result: BOOLEAN;
  8898. BEGIN
  8899. IF type = NIL THEN result := FALSE
  8900. ELSE
  8901. type := type.resolved;
  8902. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8903. END;
  8904. RETURN result
  8905. END IsPointerType;
  8906. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8907. VAR result: BOOLEAN;
  8908. BEGIN
  8909. IF type = NIL THEN result := FALSE
  8910. ELSE
  8911. type := type.resolved;
  8912. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8913. END;
  8914. RETURN result
  8915. END IsUnsafePointer;
  8916. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8917. BEGIN
  8918. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8919. END IsDisposable;
  8920. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8921. VAR result: BOOLEAN;
  8922. BEGIN
  8923. IF type = NIL THEN result := FALSE
  8924. ELSE
  8925. type := type.resolved;
  8926. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8927. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8928. result := result OR (type IS SyntaxTree.ObjectType);
  8929. END;
  8930. RETURN result
  8931. END IsPointerToRecord;
  8932. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8933. VAR result: BOOLEAN;
  8934. BEGIN
  8935. IF type = NIL THEN result := FALSE
  8936. ELSE
  8937. type := type.resolved;
  8938. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8939. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8940. ;
  8941. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8942. result := result OR (type IS SyntaxTree.ObjectType);
  8943. END;
  8944. RETURN result
  8945. END IsPointerToObject;
  8946. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8947. BEGIN
  8948. IF type # NIL THEN
  8949. RETURN type.resolved.hasPointers
  8950. ELSE
  8951. RETURN FALSE
  8952. END;
  8953. END ContainsPointer;
  8954. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8955. BEGIN
  8956. IF type = NIL THEN RETURN FALSE END;
  8957. type := type.resolved;
  8958. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8959. END IsStringType;
  8960. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8961. BEGIN
  8962. IF type = NIL THEN RETURN FALSE END;
  8963. type := type.resolved;
  8964. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8965. END IsCharacterType;
  8966. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8967. BEGIN
  8968. IF type = NIL THEN RETURN FALSE END;
  8969. type := type.resolved;
  8970. RETURN (type IS SyntaxTree.EnumerationType)
  8971. END IsEnumerationType;
  8972. (** cf. section "Type extension (base type)" in the language report **)
  8973. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8974. VAR result: BOOLEAN;
  8975. BEGIN
  8976. ASSERT(base # NIL); ASSERT(extension # NIL);
  8977. base := base.resolved; extension := extension.resolved;
  8978. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8979. result := TRUE;
  8980. ELSE
  8981. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8982. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8983. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8984. END;
  8985. WHILE (extension # NIL) & (extension # base) DO
  8986. IF extension IS SyntaxTree.RecordType THEN
  8987. extension := extension(SyntaxTree.RecordType).baseType;
  8988. IF (extension # NIL) THEN extension := extension.resolved END;
  8989. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8990. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8991. END;
  8992. ELSE extension := NIL;
  8993. END;
  8994. END;
  8995. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8996. END;
  8997. RETURN result
  8998. END IsTypeExtension;
  8999. (** check if base is the base enumeration type of extension **)
  9000. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9001. BEGIN
  9002. base := base.resolved; extension := extension.resolved;
  9003. WHILE (extension # NIL) & (extension # base) DO
  9004. IF extension IS SyntaxTree.EnumerationType THEN
  9005. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9006. IF extension # NIL THEN extension := extension.resolved END;
  9007. ELSE
  9008. extension := NIL
  9009. END;
  9010. END;
  9011. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9012. END IsEnumerationExtension;
  9013. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9014. BEGIN
  9015. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9016. RETURN TRUE
  9017. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9018. RETURN TRUE
  9019. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9020. RETURN TRUE
  9021. ELSE
  9022. RETURN FALSE
  9023. END
  9024. END IsCallable;
  9025. (** compute and return the distance of two record types
  9026. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9027. **)
  9028. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9029. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9030. BEGIN
  9031. i := 0;
  9032. WHILE (from # NIL) & (from # to) DO
  9033. baseType := from.baseType;
  9034. IF (baseType # NIL) THEN
  9035. baseType := baseType.resolved;
  9036. IF baseType IS SyntaxTree.PointerType THEN
  9037. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9038. END;
  9039. IF baseType IS SyntaxTree.RecordType THEN
  9040. from := baseType(SyntaxTree.RecordType);
  9041. ELSE
  9042. from := NIL;
  9043. END;
  9044. ELSE
  9045. from := NIL
  9046. END;
  9047. INC(i)
  9048. END;
  9049. IF from = NIL THEN i := Infinity END;
  9050. RETURN i
  9051. END RecordTypeDistance;
  9052. (** compute and return the distance of two pointer types **)
  9053. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9054. BEGIN
  9055. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9056. RETURN Infinity;
  9057. ELSE
  9058. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9059. END;
  9060. END PointerTypeDistance;
  9061. (** check if expression contains a symbol designator pointing to a type declaration.
  9062. - if so then enter type declaration into typeDeclaration and return true else return false
  9063. **)
  9064. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9065. VAR result: BOOLEAN;
  9066. BEGIN
  9067. result := FALSE;
  9068. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9069. result := TRUE;
  9070. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9071. END;
  9072. RETURN result
  9073. END IsTypeDesignator;
  9074. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9075. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9076. VAR result: BOOLEAN;
  9077. BEGIN
  9078. type := type.resolved;
  9079. IF type IS SyntaxTree.PointerType THEN
  9080. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9081. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9082. result := TRUE
  9083. ELSE
  9084. result := type IS SyntaxTree.RecordType
  9085. END;
  9086. RETURN result
  9087. END IsExtensibleType;
  9088. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9089. BEGIN
  9090. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9091. (d IS SyntaxTree.SymbolDesignator) &
  9092. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9093. OR
  9094. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9095. END IsUnextensibleRecord;
  9096. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9097. BEGIN
  9098. IF IsUnextensibleRecord(d) THEN
  9099. RETURN FALSE
  9100. ELSE RETURN IsExtensibleType(d.type.resolved)
  9101. END;
  9102. END IsExtensibleDesignator;
  9103. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9104. BEGIN
  9105. type := type.resolved;
  9106. IF (type IS SyntaxTree.PointerType) THEN
  9107. RETURN TRUE
  9108. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9109. RETURN TRUE
  9110. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9111. RETURN TRUE
  9112. ELSIF (type IS SyntaxTree.BasicType) THEN
  9113. RETURN TRUE
  9114. END;
  9115. RETURN FALSE
  9116. END IsBasicType;
  9117. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9118. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9119. BEGIN
  9120. baseType := record.baseType;
  9121. IF (baseType # NIL) THEN
  9122. baseType := baseType.resolved;
  9123. IF (baseType IS SyntaxTree.PointerType) THEN
  9124. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9125. END;
  9126. END;
  9127. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9128. recordType := baseType(SyntaxTree.RecordType);
  9129. ELSE
  9130. recordType := NIL;
  9131. END;
  9132. RETURN recordType
  9133. END RecordBase;
  9134. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9135. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9136. BEGIN
  9137. baseRecord := RecordBase(scope.ownerRecord);
  9138. IF baseRecord = NIL THEN RETURN NIL END;
  9139. scope := baseRecord.recordScope;
  9140. procedureType := procedure.type.resolved;
  9141. IF procedure IS SyntaxTree.Operator THEN
  9142. operator := scope.firstOperator;
  9143. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9144. (*
  9145. Printout.Info("not same ",procedureType);
  9146. Printout.Info("with ",operator.type);
  9147. *)
  9148. operator := operator.nextOperator;
  9149. END;
  9150. super := operator;
  9151. ELSE
  9152. super := scope.firstProcedure;
  9153. WHILE (super # NIL) & (super.name # procedure.name) DO
  9154. super := super.nextProcedure;
  9155. END;
  9156. END;
  9157. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9158. RETURN super
  9159. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9160. RETURN super
  9161. ELSE
  9162. RETURN FindSuperProcedure(scope,procedure);
  9163. END;
  9164. END FindSuperProcedure;
  9165. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9166. VAR procedure: SyntaxTree.Procedure;
  9167. BEGIN
  9168. procedure := record.recordScope.constructor;
  9169. IF procedure = NIL THEN
  9170. record := RecordBase(record);
  9171. IF record # NIL THEN
  9172. procedure := GetConstructor(record)
  9173. END;
  9174. END;
  9175. RETURN procedure;
  9176. END GetConstructor;
  9177. (* enter a case into a list of cases in a sorted way and check for collision *)
  9178. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9179. VAR prev,this,new: SyntaxTree.CaseConstant;
  9180. BEGIN
  9181. this := root;
  9182. prev := NIL;
  9183. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9184. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9185. RETURN FALSE
  9186. ELSE
  9187. IF (this # NIL) & (this.min = max+1) THEN
  9188. this.min := min
  9189. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9190. prev.max := min
  9191. ELSE
  9192. NEW(new); new.min := min; new.max := max;
  9193. new.next := this;
  9194. IF prev = NIL THEN
  9195. root := new;
  9196. ELSE
  9197. prev.next := new
  9198. END
  9199. END;
  9200. RETURN TRUE
  9201. END;
  9202. END EnterCase;
  9203. (** generate and return a new checker object, errors are entered into diagnostics **)
  9204. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9205. VAR checker: Checker;
  9206. BEGIN
  9207. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9208. RETURN checker
  9209. END NewChecker;
  9210. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9211. VAR warnings: Warnings;
  9212. BEGIN
  9213. NEW(warnings, diagnostics); RETURN warnings;
  9214. END NewWarnings;
  9215. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9216. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9217. END IsRangeType;
  9218. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9219. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9220. END IsMathArrayType;
  9221. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9222. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9223. END IsArrayType;
  9224. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9225. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9226. END IsComplexType;
  9227. (** if a type is an array-structured object type *)
  9228. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9229. VAR recordType: SyntaxTree.RecordType;
  9230. BEGIN
  9231. IF type = NIL THEN
  9232. RETURN FALSE
  9233. ELSE
  9234. type := type.resolved;
  9235. IF type IS SyntaxTree.PointerType THEN
  9236. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9237. IF type IS SyntaxTree.RecordType THEN
  9238. recordType := type(SyntaxTree.RecordType);
  9239. RETURN recordType.isObject & recordType.HasArrayStructure()
  9240. ELSE
  9241. RETURN FALSE
  9242. END
  9243. ELSE
  9244. RETURN FALSE
  9245. END
  9246. END
  9247. END IsArrayStructuredObjectType;
  9248. (** the math array structure of a type
  9249. - for math arrays: the array itself
  9250. - for pointers: the math array structure of the pointer base
  9251. - for array-structured object types: the underlying structure
  9252. - for non-math arrays and all other types: NIL
  9253. **)
  9254. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9255. VAR
  9256. result: SyntaxTree.MathArrayType;
  9257. BEGIN
  9258. IF type = NIL THEN
  9259. result := NIL
  9260. ELSE
  9261. type := type.resolved;
  9262. IF type IS SyntaxTree.PointerType THEN
  9263. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9264. END;
  9265. IF type IS SyntaxTree.MathArrayType THEN
  9266. result := type(SyntaxTree.MathArrayType)
  9267. ELSIF type IS SyntaxTree.RecordType THEN
  9268. result := type(SyntaxTree.RecordType).arrayStructure
  9269. ELSE
  9270. result := NIL
  9271. END
  9272. END;
  9273. RETURN result
  9274. END MathArrayStructureOfType;
  9275. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9276. VAR
  9277. result: BOOLEAN;
  9278. rangeExpression: SyntaxTree.RangeExpression;
  9279. BEGIN
  9280. IF x IS SyntaxTree.RangeExpression THEN
  9281. rangeExpression := x(SyntaxTree.RangeExpression);
  9282. result := TRUE;
  9283. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9284. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9285. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9286. ELSE
  9287. result := FALSE
  9288. END;
  9289. RETURN result
  9290. END IsStaticRange;
  9291. (** whether a type is a math array of tensor form **)
  9292. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9293. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9294. END IsTensor;
  9295. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9296. BEGIN
  9297. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9298. length := type(SyntaxTree.MathArrayType).staticLength;
  9299. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9300. RETURN TRUE
  9301. ELSE
  9302. RETURN FALSE
  9303. END;
  9304. END IsStaticMathArray;
  9305. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9306. BEGIN
  9307. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9308. END SymbolHasAddress;
  9309. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9310. BEGIN
  9311. RETURN
  9312. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9313. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9314. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9315. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9316. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9317. ;
  9318. END HasAddress;
  9319. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9320. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9321. BEGIN
  9322. IF (e IS SyntaxTree.Designator) THEN
  9323. d := e(SyntaxTree.Designator);
  9324. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9325. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9326. e := d.left;
  9327. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9328. END;
  9329. IF d # NIL THEN
  9330. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9331. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9332. END;
  9333. END;
  9334. RETURN FALSE;
  9335. END IsLocalVariable;
  9336. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9337. BEGIN
  9338. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9339. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9340. ELSE
  9341. RETURN TRUE
  9342. END;
  9343. END IsStaticProcedure;
  9344. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9345. CONST OptimizeMethodTable = FALSE;
  9346. BEGIN
  9347. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9348. END InMethodTable;
  9349. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9350. BEGIN
  9351. IF type = NIL THEN RETURN FALSE
  9352. ELSE
  9353. type := type.resolved;
  9354. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9355. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9356. END
  9357. END ReturnedAsParameter;
  9358. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9359. BEGIN
  9360. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9361. END StructuredReturnType;
  9362. END FoxSemanticChecker.
  9363. SystemTools.FreeDownTo FoxSemanticChecker ~