<s> import os <EOL> import sys <EOL> from glad . lang . common . generator import Generator <EOL> from glad . lang . common . util import makefiledir <EOL> KHRPLATFORM = '<STR_LIT>' <EOL> class CGenerator ( Generator ) : <EOL> NAME = '<STR_LIT:c>' <EOL> NAME_LONG = '<STR_LIT>' <EOL> def open ( self ) : <EOL> suffix = '<STR_LIT>' <EOL> if not self . spec . NAME == '<STR_LIT>' : <EOL> suffix = '<STR_LIT>' . format ( self . spec . NAME ) <EOL> self . h_include = '<STR_LIT>' . format ( suffix ) <EOL> self . _f_c = open ( make_path ( self . path , '<STR_LIT:src>' , <EOL> '<STR_LIT>' . format ( suffix ) ) , '<STR_LIT:w>' ) <EOL> self . _f_h = open ( make_path ( self . path , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' . format ( suffix ) ) , '<STR_LIT:w>' ) <EOL> khr_url = KHRPLATFORM <EOL> if os . path . exists ( '<STR_LIT>' ) : <EOL> khr_url = '<STR_LIT>' + os . path . abspath ( '<STR_LIT>' ) <EOL> khr = os . path . join ( self . path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> khrplatform = os . path . join ( khr , '<STR_LIT>' ) <EOL> if not os . path . exists ( khrplatform ) : <EOL> if not os . path . exists ( khr ) : <EOL> os . makedirs ( khr ) <EOL> self . opener . urlretrieve ( khr_url , khrplatform ) <EOL> return self <EOL> def close ( self ) : <EOL> self . _f_c . close ( ) <EOL> self . _f_h . close ( ) <EOL> def generate_header ( self ) : <EOL> self . _f_h . write ( '<STR_LIT>' ) <EOL> self . _f_h . write ( self . header ) <EOL> self . _f_h . write ( '<STR_LIT>' ) <EOL> self . _f_c . write ( '<STR_LIT>' ) <EOL> self . _f_c . write ( self . header ) <EOL> self . _f_c . write ( '<STR_LIT>' ) <EOL> def generate_loader ( self , features , extensions ) : <EOL> f = self . _f_c <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> features = { '<STR_LIT>' : [ ] , '<STR_LIT>' : [ ] } <EOL> written = set ( ) <EOL> for api , version in self . api . items ( ) : <EOL> for feature in features [ api ] : <EOL> f . write ( '<STR_LIT>' <EOL> . format ( feature . name ) ) <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> f . write ( '<STR_LIT>' . format ( feature . name ) ) <EOL> for func in feature . functions : <EOL> f . write ( '<STR_LIT>' <EOL> . format ( func . proto . name , func . proto . name . upper ( ) ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> for ext in extensions [ api ] : <EOL> if len ( list ( ext . functions ) ) == <NUM_LIT:0> or ext . name in written : <EOL> continue <EOL> f . write ( '<STR_LIT>' <EOL> . format ( ext . name ) ) <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> f . write ( '<STR_LIT>' . format ( ext . name ) ) <EOL> if ext . name == '<STR_LIT>' : f . write ( '<STR_LIT>' ) <EOL> if ext . name == '<STR_LIT>' : f . write ( '<STR_LIT>' ) <EOL> for func in ext . functions : <EOL> f . write ( '<STR_LIT>' <EOL> . format ( func . proto . name , func . proto . name . upper ( ) ) ) <EOL> if ext . name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> written . add ( ext . name ) <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> f . write ( '<STR_LIT>' ) <EOL> for ext in extensions [ api ] : <EOL> f . write ( '<STR_LIT>' . format ( ext . name ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> if api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> elif api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> else : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> self . loader . write_find_core ( f ) <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for feature in features [ api ] : <EOL> f . write ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( feature . name , num = feature . number ) ) <EOL> if self . spec . NAME == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( version [ <NUM_LIT:0> ] , version [ <NUM_LIT:1> ] ) ) <EOL> f . write ( '<STR_LIT>' . format ( version [ <NUM_LIT:0> ] ) ) <EOL> f . write ( '<STR_LIT>' . format ( version [ <NUM_LIT:1> ] ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> if api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> elif api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> else : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> self . loader . write_begin_load ( f ) <EOL> if api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> elif api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> else : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> for feature in features [ api ] : <EOL> f . write ( '<STR_LIT>' . format ( feature . name ) ) <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> for ext in extensions [ api ] : <EOL> if len ( list ( ext . functions ) ) == <NUM_LIT:0> : <EOL> continue <EOL> f . write ( '<STR_LIT>' . format ( ext . name ) ) <EOL> self . loader . write_end_load ( f ) <EOL> f . write ( '<STR_LIT>' ) <EOL> self . loader . write_header_end ( self . _f_h ) <EOL> def generate_types ( self , types ) : <EOL> f = self . _f_h <EOL> self . loader . write_header ( f ) <EOL> self . write_api_header ( f ) <EOL> for type in types : <EOL> if not self . spec . NAME in ( '<STR_LIT>' , ) and '<STR_LIT>' in type . raw : <EOL> continue <EOL> f . write ( ( type . raw + '<STR_LIT:\n>' ) . lstrip ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:U+0020>' ) ) <EOL> def generate_features ( self , features ) : <EOL> f = self . _f_h <EOL> write = set ( ) <EOL> if self . spec . NAME in ( '<STR_LIT>' , ) : <EOL> pass <EOL> elif self . spec . NAME in ( '<STR_LIT>' , ) : <EOL> self . write_enums ( f , set ( ) , features ) <EOL> for feature in features : <EOL> for func in feature . functions : <EOL> self . write_function_def ( f , func ) <EOL> else : <EOL> self . write_functions ( f , write , set ( ) , features ) <EOL> f = self . _f_c <EOL> self . write_code_head ( f ) <EOL> self . loader . write ( f ) <EOL> self . loader . write_has_ext ( f ) <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for feature in features : <EOL> f . write ( '<STR_LIT>' . format ( feature . name ) ) <EOL> for func in write : <EOL> self . write_function ( f , func ) <EOL> def generate_extensions ( self , extensions , enums , functions ) : <EOL> write = set ( ) <EOL> written = set ( enum . name for enum in enums ) | set ( function . proto . name for function in functions ) <EOL> f = self . _f_h <EOL> self . write_functions ( f , write , written , extensions ) <EOL> f = self . _f_c <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for ext in set ( ext . name for ext in extensions ) : <EOL> f . write ( '<STR_LIT>' . format ( ext ) ) <EOL> written = set ( ) <EOL> for ext in extensions : <EOL> if ext . name == '<STR_LIT>' : f . write ( '<STR_LIT>' ) <EOL> if ext . name == '<STR_LIT>' : f . write ( '<STR_LIT>' ) <EOL> for func in ext . functions : <EOL> if func in write and func not in written : <EOL> self . write_function ( f , func ) <EOL> written . add ( func ) <EOL> if ext . name in ( '<STR_LIT>' , '<STR_LIT>' ) : f . write ( '<STR_LIT>' ) <EOL> def write_functions ( self , f , write , written , extensions ) : <EOL> self . write_enums ( f , written , extensions ) <EOL> for ext in extensions : <EOL> f . write ( '<STR_LIT>' . format ( ext . name ) ) <EOL> if self . spec . NAME in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> f . write ( '<STR_LIT>' . format ( ext . name ) ) <EOL> if ext . name == '<STR_LIT>' : f . write ( '<STR_LIT>' ) <EOL> if ext . name == '<STR_LIT>' : f . write ( '<STR_LIT>' ) <EOL> for func in ext . functions : <EOL> if not func . proto . name in written : <EOL> self . write_function_prototype ( f , func ) <EOL> write . add ( func ) <EOL> written . add ( func . proto . name ) <EOL> if ext . name in ( '<STR_LIT>' , '<STR_LIT>' ) : f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> def write_enums ( self , f , written , extensions ) : <EOL> for ext in extensions : <EOL> for enum in ext . enums : <EOL> if not enum . name in written : <EOL> f . write ( '<STR_LIT>' . format ( enum . name , enum . value ) ) <EOL> written . add ( enum . name ) <EOL> def write_api_header ( self , f ) : <EOL> for api in self . api : <EOL> if api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> elif api == '<STR_LIT>' : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> else : <EOL> f . write ( '<STR_LIT>' . format ( api . upper ( ) ) ) <EOL> def write_code_head ( self , f ) : <EOL> f . write ( '<STR_LIT>' . format ( self . h_include ) ) <EOL> def write_extern ( self , fobj ) : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> def write_extern_end ( self , fobj ) : <EOL> fobj . write ( '<STR_LIT>' ) <EOL> def write_function_def ( self , fobj , func ) : <EOL> fobj . write ( '<STR_LIT>' . format ( func . proto . ret . to_c ( ) , func . proto . name ) ) <EOL> fobj . write ( '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( param . type . to_c ( ) , param . name ) for param in func . params ) ) <EOL> fobj . write ( '<STR_LIT>' ) <EOL> def write_function_prototype ( self , fobj , func ) : <EOL> fobj . write ( '<STR_LIT>' . format ( <EOL> func . proto . ret . to_c ( ) , func . proto . name . upper ( ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( param . type . to_c ( ) , param . name ) for param in func . params ) ) <EOL> ) <EOL> fobj . write ( '<STR_LIT>' . format ( func . proto . name . upper ( ) , <EOL> func . proto . name ) ) <EOL> fobj . write ( '<STR_LIT>' . format ( func . proto . name ) ) <EOL> def write_function ( self , fobj , func ) : <EOL> fobj . write ( '<STR_LIT>' . format ( func . proto . name . upper ( ) , <EOL> func . proto . name ) ) <EOL> def make_path ( path , * args ) : <EOL> path = os . path . join ( path , * args ) <EOL> makefiledir ( path ) <EOL> return path </s>
