pdecsub.pas 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. tokens,symconst,symtype,symdef,symsym;
  22. type
  23. tpdflag=(
  24. pd_body, { directive needs a body }
  25. pd_implemen, { directive can be used in implementation section }
  26. pd_interface, { directive can be used in interface section }
  27. pd_object, { directive can be used with object declaration }
  28. pd_record, { directive can be used with record declaration }
  29. pd_procvar, { directive can be used with procvar declaration }
  30. pd_notobject, { directive can not be used with object declaration }
  31. pd_notrecord, { directive can not be used with record declaration }
  32. pd_notobjintf, { directive can not be used with interface declaration }
  33. pd_notprocvar, { directive can not be used with procvar declaration }
  34. pd_dispinterface,{ directive can be used with dispinterface methods }
  35. pd_cppobject, { directive can be used with cppclass }
  36. pd_objcclass, { directive can be used with objcclass }
  37. pd_objcprot, { directive can be used with objcprotocol }
  38. pd_nothelper, { directive can not be used with record/class helper declaration }
  39. pd_javaclass, { directive can be used with Java class }
  40. pd_intfjava { directive can be used with Java interface }
  41. );
  42. tpdflags=set of tpdflag;
  43. function check_proc_directive(isprocvar:boolean):boolean;
  44. procedure insert_funcret_local(pd:tprocdef);
  45. function proc_add_definition(var currpd:tprocdef):boolean;
  46. function proc_get_importname(pd:tprocdef):string;
  47. procedure proc_set_mangledname(pd:tprocdef);
  48. procedure handle_calling_convention(pd:tabstractprocdef);
  49. procedure parse_parameter_dec(pd:tabstractprocdef);
  50. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  51. procedure parse_var_proc_directives(sym:tsym);
  52. procedure parse_object_proc_directives(pd:tabstractprocdef);
  53. procedure parse_record_proc_directives(pd:tabstractprocdef);
  54. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  55. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  56. { helper functions - they insert nested objects hierarcy to the symtablestack
  57. with object hierarchy
  58. }
  59. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  60. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  61. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  62. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  63. implementation
  64. uses
  65. SysUtils,
  66. { common }
  67. cutils,cclasses,
  68. { global }
  69. globtype,globals,verbose,constexp,
  70. systems,fpccrc,
  71. cpuinfo,
  72. { symtable }
  73. symbase,symtable,defutil,defcmp,paramgr,cpupara,
  74. { pass 1 }
  75. fmodule,node,htypechk,
  76. nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,
  77. objcutil,
  78. { parser }
  79. scanner,
  80. pbase,pexpr,ptype,pdecl
  81. ;
  82. const
  83. { Please leave this here, this module should NOT use
  84. these variables.
  85. Declaring it as string here results in an error when compiling (PFV) }
  86. current_procinfo = 'error';
  87. function push_child_hierarcy(obj:tabstractrecorddef):integer;
  88. var
  89. _class,hp : tobjectdef;
  90. begin
  91. if obj.typ=recorddef then
  92. begin
  93. symtablestack.push(obj.symtable);
  94. result:=1;
  95. exit;
  96. end;
  97. result:=0;
  98. { insert class hierarchy in the reverse order }
  99. hp:=nil;
  100. repeat
  101. _class:=tobjectdef(obj);
  102. while _class.childof<>hp do
  103. _class:=_class.childof;
  104. hp:=_class;
  105. symtablestack.push(_class.symtable);
  106. inc(result);
  107. until hp=obj;
  108. end;
  109. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  110. begin
  111. result:=0;
  112. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  113. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  114. inc(result,push_child_hierarcy(obj));
  115. end;
  116. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  117. var
  118. _class : tobjectdef;
  119. begin
  120. if obj.typ=recorddef then
  121. begin
  122. symtablestack.pop(obj.symtable);
  123. result:=1;
  124. exit;
  125. end;
  126. result:=0;
  127. _class:=tobjectdef(obj);
  128. while assigned(_class) do
  129. begin
  130. symtablestack.pop(_class.symtable);
  131. _class:=_class.childof;
  132. inc(result);
  133. end;
  134. end;
  135. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  136. begin
  137. result:=pop_child_hierarchy(obj);
  138. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  139. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  140. end;
  141. procedure insert_funcret_para(pd:tabstractprocdef);
  142. var
  143. storepos : tfileposinfo;
  144. vs : tparavarsym;
  145. paranr : word;
  146. begin
  147. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  148. not is_void(pd.returndef) and
  149. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  150. begin
  151. storepos:=current_tokenpos;
  152. if pd.typ=procdef then
  153. current_tokenpos:=tprocdef(pd).fileinfo;
  154. {$if defined(i386)}
  155. { For left to right add it at the end to be delphi compatible.
  156. In the case of safecalls with safecal-exceptions support the
  157. funcret-para is (from the 'c'-point of view) a normal parameter
  158. which has to be added to the end of the parameter-list }
  159. if (pd.proccalloption in (pushleftright_pocalls)) or
  160. ((tf_safecall_exceptions in target_info.flags) and
  161. (pd.proccalloption=pocall_safecall)) then
  162. paranr:=paranr_result_leftright
  163. else
  164. {$elseif defined(x86) or defined(arm)}
  165. if (tf_safecall_exceptions in target_info.flags) and
  166. (pd.proccalloption = pocall_safecall) then
  167. paranr:=paranr_result_leftright
  168. else
  169. {$endif}
  170. paranr:=paranr_result;
  171. { Generate result variable accessing function result }
  172. vs:=tparavarsym.create('$result',paranr,vs_var,pd.returndef,[vo_is_funcret,vo_is_hidden_para]);
  173. pd.parast.insert(vs);
  174. { Store the this symbol as funcretsym for procedures }
  175. if pd.typ=procdef then
  176. tprocdef(pd).funcretsym:=vs;
  177. current_tokenpos:=storepos;
  178. end;
  179. end;
  180. procedure insert_parentfp_para(pd:tabstractprocdef);
  181. var
  182. storepos : tfileposinfo;
  183. vs : tparavarsym;
  184. paranr : longint;
  185. begin
  186. if pd.parast.symtablelevel>normal_function_level then
  187. begin
  188. storepos:=current_tokenpos;
  189. if pd.typ=procdef then
  190. current_tokenpos:=tprocdef(pd).fileinfo;
  191. { if no support for nested procvars is activated, use the old
  192. calling convention to pass the parent frame pointer for backwards
  193. compatibility }
  194. if not(m_nested_procvars in current_settings.modeswitches) then
  195. paranr:=paranr_parentfp
  196. { nested procvars require Delphi-style parentfp passing, see
  197. po_delphi_nested_cc declaration for more info }
  198. {$ifdef i386}
  199. else if (pd.proccalloption in pushleftright_pocalls) then
  200. paranr:=paranr_parentfp_delphi_cc_leftright
  201. {$endif i386}
  202. else
  203. paranr:=paranr_parentfp_delphi_cc;
  204. { Generate result variable accessing function result, it
  205. can't be put in a register since it must be accessable
  206. from the framepointer }
  207. vs:=tparavarsym.create('$parentfp',paranr,vs_value
  208. ,voidpointertype,[vo_is_parentfp,vo_is_hidden_para]);
  209. vs.varregable:=vr_none;
  210. pd.parast.insert(vs);
  211. current_tokenpos:=storepos;
  212. end;
  213. end;
  214. procedure insert_self_and_vmt_para(pd:tabstractprocdef);
  215. var
  216. storepos : tfileposinfo;
  217. vs : tparavarsym;
  218. hdef : tdef;
  219. selfdef : tabstractrecorddef;
  220. vsp : tvarspez;
  221. aliasvs : tabsolutevarsym;
  222. sl : tpropaccesslist;
  223. begin
  224. if (pd.typ=procdef) and
  225. is_objc_class_or_protocol(tprocdef(pd).struct) and
  226. (pd.parast.symtablelevel=normal_function_level) then
  227. begin
  228. { insert Objective-C self and selector parameters }
  229. vs:=tparavarsym.create('$_cmd',paranr_objc_cmd,vs_value,objc_seltype,[vo_is_msgsel,vo_is_hidden_para]);
  230. pd.parast.insert(vs);
  231. { make accessible to code }
  232. sl:=tpropaccesslist.create;
  233. sl.addsym(sl_load,vs);
  234. aliasvs:=tabsolutevarsym.create_ref('_CMD',objc_seltype,sl);
  235. include(aliasvs.varoptions,vo_is_msgsel);
  236. tlocalsymtable(tprocdef(pd).localst).insert(aliasvs);
  237. if (po_classmethod in pd.procoptions) then
  238. { compatible with what gcc does }
  239. hdef:=objc_idtype
  240. else
  241. hdef:=tprocdef(pd).struct;
  242. vs:=tparavarsym.create('$self',paranr_objc_self,vs_value,hdef,[vo_is_self,vo_is_hidden_para]);
  243. pd.parast.insert(vs);
  244. end
  245. else if (pd.typ=procvardef) and
  246. pd.is_methodpointer then
  247. begin
  248. { Generate self variable }
  249. vs:=tparavarsym.create('$self',paranr_self,vs_value,voidpointertype,[vo_is_self,vo_is_hidden_para]);
  250. pd.parast.insert(vs);
  251. end
  252. else
  253. begin
  254. if (pd.typ=procdef) and
  255. assigned(tprocdef(pd).struct) and
  256. (pd.parast.symtablelevel=normal_function_level) then
  257. begin
  258. { static class methods have no hidden self/vmt pointer }
  259. if pd.no_self_node then
  260. exit;
  261. storepos:=current_tokenpos;
  262. current_tokenpos:=tprocdef(pd).fileinfo;
  263. { Generate VMT variable for constructor/destructor }
  264. if (pd.proctypeoption in [potype_constructor,potype_destructor]) and
  265. not(is_cppclass(tprocdef(pd).struct) or
  266. is_record(tprocdef(pd).struct) or
  267. is_javaclass(tprocdef(pd).struct)) then
  268. begin
  269. { can't use classrefdef as type because inheriting
  270. will then always file because of a type mismatch }
  271. vs:=tparavarsym.create('$vmt',paranr_vmt,vs_value,voidpointertype,[vo_is_vmt,vo_is_hidden_para]);
  272. pd.parast.insert(vs);
  273. end;
  274. { for helpers the type of Self is equivalent to the extended
  275. type or equal to an instance of it }
  276. if is_objectpascal_helper(tprocdef(pd).struct) then
  277. selfdef:=tobjectdef(tprocdef(pd).struct).extendeddef
  278. else
  279. selfdef:=tprocdef(pd).struct;
  280. { Generate self variable, for classes we need
  281. to use the generic voidpointer to be compatible with
  282. methodpointers }
  283. vsp:=vs_value;
  284. if (po_staticmethod in pd.procoptions) or
  285. (po_classmethod in pd.procoptions) then
  286. hdef:=tclassrefdef.create(selfdef)
  287. else
  288. begin
  289. if is_object(selfdef) or is_record(selfdef) then
  290. vsp:=vs_var;
  291. hdef:=selfdef;
  292. end;
  293. vs:=tparavarsym.create('$self',paranr_self,vsp,hdef,[vo_is_self,vo_is_hidden_para]);
  294. pd.parast.insert(vs);
  295. current_tokenpos:=storepos;
  296. end;
  297. end;
  298. end;
  299. procedure insert_funcret_local(pd:tprocdef);
  300. var
  301. storepos : tfileposinfo;
  302. vs : tlocalvarsym;
  303. aliasvs : tabsolutevarsym;
  304. sl : tpropaccesslist;
  305. hs : string;
  306. begin
  307. { The result from constructors and destructors can't be accessed directly }
  308. if not(pd.proctypeoption in [potype_constructor,potype_destructor]) and
  309. not is_void(pd.returndef) then
  310. begin
  311. storepos:=current_tokenpos;
  312. current_tokenpos:=pd.fileinfo;
  313. { We need to insert a varsym for the result in the localst
  314. when it is returning in a register }
  315. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  316. begin
  317. vs:=tlocalvarsym.create('$result',vs_value,pd.returndef,[vo_is_funcret]);
  318. pd.localst.insert(vs);
  319. pd.funcretsym:=vs;
  320. end;
  321. { insert the name of the procedure as alias for the function result,
  322. we can't use realname because that will not work for compilerprocs
  323. as the name is lowercase and unreachable from the code }
  324. if assigned(pd.resultname) then
  325. hs:=pd.resultname^
  326. else
  327. hs:=pd.procsym.name;
  328. sl:=tpropaccesslist.create;
  329. sl.addsym(sl_load,pd.funcretsym);
  330. aliasvs:=tabsolutevarsym.create_ref(hs,pd.returndef,sl);
  331. include(aliasvs.varoptions,vo_is_funcret);
  332. tlocalsymtable(pd.localst).insert(aliasvs);
  333. { insert result also if support is on }
  334. if (m_result in current_settings.modeswitches) then
  335. begin
  336. sl:=tpropaccesslist.create;
  337. sl.addsym(sl_load,pd.funcretsym);
  338. aliasvs:=tabsolutevarsym.create_ref('RESULT',pd.returndef,sl);
  339. include(aliasvs.varoptions,vo_is_funcret);
  340. include(aliasvs.varoptions,vo_is_result);
  341. tlocalsymtable(pd.localst).insert(aliasvs);
  342. end;
  343. current_tokenpos:=storepos;
  344. end;
  345. end;
  346. procedure insert_hidden_para(p:TObject;arg:pointer);
  347. var
  348. hvs : tparavarsym;
  349. pd : tabstractprocdef absolute arg;
  350. begin
  351. if (tsym(p).typ<>paravarsym) then
  352. exit;
  353. with tparavarsym(p) do
  354. begin
  355. { We need a local copy for a value parameter when only the
  356. address is pushed. Open arrays and Array of Const are
  357. an exception because they are allocated at runtime and the
  358. address that is pushed is patched }
  359. if (varspez=vs_value) and
  360. paramanager.push_addr_param(varspez,vardef,pd.proccalloption) and
  361. not(is_open_array(vardef) or
  362. is_array_of_const(vardef)) then
  363. include(varoptions,vo_has_local_copy);
  364. { needs high parameter ? }
  365. if paramanager.push_high_param(varspez,vardef,pd.proccalloption) then
  366. begin
  367. hvs:=tparavarsym.create('$high'+name,paranr+1,vs_const,sinttype,[vo_is_high_para,vo_is_hidden_para]);
  368. hvs.symoptions:=[];
  369. owner.insert(hvs);
  370. end
  371. else
  372. begin
  373. { Give a warning that cdecl routines does not include high()
  374. support }
  375. if (pd.proccalloption in cdecl_pocalls) and
  376. paramanager.push_high_param(varspez,vardef,pocall_default) then
  377. begin
  378. if is_open_string(vardef) then
  379. MessagePos(fileinfo,parser_w_cdecl_no_openstring);
  380. if not(po_external in pd.procoptions) and
  381. (pd.typ<>procvardef) and
  382. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  383. if is_array_of_const(vardef) then
  384. MessagePos(fileinfo,parser_e_varargs_need_cdecl_and_external)
  385. else
  386. MessagePos(fileinfo,parser_w_cdecl_has_no_high);
  387. end;
  388. if (vardef.typ=formaldef) and (Tformaldef(vardef).typed) then
  389. begin
  390. hvs:=tparavarsym.create('$typinfo'+name,paranr+1,vs_const,voidpointertype,
  391. [vo_is_typinfo_para,vo_is_hidden_para]);
  392. owner.insert(hvs);
  393. end;
  394. end;
  395. end;
  396. end;
  397. procedure check_c_para(pd:Tabstractprocdef);
  398. var
  399. i,
  400. lastparaidx : longint;
  401. sym : TSym;
  402. begin
  403. lastparaidx:=pd.parast.SymList.Count-1;
  404. for i:=0 to pd.parast.SymList.Count-1 do
  405. begin
  406. sym:=tsym(pd.parast.SymList[i]);
  407. if (sym.typ=paravarsym) and
  408. (tparavarsym(sym).vardef.typ=arraydef) then
  409. begin
  410. if not is_variant_array(tparavarsym(sym).vardef) and
  411. not is_array_of_const(tparavarsym(sym).vardef) and
  412. (tparavarsym(sym).varspez<>vs_var) then
  413. MessagePos(tparavarsym(sym).fileinfo,parser_h_c_arrays_are_references);
  414. if is_array_of_const(tparavarsym(sym).vardef) and
  415. (i<lastparaidx) and
  416. (tsym(pd.parast.SymList[i+1]).typ=paravarsym) and
  417. not(vo_is_high_para in tparavarsym(pd.parast.SymList[i+1]).varoptions) then
  418. MessagePos(tparavarsym(sym).fileinfo,parser_e_C_array_of_const_must_be_last);
  419. end;
  420. end;
  421. end;
  422. procedure check_msg_para(p:TObject;arg:pointer);
  423. begin
  424. if (tsym(p).typ<>paravarsym) then
  425. exit;
  426. with tparavarsym(p) do
  427. begin
  428. { Count parameters }
  429. if (paranr>=10) then
  430. inc(plongint(arg)^);
  431. { First parameter must be var }
  432. if (paranr=10) and
  433. (varspez<>vs_var) then
  434. MessagePos(fileinfo,parser_e_ill_msg_param);
  435. end;
  436. end;
  437. procedure set_addr_param_regable(p:TObject;arg:pointer);
  438. begin
  439. if (tsym(p).typ<>paravarsym) then
  440. exit;
  441. with tparavarsym(p) do
  442. begin
  443. if not is_managed_type(vardef) and
  444. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  445. varregable:=vr_intreg;
  446. end;
  447. end;
  448. procedure parse_parameter_dec(pd:tabstractprocdef);
  449. {
  450. handle_procvar needs the same changes
  451. }
  452. type
  453. tppv = (pv_none,pv_proc,pv_func);
  454. var
  455. sc : TFPObjectList;
  456. hdef : tdef;
  457. arrayelementdef : tdef;
  458. vs : tparavarsym;
  459. i : longint;
  460. srsym : tsym;
  461. pv : tprocvardef;
  462. varspez : Tvarspez;
  463. defaultvalue : tconstsym;
  464. defaultrequired : boolean;
  465. old_block_type : tblock_type;
  466. currparast : tparasymtable;
  467. parseprocvar : tppv;
  468. locationstr : string;
  469. paranr : integer;
  470. dummytype : ttypesym;
  471. explicit_paraloc,
  472. need_array,
  473. is_univ: boolean;
  474. begin
  475. old_block_type:=block_type;
  476. explicit_paraloc:=false;
  477. consume(_LKLAMMER);
  478. { Delphi/Kylix supports nonsense like }
  479. { procedure p(); }
  480. if try_to_consume(_RKLAMMER) and
  481. not(m_tp7 in current_settings.modeswitches) then
  482. exit;
  483. { parsing a proc or procvar ? }
  484. currparast:=tparasymtable(pd.parast);
  485. { reset }
  486. sc:=TFPObjectList.create(false);
  487. defaultrequired:=false;
  488. paranr:=0;
  489. block_type:=bt_var;
  490. is_univ:=false;
  491. repeat
  492. parseprocvar:=pv_none;
  493. if try_to_consume(_VAR) then
  494. varspez:=vs_var
  495. else
  496. if try_to_consume(_CONST) then
  497. varspez:=vs_const
  498. else
  499. if (m_out in current_settings.modeswitches) and
  500. try_to_consume(_OUT) then
  501. varspez:=vs_out
  502. else
  503. if try_to_consume(_CONSTREF) then
  504. varspez:=vs_constref
  505. else
  506. if (m_mac in current_settings.modeswitches) and
  507. try_to_consume(_POINTPOINTPOINT) then
  508. begin
  509. include(pd.procoptions,po_varargs);
  510. break;
  511. end
  512. else
  513. if (m_nested_procvars in current_settings.modeswitches) and
  514. try_to_consume(_PROCEDURE) then
  515. begin
  516. parseprocvar:=pv_proc;
  517. varspez:=vs_const;
  518. end
  519. else
  520. if (m_nested_procvars in current_settings.modeswitches) and
  521. try_to_consume(_FUNCTION) then
  522. begin
  523. parseprocvar:=pv_func;
  524. varspez:=vs_const;
  525. end
  526. else
  527. varspez:=vs_value;
  528. defaultvalue:=nil;
  529. hdef:=nil;
  530. { read identifiers and insert with error type }
  531. sc.clear;
  532. repeat
  533. inc(paranr);
  534. vs:=tparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  535. currparast.insert(vs);
  536. if assigned(vs.owner) then
  537. sc.add(vs)
  538. else
  539. vs.free;
  540. consume(_ID);
  541. until not try_to_consume(_COMMA);
  542. locationstr:='';
  543. { macpas anonymous procvar }
  544. if parseprocvar<>pv_none then
  545. begin
  546. { inline procvar definitions are always nested procvars }
  547. pv:=tprocvardef.create(normal_function_level+1);
  548. if token=_LKLAMMER then
  549. parse_parameter_dec(pv);
  550. if parseprocvar=pv_func then
  551. begin
  552. block_type:=bt_var_type;
  553. consume(_COLON);
  554. single_type(pv.returndef,[]);
  555. block_type:=bt_var;
  556. end;
  557. hdef:=pv;
  558. { possible proc directives }
  559. if check_proc_directive(true) then
  560. begin
  561. dummytype:=ttypesym.create('unnamed',hdef);
  562. parse_var_proc_directives(tsym(dummytype));
  563. dummytype.typedef:=nil;
  564. hdef.typesym:=nil;
  565. dummytype.free;
  566. end;
  567. { Add implicit hidden parameters and function result }
  568. handle_calling_convention(pv);
  569. end
  570. else
  571. { read type declaration, force reading for value paras }
  572. if (token=_COLON) or (varspez=vs_value) then
  573. begin
  574. consume(_COLON);
  575. { check for an open array }
  576. need_array:=false;
  577. { bitpacked open array are not yet supported }
  578. if (token=_PACKED) and
  579. not(cs_bitpacking in current_settings.localswitches) then
  580. begin
  581. consume(_PACKED);
  582. need_array:=true;
  583. end;
  584. if (token=_ARRAY) or
  585. need_array then
  586. begin
  587. consume(_ARRAY);
  588. consume(_OF);
  589. { define range and type of range }
  590. hdef:=tarraydef.create(0,-1,s32inttype);
  591. { array of const ? }
  592. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  593. begin
  594. consume(_CONST);
  595. srsym:=search_system_type('TVARREC');
  596. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  597. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  598. end
  599. else
  600. begin
  601. { define field type }
  602. single_type(arrayelementdef,[]);
  603. tarraydef(hdef).elementdef:=arrayelementdef;
  604. end;
  605. end
  606. else
  607. begin
  608. if (m_mac in current_settings.modeswitches) then
  609. is_univ:=try_to_consume(_UNIV);
  610. if try_to_consume(_TYPE) then
  611. hdef:=ctypedformaltype
  612. else
  613. begin
  614. block_type:=bt_var_type;
  615. single_type(hdef,[stoAllowSpecialization]);
  616. block_type:=bt_var;
  617. end;
  618. { open string ? }
  619. if is_shortstring(hdef) then
  620. begin
  621. case varspez of
  622. vs_var,vs_out,vs_constref:
  623. begin
  624. { not 100% Delphi-compatible: type xstr=string[255] cannot
  625. become an openstring there, while here it can }
  626. if (cs_openstring in current_settings.moduleswitches) and
  627. (tstringdef(hdef).len=255) then
  628. hdef:=openshortstringtype
  629. end;
  630. vs_value:
  631. begin
  632. { value "openstring" parameters don't make sense (the
  633. original string can never be modified, so there's no
  634. use in passing its original length), so change these
  635. into regular shortstring parameters (seems to be what
  636. Delphi also does) }
  637. if is_open_string(hdef) then
  638. hdef:=cshortstringtype;
  639. end;
  640. end;
  641. end;
  642. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  643. begin
  644. if (idtoken=_LOCATION) then
  645. begin
  646. consume(_LOCATION);
  647. locationstr:=cstringpattern;
  648. consume(_CSTRING);
  649. end
  650. else
  651. begin
  652. if explicit_paraloc then
  653. Message(parser_e_paraloc_all_paras);
  654. locationstr:='';
  655. end;
  656. end
  657. else
  658. locationstr:='';
  659. { default parameter }
  660. if (m_default_para in current_settings.modeswitches) then
  661. begin
  662. if try_to_consume(_EQ) then
  663. begin
  664. vs:=tparavarsym(sc[0]);
  665. if sc.count>1 then
  666. Message(parser_e_default_value_only_one_para);
  667. { prefix 'def' to the parameter name }
  668. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo);
  669. if assigned(defaultvalue) then
  670. begin
  671. include(defaultvalue.symoptions,sp_internal);
  672. pd.parast.insert(defaultvalue);
  673. end;
  674. defaultrequired:=true;
  675. end
  676. else
  677. begin
  678. if defaultrequired then
  679. Message1(parser_e_default_value_expected_for_para,vs.name);
  680. end;
  681. end;
  682. end;
  683. end
  684. else
  685. hdef:=cformaltype;
  686. { File types are only allowed for var and out parameters }
  687. if (hdef.typ=filedef) and
  688. not(varspez in [vs_out,vs_var]) then
  689. CGMessage(cg_e_file_must_call_by_reference);
  690. { Dispinterfaces are restricted to using only automatable types }
  691. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  692. not is_automatable(hdef) then
  693. Message1(type_e_not_automatable,hdef.typename);
  694. { univ cannot be used with types whose size is not known at compile
  695. time }
  696. if is_univ and
  697. not is_valid_univ_para_type(hdef) then
  698. Message1(parser_e_invalid_univ_para,hdef.typename);
  699. for i:=0 to sc.count-1 do
  700. begin
  701. vs:=tparavarsym(sc[i]);
  702. vs.univpara:=is_univ;
  703. { update varsym }
  704. vs.vardef:=hdef;
  705. vs.defaultconstsym:=defaultvalue;
  706. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  707. begin
  708. if locationstr<>'' then
  709. begin
  710. if sc.count>1 then
  711. Message(parser_e_paraloc_only_one_para);
  712. if (paranr>1) and not(explicit_paraloc) then
  713. Message(parser_e_paraloc_all_paras);
  714. explicit_paraloc:=true;
  715. include(vs.varoptions,vo_has_explicit_paraloc);
  716. if not(paramanager.parseparaloc(vs,upper(locationstr))) then
  717. message(parser_e_illegal_explicit_paraloc);
  718. end
  719. else
  720. if explicit_paraloc then
  721. Message(parser_e_paraloc_all_paras);
  722. end;
  723. end;
  724. until not try_to_consume(_SEMICOLON);
  725. if explicit_paraloc then
  726. begin
  727. pd.has_paraloc_info:=callerside;
  728. include(pd.procoptions,po_explicitparaloc);
  729. end;
  730. { remove parasymtable from stack }
  731. sc.free;
  732. { reset object options }
  733. block_type:=old_block_type;
  734. consume(_RKLAMMER);
  735. end;
  736. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;out pd:tprocdef):boolean;
  737. var
  738. hs : string;
  739. orgsp,sp : TIDString;
  740. srsym : tsym;
  741. checkstack : psymtablestackitem;
  742. procstartfilepos : tfileposinfo;
  743. searchagain : boolean;
  744. st,
  745. genericst: TSymtable;
  746. aprocsym : tprocsym;
  747. popclass : integer;
  748. ImplIntf : TImplementedInterface;
  749. old_parse_generic : boolean;
  750. old_current_structdef: tabstractrecorddef;
  751. old_current_genericdef,
  752. old_current_specializedef: tstoreddef;
  753. lasttoken,lastidtoken: ttoken;
  754. procedure parse_operator_name;
  755. begin
  756. if (lasttoken in [first_overloaded..last_overloaded]) then
  757. begin
  758. optoken:=token;
  759. end
  760. else
  761. begin
  762. case lasttoken of
  763. _CARET:
  764. Message1(parser_e_overload_operator_failed,'**');
  765. _ID:
  766. case lastidtoken of
  767. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  768. _EXPLICIT:optoken:=_OP_EXPLICIT;
  769. _INC:optoken:=_OP_INC;
  770. _DEC:optoken:=_OP_DEC;
  771. else
  772. if (m_delphi in current_settings.modeswitches) then
  773. case lastidtoken of
  774. _IMPLICIT:optoken:=_ASSIGNMENT;
  775. _NEGATIVE:optoken:=_MINUS;
  776. _POSITIVE:optoken:=_PLUS;
  777. _LOGICALNOT:optoken:=_OP_NOT;
  778. _IN:optoken:=_OP_IN;
  779. _EQUAL:optoken:=_EQ;
  780. _NOTEQUAL:optoken:=_NE;
  781. _GREATERTHAN:optoken:=_GT;
  782. _GREATERTHANOREQUAL:optoken:=_GTE;
  783. _LESSTHAN:optoken:=_LT;
  784. _LESSTHANOREQUAL:optoken:=_LTE;
  785. _ADD:optoken:=_PLUS;
  786. _SUBTRACT:optoken:=_MINUS;
  787. _MULTIPLY:optoken:=_STAR;
  788. _DIVIDE:optoken:=_SLASH;
  789. _INTDIVIDE:optoken:=_OP_DIV;
  790. _MODULUS:optoken:=_OP_MOD;
  791. _LEFTSHIFT:optoken:=_OP_SHL;
  792. _RIGHTSHIFT:optoken:=_OP_SHR;
  793. _LOGICALAND:optoken:=_OP_AND;
  794. _LOGICALOR:optoken:=_OP_OR;
  795. _LOGICALXOR:optoken:=_OP_XOR;
  796. _BITWISEAND:optoken:=_OP_AND;
  797. _BITWISEOR:optoken:=_OP_OR;
  798. _BITWISEXOR:optoken:=_OP_XOR;
  799. else
  800. Message1(parser_e_overload_operator_failed,'');
  801. end
  802. else
  803. Message1(parser_e_overload_operator_failed,'');
  804. end
  805. else
  806. Message1(parser_e_overload_operator_failed,'');
  807. end;
  808. end;
  809. sp:=overloaded_names[optoken];
  810. orgsp:=sp;
  811. end;
  812. procedure consume_proc_name;
  813. begin
  814. lasttoken:=token;
  815. lastidtoken:=idtoken;
  816. if potype=potype_operator then
  817. optoken:=NOTOKEN;
  818. if (potype=potype_operator) and (token<>_ID) then
  819. begin
  820. parse_operator_name;
  821. consume(token);
  822. end
  823. else
  824. begin
  825. sp:=pattern;
  826. orgsp:=orgpattern;
  827. consume(_ID);
  828. end;
  829. end;
  830. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  831. var
  832. storepos:tfileposinfo;
  833. srsymtable:TSymtable;
  834. begin
  835. storepos:=current_tokenpos;
  836. current_tokenpos:=procstartfilepos;
  837. searchsym(sp,result,srsymtable);
  838. if not assigned(result) then
  839. begin
  840. if gen_error then
  841. identifier_not_found(orgsp);
  842. result:=generrorsym;
  843. end;
  844. current_tokenpos:=storepos;
  845. end;
  846. function consume_generic_type_parameter:boolean;
  847. var
  848. i:integer;
  849. ok:boolean;
  850. sym:tsym;
  851. begin
  852. result:=not assigned(astruct)and(m_delphi in current_settings.modeswitches);
  853. if result then
  854. begin
  855. { a generic type parameter? }
  856. srsym:=search_object_name(sp,false);
  857. if (srsym.typ=typesym) and
  858. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  859. begin
  860. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  861. if (df_generic in astruct.defoptions) and try_to_consume(_LT) then
  862. begin
  863. ok:=true;
  864. i:=0;
  865. repeat
  866. if ok and (token=_ID) then
  867. begin
  868. ok:=false;
  869. while i<astruct.symtable.SymList.Count-1 do
  870. begin
  871. sym:=tsym(astruct.symtable.SymList[i]);
  872. if sp_generic_para in sym.symoptions then
  873. begin
  874. ok:=sym.Name=pattern;
  875. inc(i);
  876. break;
  877. end;
  878. inc(i);
  879. end;
  880. if not ok then
  881. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  882. end;
  883. consume(_ID);
  884. until not try_to_consume(_COMMA);
  885. if ok then
  886. while i<astruct.symtable.SymList.Count-1 do
  887. begin
  888. sym:=tsym(astruct.symtable.SymList[i]);
  889. if sp_generic_para in sym.symoptions then
  890. begin
  891. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  892. break;
  893. end;
  894. inc(i);
  895. end;
  896. consume(_GT);
  897. end
  898. else
  899. if (df_generic in astruct.defoptions) and (token=_POINT) then
  900. begin
  901. Message1(type_e_generic_declaration_does_not_match,astruct.RttiName);
  902. end
  903. else
  904. begin
  905. { not a method. routine name just accidentally match some structure name }
  906. astruct:=nil;
  907. if try_to_consume(_LT) then
  908. begin
  909. Message(type_e_type_parameters_are_not_allowed_here);
  910. repeat
  911. consume(_ID);
  912. until not try_to_consume(_COMMA);
  913. consume(_GT);
  914. end;
  915. end;
  916. end;
  917. end;
  918. end;
  919. begin
  920. { Save the position where this procedure really starts }
  921. procstartfilepos:=current_tokenpos;
  922. old_parse_generic:=parse_generic;
  923. result:=false;
  924. pd:=nil;
  925. aprocsym:=nil;
  926. consume_proc_name;
  927. { examine interface map: function/procedure iname.functionname=locfuncname }
  928. if assigned(astruct) and
  929. (astruct.typ=objectdef) and
  930. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  931. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  932. try_to_consume(_POINT) then
  933. begin
  934. srsym:=search_object_name(sp,true);
  935. { qualifier is interface? }
  936. ImplIntf:=nil;
  937. if (srsym.typ=typesym) and
  938. (ttypesym(srsym).typedef.typ=objectdef) then
  939. ImplIntf:=tobjectdef(astruct).find_implemented_interface(tobjectdef(ttypesym(srsym).typedef));
  940. if ImplIntf=nil then
  941. Message(parser_e_interface_id_expected);
  942. { must be a directly implemented interface }
  943. if Assigned(ImplIntf.ImplementsGetter) then
  944. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  945. consume(_ID);
  946. { Create unique name <interface>.<method> }
  947. hs:=sp+'.'+pattern;
  948. consume(_EQ);
  949. if assigned(ImplIntf) and
  950. (token=_ID) then
  951. ImplIntf.AddMapping(hs,pattern);
  952. consume(_ID);
  953. result:=true;
  954. exit;
  955. end;
  956. { method ? }
  957. if (consume_generic_type_parameter or not assigned(astruct)) and
  958. (symtablestack.top.symtablelevel=main_program_level) and
  959. try_to_consume(_POINT) then
  960. begin
  961. repeat
  962. searchagain:=false;
  963. if not assigned(astruct) then
  964. srsym:=search_object_name(sp,true);
  965. { consume proc name }
  966. procstartfilepos:=current_tokenpos;
  967. consume_proc_name;
  968. { qualifier is class name ? }
  969. if (srsym.typ=typesym) and
  970. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  971. begin
  972. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  973. if (token<>_POINT) then
  974. if (potype in [potype_class_constructor,potype_class_destructor]) then
  975. sp:=lower(sp)
  976. else
  977. if (potype=potype_operator)and(optoken=NOTOKEN) then
  978. parse_operator_name;
  979. srsym:=tsym(astruct.symtable.Find(sp));
  980. if assigned(srsym) then
  981. begin
  982. if srsym.typ=procsym then
  983. aprocsym:=tprocsym(srsym)
  984. else
  985. if (srsym.typ=typesym) and
  986. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  987. begin
  988. searchagain:=true;
  989. consume(_POINT);
  990. end
  991. else
  992. begin
  993. { we use a different error message for tp7 so it looks more compatible }
  994. if (m_fpc in current_settings.modeswitches) then
  995. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  996. else
  997. Message(parser_e_methode_id_expected);
  998. { rename the name to an unique name to avoid an
  999. error when inserting the symbol in the symtable }
  1000. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  1001. end;
  1002. end
  1003. else
  1004. begin
  1005. Message(parser_e_methode_id_expected);
  1006. { recover by making it a normal procedure instead of method }
  1007. astruct:=nil;
  1008. end;
  1009. end
  1010. else
  1011. Message(parser_e_class_id_expected);
  1012. until not searchagain;
  1013. end
  1014. else
  1015. begin
  1016. { check for constructor/destructor/class operators which are not allowed here }
  1017. if (not parse_only) and
  1018. ((potype in [potype_constructor,potype_destructor,
  1019. potype_class_constructor,potype_class_destructor]) or
  1020. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  1021. Message(parser_e_only_methods_allowed);
  1022. repeat
  1023. searchagain:=false;
  1024. current_tokenpos:=procstartfilepos;
  1025. if (potype=potype_operator)and(optoken=NOTOKEN) then
  1026. parse_operator_name;
  1027. srsym:=tsym(symtablestack.top.Find(sp));
  1028. { Also look in the globalsymtable if we didn't found
  1029. the symbol in the localsymtable }
  1030. if not assigned(srsym) and
  1031. not(parse_only) and
  1032. (symtablestack.top=current_module.localsymtable) and
  1033. assigned(current_module.globalsymtable) then
  1034. srsym:=tsym(current_module.globalsymtable.Find(sp));
  1035. { Check if overloaded is a procsym }
  1036. if assigned(srsym) then
  1037. begin
  1038. if srsym.typ=procsym then
  1039. aprocsym:=tprocsym(srsym)
  1040. else
  1041. begin
  1042. { when the other symbol is a unit symbol then hide the unit
  1043. symbol, this is not supported in tp7 }
  1044. if not(m_tp7 in current_settings.modeswitches) and
  1045. (srsym.typ=unitsym) then
  1046. begin
  1047. HideSym(srsym);
  1048. searchagain:=true;
  1049. end
  1050. else
  1051. begin
  1052. { we use a different error message for tp7 so it looks more compatible }
  1053. if (m_fpc in current_settings.modeswitches) then
  1054. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  1055. else
  1056. Message1(sym_e_duplicate_id,srsym.realname);
  1057. { rename the name to an unique name to avoid an
  1058. error when inserting the symbol in the symtable }
  1059. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  1060. end;
  1061. end;
  1062. end;
  1063. until not searchagain;
  1064. end;
  1065. { test again if assigned, it can be reset to recover }
  1066. if not assigned(aprocsym) then
  1067. begin
  1068. { create a new procsym and set the real filepos }
  1069. current_tokenpos:=procstartfilepos;
  1070. { for operator we have only one procsym for each overloaded
  1071. operation }
  1072. if (potype=potype_operator) then
  1073. begin
  1074. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  1075. if aprocsym=nil then
  1076. aprocsym:=tprocsym.create('$'+sp);
  1077. end
  1078. else
  1079. if (potype in [potype_class_constructor,potype_class_destructor]) then
  1080. aprocsym:=tprocsym.create('$'+lower(sp))
  1081. else
  1082. aprocsym:=tprocsym.create(orgsp);
  1083. symtablestack.top.insert(aprocsym);
  1084. end;
  1085. { to get the correct symtablelevel we must ignore ObjectSymtables }
  1086. st:=nil;
  1087. checkstack:=symtablestack.stack;
  1088. while assigned(checkstack) do
  1089. begin
  1090. st:=checkstack^.symtable;
  1091. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1092. break;
  1093. checkstack:=checkstack^.next;
  1094. end;
  1095. pd:=tprocdef.create(st.symtablelevel+1);
  1096. pd.struct:=astruct;
  1097. pd.procsym:=aprocsym;
  1098. pd.proctypeoption:=potype;
  1099. { methods inherit df_generic or df_specialization from the objectdef }
  1100. if assigned(pd.struct) and
  1101. (pd.parast.symtablelevel=normal_function_level) then
  1102. begin
  1103. if (df_generic in pd.struct.defoptions) then
  1104. begin
  1105. include(pd.defoptions,df_generic);
  1106. parse_generic:=true;
  1107. end;
  1108. if (df_specialization in pd.struct.defoptions) then
  1109. begin
  1110. include(pd.defoptions,df_specialization);
  1111. { Find corresponding genericdef, we need it later to
  1112. replay the tokens to generate the body }
  1113. if not assigned(pd.struct.genericdef) then
  1114. internalerror(200512113);
  1115. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  1116. if not assigned(genericst) then
  1117. internalerror(200512114);
  1118. { We are parsing the same objectdef, the def index numbers
  1119. are the same }
  1120. pd.genericdef:=tstoreddef(genericst.DefList[pd.owner.DefList.IndexOf(pd)]);
  1121. if not assigned(pd.genericdef) or
  1122. (pd.genericdef.typ<>procdef) then
  1123. internalerror(200512115);
  1124. end;
  1125. end;
  1126. { methods need to be exported }
  1127. if assigned(astruct) and
  1128. (
  1129. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  1130. (symtablestack.top.symtablelevel=main_program_level)
  1131. ) then
  1132. include(pd.procoptions,po_global);
  1133. { symbol options that need to be kept per procdef }
  1134. pd.fileinfo:=procstartfilepos;
  1135. pd.visibility:=symtablestack.top.currentvisibility;
  1136. if symtablestack.top.currentlyoptional then
  1137. include(pd.procoptions,po_optional);
  1138. { parse parameters }
  1139. if token=_LKLAMMER then
  1140. begin
  1141. { Add ObjectSymtable to be able to find nested type definitions }
  1142. popclass:=0;
  1143. if assigned(pd.struct) and
  1144. (pd.parast.symtablelevel=normal_function_level) and
  1145. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1146. begin
  1147. popclass:=push_nested_hierarchy(pd.struct);
  1148. old_current_structdef:=current_structdef;
  1149. old_current_genericdef:=current_genericdef;
  1150. old_current_specializedef:=current_specializedef;
  1151. current_structdef:=pd.struct;
  1152. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1153. current_genericdef:=current_structdef;
  1154. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1155. current_specializedef:=current_structdef;
  1156. end;
  1157. { Add parameter symtable }
  1158. if pd.parast.symtabletype<>staticsymtable then
  1159. symtablestack.push(pd.parast);
  1160. parse_parameter_dec(pd);
  1161. if pd.parast.symtabletype<>staticsymtable then
  1162. symtablestack.pop(pd.parast);
  1163. if popclass>0 then
  1164. begin
  1165. current_structdef:=old_current_structdef;
  1166. current_genericdef:=old_current_genericdef;
  1167. current_specializedef:=old_current_specializedef;
  1168. dec(popclass,pop_nested_hierarchy(pd.struct));
  1169. if popclass<>0 then
  1170. internalerror(201011260); // 11 nov 2010 index 0
  1171. end;
  1172. end;
  1173. parse_generic:=old_parse_generic;
  1174. result:=true;
  1175. end;
  1176. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef):tprocdef;
  1177. var
  1178. pd: tprocdef;
  1179. locationstr: string;
  1180. i: integer;
  1181. found: boolean;
  1182. procedure read_returndef(pd: tprocdef);
  1183. var
  1184. popclass: integer;
  1185. old_parse_generic: boolean;
  1186. old_current_structdef: tabstractrecorddef;
  1187. old_current_genericdef,
  1188. old_current_specializedef: tstoreddef;
  1189. begin
  1190. old_parse_generic:=parse_generic;
  1191. { Add ObjectSymtable to be able to find generic type definitions }
  1192. popclass:=0;
  1193. if assigned(pd.struct) and
  1194. (pd.parast.symtablelevel=normal_function_level) and
  1195. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1196. begin
  1197. popclass:=push_nested_hierarchy(pd.struct);
  1198. parse_generic:=(df_generic in pd.struct.defoptions);
  1199. old_current_structdef:=current_structdef;
  1200. old_current_genericdef:=current_genericdef;
  1201. old_current_specializedef:=current_specializedef;
  1202. current_structdef:=pd.struct;
  1203. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1204. current_genericdef:=current_structdef;
  1205. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1206. current_specializedef:=current_structdef;
  1207. end;
  1208. single_type(pd.returndef,[stoAllowSpecialization]);
  1209. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1210. Message1(type_e_not_automatable,pd.returndef.typename);
  1211. if popclass>0 then
  1212. begin
  1213. current_structdef:=old_current_structdef;
  1214. current_genericdef:=old_current_genericdef;
  1215. current_specializedef:=old_current_specializedef;
  1216. dec(popclass,pop_nested_hierarchy(pd.struct));
  1217. if popclass<>0 then
  1218. internalerror(201012020);
  1219. end;
  1220. parse_generic:=old_parse_generic;
  1221. end;
  1222. begin
  1223. locationstr:='';
  1224. pd:=nil;
  1225. case token of
  1226. _FUNCTION :
  1227. begin
  1228. consume(_FUNCTION);
  1229. if parse_proc_head(astruct,potype_function,pd) then
  1230. begin
  1231. { pd=nil when it is a interface mapping }
  1232. if assigned(pd) then
  1233. begin
  1234. if try_to_consume(_COLON) then
  1235. begin
  1236. read_returndef(pd);
  1237. if (target_info.system in [system_m68k_amiga]) then
  1238. begin
  1239. if (idtoken=_LOCATION) then
  1240. begin
  1241. if po_explicitparaloc in pd.procoptions then
  1242. begin
  1243. consume(_LOCATION);
  1244. locationstr:=cstringpattern;
  1245. consume(_CSTRING);
  1246. end
  1247. else
  1248. { I guess this needs a new message... (KB) }
  1249. Message(parser_e_paraloc_all_paras);
  1250. end
  1251. else
  1252. begin
  1253. if po_explicitparaloc in pd.procoptions then
  1254. { assign default locationstr, if none specified }
  1255. { and we've arguments with explicit paraloc }
  1256. locationstr:='D0';
  1257. end;
  1258. end;
  1259. end
  1260. else
  1261. begin
  1262. if (
  1263. parse_only and
  1264. not(is_interface(pd.struct))
  1265. ) or
  1266. (m_repeat_forward in current_settings.modeswitches) then
  1267. begin
  1268. consume(_COLON);
  1269. consume_all_until(_SEMICOLON);
  1270. end;
  1271. end;
  1272. if isclassmethod then
  1273. include(pd.procoptions,po_classmethod);
  1274. end;
  1275. end
  1276. else
  1277. begin
  1278. { recover }
  1279. consume(_COLON);
  1280. consume_all_until(_SEMICOLON);
  1281. end;
  1282. end;
  1283. _PROCEDURE :
  1284. begin
  1285. consume(_PROCEDURE);
  1286. if parse_proc_head(astruct,potype_procedure,pd) then
  1287. begin
  1288. { pd=nil when it is an interface mapping }
  1289. if assigned(pd) then
  1290. begin
  1291. pd.returndef:=voidtype;
  1292. if isclassmethod then
  1293. include(pd.procoptions,po_classmethod);
  1294. end;
  1295. end;
  1296. end;
  1297. _CONSTRUCTOR :
  1298. begin
  1299. consume(_CONSTRUCTOR);
  1300. if isclassmethod then
  1301. parse_proc_head(astruct,potype_class_constructor,pd)
  1302. else
  1303. parse_proc_head(astruct,potype_constructor,pd);
  1304. if not isclassmethod and
  1305. assigned(pd) and
  1306. assigned(pd.struct) then
  1307. begin
  1308. { Set return type, class constructors return the
  1309. created instance, object constructors return boolean }
  1310. if is_class(pd.struct) or
  1311. is_record(pd.struct) or
  1312. is_javaclass(pd.struct) then
  1313. pd.returndef:=pd.struct
  1314. else
  1315. {$ifdef CPU64bitaddr}
  1316. pd.returndef:=bool64type;
  1317. {$else CPU64bitaddr}
  1318. pd.returndef:=bool32type;
  1319. {$endif CPU64bitaddr}
  1320. end
  1321. else
  1322. pd.returndef:=voidtype;
  1323. end;
  1324. _DESTRUCTOR :
  1325. begin
  1326. consume(_DESTRUCTOR);
  1327. if isclassmethod then
  1328. parse_proc_head(astruct,potype_class_destructor,pd)
  1329. else
  1330. parse_proc_head(astruct,potype_destructor,pd);
  1331. if assigned(pd) then
  1332. pd.returndef:=voidtype;
  1333. end;
  1334. else
  1335. if (token=_OPERATOR) or
  1336. (isclassmethod and (idtoken=_OPERATOR)) then
  1337. begin
  1338. consume(_OPERATOR);
  1339. parse_proc_head(astruct,potype_operator,pd);
  1340. if assigned(pd) then
  1341. begin
  1342. { operators always need to be searched in all units }
  1343. include(pd.procoptions,po_overload);
  1344. if pd.parast.symtablelevel>normal_function_level then
  1345. Message(parser_e_no_local_operator);
  1346. if isclassmethod then
  1347. include(pd.procoptions,po_classmethod);
  1348. if token<>_ID then
  1349. begin
  1350. if not(m_result in current_settings.modeswitches) then
  1351. consume(_ID);
  1352. end
  1353. else
  1354. begin
  1355. pd.resultname:=stringdup(orgpattern);
  1356. consume(_ID);
  1357. end;
  1358. if not try_to_consume(_COLON) then
  1359. begin
  1360. consume(_COLON);
  1361. pd.returndef:=generrordef;
  1362. consume_all_until(_SEMICOLON);
  1363. end
  1364. else
  1365. begin
  1366. read_returndef(pd);
  1367. { check that class operators have either return type of structure or }
  1368. { at least one argument of that type }
  1369. if (po_classmethod in pd.procoptions) and
  1370. (pd.returndef <> pd.struct) then
  1371. begin
  1372. found:=false;
  1373. for i := 0 to pd.parast.SymList.Count - 1 do
  1374. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1375. begin
  1376. found:=true;
  1377. break;
  1378. end;
  1379. if not found then
  1380. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName);
  1381. end;
  1382. if (optoken in [_EQ,_NE,_GT,_LT,_GTE,_LTE,_OP_IN]) and
  1383. ((pd.returndef.typ<>orddef) or
  1384. (torddef(pd.returndef).ordtype<>pasbool8)) then
  1385. Message(parser_e_comparative_operator_return_boolean);
  1386. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1387. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1388. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1389. message(parser_e_no_such_assignment)
  1390. else if not isoperatoracceptable(pd,optoken) then
  1391. Message(parser_e_overload_impossible);
  1392. end;
  1393. end
  1394. else
  1395. begin
  1396. { recover }
  1397. try_to_consume(_ID);
  1398. consume(_COLON);
  1399. consume_all_until(_SEMICOLON);
  1400. end;
  1401. end;
  1402. end;
  1403. { file types can't be function results }
  1404. if assigned(pd) and
  1405. (pd.returndef.typ=filedef) then
  1406. message(parser_e_illegal_function_result);
  1407. { support procedure proc stdcall export; }
  1408. if not(check_proc_directive(false)) then
  1409. begin
  1410. if (token=_COLON) then
  1411. begin
  1412. message(parser_e_field_not_allowed_here);
  1413. consume_all_until(_SEMICOLON);
  1414. end;
  1415. consume(_SEMICOLON);
  1416. end;
  1417. result:=pd;
  1418. if locationstr<>'' then
  1419. begin
  1420. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1421. { I guess this needs a new message... (KB) }
  1422. message(parser_e_illegal_explicit_paraloc);
  1423. end;
  1424. end;
  1425. {****************************************************************************
  1426. Procedure directive handlers
  1427. ****************************************************************************}
  1428. procedure pd_far(pd:tabstractprocdef);
  1429. begin
  1430. Message1(parser_w_proc_directive_ignored,'FAR');
  1431. end;
  1432. procedure pd_near(pd:tabstractprocdef);
  1433. begin
  1434. Message1(parser_w_proc_directive_ignored,'NEAR');
  1435. end;
  1436. procedure pd_export(pd:tabstractprocdef);
  1437. begin
  1438. if pd.typ<>procdef then
  1439. internalerror(200304264);
  1440. if assigned(tprocdef(pd).struct) then
  1441. Message(parser_e_methods_dont_be_export);
  1442. if pd.parast.symtablelevel>normal_function_level then
  1443. Message(parser_e_dont_nest_export);
  1444. end;
  1445. procedure pd_forward(pd:tabstractprocdef);
  1446. begin
  1447. if pd.typ<>procdef then
  1448. internalerror(200304265);
  1449. tprocdef(pd).forwarddef:=true;
  1450. end;
  1451. procedure pd_alias(pd:tabstractprocdef);
  1452. begin
  1453. if pd.typ<>procdef then
  1454. internalerror(200304266);
  1455. consume(_COLON);
  1456. tprocdef(pd).aliasnames.insert(get_stringconst);
  1457. include(pd.procoptions,po_has_public_name);
  1458. end;
  1459. procedure pd_public(pd:tabstractprocdef);
  1460. begin
  1461. if pd.typ<>procdef then
  1462. internalerror(200304266);
  1463. if try_to_consume(_NAME) then
  1464. begin
  1465. tprocdef(pd).aliasnames.insert(get_stringconst);
  1466. include(pd.procoptions,po_has_public_name);
  1467. end;
  1468. end;
  1469. procedure pd_asmname(pd:tabstractprocdef);
  1470. begin
  1471. if pd.typ<>procdef then
  1472. internalerror(200304267);
  1473. if token=_CCHAR then
  1474. begin
  1475. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1476. consume(_CCHAR)
  1477. end
  1478. else
  1479. begin
  1480. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1481. consume(_CSTRING);
  1482. end;
  1483. { we don't need anything else }
  1484. tprocdef(pd).forwarddef:=false;
  1485. end;
  1486. procedure pd_internconst(pd:tabstractprocdef);
  1487. var v:Tconstexprint;
  1488. begin
  1489. if pd.typ<>procdef then
  1490. internalerror(200304268);
  1491. consume(_COLON);
  1492. v:=get_intconst;
  1493. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1494. message(parser_e_range_check_error)
  1495. else
  1496. Tprocdef(pd).extnumber:=longint(v.svalue);
  1497. end;
  1498. procedure pd_internproc(pd:tabstractprocdef);
  1499. var v:Tconstexprint;
  1500. begin
  1501. if pd.typ<>procdef then
  1502. internalerror(200304268);
  1503. consume(_COLON);
  1504. v:=get_intconst;
  1505. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1506. message(parser_e_range_check_error)
  1507. else
  1508. Tprocdef(pd).extnumber:=longint(v.svalue);
  1509. { the proc is defined }
  1510. tprocdef(pd).forwarddef:=false;
  1511. end;
  1512. procedure pd_interrupt(pd:tabstractprocdef);
  1513. var v: Tconstexprint;
  1514. begin
  1515. if pd.parast.symtablelevel>normal_function_level then
  1516. Message(parser_e_dont_nest_interrupt);
  1517. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1518. if target_info.system in systems_interrupt_table then
  1519. begin
  1520. if token<>_SEMICOLON then
  1521. begin
  1522. pd.proccalloption:=pocall_interrupt;
  1523. v:=get_intconst;
  1524. Tprocdef(pd).interruptvector:=v.uvalue;
  1525. end;
  1526. end;
  1527. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1528. end;
  1529. procedure pd_abstract(pd:tabstractprocdef);
  1530. begin
  1531. if pd.typ<>procdef then
  1532. internalerror(200304269);
  1533. if is_objectpascal_helper(tprocdef(pd).struct) then
  1534. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1535. if assigned(tprocdef(pd).struct) and
  1536. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1537. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1538. else if (po_virtualmethod in pd.procoptions) then
  1539. include(pd.procoptions,po_abstractmethod)
  1540. else
  1541. Message(parser_e_only_virtual_methods_abstract);
  1542. { the method is defined }
  1543. tprocdef(pd).forwarddef:=false;
  1544. end;
  1545. procedure pd_final(pd:tabstractprocdef);
  1546. begin
  1547. if pd.typ<>procdef then
  1548. internalerror(200910170);
  1549. if is_objectpascal_helper(tprocdef(pd).struct) and
  1550. (m_objfpc in current_settings.modeswitches) then
  1551. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1552. if (po_virtualmethod in pd.procoptions) then
  1553. include(pd.procoptions,po_finalmethod)
  1554. else
  1555. Message(parser_e_only_virtual_methods_final);
  1556. end;
  1557. procedure pd_enumerator(pd:tabstractprocdef);
  1558. begin
  1559. if pd.typ<>procdef then
  1560. internalerror(200910250);
  1561. if (token = _ID) then
  1562. begin
  1563. if pattern='MOVENEXT' then
  1564. begin
  1565. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1566. message(parser_e_only_one_enumerator_movenext);
  1567. pd.calcparas;
  1568. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1569. (pd.minparacount = 0) then
  1570. begin
  1571. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1572. include(pd.procoptions,po_enumerator_movenext);
  1573. end
  1574. else
  1575. Message(parser_e_enumerator_movenext_is_not_valid)
  1576. end
  1577. else
  1578. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1579. consume(token);
  1580. end
  1581. else
  1582. Message(parser_e_enumerator_identifier_required);
  1583. end;
  1584. procedure pd_virtual(pd:tabstractprocdef);
  1585. {$ifdef WITHDMT}
  1586. var
  1587. pt : tnode;
  1588. {$endif WITHDMT}
  1589. begin
  1590. if (not assigned(pd.owner.defowner) or
  1591. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1592. (po_external in pd.procoptions) then
  1593. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1594. if pd.typ<>procdef then
  1595. internalerror(2003042610);
  1596. if (pd.proctypeoption=potype_constructor) and
  1597. is_object(tprocdef(pd).struct) then
  1598. Message(parser_e_constructor_cannot_be_not_virtual);
  1599. if is_objectpascal_helper(tprocdef(pd).struct) and
  1600. (m_objfpc in current_settings.modeswitches) then
  1601. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1602. {$ifdef WITHDMT}
  1603. if is_object(tprocdef(pd).struct) and
  1604. (token<>_SEMICOLON) then
  1605. begin
  1606. { any type of parameter is allowed here! }
  1607. pt:=comp_expr(true);
  1608. if is_constintnode(pt) then
  1609. begin
  1610. include(pd.procoptions,po_msgint);
  1611. pd.messageinf.i:=pt.value;
  1612. end
  1613. else
  1614. Message(parser_e_ill_msg_expr);
  1615. disposetree(pt);
  1616. end;
  1617. {$endif WITHDMT}
  1618. end;
  1619. procedure pd_dispid(pd:tabstractprocdef);
  1620. var pt:Tnode;
  1621. begin
  1622. if pd.typ<>procdef then
  1623. internalerror(200604301);
  1624. pt:=comp_expr(true,false);
  1625. if is_constintnode(pt) then
  1626. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1627. message(parser_e_range_check_error)
  1628. else
  1629. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1630. else
  1631. message(parser_e_dispid_must_be_ord_const);
  1632. pt.free;
  1633. end;
  1634. procedure pd_static(pd:tabstractprocdef);
  1635. begin
  1636. if pd.typ=procdef then
  1637. include(tprocdef(pd).procsym.symoptions,sp_static);
  1638. include(pd.procoptions,po_staticmethod);
  1639. end;
  1640. procedure pd_override(pd:tabstractprocdef);
  1641. begin
  1642. if pd.typ<>procdef then
  1643. internalerror(2003042611);
  1644. if is_objectpascal_helper(tprocdef(pd).struct) then
  1645. begin
  1646. if m_objfpc in current_settings.modeswitches then
  1647. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1648. end
  1649. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1650. Message(parser_e_no_object_override)
  1651. else if is_objccategory(tprocdef(pd).struct) then
  1652. Message(parser_e_no_category_override)
  1653. else if (po_external in pd.procoptions) and
  1654. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1655. not is_cppclass(tprocdef(pd).struct) and
  1656. not is_java_class_or_interface(tprocdef(pd).struct) then
  1657. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1658. end;
  1659. procedure pd_overload(pd:tabstractprocdef);
  1660. begin
  1661. if pd.typ<>procdef then
  1662. internalerror(2003042612);
  1663. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1664. end;
  1665. procedure pd_message(pd:tabstractprocdef);
  1666. var
  1667. pt : tnode;
  1668. paracnt : longint;
  1669. begin
  1670. if pd.typ<>procdef then
  1671. internalerror(2003042613);
  1672. if is_objectpascal_helper(tprocdef(pd).struct) then
  1673. begin
  1674. if m_objfpc in current_settings.modeswitches then
  1675. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1676. end
  1677. else
  1678. if not is_class(tprocdef(pd).struct) and
  1679. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1680. Message(parser_e_msg_only_for_classes);
  1681. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1682. Message(parser_e_multiple_messages);
  1683. { check parameter type }
  1684. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1685. begin
  1686. if po_external in pd.procoptions then
  1687. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1688. paracnt:=0;
  1689. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1690. if paracnt<>1 then
  1691. Message(parser_e_ill_msg_param);
  1692. end;
  1693. pt:=comp_expr(true,false);
  1694. { message is 1-character long }
  1695. if is_constcharnode(pt) then
  1696. begin
  1697. include(pd.procoptions,po_msgstr);
  1698. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1699. end
  1700. else if pt.nodetype=stringconstn then
  1701. begin
  1702. include(pd.procoptions,po_msgstr);
  1703. if (tstringconstnode(pt).len>255) then
  1704. Message(parser_e_message_string_too_long);
  1705. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1706. end
  1707. else
  1708. if is_constintnode(pt) and
  1709. (is_class(tprocdef(pd).struct) or
  1710. is_objectpascal_helper(tprocdef(pd).struct)) then
  1711. begin
  1712. include(pd.procoptions,po_msgint);
  1713. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1714. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1715. message(parser_e_range_check_error)
  1716. else
  1717. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1718. end
  1719. else
  1720. Message(parser_e_ill_msg_expr);
  1721. { check whether the selector name is valid in case of Objective-C }
  1722. if (po_msgstr in pd.procoptions) and
  1723. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1724. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1725. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1726. pt.free;
  1727. end;
  1728. procedure pd_reintroduce(pd:tabstractprocdef);
  1729. begin
  1730. if pd.typ<>procdef then
  1731. internalerror(200401211);
  1732. if is_objectpascal_helper(tprocdef(pd).struct) then
  1733. begin
  1734. if m_objfpc in current_settings.modeswitches then
  1735. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1736. end
  1737. else
  1738. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1739. not(is_objccategory(tprocdef(pd).struct)) then
  1740. Message(parser_e_no_object_reintroduce);
  1741. end;
  1742. procedure pd_syscall(pd:tabstractprocdef);
  1743. {$if defined(powerpc) or defined(m68k)}
  1744. var
  1745. vs : tparavarsym;
  1746. sym : tsym;
  1747. symtable : TSymtable;
  1748. v: Tconstexprint;
  1749. {$endif defined(powerpc) or defined(m68k)}
  1750. begin
  1751. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1752. internalerror(2003042614);
  1753. tprocdef(pd).forwarddef:=false;
  1754. {$ifdef m68k}
  1755. if target_info.system in [system_m68k_amiga] then
  1756. begin
  1757. include(pd.procoptions,po_syscall_legacy);
  1758. if consume_sym(sym,symtable) then
  1759. begin
  1760. if (sym.typ=staticvarsym) and
  1761. (
  1762. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1763. is_32bitint(tabstractvarsym(sym).vardef)
  1764. ) then
  1765. begin
  1766. tprocdef(pd).libsym:=sym;
  1767. if po_syscall_legacy in tprocdef(pd).procoptions then
  1768. begin
  1769. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1770. paramanager.parseparaloc(vs,'A6');
  1771. pd.parast.insert(vs);
  1772. end
  1773. end
  1774. else
  1775. Message(parser_e_32bitint_or_pointer_variable_expected);
  1776. end;
  1777. (paramanager as tm68kparamanager).create_funcretloc_info(pd,calleeside);
  1778. (paramanager as tm68kparamanager).create_funcretloc_info(pd,callerside);
  1779. v:=get_intconst;
  1780. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1781. message(parser_e_range_check_error)
  1782. else
  1783. Tprocdef(pd).extnumber:=v.uvalue;
  1784. end;
  1785. {$endif m68k}
  1786. {$ifdef powerpc}
  1787. if target_info.system = system_powerpc_amiga then
  1788. begin
  1789. include(pd.procoptions,po_syscall_basesysv);
  1790. if consume_sym(sym,symtable) then
  1791. begin
  1792. if (sym.typ=staticvarsym) and
  1793. (
  1794. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1795. is_32bitint(tabstractvarsym(sym).vardef)
  1796. ) then
  1797. begin
  1798. tprocdef(pd).libsym:=sym;
  1799. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1800. pd.parast.insert(vs);
  1801. end
  1802. else
  1803. Message(parser_e_32bitint_or_pointer_variable_expected);
  1804. end;
  1805. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1806. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1807. v:=get_intconst;
  1808. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1809. message(parser_e_range_check_error)
  1810. else
  1811. Tprocdef(pd).extnumber:=v.uvalue;
  1812. end else
  1813. if target_info.system = system_powerpc_morphos then
  1814. begin
  1815. if idtoken=_LEGACY then
  1816. begin
  1817. consume(_LEGACY);
  1818. include(pd.procoptions,po_syscall_legacy);
  1819. end
  1820. else if idtoken=_SYSV then
  1821. begin
  1822. consume(_SYSV);
  1823. include(pd.procoptions,po_syscall_sysv);
  1824. end
  1825. else if idtoken=_BASESYSV then
  1826. begin
  1827. consume(_BASESYSV);
  1828. include(pd.procoptions,po_syscall_basesysv);
  1829. end
  1830. else if idtoken=_SYSVBASE then
  1831. begin
  1832. consume(_SYSVBASE);
  1833. include(pd.procoptions,po_syscall_sysvbase);
  1834. end
  1835. else if idtoken=_R12BASE then
  1836. begin
  1837. consume(_R12BASE);
  1838. include(pd.procoptions,po_syscall_r12base);
  1839. end
  1840. else
  1841. if syscall_convention='LEGACY' then
  1842. include(pd.procoptions,po_syscall_legacy)
  1843. else if syscall_convention='SYSV' then
  1844. include(pd.procoptions,po_syscall_sysv)
  1845. else if syscall_convention='BASESYSV' then
  1846. include(pd.procoptions,po_syscall_basesysv)
  1847. else if syscall_convention='SYSVBASE' then
  1848. include(pd.procoptions,po_syscall_sysvbase)
  1849. else if syscall_convention='R12BASE' then
  1850. include(pd.procoptions,po_syscall_r12base)
  1851. else
  1852. internalerror(2005010404);
  1853. if consume_sym(sym,symtable) then
  1854. begin
  1855. if (sym.typ=staticvarsym) and
  1856. (
  1857. (tabstractvarsym(sym).vardef.typ=pointerdef) or
  1858. is_32bitint(tabstractvarsym(sym).vardef)
  1859. ) then
  1860. begin
  1861. tprocdef(pd).libsym:=sym;
  1862. if po_syscall_legacy in tprocdef(pd).procoptions then
  1863. begin
  1864. vs:=tparavarsym.create('$syscalllib',paranr_syscall_legacy,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1865. paramanager.parseparaloc(vs,'A6');
  1866. pd.parast.insert(vs);
  1867. end
  1868. else if po_syscall_sysv in tprocdef(pd).procoptions then
  1869. begin
  1870. { Nothing to be done for sysv here for now, but this might change }
  1871. end
  1872. else if po_syscall_basesysv in tprocdef(pd).procoptions then
  1873. begin
  1874. vs:=tparavarsym.create('$syscalllib',paranr_syscall_basesysv,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1875. pd.parast.insert(vs);
  1876. end
  1877. else if po_syscall_sysvbase in tprocdef(pd).procoptions then
  1878. begin
  1879. vs:=tparavarsym.create('$syscalllib',paranr_syscall_sysvbase,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para]);
  1880. pd.parast.insert(vs);
  1881. end
  1882. else if po_syscall_r12base in tprocdef(pd).procoptions then
  1883. begin
  1884. vs:=tparavarsym.create('$syscalllib',paranr_syscall_r12base,vs_value,tabstractvarsym(sym).vardef,[vo_is_syscall_lib,vo_is_hidden_para,vo_has_explicit_paraloc]);
  1885. paramanager.parseparaloc(vs,'R12');
  1886. pd.parast.insert(vs);
  1887. end
  1888. else
  1889. internalerror(2005010501);
  1890. end
  1891. else
  1892. Message(parser_e_32bitint_or_pointer_variable_expected);
  1893. end;
  1894. (paramanager as tppcparamanager).create_funcretloc_info(pd,calleeside);
  1895. (paramanager as tppcparamanager).create_funcretloc_info(pd,callerside);
  1896. v:=get_intconst;
  1897. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  1898. message(parser_e_range_check_error)
  1899. else
  1900. Tprocdef(pd).extnumber:=v.uvalue;
  1901. end;
  1902. {$endif powerpc}
  1903. end;
  1904. procedure pd_external(pd:tabstractprocdef);
  1905. {
  1906. If import_dll=nil the procedure is assumed to be in another
  1907. object file. In that object file it should have the name to
  1908. which import_name is pointing to. Otherwise, the procedure is
  1909. assumed to be in the DLL to which import_dll is pointing to. In
  1910. that case either import_nr<>0 or import_name<>nil is true, so
  1911. the procedure is either imported by number or by name. (DM)
  1912. }
  1913. var
  1914. hs : string;
  1915. v:Tconstexprint;
  1916. is_java_external: boolean;
  1917. begin
  1918. if pd.typ<>procdef then
  1919. internalerror(2003042615);
  1920. { Allow specifying a separate external name for methods in external Java
  1921. because its identifier naming constraints are laxer than FPC's
  1922. (e.g., case sensitive).
  1923. Limitation: only allows specifying the symbol name and not the package name,
  1924. and only for external classes/interfaces }
  1925. is_java_external:=
  1926. (pd.typ=procdef) and
  1927. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  1928. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  1929. with tprocdef(pd) do
  1930. begin
  1931. forwarddef:=false;
  1932. { forbid local external procedures }
  1933. if parast.symtablelevel>normal_function_level then
  1934. Message(parser_e_no_local_proc_external);
  1935. { If the procedure should be imported from a DLL, a constant string follows.
  1936. This isn't really correct, an contant string expression follows
  1937. so we check if an semicolon follows, else a string constant have to
  1938. follow (FK) }
  1939. if not is_java_external and
  1940. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  1941. begin
  1942. { Always add library prefix and suffix to create an uniform name }
  1943. hs:=get_stringconst;
  1944. if ExtractFileExt(hs)='' then
  1945. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  1946. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  1947. hs:=target_info.sharedlibprefix+hs;
  1948. { the JVM expects java/lang/Object rather than java.lang.Object }
  1949. if target_info.system=system_jvm_java32 then
  1950. Replace(hs,'.','/');
  1951. import_dll:=stringdup(hs);
  1952. include(procoptions,po_has_importdll);
  1953. if (idtoken=_NAME) then
  1954. begin
  1955. consume(_NAME);
  1956. import_name:=stringdup(get_stringconst);
  1957. include(procoptions,po_has_importname);
  1958. if import_name^='' then
  1959. message(parser_e_empty_import_name);
  1960. end;
  1961. if (idtoken=_INDEX) then
  1962. begin
  1963. {After the word index follows the index number in the DLL.}
  1964. consume(_INDEX);
  1965. v:=get_intconst;
  1966. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  1967. message(parser_e_range_check_error)
  1968. else
  1969. import_nr:=longint(v.svalue);
  1970. end;
  1971. { default is to used the realname of the procedure }
  1972. if (import_nr=0) and not assigned(import_name) then
  1973. begin
  1974. import_name:=stringdup(procsym.realname);
  1975. include(procoptions,po_has_importname);
  1976. end;
  1977. end
  1978. else
  1979. begin
  1980. if (idtoken=_NAME) or
  1981. is_java_external then
  1982. begin
  1983. consume(_NAME);
  1984. import_name:=stringdup(get_stringconst);
  1985. include(procoptions,po_has_importname);
  1986. if import_name^='' then
  1987. message(parser_e_empty_import_name);
  1988. end;
  1989. end;
  1990. end;
  1991. end;
  1992. procedure pd_weakexternal(pd:tabstractprocdef);
  1993. begin
  1994. if not(target_info.system in systems_weak_linking) then
  1995. message(parser_e_weak_external_not_supported)
  1996. else
  1997. pd_external(pd);
  1998. end;
  1999. type
  2000. pd_handler=procedure(pd:tabstractprocdef);
  2001. proc_dir_rec=record
  2002. idtok : ttoken;
  2003. pd_flags : tpdflags;
  2004. handler : pd_handler;
  2005. pocall : tproccalloption;
  2006. pooption : tprocoptions;
  2007. mutexclpocall : tproccalloptions;
  2008. mutexclpotype : tproctypeoptions;
  2009. mutexclpo : tprocoptions;
  2010. end;
  2011. const
  2012. {Should contain the number of procedure directives we support.}
  2013. num_proc_directives=42;
  2014. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2015. (
  2016. (
  2017. idtok:_ABSTRACT;
  2018. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2019. handler : @pd_abstract;
  2020. pocall : pocall_none;
  2021. pooption : [po_abstractmethod];
  2022. mutexclpocall : [pocall_internproc];
  2023. mutexclpotype : [];
  2024. mutexclpo : [po_exports,po_interrupt,po_inline]
  2025. ),(
  2026. idtok:_ALIAS;
  2027. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2028. handler : @pd_alias;
  2029. pocall : pocall_none;
  2030. pooption : [];
  2031. mutexclpocall : [];
  2032. mutexclpotype : [];
  2033. mutexclpo : [po_external,po_inline]
  2034. ),(
  2035. idtok:_ASMNAME;
  2036. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2037. handler : @pd_asmname;
  2038. pocall : pocall_cdecl;
  2039. pooption : [po_external];
  2040. mutexclpocall : [pocall_internproc];
  2041. mutexclpotype : [];
  2042. mutexclpo : [po_external,po_inline]
  2043. ),(
  2044. idtok:_ASSEMBLER;
  2045. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2046. handler : nil;
  2047. pocall : pocall_none;
  2048. pooption : [po_assembler];
  2049. mutexclpocall : [];
  2050. mutexclpotype : [];
  2051. mutexclpo : [po_external]
  2052. ),(
  2053. idtok:_C; {same as cdecl for mode mac}
  2054. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2055. handler : nil;
  2056. pocall : pocall_cdecl;
  2057. pooption : [];
  2058. mutexclpocall : [];
  2059. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2060. mutexclpo : [po_assembler,po_external]
  2061. ),(
  2062. idtok:_CDECL;
  2063. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2064. handler : nil;
  2065. pocall : pocall_cdecl;
  2066. pooption : [];
  2067. mutexclpocall : [];
  2068. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2069. mutexclpo : [po_assembler,po_external]
  2070. ),(
  2071. idtok:_DISPID;
  2072. pd_flags : [pd_dispinterface];
  2073. handler : @pd_dispid;
  2074. pocall : pocall_none;
  2075. pooption : [po_dispid];
  2076. mutexclpocall : [pocall_internproc];
  2077. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2078. mutexclpo : [po_interrupt,po_external,po_inline]
  2079. ),(
  2080. idtok:_DYNAMIC;
  2081. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2082. handler : @pd_virtual;
  2083. pocall : pocall_none;
  2084. pooption : [po_virtualmethod];
  2085. mutexclpocall : [pocall_internproc];
  2086. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2087. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2088. ),(
  2089. idtok:_EXPORT;
  2090. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2091. handler : @pd_export;
  2092. pocall : pocall_none;
  2093. pooption : [po_exports,po_global];
  2094. mutexclpocall : [pocall_internproc];
  2095. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2096. mutexclpo : [po_external,po_interrupt,po_inline]
  2097. ),(
  2098. idtok:_EXTERNAL;
  2099. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2100. handler : @pd_external;
  2101. pocall : pocall_none;
  2102. pooption : [po_external];
  2103. mutexclpocall : [pocall_internproc,pocall_syscall];
  2104. { allowed for external cpp classes }
  2105. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2106. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2107. ),(
  2108. idtok:_FAR;
  2109. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2110. handler : @pd_far;
  2111. pocall : pocall_none;
  2112. pooption : [];
  2113. mutexclpocall : [pocall_internproc];
  2114. mutexclpotype : [];
  2115. mutexclpo : [po_inline]
  2116. ),(
  2117. idtok:_FAR16;
  2118. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2119. handler : nil;
  2120. pocall : pocall_far16;
  2121. pooption : [];
  2122. mutexclpocall : [];
  2123. mutexclpotype : [];
  2124. mutexclpo : [po_external]
  2125. ),(
  2126. idtok:_FINAL;
  2127. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2128. handler : @pd_final;
  2129. pocall : pocall_none;
  2130. pooption : [po_finalmethod];
  2131. mutexclpocall : [pocall_internproc];
  2132. mutexclpotype : [];
  2133. mutexclpo : [po_exports,po_interrupt,po_inline]
  2134. ),(
  2135. idtok:_FORWARD;
  2136. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2137. handler : @pd_forward;
  2138. pocall : pocall_none;
  2139. pooption : [];
  2140. mutexclpocall : [pocall_internproc];
  2141. mutexclpotype : [];
  2142. mutexclpo : [po_external,po_inline]
  2143. ),(
  2144. idtok:_OLDFPCCALL;
  2145. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2146. handler : nil;
  2147. pocall : pocall_oldfpccall;
  2148. pooption : [];
  2149. mutexclpocall : [];
  2150. mutexclpotype : [];
  2151. mutexclpo : []
  2152. ),(
  2153. idtok:_INLINE;
  2154. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2155. handler : nil;
  2156. pocall : pocall_none;
  2157. pooption : [po_inline];
  2158. mutexclpocall : [];
  2159. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2160. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod]
  2161. ),(
  2162. idtok:_INTERNCONST;
  2163. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2164. handler : @pd_internconst;
  2165. pocall : pocall_none;
  2166. pooption : [po_internconst];
  2167. mutexclpocall : [];
  2168. mutexclpotype : [potype_operator];
  2169. mutexclpo : []
  2170. ),(
  2171. idtok:_INTERNPROC;
  2172. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2173. handler : @pd_internproc;
  2174. pocall : pocall_internproc;
  2175. pooption : [];
  2176. mutexclpocall : [];
  2177. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2178. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2179. ),(
  2180. idtok:_INTERRUPT;
  2181. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2182. handler : @pd_interrupt;
  2183. pocall : pocall_oldfpccall;
  2184. pooption : [po_interrupt];
  2185. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,
  2186. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2187. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2188. mutexclpo : [po_external,po_inline]
  2189. ),(
  2190. idtok:_IOCHECK;
  2191. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2192. handler : nil;
  2193. pocall : pocall_none;
  2194. pooption : [po_iocheck];
  2195. mutexclpocall : [pocall_internproc];
  2196. mutexclpotype : [];
  2197. mutexclpo : [po_external]
  2198. ),(
  2199. idtok:_LOCAL;
  2200. pd_flags : [pd_implemen,pd_body];
  2201. handler : nil;
  2202. pocall : pocall_none;
  2203. pooption : [po_kylixlocal];
  2204. mutexclpocall : [pocall_internproc,pocall_far16];
  2205. mutexclpotype : [];
  2206. mutexclpo : [po_external,po_exports]
  2207. ),(
  2208. idtok:_MESSAGE;
  2209. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2210. handler : @pd_message;
  2211. pocall : pocall_none;
  2212. pooption : []; { can be po_msgstr or po_msgint }
  2213. mutexclpocall : [pocall_internproc];
  2214. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2215. mutexclpo : [po_interrupt,po_inline]
  2216. ),(
  2217. idtok:_MWPASCAL;
  2218. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2219. handler : nil;
  2220. pocall : pocall_mwpascal;
  2221. pooption : [];
  2222. mutexclpocall : [];
  2223. mutexclpotype : [];
  2224. mutexclpo : []
  2225. ),(
  2226. idtok:_NEAR;
  2227. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2228. handler : @pd_near;
  2229. pocall : pocall_none;
  2230. pooption : [];
  2231. mutexclpocall : [pocall_internproc];
  2232. mutexclpotype : [];
  2233. mutexclpo : []
  2234. ),(
  2235. idtok:_NOSTACKFRAME;
  2236. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2237. handler : nil;
  2238. pocall : pocall_none;
  2239. pooption : [po_nostackframe];
  2240. mutexclpocall : [pocall_internproc];
  2241. mutexclpotype : [];
  2242. mutexclpo : []
  2243. ),(
  2244. idtok:_OVERLOAD;
  2245. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava];
  2246. handler : @pd_overload;
  2247. pocall : pocall_none;
  2248. pooption : [po_overload];
  2249. mutexclpocall : [pocall_internproc];
  2250. mutexclpotype : [];
  2251. mutexclpo : []
  2252. ),(
  2253. idtok:_OVERRIDE;
  2254. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2255. handler : @pd_override;
  2256. pocall : pocall_none;
  2257. pooption : [po_overridingmethod,po_virtualmethod];
  2258. mutexclpocall : [pocall_internproc];
  2259. mutexclpotype : [];
  2260. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2261. ),(
  2262. idtok:_PASCAL;
  2263. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2264. handler : nil;
  2265. pocall : pocall_pascal;
  2266. pooption : [];
  2267. mutexclpocall : [];
  2268. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2269. mutexclpo : [po_external]
  2270. ),(
  2271. idtok:_PUBLIC;
  2272. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2273. handler : @pd_public;
  2274. pocall : pocall_none;
  2275. pooption : [po_public,po_global];
  2276. mutexclpocall : [pocall_internproc];
  2277. mutexclpotype : [];
  2278. mutexclpo : [po_external,po_inline]
  2279. ),(
  2280. idtok:_REGISTER;
  2281. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2282. handler : nil;
  2283. pocall : pocall_register;
  2284. pooption : [];
  2285. mutexclpocall : [];
  2286. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2287. mutexclpo : [po_external]
  2288. ),(
  2289. idtok:_REINTRODUCE;
  2290. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord];
  2291. handler : @pd_reintroduce;
  2292. pocall : pocall_none;
  2293. pooption : [po_reintroduce];
  2294. mutexclpocall : [pocall_internproc];
  2295. mutexclpotype : [];
  2296. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2297. ),(
  2298. idtok:_SAFECALL;
  2299. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2300. handler : nil;
  2301. pocall : pocall_safecall;
  2302. pooption : [];
  2303. mutexclpocall : [];
  2304. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2305. mutexclpo : [po_external]
  2306. ),(
  2307. idtok:_SOFTFLOAT;
  2308. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2309. handler : nil;
  2310. pocall : pocall_softfloat;
  2311. pooption : [];
  2312. mutexclpocall : [];
  2313. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2314. { it's available with po_external because the libgcc floating point routines on the arm
  2315. uses this calling convention }
  2316. mutexclpo : []
  2317. ),(
  2318. idtok:_STATIC;
  2319. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2320. handler : @pd_static;
  2321. pocall : pocall_none;
  2322. pooption : [po_staticmethod];
  2323. mutexclpocall : [pocall_internproc];
  2324. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2325. mutexclpo : [po_interrupt,po_exports]
  2326. ),(
  2327. idtok:_STDCALL;
  2328. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2329. handler : nil;
  2330. pocall : pocall_stdcall;
  2331. pooption : [];
  2332. mutexclpocall : [];
  2333. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2334. mutexclpo : [po_external]
  2335. ),(
  2336. idtok:_SYSCALL;
  2337. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2338. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2339. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2340. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2341. handler : @pd_syscall;
  2342. pocall : pocall_syscall;
  2343. pooption : [];
  2344. mutexclpocall : [];
  2345. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2346. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2347. ),(
  2348. idtok:_VIRTUAL;
  2349. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2350. handler : @pd_virtual;
  2351. pocall : pocall_none;
  2352. pooption : [po_virtualmethod];
  2353. mutexclpocall : [pocall_internproc];
  2354. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2355. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2356. ),(
  2357. idtok:_CPPDECL;
  2358. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2359. handler : nil;
  2360. pocall : pocall_cppdecl;
  2361. pooption : [];
  2362. mutexclpocall : [];
  2363. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2364. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2365. ),(
  2366. idtok:_VARARGS;
  2367. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2368. handler : nil;
  2369. pocall : pocall_none;
  2370. pooption : [po_varargs];
  2371. mutexclpocall : [pocall_internproc,pocall_stdcall,pocall_register,
  2372. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2373. mutexclpotype : [];
  2374. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2375. ),(
  2376. idtok:_COMPILERPROC;
  2377. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2378. handler : nil;
  2379. pocall : pocall_none;
  2380. pooption : [po_compilerproc];
  2381. mutexclpocall : [];
  2382. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2383. mutexclpo : [po_interrupt]
  2384. ),(
  2385. idtok:_WEAKEXTERNAL;
  2386. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2387. handler : @pd_weakexternal;
  2388. pocall : pocall_none;
  2389. { mark it both external and weak external, so we don't have to
  2390. adapt all code for external symbols to also check for weak external
  2391. }
  2392. pooption : [po_external,po_weakexternal];
  2393. mutexclpocall : [pocall_internproc,pocall_syscall];
  2394. { allowed for external cpp classes }
  2395. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2396. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2397. ),(
  2398. idtok:_ENUMERATOR;
  2399. pd_flags : [pd_interface,pd_object,pd_record];
  2400. handler : @pd_enumerator;
  2401. pocall : pocall_none;
  2402. pooption : [];
  2403. mutexclpocall : [pocall_internproc];
  2404. mutexclpotype : [];
  2405. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2406. )
  2407. );
  2408. function check_proc_directive(isprocvar:boolean):boolean;
  2409. var
  2410. i : longint;
  2411. begin
  2412. result:=false;
  2413. for i:=1 to num_proc_directives do
  2414. if proc_direcdata[i].idtok=idtoken then
  2415. begin
  2416. if ((not isprocvar) or
  2417. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2418. { don't eat a public directive in classes }
  2419. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2420. result:=true;
  2421. exit;
  2422. end;
  2423. end;
  2424. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2425. {
  2426. Parse the procedure directive, returns true if a correct directive is found
  2427. }
  2428. var
  2429. p : longint;
  2430. found : boolean;
  2431. name : TIDString;
  2432. begin
  2433. parse_proc_direc:=false;
  2434. name:=tokeninfo^[idtoken].str;
  2435. found:=false;
  2436. { Hint directive? Then exit immediatly }
  2437. if (m_hintdirective in current_settings.modeswitches) then
  2438. begin
  2439. case idtoken of
  2440. _LIBRARY,
  2441. _PLATFORM,
  2442. _UNIMPLEMENTED,
  2443. _EXPERIMENTAL,
  2444. _DEPRECATED :
  2445. exit;
  2446. end;
  2447. end;
  2448. { C directive is MacPas only, because it breaks too much existing code
  2449. on other platforms (PFV) }
  2450. if (idtoken=_C) and
  2451. not(m_mac in current_settings.modeswitches) then
  2452. exit;
  2453. { retrieve data for directive if found }
  2454. for p:=1 to num_proc_directives do
  2455. if proc_direcdata[p].idtok=idtoken then
  2456. begin
  2457. found:=true;
  2458. break;
  2459. end;
  2460. { Check if the procedure directive is known }
  2461. if not found then
  2462. begin
  2463. { parsing a procvar type the name can be any
  2464. next variable !! }
  2465. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2466. not(idtoken=_PROPERTY) then
  2467. Message1(parser_w_unknown_proc_directive_ignored,name);
  2468. exit;
  2469. end;
  2470. { check if method and directive not for object, like public.
  2471. This needs to be checked also for procvars }
  2472. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2473. (symtablestack.top.symtabletype=ObjectSymtable) and
  2474. { directive allowed for cpp classes? }
  2475. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2476. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2477. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2478. exit;
  2479. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2480. (symtablestack.top.symtabletype=recordsymtable) then
  2481. exit;
  2482. { Conflicts between directives ? }
  2483. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2484. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2485. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2486. begin
  2487. Message1(parser_e_proc_dir_conflict,name);
  2488. exit;
  2489. end;
  2490. { set calling convention }
  2491. if proc_direcdata[p].pocall<>pocall_none then
  2492. begin
  2493. if (po_hascallingconvention in pd.procoptions) then
  2494. begin
  2495. Message2(parser_w_proc_overriding_calling,
  2496. proccalloptionStr[pd.proccalloption],
  2497. proccalloptionStr[proc_direcdata[p].pocall]);
  2498. end;
  2499. { check if the target processor supports this calling convention }
  2500. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2501. begin
  2502. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2503. { recover }
  2504. proc_direcdata[p].pocall:=pocall_stdcall;
  2505. end;
  2506. pd.proccalloption:=proc_direcdata[p].pocall;
  2507. include(pd.procoptions,po_hascallingconvention);
  2508. end;
  2509. if pd.typ=procdef then
  2510. begin
  2511. { Check if the directive is only for objects }
  2512. if (pd_object in proc_direcdata[p].pd_flags) and
  2513. not assigned(tprocdef(pd).struct) then
  2514. exit;
  2515. { Check if the directive is only for records }
  2516. if (pd_record in proc_direcdata[p].pd_flags) and
  2517. not assigned(tprocdef(pd).struct) then
  2518. exit;
  2519. { check if method and directive not for interface }
  2520. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2521. is_interface(tprocdef(pd).struct) then
  2522. exit;
  2523. { check if method and directive not for interface }
  2524. if is_dispinterface(tprocdef(pd).struct) and
  2525. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2526. exit;
  2527. { check if method and directive not for objcclass }
  2528. if is_objcclass(tprocdef(pd).struct) and
  2529. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2530. exit;
  2531. { check if method and directive not for objcprotocol }
  2532. if is_objcprotocol(tprocdef(pd).struct) and
  2533. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2534. exit;
  2535. { check if method and directive not for record/class helper }
  2536. if is_objectpascal_helper(tprocdef(pd).struct) and
  2537. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2538. { check if method and directive not for java class }
  2539. if is_javaclass(tprocdef(pd).struct) and
  2540. not(pd_javaclass in proc_direcdata[p].pd_flags) then
  2541. exit;
  2542. { check if method and directive not for java interface }
  2543. if is_javainterface(tprocdef(pd).struct) and
  2544. not(pd_intfjava in proc_direcdata[p].pd_flags) then
  2545. exit;
  2546. end;
  2547. { consume directive, and turn flag on }
  2548. consume(token);
  2549. parse_proc_direc:=true;
  2550. { Check the pd_flags if the directive should be allowed }
  2551. if (pd_interface in pdflags) and
  2552. not(pd_interface in proc_direcdata[p].pd_flags) then
  2553. begin
  2554. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2555. exit;
  2556. end;
  2557. if (pd_implemen in pdflags) and
  2558. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2559. begin
  2560. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2561. exit;
  2562. end;
  2563. if (pd_procvar in pdflags) and
  2564. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2565. begin
  2566. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2567. exit;
  2568. end;
  2569. { Return the new pd_flags }
  2570. if not(pd_body in proc_direcdata[p].pd_flags) then
  2571. exclude(pdflags,pd_body);
  2572. { Add the correct flag }
  2573. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2574. { Call the handler }
  2575. if pointer(proc_direcdata[p].handler)<>nil then
  2576. proc_direcdata[p].handler(pd);
  2577. end;
  2578. function proc_get_importname(pd:tprocdef):string;
  2579. var
  2580. dllname, importname : string;
  2581. begin
  2582. result:='';
  2583. if not(po_external in pd.procoptions) then
  2584. internalerror(200412151);
  2585. { external name or number is specified }
  2586. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2587. begin
  2588. if assigned(pd.import_dll) then
  2589. dllname:=pd.import_dll^
  2590. else
  2591. dllname:='';
  2592. if assigned(pd.import_name) then
  2593. importname:=pd.import_name^
  2594. else
  2595. importname:='';
  2596. proc_get_importname:=make_dllmangledname(dllname,
  2597. importname,pd.import_nr,pd.proccalloption);
  2598. end
  2599. else
  2600. begin
  2601. { Default names when importing variables }
  2602. case pd.proccalloption of
  2603. pocall_cdecl :
  2604. begin
  2605. if assigned(pd.struct) then
  2606. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2607. else
  2608. result:=target_info.Cprefix+pd.procsym.realname;
  2609. end;
  2610. pocall_cppdecl :
  2611. begin
  2612. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2613. end;
  2614. else
  2615. begin
  2616. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2617. { but according to MacPas mode description
  2618. Cprefix should still be used PM }
  2619. if (m_mac in current_settings.modeswitches) then
  2620. result:=target_info.Cprefix+tprocdef(pd).procsym.realname;
  2621. end;
  2622. end;
  2623. end;
  2624. end;
  2625. procedure proc_set_mangledname(pd:tprocdef);
  2626. var
  2627. s : string;
  2628. begin
  2629. { When the mangledname is already set we aren't allowed to change
  2630. it because it can already be used somewhere (PFV) }
  2631. if not(po_has_mangledname in pd.procoptions) then
  2632. begin
  2633. if (po_external in pd.procoptions) then
  2634. begin
  2635. { External Procedures are only allowed to change the mangledname
  2636. in their first declaration }
  2637. if (pd.forwarddef or (not pd.hasforward)) then
  2638. begin
  2639. s:=proc_get_importname(pd);
  2640. if s<>'' then
  2641. begin
  2642. pd.setmangledname(s);
  2643. end;
  2644. end;
  2645. end
  2646. else
  2647. { Normal procedures }
  2648. begin
  2649. if (po_compilerproc in pd.procoptions) then
  2650. begin
  2651. pd.setmangledname(lower(pd.procsym.name));
  2652. end;
  2653. end;
  2654. end;
  2655. { Public/exported alias names }
  2656. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2657. not(po_has_public_name in pd.procoptions) then
  2658. begin
  2659. case pd.proccalloption of
  2660. pocall_cdecl :
  2661. begin
  2662. if assigned(pd.struct) then
  2663. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2664. else
  2665. begin
  2666. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2667. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2668. (po_exports in pd.procoptions) then
  2669. pd.aliasnames.insert(pd.procsym.realname)
  2670. else
  2671. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2672. end;
  2673. end;
  2674. pocall_cppdecl :
  2675. begin
  2676. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2677. end;
  2678. end;
  2679. { prevent adding the alias a second time }
  2680. include(pd.procoptions,po_has_public_name);
  2681. end;
  2682. end;
  2683. procedure handle_calling_convention(pd:tabstractprocdef);
  2684. begin
  2685. { set the default calling convention if none provided }
  2686. if (pd.typ=procdef) and
  2687. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2688. is_cppclass(tprocdef(pd).struct)) then
  2689. begin
  2690. { none of the explicit calling conventions should be allowed }
  2691. if (po_hascallingconvention in pd.procoptions) then
  2692. internalerror(2009032501);
  2693. if is_cppclass(tprocdef(pd).struct) then
  2694. pd.proccalloption:=pocall_cppdecl
  2695. else
  2696. pd.proccalloption:=pocall_cdecl;
  2697. end
  2698. else if not(po_hascallingconvention in pd.procoptions) then
  2699. pd.proccalloption:=current_settings.defproccall
  2700. else
  2701. begin
  2702. if pd.proccalloption=pocall_none then
  2703. internalerror(200309081);
  2704. end;
  2705. { handle proccall specific settings }
  2706. case pd.proccalloption of
  2707. pocall_cdecl,
  2708. pocall_cppdecl :
  2709. begin
  2710. { check C cdecl para types }
  2711. check_c_para(pd);
  2712. end;
  2713. pocall_far16 :
  2714. begin
  2715. { Temporary stub, must be rewritten to support OS/2 far16 }
  2716. Message1(parser_w_proc_directive_ignored,'FAR16');
  2717. end;
  2718. end;
  2719. { Inlining is enabled and supported? }
  2720. if (po_inline in pd.procoptions) and
  2721. not(cs_do_inline in current_settings.localswitches) then
  2722. begin
  2723. { Give an error if inline is not supported by the compiler mode,
  2724. otherwise only give a warning that this procedure will not be inlined }
  2725. if not(m_default_inline in current_settings.modeswitches) then
  2726. Message(parser_e_proc_inline_not_supported)
  2727. else
  2728. Message(parser_w_inlining_disabled);
  2729. exclude(pd.procoptions,po_inline);
  2730. end;
  2731. { For varargs directive also cdecl and external must be defined }
  2732. if (po_varargs in pd.procoptions) then
  2733. begin
  2734. { check first for external in the interface, if available there
  2735. then the cdecl must also be there since there is no implementation
  2736. available to contain it }
  2737. if parse_only then
  2738. begin
  2739. { if external is available, then cdecl must also be available,
  2740. procvars don't need external }
  2741. if not((po_external in pd.procoptions) or
  2742. (pd.typ=procvardef) or
  2743. { for objcclasses this is checked later, because the entire
  2744. class may be external. }
  2745. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2746. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2747. Message(parser_e_varargs_need_cdecl_and_external);
  2748. end
  2749. else
  2750. begin
  2751. { both must be defined now }
  2752. if not((po_external in pd.procoptions) or
  2753. (pd.typ=procvardef)) or
  2754. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal])) then
  2755. Message(parser_e_varargs_need_cdecl_and_external);
  2756. end;
  2757. end;
  2758. { insert hidden high parameters }
  2759. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2760. { insert hidden self parameter }
  2761. insert_self_and_vmt_para(pd);
  2762. { insert funcret parameter if required }
  2763. insert_funcret_para(pd);
  2764. { Make var parameters regable, this must be done after the calling
  2765. convention is set. }
  2766. { this must be done before parentfp is insert, because getting all cases
  2767. where parentfp must be in a memory location isn't catched properly so
  2768. we put parentfp never in a register }
  2769. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2770. { insert parentfp parameter if required }
  2771. insert_parentfp_para(pd);
  2772. { Calculate parameter tlist }
  2773. pd.calcparas;
  2774. end;
  2775. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2776. {
  2777. Parse the procedure directives. It does not matter if procedure directives
  2778. are written using ;procdir; or ['procdir'] syntax.
  2779. }
  2780. var
  2781. res : boolean;
  2782. begin
  2783. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2784. begin
  2785. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2786. include(pd.procoptions,po_public);
  2787. include(pd.procoptions,po_has_public_name);
  2788. include(pd.procoptions,po_global);
  2789. end;
  2790. { methods from external class definitions are all external themselves }
  2791. if (pd.typ=procdef) and
  2792. assigned(tprocdef(pd).struct) and
  2793. (tprocdef(pd).struct.typ=objectdef) and
  2794. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  2795. tprocdef(pd).make_external;
  2796. { Class constructors and destructor are static class methods in real. }
  2797. { There are many places in the compiler where either class or static }
  2798. { method flag changes the behavior. It is simplier to add them to }
  2799. { the class constructors/destructors options than to fix all the }
  2800. { occurencies. (Paul) }
  2801. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  2802. begin
  2803. include(pd.procoptions,po_classmethod);
  2804. include(pd.procoptions,po_staticmethod);
  2805. end;
  2806. while token in [_ID,_LECKKLAMMER] do
  2807. begin
  2808. if try_to_consume(_LECKKLAMMER) then
  2809. begin
  2810. repeat
  2811. parse_proc_direc(pd,pdflags);
  2812. until not try_to_consume(_COMMA);
  2813. consume(_RECKKLAMMER);
  2814. { we always expect at least '[];' }
  2815. res:=true;
  2816. end
  2817. else
  2818. begin
  2819. res:=parse_proc_direc(pd,pdflags);
  2820. end;
  2821. { A procedure directive normally followed by a semicolon, but in
  2822. a const section or reading a type we should stop when _EQ is found,
  2823. because a constant/default value follows }
  2824. if res then
  2825. begin
  2826. if (block_type=bt_const_type) and
  2827. (token=_EQ) then
  2828. break;
  2829. { support procedure proc;stdcall export; }
  2830. if not(check_proc_directive((pd.typ=procvardef))) then
  2831. begin
  2832. { support "record p : procedure stdcall end;" and
  2833. "var p : procedure stdcall = nil;" }
  2834. if (pd_procvar in pdflags) and
  2835. (token in [_END,_RKLAMMER,_EQ]) then
  2836. break
  2837. else
  2838. begin
  2839. if (token=_COLON) then
  2840. begin
  2841. Message(parser_e_field_not_allowed_here);
  2842. consume_all_until(_SEMICOLON);
  2843. end;
  2844. consume(_SEMICOLON)
  2845. end;
  2846. end;
  2847. end
  2848. else
  2849. break;
  2850. end;
  2851. end;
  2852. procedure parse_var_proc_directives(sym:tsym);
  2853. var
  2854. pdflags : tpdflags;
  2855. pd : tabstractprocdef;
  2856. begin
  2857. pdflags:=[pd_procvar];
  2858. pd:=nil;
  2859. case sym.typ of
  2860. fieldvarsym,
  2861. staticvarsym,
  2862. localvarsym,
  2863. paravarsym :
  2864. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  2865. typesym :
  2866. pd:=tabstractprocdef(ttypesym(sym).typedef);
  2867. else
  2868. internalerror(2003042617);
  2869. end;
  2870. if pd.typ<>procvardef then
  2871. internalerror(2003042618);
  2872. { names should never be used anyway }
  2873. parse_proc_directives(pd,pdflags);
  2874. end;
  2875. procedure parse_object_proc_directives(pd:tabstractprocdef);
  2876. var
  2877. pdflags : tpdflags;
  2878. begin
  2879. pdflags:=[pd_object];
  2880. parse_proc_directives(pd,pdflags);
  2881. end;
  2882. procedure parse_record_proc_directives(pd:tabstractprocdef);
  2883. var
  2884. pdflags : tpdflags;
  2885. begin
  2886. pdflags:=[pd_record];
  2887. parse_proc_directives(pd,pdflags);
  2888. end;
  2889. function proc_add_definition(var currpd:tprocdef):boolean;
  2890. {
  2891. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  2892. forwarddef is found and reused it returns true
  2893. }
  2894. var
  2895. fwpd : tprocdef;
  2896. currparasym,
  2897. fwparasym : tsym;
  2898. currparacnt,
  2899. fwparacnt,
  2900. curridx,
  2901. fwidx,
  2902. i : longint;
  2903. po_comp : tprocoptions;
  2904. paracompopt: tcompare_paras_options;
  2905. forwardfound : boolean;
  2906. symentry: TSymEntry;
  2907. begin
  2908. forwardfound:=false;
  2909. { check overloaded functions if the same function already exists }
  2910. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  2911. begin
  2912. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  2913. { Skip overloaded definitions that are declared in other units }
  2914. if fwpd.procsym<>currpd.procsym then
  2915. continue;
  2916. { check the parameters, for delphi/tp it is possible to
  2917. leave the parameters away in the implementation (forwarddef=false).
  2918. But for an overload declared function this is not allowed }
  2919. if { check if empty implementation arguments match is allowed }
  2920. (
  2921. not(m_repeat_forward in current_settings.modeswitches) and
  2922. not(currpd.forwarddef) and
  2923. is_bareprocdef(currpd) and
  2924. not(po_overload in fwpd.procoptions)
  2925. ) or
  2926. { check arguments, we need to check only the user visible parameters. The hidden parameters
  2927. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV) }
  2928. (
  2929. (compare_paras(currpd.paras,fwpd.paras,cp_none,[cpo_comparedefaultvalue,cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  2930. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  2931. ) then
  2932. begin
  2933. { Check if we've found the forwarddef, if found then
  2934. we need to update the forward def with the current
  2935. implementation settings }
  2936. if fwpd.forwarddef then
  2937. begin
  2938. forwardfound:=true;
  2939. if not(m_repeat_forward in current_settings.modeswitches) and
  2940. (fwpd.proccalloption<>currpd.proccalloption) then
  2941. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  2942. else
  2943. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  2944. { Check calling convention }
  2945. if (fwpd.proccalloption<>currpd.proccalloption) then
  2946. begin
  2947. { In delphi it is possible to specify the calling
  2948. convention in the interface or implementation if
  2949. there was no convention specified in the other
  2950. part }
  2951. if (m_delphi in current_settings.modeswitches) then
  2952. begin
  2953. if not(po_hascallingconvention in currpd.procoptions) then
  2954. currpd.proccalloption:=fwpd.proccalloption
  2955. else
  2956. if not(po_hascallingconvention in fwpd.procoptions) then
  2957. fwpd.proccalloption:=currpd.proccalloption
  2958. else
  2959. begin
  2960. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2961. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2962. { restore interface settings }
  2963. currpd.proccalloption:=fwpd.proccalloption;
  2964. end;
  2965. end
  2966. else
  2967. begin
  2968. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  2969. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  2970. { restore interface settings }
  2971. currpd.proccalloption:=fwpd.proccalloption;
  2972. end;
  2973. end;
  2974. { Check static }
  2975. if (po_staticmethod in fwpd.procoptions) then
  2976. begin
  2977. if not (po_staticmethod in currpd.procoptions) then
  2978. begin
  2979. include(currpd.procoptions, po_staticmethod);
  2980. if (po_classmethod in currpd.procoptions) then
  2981. begin
  2982. { remove self from the hidden paras }
  2983. symentry:=currpd.parast.Find('self');
  2984. if symentry<>nil then
  2985. begin
  2986. currpd.parast.Delete(symentry);
  2987. currpd.calcparas;
  2988. end;
  2989. end;
  2990. end;
  2991. end;
  2992. { Check if the procedure type and return type are correct,
  2993. also the parameters must match also with the type }
  2994. if ((m_repeat_forward in current_settings.modeswitches) or
  2995. not is_bareprocdef(currpd)) and
  2996. ((compare_paras(currpd.paras,fwpd.paras,cp_all,paracompopt)<>te_exact) or
  2997. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)) then
  2998. begin
  2999. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3000. fwpd.fullprocname(false));
  3001. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3002. break;
  3003. end;
  3004. { Check if both are declared forward }
  3005. if fwpd.forwarddef and currpd.forwarddef then
  3006. begin
  3007. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  3008. currpd.fullprocname(false));
  3009. end;
  3010. { internconst or internproc only need to be defined once }
  3011. if (fwpd.proccalloption=pocall_internproc) then
  3012. currpd.proccalloption:=fwpd.proccalloption
  3013. else
  3014. if (currpd.proccalloption=pocall_internproc) then
  3015. fwpd.proccalloption:=currpd.proccalloption;
  3016. { Check procedure options, Delphi requires that class is
  3017. repeated in the implementation for class methods }
  3018. if (m_fpc in current_settings.modeswitches) then
  3019. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  3020. else
  3021. po_comp:=[po_classmethod,po_methodpointer];
  3022. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  3023. (fwpd.proctypeoption <> currpd.proctypeoption) then
  3024. begin
  3025. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3026. fwpd.fullprocname(false));
  3027. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  3028. { This error is non-fatal, we can recover }
  3029. end;
  3030. { Forward declaration is external? }
  3031. if (po_external in fwpd.procoptions) then
  3032. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  3033. { Check parameters }
  3034. if (m_repeat_forward in current_settings.modeswitches) or
  3035. (currpd.minparacount>0) then
  3036. begin
  3037. { If mangled names are equal then they have the same amount of arguments }
  3038. { We can check the names of the arguments }
  3039. { both symtables are in the same order from left to right }
  3040. curridx:=0;
  3041. fwidx:=0;
  3042. currparacnt:=currpd.parast.SymList.Count;
  3043. fwparacnt:=fwpd.parast.SymList.Count;
  3044. repeat
  3045. { skip default parameter constsyms }
  3046. while (curridx<currparacnt) and
  3047. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  3048. inc(curridx);
  3049. while (fwidx<fwparacnt) and
  3050. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3051. inc(fwidx);
  3052. { stop when one of the two lists is at the end }
  3053. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3054. break;
  3055. { compare names of parameters, ignore implictly
  3056. renamed parameters }
  3057. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3058. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3059. if not(sp_implicitrename in currparasym.symoptions) and
  3060. not(sp_implicitrename in fwparasym.symoptions) then
  3061. begin
  3062. if (currparasym.name<>fwparasym.name) then
  3063. begin
  3064. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3065. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3066. break;
  3067. end;
  3068. end;
  3069. { next parameter }
  3070. inc(curridx);
  3071. inc(fwidx);
  3072. until false;
  3073. end;
  3074. { Everything is checked, now we can update the forward declaration
  3075. with the new data from the implementation }
  3076. fwpd.forwarddef:=currpd.forwarddef;
  3077. fwpd.hasforward:=true;
  3078. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3079. { marked as local but exported from unit? }
  3080. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3081. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3082. if fwpd.extnumber=$ffff then
  3083. fwpd.extnumber:=currpd.extnumber;
  3084. while not currpd.aliasnames.empty do
  3085. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3086. { update fileinfo so position references the implementation,
  3087. also update funcretsym if it is already generated }
  3088. fwpd.fileinfo:=currpd.fileinfo;
  3089. if assigned(fwpd.funcretsym) then
  3090. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3091. if assigned(currpd.deprecatedmsg) then
  3092. begin
  3093. stringdispose(fwpd.deprecatedmsg);
  3094. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3095. end;
  3096. { import names }
  3097. if assigned(currpd.import_dll) then
  3098. begin
  3099. stringdispose(fwpd.import_dll);
  3100. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3101. end;
  3102. if assigned(currpd.import_name) then
  3103. begin
  3104. stringdispose(fwpd.import_name);
  3105. fwpd.import_name:=stringdup(currpd.import_name^);
  3106. end;
  3107. fwpd.import_nr:=currpd.import_nr;
  3108. { for compilerproc defines we need to rename and update the
  3109. symbolname to lowercase }
  3110. if (po_compilerproc in fwpd.procoptions) then
  3111. begin
  3112. { rename to lowercase so users can't access it }
  3113. fwpd.procsym.realname:='$'+lower(fwpd.procsym.name);
  3114. { the mangeled name is already changed by the pd_compilerproc }
  3115. { handler. It must be done immediately because if we have a }
  3116. { call to a compilerproc before it's implementation is }
  3117. { encountered, it must already use the new mangled name (JM) }
  3118. end;
  3119. { Release current procdef }
  3120. currpd.owner.deletedef(currpd);
  3121. currpd:=fwpd;
  3122. end
  3123. else
  3124. begin
  3125. { abstract methods aren't forward defined, but this }
  3126. { needs another error message }
  3127. if (po_abstractmethod in fwpd.procoptions) then
  3128. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3129. else
  3130. begin
  3131. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3132. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3133. end;
  3134. end;
  3135. { we found one proc with the same arguments, there are no others
  3136. so we can stop }
  3137. break;
  3138. end;
  3139. { check for allowing overload directive }
  3140. if not(m_fpc in current_settings.modeswitches) then
  3141. begin
  3142. { overload directive turns on overloading }
  3143. if ((po_overload in currpd.procoptions) or
  3144. (po_overload in fwpd.procoptions)) then
  3145. begin
  3146. { check if all procs have overloading, but not if the proc is a method or
  3147. already declared forward, then the check is already done }
  3148. if not(fwpd.hasforward or
  3149. assigned(currpd.struct) or
  3150. (currpd.forwarddef<>fwpd.forwarddef) or
  3151. ((po_overload in currpd.procoptions) and
  3152. (po_overload in fwpd.procoptions))) then
  3153. begin
  3154. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3155. break;
  3156. end;
  3157. end
  3158. else
  3159. begin
  3160. if not(fwpd.forwarddef) then
  3161. begin
  3162. if (m_tp7 in current_settings.modeswitches) then
  3163. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3164. else
  3165. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3166. break;
  3167. end;
  3168. end;
  3169. end; { equal arguments }
  3170. end;
  3171. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3172. list }
  3173. if not forwardfound then
  3174. begin
  3175. { can happen in Delphi mode }
  3176. if (currpd.proctypeoption = potype_function) and
  3177. is_void(currpd.returndef) then
  3178. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3179. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3180. end;
  3181. proc_add_definition:=forwardfound;
  3182. end;
  3183. end.