pdecsub.pas 123 KB

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