pdecsub.pas 129 KB

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